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

Indicators - Indexing at __init__()



  • Hello All,
    Can someone help shed some light on a little issue I have? I think I am missing a platform fundamental / concept

    Below is a simple example code that demonstrates my issue.

    Basically I am trying to create a data feed that is calculated at init and compares the current value of a data feed with the previous value before deciding what to assign to another data.

    I seem to be able to get away with it when looking at data.close but not with a data feed created during init self.data.hlc3

    class TestInd(bt.Indicator):
    
        lines = ('sig',)
    
        def __init__(self):
            self.addminperiod(55)
            
            self.data.hlc3 = (self.data.high + self.data.low + self.data.close) / 3
            # This works
            self.data.vd = bt.If(self.data.close[0] >= self.data.close[-1], self.data.volume, -self.data.volume)
            # This does not
            self.data.sd = bt.If(self.data.hlc3[0] >= self.data.hlc3[-1], self.data.volume, -self.data.volume)
    
            self.lines.sig = bt.indicators.EMA(self.data.vd, period=34) - bt.indicators.EMA(self.data.vd, period=55)
    
    

    If someone could jog my memory and remind me what I am doing wrong, that would be great.


  • administrators

    @thatblokedave said in Indicators - Indexing at __init__():

    self.data.vd = bt.If(self.data.close[0] >= self.data.close[-1], self.data.volume, -self.data.volume)

    Wrong indexing. During __init__ you use (0).

    See Docs - Platform Concepts - Stage1 and Stage 2



  • @backtrader Thanks! I knew I was forgetting something. Will give it a go.