Hello
I am playing with QC and it seems not logic to plot a chart with SPY and its fast and slow SMAs (e.g. 9,20).
Could someone provide a working code please ?
For example, I want to visualize the SPY on the period 2018-2020 and ON THE SAME CHART, I want to see the SMA9 and SMA20 (say in red and green).
Any help welcome. Thanks !
Mia Alissi
Sure, I can provide a simple example of how to plot SPY with its fast and slow SMAs on the same chart. Here's a Python code snippet based on the information extracted from the sources:
In this code, we first initialize the algorithm with the desired start and end dates. We then add SPY to our universe and create two SMA indicators, one with a period of 9 days (fast) and another with a period of 20 days (slow). In the
OnData
method, we check if both SMAs are ready, and if they are, we plot them on the same chart.Please note that the colors of the lines in the plot are automatically assigned and cannot be manually set in the current version of LEAN.
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.
Ashlon
Many thanks Mia ! - and if one wants to add SPY on the same chart ? One would have to add:
self.Plot("SPY with SMAs", self.AddEquity("SPY", Resolution.Daily))
or
self.Plot("SPY with SMAs", self.spy)
or something like that, but none of both works.Â
Any idea ?
Mia Alissi
To plot the price of SPY on the same chart, you would need to use the
Plot
method with the SPY's closing price. Here's how you can modify theOnData
method to include SPY:In this code,
self.Securities[self.spy].Close
is used to get the closing price of SPY. This value is then plotted on the same chart as the SMAs.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.
Ashlon
Many thanks again Mia.
I had tried several alternatives, but something was still missing.
Have a nice day !
Mia Alissi
You're welcome, Ashlon! I'm glad I could help. Don't hesitate to ask if you have any more questions. Have a great day!
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.
Ashlon
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!