Hello, I hope you all are having a great day. I am a beginner at QuantConnect and I am having difficulties trying to make this algorithm work. The first difficulty I had with this bot was with the StopMarketOrders. Even though I defined StopMarketOrders after every SetHoldings they didn't work in the backtest. After that, I decided to change the StopMarketOrders to Trailing Stop, hoping it would work, but it didn't succeed. I am still struggling with this problem.

Also, after adding the if isWarmingUp statement the backtest started to give errors saying “self.stopLongTicket is not defined”. Is there anyone who can help me? It is my first proper code at QuantConnect and I started to understand I have a lot missing. My code:

# region imports
from AlgorithmImports import *
# endregion

class FocusedBrownGoat(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2020, 1, 1)  # Set Start Date
        self.SetEndDate(2021, 1, 1)
        self.SetCash(100000)  # Set Strategy Cash
        self.ethusd = self.AddCrypto("ETHUSD", Resolution.Daily).Symbol
        
        self.SetWarmUp(34)
        self.ema34 = self.EMA(self.ethusd,34,Resolution.Daily)
        self.ema8 = self.EMA(self.ethusd, 8, Resolution.Daily)

        self.ema34window = RollingWindow[IndicatorDataPoint](2)
        self.ema8window = RollingWindow[IndicatorDataPoint](2)

        self.ema34.Updated += self.Ema34Updated
        self.ema8.Updated += self.Ema8Updated

        emachart = Chart("Trade Plot")
        emachart.AddSeries(Series("Ema8", SeriesType.Line,0))
        emachart.AddSeries(Series("Ema34", SeriesType.Line,0))
        emachart.AddSeries(Series("Long", SeriesType.Scatter,0))
        emachart.AddSeries(Series("SoldLong", SeriesType.Scatter,0))
        emachart.AddSeries(Series("Short", SeriesType.Scatter,0))
        emachart.AddSeries(Series("SoldShort", SeriesType.Scatter,0))
        self.AddChart(emachart)

        self.highestPrice = 0
        self.lowestPrice = 0
        self.isLong = False
        self.isShort = False

        self.SetBenchmark(self.ethusd)


    def OnData(self, data):
        if self.ethusd not in data:
            return

        if self.IsWarmingUp:
            return

        price = self.Securities[self.ethusd].Close

        self.Plot("Trade Plot", "Ema8", self.ema8.Current.Value)
        self.Plot("Trade Plot", "Ema34", self.ema34.Current.Value)

        conditionlong = self.ema8window[0] > self.ema34window[0] and self.ema34window[1] > self.ema8window[1]
        conditionshort = self.ema8window[0] < self.ema34window[0] and self.ema34window[1] < self.ema8window[1]

        if not self.Portfolio.Invested:
            if conditionlong == True:
                self.SetHoldings(self.ethusd, 1)
                self.stopLongTicket = self.StopMarketOrder(self.ethusd, -(self.Portfolio.TotalPortfolioValue/price), price * 0.98)
                self.highestPrice = price
                self.isLong == True
                self.Log("Long")
                self.Plot("Trade Plot", "Long", self.Securities[self.ethusd].Close)

        if not self.Portfolio.Invested:
            if conditionshort == True:
                self.SetHoldings(self.ethusd, -1)
                self.stopShortTicket = self.StopMarketOrder(self.ethusd, -(self.Portfolio.TotalPortfolioValue/price), price * 1.02)
                self.lowestPrice = price
                self.isShort == True
                self.Log("Short")
                self.Plot("Trade Plot", "Short", self.Securities[self.ethusd].Close)

        else:
            if self.Securities[self.ethusd].Close > self.highestPrice:
                if self.isLong == True:
                    self.highestPrice = self.Securities[self.ethusd].Close
                    updateFields = UpdateOrderFields()
                    updateFields.StopPrice = self.Securities[self.ethusd].Close*0.98
                    self.stopLongTicket.Update(updateFields)
                    

            if self.Securities[self.ethusd].Close < self.lowestPrice:
                if self.isShort == True:
                    self.lowestPrice = self.Securities[self.ethusd].Close
                    updateFields = UpdateOrderFields()
                    updateFields.StopPrice = self.Securities[self.ethusd].Close*1.02
                    self.stopShortTicket.Update(updateFields)

    def OnOrderEvent(self, orderEvent):
        if self.stopLongTicket is not None and self.stopLongTicket.OrderId == orderEvent.OrderId:
            self.isLong = False
            self.Plot("Trade Plot", "SoldLong", self.Securities[self.ethusd].Close)

        if self.stopShortTicket is not None and self.stopShortTicket.OrderId == orderEvent.OrderId:
            self.isShort = False
            self.Plot("Trade Plot", "SoldShort", self.Securities[self.ethusd].Close)

    def Ema34Updated(self, sender, updated):
        self.ema34window.Add(updated)

    def Ema8Updated(self, sender, updated):
        self.ema8window.Add(updated)

Author