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/

    "Standard" Way of Creating a Hankel Matrix

    General Code/Help
    1
    1
    55
    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.
    • Logan Bell
      Logan Bell last edited by

      I am trying to create an indicator which gives a (not necessarily square) Hankel matrix with some input period (number of columns) and some input number of periods (number of rows). Here is what I have come up with:

      class HankelMatrix(bt.Indicator):
          # This indicator is simply a way to store a Hankel matrix
          # given some stream of data.
          
          lines = ('hankel',)
          params = (('period', 20), ('numPeriods', 10),)
          
          def __init__(self):
              self.addminperiod(self.params.period+self.params.numPeriods-1)
          
          def next(self):
              hankelData = self.data.get(
                  size=self.params.period+self.params.numPeriods-1)
              self.hankel = [
                  [hankelData[-i] for i in range(j,j+self.params.period)]
                  for j in range(0,self.params.numPeriods)]
      

      I've tested and it seems to work, but it feels wrong in the sense that it feels like it isn't the "standard" way to implement it in Backtrader. Mostly this feeling stems from that fact that I don't have a great understanding of how exactly data are stored/how to access and manipulate data within indicators. Is there a better solution?

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