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.

  1. from AlgorithmImports import *
  2. class TQQQRSI(QCAlgorithm):
  3. def Initialize(self):
  4. self.SetStartDate(2018, 1, 1)
  5. self.SetCash(10000)
  6. self.VOLA = 200
  7. res = Resolution.Daily
  8. self.SPY = self.AddEquity('SPY', Resolution.Minute).Symbol
  9. self.UVXY = self.AddEquity('UVXY', Resolution.Minute).Symbol
  10. self.TQQQ = self.AddEquity('TQQQ', Resolution.Minute).Symbol
  11. self.Securities["SPY"].SetDataNormalizationMode(DataNormalizationMode.Raw);
  12. self.Securities["UVXY"].SetDataNormalizationMode(DataNormalizationMode.Raw);
  13. self.Securities["TQQQ"].SetDataNormalizationMode(DataNormalizationMode.Raw);
  14. self.spySMA = self.SMA("SPY", 200, res)
  15. self.tqqqRSI = self.RSI("TQQQ", 10, MovingAverageType.Wilders, res)
  16. self.SetWarmUp(self.VOLA, res)
  17. def OnData(self, data):
  18. if not (self.Time.hour == 9 and self.Time.minute == 31): return
  19. if not self.spySMA.IsReady:
  20. return
  21. if data.ContainsKey(self.SPY):
  22. spyclose = data[self.SPY].Close
  23. if spyclose > self.spySMA.Current.Value:
  24. if self.tqqqRSI.Current.Value > 79:
  25. self.SetHoldings(self.TQQQ, 0.0)
  26. self.SetHoldings(self.UVXY, 1.0)
  27. else:
  28. self.SetHoldings(self.UVXY, 0.0)
  29. self.SetHoldings(self.TQQQ, 1.0)
  30. else:
  31. self.SetHoldings(self.UVXY, 0.0)
  32. self.SetHoldings(self.TECL, 0.0)
+ Expand

Author

Vidal boreal

December 2023