Back

Allocation Model Needs Help

HI, so I am a new student at quantconnnect, and I was making a basic model. In this model I need the values of 10% above SPY's moving average and I'm not sure what esactly to do. I have attached the code down below

 

 

class MultidimensionalTransdimensionalPrism(QCAlgorithm):
    def Initialize(self):
        self.SetStartDate(2010, 2, 1)               # Start and End Date for Stocks
        self.SetEndDate(2020, 1, 1)
        self.SetCash(10000) # Set Amount of Money to be used
        self.AddEquity("SPY", Resolution.Hour)    # SPY, etf
        self.AddEquity("TLT", Resolution.Hour)     # Bonds TLT
        # self.AddEquity("TVIX", Resolution.Hour)
        self._sma = self.SMA("SPY", 50, Resolution.Daily)
        self._smab = self.SMA("SPY", 10, Resolution.Daily)
        self._smabg = self.SMA("SPY", 200, Resolution.Daily)
        self._smabe = self.SMA("TLT", 200, Resolution.Daily)
        self.smi =  self._smabg * 1.1
        self.smo = self._smabe * 1.1
        self.rebal = 4                            # Rebalance every 4 weeks
        self.rebalTimer = self.rebal - 1            # Initialize to trigger first week
        self.flag1 = 0                              # Flag to initate trades
        
        # Increment rebalance timer at every week start
        self.Schedule.On(self.DateRules.WeekStart("SPY"), self.TimeRules.AfterMarketOpen("SPY", 150), self.Rebalance) 
      
    
    def OnData(self, data):
        if self.flag1 == 1:                     
            if data["SPY"].Close > self._smabg.Current.Value and self._smab > self._sma:
                x = 1
                self.rebalTimer = 0
            elif data["SPY"].Close > self._smabg.Current.Value and self._smab < self._sma:
                self.SetHoldings("SPY", .7, True)
                self.SetHoldings("TLT", .3, True)
                self.rebalTimer = 0
            elif data["SPY"].Close < self._smabg.Current.Value and self._smab > self._sma:
                self.SetHoldings("SPY", .3, True)
                self.SetHoldings("TLT", .7, True)
                self.rebalTimer = 0
            else:
                x = 2
            if x == 1:
                if data["SPY"].Close > self.smi:
                    self.SetHoldings("TLT", 1, True)
                else:
                    self.SetHoldings("SPY", 1, True)
            if x == 2:
                if data["TLT"].Close > self.smo:
                    self.SetHoldings("SPY", 1, True)
                else:
                    self.SetHoldings('TLT', 1)
                
        self.flag1 = 0          
        
    def Rebalance(self):
        self.rebalTimer +=1
        if self.rebalTimer == self.rebal:
            self.flag1 = 1

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.


Hi Aditya,

The reason the algorithm runs into errors when trying to backtest is because of the way `smi` and `smo` have been calculated. In the lines,

self.smi = self._smabg * 1.1
self.smo = self._smabe * 1.1

we are trying to multiply a SimpleMovingAverage object with a float.

Instead, to get the price level that is 10% above the SMAs, we use

smi = self._smabg.Current.Value * 1.1
smo = self._smabe.Current.Value * 1.1

It is by calling `.Current.Value` on a SimpleMovingAverage object that we get the SMAs current value. This is explained in our Basic Indicator Usage documentation and  in several of our Bootcamp lessons. I recommend all users complete the Bootcamp series if they have not already.

See the attached backtest for the full solution file.

Best,
Derek Melchin

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