Back

Attribute Error

Sometimes the algorithms doesn't see bar properties. And I recieve an Attribute Error.
This is my code:

import numpy as np
from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Securities import *
from datetime import timedelta
from decimal import *


class Exeption_Example(QCAlgorithm):

def Initialize(self):

self.SetStartDate(2017,06,07) #Set Start Date
self.SetEndDate(2017,10,11) #Set End Date
self.SetCash(100000) #Set Strategy Cash

future = self.AddFuture(Futures.Metals.Gold, Resolution.Minute)
future.SetFilter(timedelta(0), timedelta(182))
self.FutureSymbol = future.Symbol;


self.exeption_count = 0
self.non_exeption_count = 0
self.no_contract_count = 0
self.minute_count = 0


def OnData(self, slice):
self.minute_count += 1
for chain in slice.FutureChains:
contracts = filter(lambda x: x.Expiry > self.Time + timedelta(90), chain.Value)
if len(contracts) == 0:
self.no_contract_count += 1
continue
else:
self.front = sorted(contracts, key = lambda x: x.Expiry, reverse=True)[0]
bar = slice[self.front.Symbol]
try:
bar.High # try if bar.High is defined
except AttributeError:
self.exeption_count += 1 # if bar "list" has no "High" attribute
else:
self.non_exeption_count += 1 # if "bar" list has "High" attribute
if self.minute_count % (1440*5) == 0: # print exeption review ones a week
self.print_exeption_review()

def print_exeption_review(self):
self.Log("No contract count " + str(self.no_contract_count))
self.Log("Exeption count " + str(self.exeption_count))
self.Log("No exeption count " + str(self.non_exeption_count))
self.Log("__________________________")

There is the last log message after four month backtest:

2017-10-10 15:41:00 No contract count 19
2017-10-10 15:41:00 Exeption count 43924
2017-10-10 15:41:00 No exeption count 78457
2017-10-10 15:41:00 __________________________

Look, there is more then half exeptions for the whole backtest. What I am doing wrong?

Update Backtest








When we get data from the Slice object from its key, slice[self.front.Symbol], we can get QuoteBar or TradeBar or OpenInterest data. When I debugged your code, I found out that, for example, self.front.Symbol was GCG18, but there were no QuoteBar nor TradeBar from that symbol, only for GCQ17 and GCZ17, so slice[self.front.Symbol] returned OpenInterest data.
Since OpenInterest data is a Tick object, the returned type is a list, because Tick objects are placed in a list of Tick in the Slice object (slice.Ticks). Therefore, you use the followoing check instead of the try/except:

if type(bar) is list: continue
0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Update Backtest





0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Loading...

This discussion is closed