Just enumerate through the datas in a pythonic way by referencing d. if len(d) > self.lenofdata....
If you reference self.datas you'll only ever access self.datas. Each timeframe and datafeed will have its own data in datas. datas datas datas... datas[n]. Also self.len has the potential for a keyword conflict. I suggest following backtraders example above.
Have you reviewed the multiple datas example? You might want to search that out.
I don't think that this is the only thing you are missing. To initialize the value is very easy:
self.l.f1 = 0.0
But here is couple other items to correct:
you call undefined variables such as self.f1 or self.pf - this is the main reason of the error returned
this in the next()
self.v3 = bt.ind.Lowest(self.pf, period=self.p.length)
self.v4 = bt.ind.Highest(self.pf, period=self.p.length) - self.v3
.... self.v3 ....
.... self.v4 ....
Interesting. In your script you apply the indicator to the self.data0 ie 5 min data feed, but indicator itself works with the self.data1 which is daily bars. I would assume that self.addminperiod is also applied to minutes data feed, so you need lot to get 5 bars of daily data feed.
Try this (didn' run it by myself):
lines = ('dayHigh',)
params = (('period', 5),)
self.dayHigh = self.data.high[-self.p.period]
call in the strategy:
self.dayHigh = dummyIndicator(self.data1)
Hi, it should be compresssion = 15. Note: if your data is large, you may want to consider bt.feed.PandasDataDirect that seems to be faster than PandasData. Someone has conducted a perf analysis on this forum. you should be able to find easily. Cheers
Thanks a lot @Zachary-Harris
I've got the same error during live trading through IB (see my post here: https://community.backtrader.com/topic/642/iqfeed-store-and-feed-help-needed/12 ) and your fix seems to be working great.
This seems to be a pretty serious issue that should be fixed in the official source.
I just wonder why there are so little reports about it - it should have happen pretty often to everyone using backtrader for live trading.
Just to bring one more point regarding the throttling mechanism. Starting from v.974 of TWS API there is a '+PACEAPI' connection option that is effectively pacing the incoming messages. Quoting (https://www.interactivebrokers.com/en/index.php?f=5061):
API messages sent at a higher rate than 50/second can now be paced by TWS at the 50/second rate instead of potentially causing a disconnection. This is now done automatically by the RTD Server API and can be done with other API technologies by invoking SetConnectOptions("+PACEAPI") prior to eConnect.
Unfortunately I'm not sure IbPy library is updated to include the SetConnectOptions method. AFAIU the IbPy is just generating its source code from the official TWS Java API (by translating it to Python), but it hasn't been updated to the latest version yet. This is probably one of the reasons to directly use the official TWS Python API instead of IbPy.
Hi, IB tells you "No market data permissions for NYSE STK". Have you subscribed to NYSE stocks? If not, that is probably the root cause. If I remember correctly, seeing hist data on TWS and getting this data through API are 2 different things from a subscription perspective.
@ab_trader Thank you for the reply. I wasn't using the condition to check if self.order exists then return.
It worked for me. So anyone having the same issue in the future kindly make sure these 3 things are taken care of. I think I have solved the issue entirely but if not I'll update this in the future.
Use self.order condition in code to check if we already have a similar order in place.
if not pos: # no market / no orders
if self.inds[d]['atr14'][-1] * 10 < (d.high-d.low) and (d.close-d.open)>0:
# self.buy(data=d, size=1)
self.log('We already have a buy order for %s in place' % d._name)
self.log(' BUYING. ||| symbol: %s , atr14 : %.2f , close-open : %.2f , rsi3crossD: %.2f' % (d._name,self.inds[d]['atr14'],(d.close-d.open),self.inds[d]['rsi3crossD']))
self.order[d._name] = self.order_target_value(data=d, target = 30000)
# elif self.inds[d]['cross'] == -1:
# # self.sell(data=d, size=1)
# self.order_target_percent(data=d, target = -0.16)
self.log('We already have a sell order for %s in place' % d._name)
self.order[d._name] = self.close(data=d)
self.log(' SELLING. ||| symbol: %s ,rsi3crossD: %.2f' % (d._name,self.inds[d]['rsi3crossD']))
Make sure that crossovers are used in the way above i.e. crossovercondition==1
Make sure you add the pandas data with parameters timeframe, compression, session start and session end.