Hi there,
I'm still working on the basics with python, but this simple pullback algo doesn't have correct entries based on the RSI. Should buy when RSI < 25. However the debug code logs RSI values at 0.0 sometimes, which doesn't occur for SPY. Any thoughts as to why the 0.0 values?
thanks!
Derek Melchin
Hi Mark,
The RSI indicator can have a 0 value when its internal AverageGain indicator has a value of 0. Refer to the source code here. To avoid this, increase the `period` argument that's passed to the RSI method.
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.
Mark hatlan
Thanks for responding Derek. So in order to prevent 0 or 100 exact values if I have a low period (or if there is no gain in the timeframe) I have to write it as a custom indicator? I'm learning python and not C (the GitHub link has the RSI example in C), is there an example of this in .py?
I would have thought this zeroing scenario would already be accounted for under the hood in LEAN since other charting programs I've used don't show zeros with a short period.
thanks!
Derek Melchin
Hi Mark,
By default, the RSI indicator uses the Wilders moving average type. See the source code here. Note that the third argument passed to the RSI method is the moving average type for the indicator. Thus, the line in the above algorithm that reads
self.rsi = self.RSI("SPY", 2, Resolution.Daily)
passes a Resolution enum as the third argument. This enum value is casted to the number 4, which means the RSI indicator is constructed with the double exponential moving average type. This is probably why the values are not matching other sources. To use the Wilders moving average, the line should read
self.rsi = self.RSI("SPY", 2, MovingAverageType.Wilders, Resolution.Daily)
See the attached backtest and plot for reference.
Our implementation of the RSI indicator results in a value of 100 (0) when the internal average loss (gain) moving average is 0. To have other functionality, a custom indicator could be used.
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.
Mark hatlan
Thats great, thanks Derek. I made that change and now I don't get the zero values anymore.
Mark hatlan
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!