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 & Machine Learning

    Indicators/Strategies/Analyzers
    2
    2
    1476
    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.
    • Sahli Simo
      Sahli Simo last edited by Sahli Simo

      Lets say that I developed an indicator to predict stock prices for the following 5 days.
      I followed your tutorial and I could write a custom indicator class. But I couldnt assign the values for the coming 4 days for example. Maybe the following code will show the problem

      class AlphayaPredictor(bt.Indicator):
        lines = ('lstm',)
        params = (('period', 5),)
      
        plotinfo = dict(subplot=False)
      
        # def __init__(self):
        #     self.addminperiod(self.params.period)
      
        def next(self):
          # datasum = math.fsum(self.data.get(size=self.params.period))
          current_date = self.datas[0].datetime
          prices = [[self.data.open[-i],
                           self.data.high[-i],
                           self.data.low[-i],
                           self.data.close[-i],
                           self.data.volume[-i]
                           ] for i in range(self.params.period-1,-1,-1)]
          pred_prices = predict_future_prices(prices)    # it returns a list of future prices
      
          for i in range(self.params.period):
                  self.lines.lstm[i] = pred_prices[i]        # <---- here is the problem
          # self.lines.lstm[0] = pred_prices[0]          # <---- this is okay, but it's not my objective.
      

      Could u plz tell me how to do it? I appreciate your help.

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

        You cannot put values into the future, which is what you are trying to do.

        Keep the values in a ring buffer (use for example collections.deque with a maxlen during creation), push the last predicted value and pop the value from the left which is the current value.

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