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?