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

Need help in finding the pivot high and low



  • I want to find the pivot high and low. For example, if the parameter is 5, the high in 5 days ago is the highest value over the past 11 (5+5+1) days.

    But the error occurs that is "TypeError: 'int' object is not subscriptable"

    The code is:

    class MySignal(bt.Indicator):
        params = (('r_high', 10),('r_low', 10),('l_high', 10),('l_low', 10),)
    
        def __init__(self):
            high = bt.ind.Highest(self.data.high, period=self.p.r_high+self.p.l_high+1)
            low = bt.ind.Lowest(self.data.low, period=self.p.r_low+self.p.l_low+1)
            
            self.pivot_h = 0
            self.pivot_l = 0
            self.pivot_h = bt.If(self.data.high[-self.p.r_high] == high, self.data.high[-self.p.r_high], self.pivot_h[-1])
            self.pivot_l = bt.If(self.data.low[-self.p.r_low] == low, self.data.low[-self.p.r_low], self.pivot_l[-1])
    

    Thanks for help!


  • administrators

    Without looking deeply into the code for other errors:

    @andy11 said in Need help in finding the pivot high and low:

    self.pivot_h = 0
    

    @andy11 said in Need help in finding the pivot high and low:

    ..., self.pivot_h[-1]
    

    @andy11 said in Need help in finding the pivot high and low:

    But the error occurs that is "TypeError: 'int' object is not subscriptable"

    So yes, you are trying to use an int as an array. The same applies to the usage of self.pivot_l[-1].

    Additionally

    @andy11 said in Need help in finding the pivot high and low:

    self.pivot_h = bt.If(self.data.high[-self.p.r_high] == high, self.data.high[-self.p.r_high]
    

    You cannot subscript indicators/lines during __init__ because they haven't been calculated yet. You probably want to read this: Docs - Platform Concepts and read the Stage 1 and Stage 2 sections.

    Without really understanding what you are trying to do, it might seem you want to do the same as the Fractal study. See Docs - Indicators