Preserving a linebuffer type in assignment
RandyT last edited by
Having one other struggle here that I am hoping I could get some guidance on.
I have an indicator that I am porting that needs to track a min() value over a rolling window of 10 floats.
In the next() method for the indicator, I have a series of linebuffer values that I am updating from a PandasData feed.
The following bit of code is converting the type of linebuffer for this variable to float as soon as I assign the value to it as follows:
def __init__(self): self.dval1 = bt.linebuffer.LineBuffer() super(IND, self).__init__() def next(self): self.dval1 = min(self.data.low.get(size=10))
As soon as the min assignment happens, the self.dval1 is now type 'float'
How do I preserve that to allow me to get "slices" of these linebuffers for other calcs in next()?
backtrader administrators last edited by backtrader
This is a non-expected usage pattern and for sure one which is not going to work.
LineBufferis an internal object which is not meant for user consumption. And of course
self.dval1is turning into a float, you are assigning a float to the member attribute you created yourself.
This is not the same as
self.lines.xxx = yyyduring the
__init__phase, because in that case
self.linesis an object, and
xxxis constructed by means of
Descriptors, which allows controlling things like assignment (via
__set__). But assignment cannot be controlled on a member attribute you have created.
The use case is actually a lot easier:
def __init__(self): self.dval1 = bt.ind.Lowest(self.data.low, period=10) # or bt.ind.MinN(self.data.low, period=10) def next(self): print('the current lowest value is:', self.dval1)
The indicator family is big and tries to cover all possible aspects