I am trying to create an indicator which gives a (not necessarily square) Hankel matrix with some input period (number of columns) and some input number of periods (number of rows). Here is what I have come up with:
class HankelMatrix(bt.Indicator): # This indicator is simply a way to store a Hankel matrix # given some stream of data. lines = ('hankel',) params = (('period', 20), ('numPeriods', 10),) def __init__(self): self.addminperiod(self.params.period+self.params.numPeriods-1) def next(self): hankelData = self.data.get( size=self.params.period+self.params.numPeriods-1) self.hankel = [ [hankelData[-i] for i in range(j,j+self.params.period)] for j in range(0,self.params.numPeriods)]
I've tested and it seems to work, but it feels wrong in the sense that it feels like it isn't the "standard" way to implement it in Backtrader. Mostly this feeling stems from that fact that I don't have a great understanding of how exactly data are stored/how to access and manipulate data within indicators. Is there a better solution?