Hello,

I am creating a simple crossover strategy (buy when SMA50 > SMA200, liquidate when SMA200 < SMA50) and a plot of the trades taken. I am trying to plot a line between the buy scatters and the sell scatters on the trade plot. Ideally the line would be green if the trade was profitable and red if it was loss-making. This is a visualization of what I mean:

thanos_1727244472.jpg

 

I use the code below but the result is a line that only connects the sells. Any feedback on what I am doing wrong?

 

from AlgorithmImports import *

class SmaCrossoverBot(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2020, 1, 1)
        self.SetEndDate(2022, 3, 30)
        self.SetCash(100000)  # Set initial capital
        
        # Add SPY index with hourly resolution
        self.spy = self.AddEquity("SPY", Resolution.Hour).Symbol
        
        # Initialize SMA indicators
        self.sma50 = self.SMA(self.spy, 50, Resolution.Hour)
        self.sma200 = self.SMA(self.spy, 200, Resolution.Hour)
        
        self.position_open = False
        self.buy_price = None
        self.buy_time = None

        # Create a custom chart to plot trades and indicators
        trade_plot = Chart("Trade Plot")
        trade_plot.AddSeries(Series("Price", SeriesType.Line, 0))
        trade_plot.AddSeries(Series("SMA50", SeriesType.Line, 0))
        trade_plot.AddSeries(Series("SMA200", SeriesType.Line, 0))
        trade_plot.AddSeries(Series("Buy", SeriesType.Scatter, 0))
        trade_plot.AddSeries(Series("Sell", SeriesType.Scatter, 0))
        trade_plot.AddSeries(Series("Trade Line", SeriesType.Line, 0))
        self.AddChart(trade_plot)

    def OnData(self, data):
        if not self.sma50.IsReady or not self.sma200.IsReady:
            return
        
        # Current price
        price = self.Securities[self.spy].Close
        
        # Plot the current price and SMA values
        self.Plot("Trade Plot", "Price", price)
        self.Plot("Trade Plot", "SMA50", self.sma50.Current.Value)
        self.Plot("Trade Plot", "SMA200", self.sma200.Current.Value)
        
        # Check for SMA crossover to enter a trade
        if not self.position_open and self.sma50.Current.Value > self.sma200.Current.Value and self.sma50.Previous.Value <= self.sma200.Previous.Value:
            self.SetHoldings(self.spy, 1)  
            self.position_open = True
            self.buy_price = price
            self.buy_time = self.Time
            self.Log("BUY SPY @" + str(price))
            self.Plot("Trade Plot", "Buy", price)
            
            

        # Check for SMA crossover to exit a trade
        elif self.position_open and self.sma50.Current.Value < self.sma200.Current.Value and self.sma50.Previous.Value >= self.sma200.Previous.Value:
            self.Liquidate(self.spy)
            self.position_open = False
            self.Log("SOLD SPY @" + str(price))
            self.Plot("Trade Plot", "Sell", price)
            
          # Plot a line from the buy point to the sell point
            if self.buy_price is not None:
                self.Plot("Trade Plot", "Trade Line", self.buy_price)
                self.Plot("Trade Plot", "Trade Line", price)
                
                
                # Reset buy price after plotting the line
                self.buy_price = None
                self.buy_time = None

Thank you,

Thanos