I'm working on a strategy that calculates the RSI of closing prices of ETFs. I am going to use the (extensive!) indicators library to my make work a little simpler. For example, in the code snippet below, I can get the RSI of the close price of SPY.
One question I have is, how do I include the current price into the calculation being done in the indicator? If I understand the docs correctly, the 3-period RSI calculation being done below only includes data for T-3, T-2, and T-1 (T being today). As I am estimating the close and am executing right before it, I would like my RSI calculation to include data for T, T-1 and T-2.
Basically, my question is:
Is there an easy way to get indicators to include the current price data along with historical data in daily algorithms that execute at the close?
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Indicators import *
class Strategy(QCAlgorithm):
self.SetCash(100000)
self.SetStartDate(2017, 12, 1)
self.SetEndDate(2017, 12, 20)
self.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage, AccountType.Margin)
self.etfs = ['SPY']
self.rsi_data = {}
for etf in self.etfs:
self.AddEquity(etf, Resolution.Daily)
self.Securities[etf].FeeModel = ConstantFeeTransactionModel(0)
self.Securities[etf].SlippageModel = ConstantSlippageModel(0)
#add indicators here
self.rsi_data[etf] = self.RSI(etf, 3)
self.Schedule.On(self.DateRules.EveryDay('SPY'), self.TimeRules.BeforeMarketClose('SPY', 1), Action(self.Rebalance))
def OnData(self, slice):
pass
def Rebalance(self):
for etf in self.etfs:
if self.rsi_data[etf].IsReady:
self.Log(str(self.rsi_data[etf]))
Jing Wu
Hi, you could use the current price to update the rsi indicator
self.rsi.Update(self.Securities[symbol].Price)
After making trade dcision based on the updated rsi value, reset the indicator and initialize the indicator with the history again
# loop the history dataframe to get the time index and the price value for bar in self.History([Symbol], 3, Resolution.Daily).iloc[Symbol]['close']: self.rsi.Update(time index, price value)
KILLC
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!