Hi! I'm trying to add the Keltner Channels indicator as an extra filter for my universe selection, but I seem to be encountering some problems. Before trying to add the keltner indicator, I was only constructing EMAs and an RSI indicator to use for my universe selection, but I wasn't encountering this problem then with just these indicators. After trying to add the Keltner Channel, I encounter this error:
Runtime Error: TypeError : 'Timestamp' object does not support indexing
at <lambda> in FundamentalUniverseSelectionModel.py:46
TypeError : 'Timestamp' object does not support indexing (Open Stacktrace)
This is the class constructor:
class SelectionData():
def __init__(self, history):
self.slow = ExponentialMovingAverage(200)
self.fast = ExponentialMovingAverage(50)
self.keltner = KeltnerChannels(10, 2, MovingAverageType.Simple)
for bar in history.itertuples():
tradeBar = TradeBar(bar.Index[1], bar.Index[0], bar.open, bar.high, bar.low, bar.close, bar.volume, timedelta(1))
self.fast.Update(bar.Index[1], bar.close)
self.slow.Update(bar.Index[1], bar.close)
self.keltner.Update(tradeBar)
def is_ready(self):
return self.slow.IsReady and self.fast.IsReady and self.keltner.IsReady
def update(self, time, price):
self.fast.Update(time, price)
self.slow.Update(time, price)
And how I apply it to the symbols:
for security in sortedByDollarVolume:
symbol = security.Symbol
dollar = self.dollarVolumeBySymbol[symbol]
price = self.AdjustedPrice[symbol]
if symbol in history_symbols:
if not str(symbol) in history.index:
continue
self.averages[symbol] = SelectionData(history.loc[symbol])
self.averages[symbol].update(algorithm.Time, price)
selected[symbol] = dollar
https://www.quantconnect.com/forum/discussion/8840/keltner-channel-on-a-class
Derek Melchin
Hi Vncne,
Try replacing
self.averages[symbol] = SelectionData(history.loc[symbol])
with
self.averages[symbol] = SelectionData(history)
If that doesn't fix it, please attach a backtest which demonstrates the issue so I can further assist.
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.
Nicholas Fitzgerald
Hi Vncne,
I encountered the same issue with my algorithm. I found that the issue came from modularisation for me. When I incorporated the universe selection into my main.py file, the algorithm ran without an issue. My algorithm is similar to yours, only it uses bollinger bands instead of EMA's. My code is attached, I hope this helps, it stumped me for days!
John J
Hi Nicholas,
2. Also I don't see your OnData section. I see you append your selected stocks to your selected list. I imagine you handle order something like this:
What I'm trying to figure now is after we do all this is, do I need to also remove the securities after sending the order and when they are removed from the universe should I clear their data? Basically I have an indicator condition I'm looking for and when that happens I want to send an order. Once the condition is met to sell that stock I want to buy it again if the indicator condition presents itself again.
3. Do you have more example code on how you handle the orders with this algorithim?
Vncne
Hi @John J,
You could maintain a list of your universe in OnSecuritiesChanged like this and send an order for each symbol in OnData (I created self.universe and self.removed in def Initialize)
Not the most elegant, but I guess it gets the job done, unless you were thinking of something else?
Also, is your indicator condition an indicator that has to be attached to each symbol in the universe?
Vncne
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!