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/

    Slicing in custom indicator on a different custom indicator gives empty array

    Indicators/Strategies/Analyzers
    1
    1
    113
    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.
    • Escape
      Escape last edited by

      The code below is where the issue is (I think). My slice is returning an empty array, when I would assume it should be returning the specified array length.

      note: period is 1500 bars in the case.

      class CustomIndicator(bt.Indicator):
      
          def __init__(self):
              self.addminperiod(self.p.period)
              self.ac_data_zero = bt.ind.Accum(self.datas[0])
              self.ac_data_one = bt.ind.Accum(self.datas[1])
      
          def next(self):
      
              time, day = self.get_datetime()
              last_element = last_element_2 = None
              if time.hour % 8 == 0 and time.minute == 0 and time.second == 0:
                  self.get_data()
                  # do stuff here unrelated
      
          def get_data(self):
              if self.p.ma_filter:
                  d1 = self.ac_data_zero.get(size=100)
                  # this gives an empty array
                  d2 = self.ac_data_zero[-1]
                  # this gives a float value back
                  d1 = self.ac_data_one.get(size=100)
                  # this gives an empty array
                  d2 = self.ac_data_one[-1]
                  # this gives a float value back
      

      data 0 and data 1 are given through this custom indicator

      class NormalizedReturn(bt.Indicator):
          plotinfo = dict(plot=True)
          params = (('period', 1), ('ma_filter', False))
      
          alias = ('Return', 'NormalizedReturn',)
          lines = ('_return',)
          plotlines = dict(VWAP=dict(alpha=1, linestyle='-', linewidth=2.0))
      
          def __init__(self):
              # Before super to ensure mixins (right-hand side in subclassing)
              # can see the assignment operation and operate on the line
              self.addminperiod(2)  # when to deliver values
              if self.p.ma_filter:
                  close = self.datas[0]
                  close_last_bar = self.datas[0](-1)
              else:
                  close = self.datas[0].close
                  close_last_bar = self.datas[0].close(-1)
              norm_return = (close - close_last_bar)/close_last_bar
              self.lines._return = norm_return
      
      

      I feel like missing something basic, but sometimes brain no work well.

      Any help would be greatly appreciated.

      Thanks!

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