Hi Mia, I have coded below to calculate RSI of TQQQ. However, I found that the RSI calculation may not be correct if coded in this way. If the period is short, like 2 or 3, the smoothing is much less than longer period. More history data and warmup should be done for RSI before use it. Could you help to modify the code? Thanks.
from AlgorithmImports import *
class TQQQRSI(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2018, 1, 1)
self.SetCash(10000)
self.VOLA = 200
res = Resolution.Daily
self.SPY = self.AddEquity('SPY', Resolution.Minute).Symbol
self.UVXY = self.AddEquity('UVXY', Resolution.Minute).Symbol
self.TQQQ = self.AddEquity('TQQQ', Resolution.Minute).Symbol
self.Securities["SPY"].SetDataNormalizationMode(DataNormalizationMode.Raw);
self.Securities["UVXY"].SetDataNormalizationMode(DataNormalizationMode.Raw);
self.Securities["TQQQ"].SetDataNormalizationMode(DataNormalizationMode.Raw);
self.spySMA = self.SMA("SPY", 200, res)
self.tqqqRSI = self.RSI("TQQQ", 10, MovingAverageType.Wilders, res)
self.SetWarmUp(self.VOLA, res)
def OnData(self, data):
if not (self.Time.hour == 9 and self.Time.minute == 31): return
if not self.spySMA.IsReady:
return
if data.ContainsKey(self.SPY):
spyclose = data[self.SPY].Close
if spyclose > self.spySMA.Current.Value:
if self.tqqqRSI.Current.Value > 79:
self.SetHoldings(self.TQQQ, 0.0)
self.SetHoldings(self.UVXY, 1.0)
else:
self.SetHoldings(self.UVXY, 0.0)
self.SetHoldings(self.TQQQ, 1.0)
else:
self.SetHoldings(self.UVXY, 0.0)
self.SetHoldings(self.TECL, 0.0)
Mia Alissi
Hi Vidal,
You're correct that the RSI calculation requires a warm-up period before it can be used. This is because the RSI calculation involves a moving average, which requires a certain amount of historical data to calculate.
In your code, you've set the warm-up period to 200 days, which should be sufficient for an RSI with a period of 10. However, if you're finding that the RSI values are not accurate, you might want to increase the warm-up period.
Here's how you can modify your code to increase the warm-up period:
This should help ensure that the RSI calculation has enough historical data to be accurate.
Remember, the warm-up period should be at least as long as the longest period used in any of your indicators. In this case, since you're using a 200-day SMA and a 10-day RSI, a warm-up period of 200 days should be sufficient. However, if you're still seeing inaccuracies, increasing the warm-up period may help.
Sources:
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.
Vidal boreal
Yes, Mia. I find that the RSI calculated is correct and the period is sufficient to warm up.Â
It just calculate the RSI based on yesterday's close. How could I modify the code if I want to execute the trade between 3pm and 4pm, calculate the RSI at that time and execute the trade based on the new RSI.
Mia Alissi
To execute trades between 3pm and 4pm based on the RSI at that time, you can modify your
OnData
method to check the current time and only execute trades during that window. You can use theself.Time
property to get the current time in the algorithm.Here's how you can modify your code:
This modification will ensure that your algorithm only executes trades between 3pm and 4pm, and it will calculate the RSI based on the data available at that time.
Sources:
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.
Vidal boreal
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!