Back

Help!! :-) Problems with my first algo

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

Update Backtest








I'm fairly new here and new to python but this is what I'd do. A python expert could do much better with it.

 

import numpy as np

class BasicTemplateAlgorithm(QCAlgorithm):


def Initialize(self):

self.SetStartDate(2014,8,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):

history = self.History(["AAL", "UAL"], 3, Resolution.Daily)

close1 = history.loc["AAL"]['close']
close2 = history.loc["UAL"]['close']

mavg_30 = np.mean(close1 - close2)
std_30 = np.std(close1 - close2)

mavg_1 = np.mean(close1 - close2)

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
0

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