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/

    Indicator per ticker in multi-ticker strategy

    Indicators/Strategies/Analyzers
    2
    8
    1425
    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 Trade Prophet

      Hello,
      I have implemented an indicator that is initialized in Strategy init for a multi-ticker strategy,
      The indicator's next() is calculated OK for the first ticker(data0) but not for the rest of the tickers

      code:
      Here is the Strategy init code:

      self.lr = {data._name: lr(lr_lookback=self.p.rank_period, ticker_data=data) for data in self.datas}
      

      the indicators code

      class LinearRegression(bt.Indicator):
          lines = ('slope','r2',)
      
          def __init__(self, lr_lookback, ticker_data):
              self.addminperiod(lr_lookback)
              self.ticker_data = ticker_data
              self.lr_lookback = lr_lookback
      
          def next(self):
              samples = self.ticker_data.get(size=self.lr_lookback)# ERROR return 0 for ALL tickers other than the first one
              if len(samples):
                  slope, intercept, r_value, p_value, std_err = stats.linregress(np.arange(self.lr_lookback), np.array(samples))
              else:
                  slope=0
                  r_value=0
              self.lines.slope[0] = slope
              self.lines.r2[0] = r_value ** 2
      

      please advise,
      Thank you

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

        You are never passing any specific data feed to the indicator. And the 1st data get passed automatically.

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

          OK,
          What am I doing wrong?
          How can I pass specific data feed to an indicator?
          Can you give an example?

          Thank you

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

            indicator(datax)
            
            1 Reply Last reply Reply Quote 0
            • T
              Trade Prophet last edited by

              in the next() method?

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

                @trade-prophet said in Indicator per ticker in multi-ticker strategy:

                Here is the Strategy init code:

                self.lr = {data._name: lr(lr_lookback=self.p.rank_period, ticker_data=data) for data in self.datas}
                
                T 1 Reply Last reply Reply Quote 1
                • T
                  Trade Prophet @backtrader last edited by

                  @backtrader
                  Sorry for the late response,
                  in the line

                  self.lr = {data._name: lr(lr_lookback=self.p.rank_period, ticker_data=data) for data in self.datas}
                  

                  I pass the specific data in ... ticker_data=data) for data in self.datas
                  Or am I missing something ?

                  Thank you.

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

                    Docs - Platform Concepts - Data Feeds - Passing them around

                    @backtrader said in Indicator per ticker in multi-ticker strategy:

                    indicator(datax)
                    
                    1 Reply Last reply Reply Quote 0
                    • 1 / 1
                    • First post
                      Last post
                    Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
                    $(document).ready(function () { app.coldLoad(); }); }