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:
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
Mia Alissi
Sources: Custom Charting Algorithm, Object Store - QuantConnect.com, Charting - QuantConnect.com, Live Analysis - QuantConnect.com, Indicator Suite Algorithm
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Thanos
This is not working…
Louis Szeto
Hi Thanos
It is not possible to plot a discontinued look-back line plot, since the plot series is based on forward time-value point pair. However, you can plot a scatter plot with 2 series, indicating buy and sell with different markers to do similar.
Best
Louis
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Thanos
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!