Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    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__()

    General Code/Help
    2
    3
    653
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • T
      ThatBlokeDave last edited by

      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.

      B 1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators @ThatBlokeDave last edited by

        @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

        T 1 Reply Last reply Reply Quote 1
        • T
          ThatBlokeDave @backtrader last edited by

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

          1 Reply Last reply Reply Quote 0
          • 1 / 1
          • First post
            Last post
          Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors