Hi, please help, i tried my first algo as i learned in a tutorial but i cannot backtest it. Where is the mistake? I guess it is about the downloading of the historical data or something like that?
import numpy as np
class BasicTemplateAlgorithm(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2014,10,07) #Set Start Date
self.SetEndDate(2014,10,11) #Set End Date
self.SetCash(100000) #Set Strategy Cash
self.AddEquity("AAL", Resolution.Daily)
self.AddEquity("UAL", Resolution.Daily)
self.long_on_spread = False
self.shorting_spread = False
def OnData(self, data):
prices = self.History(["AAL","UAL"], 3)
short_prices = prices.iloc[-1:]
mavg_30 = np.mean(prices["AAL"]-prices["UAL"])
std_30 = np.std(prices["AAL"]-prices["UAL"])
mavg_1 = np.mean(short_prices["AAL"] - short_prices["UAL"])
if std_30 > 0:
zscore = (mavg_1 - mavg_30)/std_30
if zscore > 1.0 and not self.shorting_spread:
self.SetHoldings("AAL", -0.5)
self.SetHoldings("UAL", 0.5)
self.shorting_spread = True
self.long_on_spread = False
elif zscore < 1.0 and not self.long_on_spread:
self.SetHoldings("AAL", 0.5)
self.SetHoldings("UAL", -0.5)
self.shorting_spread = False
self.long_on_spread = True
elif abs(zscore) < 0.1:
self.SetHoldings("AAL", 0)
self.SetHoldings("UAL", 0)
self.shorting_spread = False
self.long_on_spread = False