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

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

    class TestInd(bt.Indicator):
        lines = ('sig',)
        def __init__(self):
   = ( + + / 3
            # This works
   = bt.If([0] >=[-1],,
            # This does not
   = bt.If([0] >=[-1],,
            self.lines.sig = bt.indicators.EMA(, period=34) - bt.indicators.EMA(, 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__(): = bt.If([0] >=[-1],,

    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.

Log in to reply