class EmaCrossFuturesFrontMonthAlgorithm(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2015, 1, 1)
        self.SetEndDate(2020, 12, 30)
        self.SetCash(1000000)
        

        future = self.AddFuture(Futures.Energies.CrudeOilWTI)

        # Only consider the front month contract
        # Update the universe once per day to improve performance
        future.SetFilter(lambda x: x.FrontMonth().OnlyApplyFilterAtMarketOpen())

        # Symbol of the current contract
        self.symbol = None

        # Create two exponential moving averages
        self.fast = ExponentialMovingAverage(9)
        self.slow = ExponentialMovingAverage(21)
        self.tolerance = 0.01
        self.consolidator = None

        # Add a custom chart to track the EMA cross
        chart = Chart('EMA Cross')
        chart.AddSeries(Series('Fast', SeriesType.Line, 0))
        chart.AddSeries(Series('Slow', SeriesType.Line, 0))
        self.AddChart(chart)
        
        

    def OnData(self,slice):
        
        holding = None if self.symbol is None else self.Portfolio.get(self.symbol)
        position = self.Portfolio[self.symbol].Quantity
        if holding is None:
            return
        if self.slow.IsReady:
            # Buy the futures' front contract when the fast EMA is above the slow one
            if self.fast.Current.Value > self.slow.Current.Value * (1 + self.tolerance):
                if not holding.Invested:
                    self.MarketOrder(self.symbol, 1)
                    self.PlotEma()
                elif holding.Invested and position < 0:
                    self.MarketOrder(self.symbol, 2)
                    self.PlotEma()
            if self.fast.Current.Value < self.slow.Current.Value * (1 + self.tolerance):
                if not holding.Invested:
                    self.MarketOrder(self.symbol, -1)
                    self.PlotEma()
                elif holding.Invested and position > 0:
                    self.MarketOrder(self.symbol, -2)
                    self.PlotEma()

    def OnSecuritiesChanged(self, changes):
        if len(changes.RemovedSecurities) > 0:
            # Remove the consolidator for the previous contract
            # and reset the indicators
            if self.symbol is not None and self.consolidator is not None:
                self.SubscriptionManager.RemoveConsolidator(self.symbol, self.consolidator)
                self.fast.Reset()
                self.slow.Reset()
            # We don't need to call Liquidate(_symbol),
            # since its positions are liquidated because the contract has expired.

        # Only one security will be added: the new front contract
        self.symbol = changes.AddedSecurities[0].Symbol

        # Create a new consolidator and register the indicators to it
        self.consolidator = self.ResolveConsolidator(self.symbol, Resolution.Daily)
        self.RegisterIndicator(self.symbol, self.fast, self.consolidator)
        self.RegisterIndicator(self.symbol, self.slow, self.consolidator)

        #  Warm up the indicators
        
        self.WarmUpIndicator(self.symbol, self.fast, Resolution.Daily)
        self.WarmUpIndicator(self.symbol, self.slow, Resolution.Daily)

        self.PlotEma()

    def PlotEma(self):
        self.Plot('EMA Cross', 'Fast', self.fast.Current.Value)
        self.Plot('EMA Cross', 'Slow', self.slow.Current.Value)

Hello, I am working on an ema cross for futures markets. I am interested in running a program on multiple futures markets long/short and am currently working out the code for one underlying at a time. The system looks like its executing properly, as a contract is delisted it liquidates and reenters if the trend is still in tackt. My problem is that every backtest is giving me different results with the same code, no parameter changes. Can you help me with this?

 

Author