Got "IndexError: array index out of range" with current data
-
To access datetime in indicator next function, I pass self.datas[0].datetime as extra data:
class UserStockStrategy(bt.Strategy): def __init__(self): self.indicator = my_indicator(self.datas[0], self.datas[0].datetime) class my_indicator(bt.Indicator): def next(self): print(self.datas[0][0]) #This is ok print( bt.num2date(self.datas[1][0] ) # This line give error at the final, while self.datas[0][0] is avaliable.
I didn't access the futher data, why did I get IndexError.
Could anybody help me, Thanks in advance
-
@yacc-don said in Got "IndexError: array index out of range" with current data:
To access datetime in indicator next function, I pass self.datas[0].datetime as extra data
no, you don't do this. you initiated indicator class with additional argument, but within indicator class this argument is not specified. calling
self.datas[1]
you just refer to 2nd data feed in the
bt
environment, which (my guess) doesn't exist.iirc data feeds are passed to the indicators as default, so this
class UserStockStrategy(bt.Strategy): def __init__(self): self.indicator = my_indicator() class my_indicator(bt.Indicator): def next(self): print(self.datas[0][0]) print(self.datas[0].datetime)
should give you what you want. maybe you need to massage second
print
statement a little bit, it was long time ago when i used it. -
@ab_trader Your solution is very good, thank you.