For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

Preserving a linebuffer type in assignment



  • 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()?


  • administrators

    This is a non-expected usage pattern and for sure one which is not going to work.

    LineBuffer is an internal object which is not meant for user consumption. And of course self.dval1 is 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 = yyy during the __init__ phase, because in that case self.lines is an object, and xxx is 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[0])
    

    The indicator family is big and tries to cover all possible aspects