Hey all,
Working on a very simple algorithm but am having trouble graphing two Indicators, and am not sure what I'm doing wrong as I'm following the docs:
from datetime import timedelta
class OptimizedMultidimensionalAtmosphericScrubbers(QCAlgorithm):
#
# buy when the SMA-200 > SMA-50
# sell when the SMA-200 < SMA-50
#
def Initialize(self):
self.SetStartDate(2020, 5, 9)
self.SetCash(1000)
self.equity = self.AddEquity("SPY", Resolution.Daily)
self.symbol = self.equity.Symbol
# warm up and create our SMA indicators
self.SetWarmUp(timedelta(200))
self.sma_50 = self.SMA(self.symbol, 50, Resolution.Daily)
self.sma_200 = self.SMA(self.symbol, 200, Resolution.Daily)
# setup the chart
my_chart = Chart("Indicator Chart")
my_chart.AddSeries(Series("SMA50", SeriesType.Line, 0))
my_chart.AddSeries(Series("SMA200", SeriesType.Line, 0))
self.AddChart(my_chart)
def OnData(self, data):
if not self.sma_50.IsReady or not self.sma_200.IsReady:
pass
self.Plot("Indicator Chart", "SMA50", self.sma_50)
self.Plot("Indicator Chart", "SMA200", self.sma_200)
def OnWarmupFinished(self):
if not self.Securities[self.symbol].Invested:
self.SetHoldings(self.symbol, 1.0)
Error I get is:
Runtime Error: Trying to dynamically access a method that does not exist throws a TypeError exception. To prevent the exception, ensure each parameter type matches those required by the Plot method. Please checkout the API documentation.
at OnData in main.py:line 31
TypeError : No method matches given arguments for Plot (Open Stacktrace)
Now, if I remove the second argument of the Plot methods in OnData(), the backtest runs but I don't see the Indicators being graphed. Any help is appreciated.
Derek Melchin
Hi Matt,
To plot the value of the SMA, we need to use
self.Plot("Indicator Chart", "SMA50", self.sma_50.Current.Value) self.Plot("Indicator Chart", "SMA200", self.sma_200.Current.Value)
Alternative, instead of plotting at each timestep, we can just add
self.PlotIndicator("Indicators", self.sma_50) self.PlotIndicator("Indicators", self.sma_200)
to Initialize instead.
See the attached backtest for reference.
Best,
Derek Melchin
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.
Matt S
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!