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/

    Handling multi assets with custom indicator

    Indicators/Strategies/Analyzers
    3
    11
    2494
    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
      Trade Prophet last edited by

      Hello, first of all, thank you for the great professional work!

      I have created a custom indicator,hoping to run it on a list of symbols

      class CLinearRegression(bt.Indicator):
          lines = ('slope','r_squared',)
          params = (('rank_period',90),)
      
          def __init__(self,bar_stream):
              self.addminperiod(self.p.rank_period)
              self.bar_stream = bar_stream
      
          def next(self):
              rank_period_quotes = self.bar_stream.get(size=self.p.rank_period)  # Getting a slice
              x = np.arange(self.p.rank_period)
              slope, intercept, r_value, p_value, std_err = stats.linregress(x, rank_period_quotes)
              self.lines.slope[0] = slope
              self.lines.r_squared[0] = r_value**2
      

      I have loaded a number of assets data and I'm trying to compute the custom indicator,in strategy init, for each asset in the datas list,like this

          def __init__(self):
              self.lrs=dict()
              self.lrs = {data._name: CLinearRegression(bar_stream=data,rank_period=self.p.rank_period)for data in self.datas}
      ...
      

      The indicators Next() runs OK for the first asset , but starts the second asset with the last bar, as if continuing from the first asset , not re-running the custom indicator on the new asset in the list
      What am I doing wrong?

      1 Reply Last reply Reply Quote 0
      • T
        Trade Prophet last edited by

        Anyone?
        Can someone explain why indicators next() method run for second feed starts at the end of the data and does not reiterate (like the first feed)?

        1 Reply Last reply Reply Quote 0
        • T
          Trade Prophet last edited by

          I think there is a bug in the system ( Or I'm doing something wrong),
          ways to reproduce:

          1. Create custom indicator( with init and next methods)
            in next method - print the date of the last bar in data for the custom indicator
          2. In Cerebro main - add a number (>1) of datas (for example , AAPL, XOM,etc) in the main method
          3. In strategy Init-initialize custom indicator on every data in datas
            The BUG -> the data of the last bar is printed OK for the first data(1st ticker) but stays the same for the second,third data
          P 1 Reply Last reply Reply Quote 0
          • P
            Paska Houso @Trade Prophet last edited by Paska Houso

            @Trade-Prophet said in Handling multi assets with custom indicator:

            I think there is a bug in the system ( Or I'm doing something wrong),

            Let me vouch for the second ... the BUG

            @Trade-Prophet said in Handling multi assets with custom indicator:

                def __init__(self,bar_stream):
                    self.addminperiod(self.p.rank_period)
                    self.bar_stream = bar_stream
            

            You are trying to force the data feed (aka bar_stream in your code) as a parameter, thus breaking the platform conventions.

            See:

            • https://community.backtrader.com/topic/753/indicator-slicing/4
            • https://www.backtrader.com/docu/concepts.html (Section: https://www.backtrader.com/docu/concepts.html#data-feeds-passing-them-around)

            You don't need to capture the data feeds as parameters of the strategy or indicators. You simply pass them and they are collected in self.datas

            1 Reply Last reply Reply Quote 1
            • P
              Paska Houso last edited by

              You probably want to have a look at how to develop an indicator

              • Developing an Indicator (Blog): https://www.backtrader.com/blog/posts/2015-07-18-developing-an-indicator/developing-an-indicator.html

              and/or this:

              • Indicator Development (Docs): https://www.backtrader.com/docu/inddev.html
              1 Reply Last reply Reply Quote 1
              • A
                Alon Horesh last edited by Alon Horesh

                oops , mistake ..

                P 1 Reply Last reply Reply Quote 0
                • P
                  Paska Houso @Alon Horesh last edited by Paska Houso

                  @Alon-Horesh said in Handling multi assets with custom indicator:

                  How do I know in the custom indicator start() method which is the relevant data in datas?

                  Sorry but I do really fail to understand the question. Which start() method?

                  May it be that you mean you wanted to do this:

                  self.lrs = {data._name: CLinearRegression(data, rank_period=self.p.rank_period)for data in self.datas}
                  

                  You pass the data you want to linear-regress (to give it a fancy name)

                  1 Reply Last reply Reply Quote 0
                  • T
                    Trade Prophet last edited by

                    Sorry about the mixup in the previous question ..
                    I'm probably missing something,
                    when inside the custom indicator next() method,assuming I have pre-loaded a number of datas (say n in number),which data (data[0],data[1],..data[n]) is the relevant data for the specific linear-regress custom indicator?
                    when the framework calls custom indicators next() method (which will be called n time) which data in datas is this specific instantiation of the indicators data ?
                    hope I made myself clearer ..

                    1 Reply Last reply Reply Quote 0
                    • P
                      Paska Houso last edited by

                      Because you pass the specific data. See the answer above.

                      1 Reply Last reply Reply Quote 0
                      • T
                        Trade Prophet last edited by

                        OK,
                        Thank you again for the high quality implementation and support

                        1 Reply Last reply Reply Quote 0
                        • P
                          Paska Houso last edited by

                          Not mine.

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