Hello guys, hope you're all doing great!

I have a problem that I can't solve and I would love it if anybody could take a look at it.

I have a tick-type futures strategy that also trades in the extended market. Even though I have a strict SL&TP there were multiple occasions where the profit/loss was bigger than the normal +/- 200. I took a look at the code and it seems like the positions do not close /liquidate the right way. Because as soon as the market opens up again the positions close and it loses a lot of money. Maybe it has to do something with the stop market order or limit order.

For solving purposes, I only included the buying and selling. To show this problem I added an image of a chart that can be found on the backtest under"PROFIT"(shows last trade profit). Normally this number should be somewhere between -/+200 but sometimes it is -2k.

192008_1677057066.jpg

It might have to do something with the way it receives trade bars but I don't understand how to rewrite the code so that it doesn't happen anymore…
This chart shows liquidation(red, happens multiple times??), BarHandler before(green) and after(blue) StopTrading barrier that activates after the liquidation process

192008_1677057096.jpg

The objective: close open positions before market close (dates from GetMarketHours with extended market hours)

Code to get next open and close(extended):

hours = self.Securities[self.future.Symbol].Exchange.Hours
            if hours.IsDateOpen(self.Time):
                self.StopTrading = False
                
            
                #get next open/close date
                self.todayOpen = hours.GetNextMarketOpen(self.Time, True)
                self.todayClose = hours.GetNextMarketClose(self.Time, True)

                #liquidate position 15 minutes before close if unrealized profit > 30
                if self.todayClose - timedelta(minutes=15) < self.Time and self.StopTrading is not True:
                    if self.Portfolio[self.future.Symbol].UnrealizedProfit > 30:
                        self.StopTrading = True
                        
                        
                #liquidate position 2 minutes before close
                if self.todayClose - timedelta(minutes=2) < self.Time and self.StopTrading is not True:
                    self.StopTrading = True
                    self.Liquidate() 
                    self.Plot("CLOSING", "Liquidation_signal",0.5) 
                    self.Transactions.CancelOpenOrders() 
                    
                #activate trading 2 minutes before open
                if self.todayOpen - timedelta(minutes=2) < self.Time:
                    selfStopTrading = False

I also tried it with scheduled events:

self.Schedule.On(self.DateRules.EveryDay(self.future.Symbol), self.TimeRules.BeforeMarketClose(self.future.Symbol, 1, True), self.CloseMarket)
            self.Schedule.On(self.DateRules.EveryDay(self.future.Symbol), self.TimeRules.AfterMarketOpen(self.future.Symbol, 0, True), self.OpenMarket)
        
        def OpenMarket(self):
            self.StopTrading = False
        def CloseMarket(self):
            self.StopTrading = True
            self.Transactions.CancelOpenOrders()
            self.Liquidate()
            self.Plot("CLOSING", "Liquidation_signal",1.5)
            

Both do not work.

 

I would be very thankful if someone could help me with this 😊
Thanks in advance!