HI I need help with the update indicators portion. My code intends to update indicators 3 minutes before close for SMA & RSI, then execute sell and buy trades right before closing.
Two concerns:
- My live and backtest RSI and SMA values were really different. I narrowed this down to the fact that live uses ticks while backtest uses the closing of the minute, hence I created version 1 of UpdateIndicators which uses the history function to get the last minute closing price 2.5 minutes before market closes. However, there was still a discrepancy between the live trading indicator values and the backtest indicator values
- I need to reset indicators on corporate events like stock splits and dividends, however, I can't figure out how to warmup the indicators using the 3rd last minute of every trading day instead of just the daily closing price.
Any suggestions would be greatly appreciated
Thank you!
from AlgorithmImports import *
class RSIAndMovingAverageStrategy(QCAlgorithm):
def Initialize(self):
self.symbols = {
"SPY": {"symbol": self.AddEquity("SPY", Resolution.Minute).Symbol, "sma": SimpleMovingAverage(200), "rsi": RelativeStrengthIndex(10, MovingAverageType.Wilders)},
"UVXY": {"symbol": self.AddEquity("UVXY", Resolution.Minute, dataNormalizationMode=DataNormalizationMode.Raw).Symbol, "rsi": RelativeStrengthIndex(10, MovingAverageType.Wilders)},
"SQQQ": {"symbol": self.AddEquity("SQQQ", Resolution.Minute).Symbol, "rsi": RelativeStrengthIndex(9, MovingAverageType.Wilders)},
}
self.Schedule.On(self.DateRules.EveryDay(self.symbols["SPY"]["symbol"]), self.TimeRules.BeforeMarketClose(self.symbols["SPY"]["symbol"], 3), self.UpdateIndicators)
self.Schedule.On(self.DateRules.EveryDay(self.symbols["SPY"]["symbol"]), self.TimeRules.BeforeMarketClose(self.symbols["SPY"]["symbol"], 1), self.LiquidateIfNecessary)
self.Schedule.On(self.DateRules.EveryDay(self.symbols["SPY"]["symbol"]), self.TimeRules.BeforeMarketClose(self.symbols["SPY"]["symbol"], 0.5), self.ExecuteBuyOrders)
self.SetWarmUp(230, Resolution.Daily)
#version 1
def UpdateIndicators(self):
for symbol_key, symbol_data in self.symbols.items():
symbol = symbol_data["symbol"]
history = self.History(symbol, 1, Resolution.Minute)
if not history.empty:
price = history['close'].iloc[-1]
time = history.index[-1][1]
for key in ['sma', 'rsi','rsi_13', 'sma_20', 'sma_45', 'sma_190', 'sma_230']:
if key in symbol_data:
symbol_data[key].Update(time, price)
#version 2
def UpdateIndicators(self):
for symbol_data in self.symbols.values():
if self.Securities.ContainsKey(symbol_data["symbol"]) and self.Securities[symbol_data["symbol"]].HasData:
price = self.Securities[symbol_data["symbol"]].Price
for key in ['sma', 'rsi', 'rsi_13', 'sma_20', 'sma_45', 'sma_190', 'sma_230']:
if key in symbol_data:
symbol_data[key].Update(self.Time, price)
Mia Alissi
Sources: Key Concepts - QuantConnect.com, Automatic Indicators - QuantConnect.com, Indicator Warmup Algorithm, Warmup Algorithm, E T F Constituent Universe R S I Alpha Model Algorithm
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.
Esheng Chew
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!