181658_1707684964.jpg

source code: 

class MACDTrendAlgorithm(QCAlgorithm):


 

    def Initialize(self):

        '''Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.'''


 

        self.SetStartDate(2022, 1, 1)  

        self.SetEndDate(2024, 1, 1)    

        self.SetCash(1000)         # Find more symbols here: http://quantconnect.com/data

        self.symbol=self.AddEquity("SPY", Resolution.Hour)


 

        # define our daily macd(12,26) with a 9 day signal

        self.__macd = self.MACD("SPY", 12, 26, 9, MovingAverageType.Exponential, Resolution.Hour)

        self.__previous = datetime.min

        self.PlotIndicator("MACD", True, self.__macd, self.__macd.Signal)

        self.PlotIndicator("SPY", self.__macd.Fast, self.__macd.Slow)

       

        #self.Plot("Trade Plot", "Price", self.Securities[self.symbol].Price)

        #self.Plot("Trade Plot", "ema_slow", self.ema_slow.Current.Value)


 

        self.crossupTOLERANCE = 0.0025;

        self.crossDownTOLERANCE=0.0025

       

        stockPlot = Chart('Trade Plot')


 

        # Import the necessary module before using Custom color

        from System.Drawing import Color

        stockPlot.AddSeries(Series('Price', SeriesType.Line, '$', Color.Green))

        stockPlot.AddSeries(Series('Buy', SeriesType.Scatter, '$', Color.Red, ScatterMarkerSymbol.Triangle))

        stockPlot.AddSeries(Series('Sell', SeriesType.Scatter, '$', Color.Blue, ScatterMarkerSymbol.TriangleDown))

        self.AddChart(stockPlot)

 




 

    def OnData(self, data):

        '''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.'''

        # wait for our macd to fully initialize

        if not self.__macd.IsReady: return


 

        # only once per day per hour

        #if self.__previous.date() == self.Time.date() and (self.__previous.hour() == self.Time.hour()): return


 

        #if not data.Bars.ContainsKey("SPY"):

        #    print("missing symbol quote")

        #    return


 

        # define a small tolerance on our checks to avoid bouncing

        tolerance = 0.0025


 

        holdings = self.Portfolio["SPY"].Quantity

       

        #price=self.Securities[self.symbol].Price

        price=data["SPY"].Close


 

        signalDeltaPercent = (self.__macd.Current.Value - self.__macd.Signal.Current.Value)/self.__macd.Fast.Current.Value


 

        # if our macd is greater than our signal, then let's go long

        if holdings <= 0 and signalDeltaPercent > self.crossupTOLERANCE:  # 0.01%

            # longterm says buy as well

            self.SetHoldings("SPY", 1.0)

            self.Plot("SPY", "Buy", price)


 

        # of our macd is less than our signal, then let's go short

        elif holdings >= 0 and signalDeltaPercent < -self.crossDownTOLERANCE:

            self.Liquidate("SPY")

            self.Plot("SPY", "Sell", price)



 

        self.__previous = self.Time

 

the error happens in march 2022. 

 

181658_1707685127.jpg