I need to get historical data to train my Machine Learning model for the following 5 SPY indicators: STD, BB, RSI, MOM, and EMA.
In research environment I can get them using the following statements:
history = qb.History(["SPY"], start_time, end_time, Resolution.Minute)
STD = qb.Indicator(StandardDeviation(36), spy.Symbol, start_time, end_time, Resolution.Minute)
BB = qb.Indicator(BollingerBands(30, 2), spy.Symbol, start_time, end_time, Resolution.Minute)
RSI = qb.Indicator(RelativeStrengthIndex(5), spy.Symbol, start_time, end_time, Resolution.Minute)
MOM = qb.Indicator(Momentum(5), spy.Symbol, start_time, end_time, Resolution.Minute)
EMA9 = qb.Indicator(ExponentialMovingAverage(9), spy.Symbol, start_time, end_time, Resolution.Minute)
How can I get them for main.py?
I really appreciate your help!!
Mario
PS: This is the second time I sent this question. Nobody answered to he first one.
Aaron Janeiro Stone
After registering the indicators in the initialization step (using self.Indicator instead of qb.indicator), you can access it by calling the variable you assigned it to. For example,
self.STD = self.Indicator(StandardDeviation(36), spy.Symbol, start_time, end_time, Resolution.Minute)
for more info:
https://www.quantconnect.com/docs/algorithm-reference/indicatorsMario Schmidt
Excuse me Aaron, but "self.Indicator" is not supported in main.py.
Aaron Janeiro Stone
Apologies, I should have done it in terms of RegisterIndicator (see this example in the docs )
self.RegisterIndicator(spy.Symbol,StandardDeviation(36),Resolution.Minute)
where RegisterIndicator can be used with any of the following inputs:
# Registers the consolidator to receive automatic updates as well as configures # the indicator to receive updates from the consolidator. self.RegisterIndicator(symbol, indicator, resolution, selector = null): pass # Registers the consolidator to receive automatic updates as well as configures # the indicator to receive updates from the consolidator. self.RegisterIndicator(symbol, indicator, pyObject, selector = null): pass # Registers the consolidator to receive automatic updates as well as configures # the indicator to receive updates from the consolidator. self.RegisterIndicator(symbol, indicator, consolidator, selector = null): pass # Registers the consolidator to receive automatic updates as well as configures # the indicator to receive updates from the consolidator. self.RegisterIndicator(symbol, indicator, resolution = null): pass
Mario Schmidt
Excuse me again Aaron, but I am not able to understand what do you mean.
* What statements is this ending in ":"? Is it an "if", or "for" or what?
self.RegisterIndicator(symbol, indicator, resolution = null):
pass
* Should I write it in Initialize or in OnData?
* What consolidator are you talking about? Should I define a new one or it simulates one?
Could you show me an example, please?
I will be waiting for your answer
Thanks in advance and regards,
Aaron Janeiro Stone
Apologies for any confusion,
1. In the above, the statements ending in ":" are the different ways to initialize an indicator from the self.Initialize portion of your algorithm. For example:
self.RegisterIndicator(spy.Symbol,StandardDeviation(36),Resolution.Minute)
was initialized using the form:
self.RegisterIndicator(symbol, indicator, resolution)
2. You register in Initialize. Following that, because OnData is called whenever new data is processed, this is when new data will be fed into the indicator.
3. In your example, no consolidator is needed. What a consolidator does is accumulate smaller resolutions of data in order to analyze them as TradeBars. For example, lets say I want to have each point of data for my indicator be the TradeBar (which allows us to look at its OHLC, volume, variance, etc) of thirty minutes. Note that there is no resolution of form "thirty minitues" -- there is minute data, second data, and hourly data, but to get an OHLC bar for each thirty minutes, I would need to accumulate minutely data.
Thus, I could write:
# request the equity data in minute resolution
self.AddEquity("SPY", Resolution.Minute)
# define a 10-period RSI indicator with indicator constructor
self.rsi = RelativeStrengthIndex(10, MovingAverageType.Simple)
# create the 30-minutes data consolidator
thirtyMinuteConsolidator = TradeBarConsolidator(timedelta(minutes=30))
self.SubscriptionManager.AddConsolidator("SPY", thirtyMinuteConsolidator)
# register the 30-minute consolidated bar data to automatically update the indicator
self.RegisterIndicator("SPY", self.rsi, thirtyMinuteConsolidator)
Mario Schmidt
Dear Aaron,
I have tried to do my best up to where I understood you, but as you can see in the Debug file, the indicators value remain fixed without update themselves.
I attach backtest with the code..
I''l appreciate your new help.
Regards,
Mario
Aaron Janeiro Stone
There seems to be a lot going on in your backtest. To keep it simple, I have attached one where
1. An indicator is registered + consolidated.
2. When the indicator is ready, it logs its current value.
where the initialization of the indicator is done using the code I sent beforehand. Hopefully this should make it clear exactly how to interact with a registered indicator. Note that it is getting the values without me needing to call "update"
Aaron Janeiro Stone
Can't edit the prior post, but I have attached one here which logs less frequently than the backtest attached prior (so that you can conserve your log quota).
Mario Schmidt
Hi Aaron,
it looks that I was not clear enough and we do not understand ouselves. Apologize for it.
I have found the solution after trying time after time, based on Update, as follow:
start_time = datetime(2017, 7, 6, 9, 31) # start datetime for history call
end_time = datetime(2017, 7, 6, 10, 39) # end datetime for history call
df = self.History(["SPY"], start_time, end_time, Resolution.Minute)
if not df.empty:
history = df.loc["SPY"]
history.drop(['askclose', 'askhigh', 'asklow', 'askopen', 'asksize', 'bidclose','bidhigh', 'bidlow', 'bidopen', 'bidsize', 'open', 'volume'], axis=1, inplace=True)
for i in range(0,len(history)):
if self.std.IsReady:
self.std.Update(history.index[i],history.loc[history.index[i],'close'])
history['STD']=self.std.Current.Value
Thanks anyway.
Best regards,
Mario
Mario Schmidt
Dear Aaron,
I have tried to do what you have suggested to me, but I am getting only fixed values.
I'll really apprecite you new help.
def Initialize(self):
self.SetStartDate(2017, 7, 7) # Set Start Date
self.SetCash(100000) # Set Strategy Cash
self.AddEquity("SPY", Resolution.Minute)
dict = {'close': [],
'RSI': [],
'EMA9': [],
}
Tiempo=[]
self.dfIndicators=pd.DataFrame(dict, index = Tiempo)
self.rsi = self.RSI("SPY", 5)
self.ema9 = self.EMA("SPY", 9)
# create the 36-minutes data consolidator
thirtyMinuteConsolidator = TradeBarConsolidator(timedelta(minutes=37))
# create the 36-minutes data consolidator
thirtyMinuteConsolidator = TradeBarConsolidator(timedelta(minutes=37))
self.SubscriptionManager.AddConsolidator("SPY", thirtyMinuteConsolidator)
# register the 30-minute consolidated bar data to automatically update the indicator
self.RegisterIndicator("SPY", self.rsi, thirtyMinuteConsolidator)
self.RegisterIndicator("SPY", self.ema9, thirtyMinuteConsolidator)
def OnData(self, data):
openhour = "10:08:51"
openhour=parser.parse(openhour)
tempodf = self.History(["SPY"], 390, Resolution.Minute)
if not tempodf.empty:
history = tempodf.loc["SPY"]
history.drop(['askclose', 'askhigh', 'asklow', 'askopen', 'asksize', 'bidclose','bidhigh', 'bidlow', 'bidopen', 'bidsize', 'open', 'low', 'high', 'volume'], axis=1, inplace=True)
for i in range(0,len(history)):
horafila=history.index[i]
if datetime.time(horafila) > datetime.time(openhour):
dictIndicators = {
'close': float(history.loc[history.index[i],'close']),
'RSI': float(self.rsi.Current.Value),
'EMA9': float(self.ema9.Current.Value),
'Tiempo': history.index[i]
}
self.dfIndicators=self.dfIndicators.append(dictIndicators,ignore_index=True)
OUTPUT:
same happens with other indicators I am trying to use liek BB, STD, MOM
Tiempo close RSI MOM EMA9 EMA36
180 2017-07-06 13:09:00 227.156097 100.0 0.0 226.789519 226.789519
181 2017-07-06 13:10:00 227.156097 100.0 0.0 226.789519 226.789519
182 2017-07-06 13:11:00 227.146698 100.0 0.0 226.789519 226.789519
183 2017-07-06 13:12:00 227.146698 100.0 0.0 226.789519 226.789519
184 2017-07-06 13:13:00 227.174896 100.0 0.0 226.789519 226.789519
185 2017-07-06 13:14:00 227.193695 100.0 0.0 226.789519 226.789519
186 2017-07-06 13:15:00 227.193695 100.0 0.0 226.789519 226.789519
187 2017-07-06 13:16:00 227.221893 100.0 0.0 226.789519 226.789519
Derek Melchin
Hi Mario,
The indicators aren't updated because `RegisterIndicator` needs to be passed a timedelta, not a `TradeBarConsolidator`. In addition, when consolidating data, we should use the full class name (`RelativeStrengthIndex`) instead of the shortcut methods (`self.RSI`). Otherwise, the indicator will be updated each minute bar and each consolidated bar.
See the attached backtest for a working example.
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.
Mario Schmidt
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!