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/

    Issues in run a strategy

    Indicators/Strategies/Analyzers
    2
    3
    71
    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.
    • Pedro Ivo Paulo da Conceição
      Pedro Ivo Paulo da Conceição last edited by

      Hey guys, i'm new here and learning how to use this lib. I'm trying use somme of Teddy Koker's strategies to test a particular data and i'm having problems in some part of the code.

      here is the original code: https://teddykoker.com/2019/05/improving-cross-sectional-mean-reversion-strategy-in-python/

      the problem is in this part of code when try put the indicators:

      def max_n(array, n):
          return np.argpartition(array, -n)[-n:]
      
      
      class CrossSectionalMR2(bt.Strategy):
          params = (
              ('num_positions', 100),
          )
      
          def __init__(self):
              self.inds = {}
              for d in self.datas:
                  self.inds[d] = {}
      
      #the problem is in the following line
      
                  self.inds[d]["pct"] = bt.indicators.PercentChange(d.close, period=1)
      
          def prenext(self):
              self.next()
      
          def next(self):
              available = list(filter(lambda d: len(d), self.datas))  # only look at data that existed yesterday
              rets = np.zeros(len(available))
              for i, d in enumerate(available):
                  rets[i] = self.inds[d]['pct'][0]
      
              market_ret = np.mean(rets)
              weights = -(rets - market_ret)
              max_weights_index = max_n(np.abs(weights), self.params.num_positions)
              max_weights = weights[max_weights_index]
              weights = weights / np.sum(np.abs(max_weights))
      
              for i, d in enumerate(available):
                  if i in max_weights_index:
                      self.order_target_percent(d, target=weights[i])
                  else:
                      self.order_target_percent(d, 0)
      

      I simply copied the and run in the pycharm. So i got this:

      b9ad7966-58bf-4bb7-9780-04f654cea196-image.png

      A 1 Reply Last reply Reply Quote 0
      • A
        ab_trader last edited by

        The issue is in the max_n function based on the error report. This is not related to bt, so you may want to debug it and check what is going on.

        • If my answer helped, hit reputation up arrow at lower right corner of the post.
        • Python Debugging With Pdb
        • New to python and bt - check this out
        1 Reply Last reply Reply Quote 1
        • A
          ab_trader @Pedro Ivo Paulo da Conceição last edited by

          @Pedro-Ivo-Paulo-da-Conceição said in Issues in run a strategy:

          i'm new here and learning how to use this lib.

          I would recommend you to start with the Quickstart Guide and samples in the Docs in this case, not with the fancy complex solutions of advanced coders.

          • If my answer helped, hit reputation up arrow at lower right corner of the post.
          • Python Debugging With Pdb
          • New to python and bt - check this out
          1 Reply Last reply Reply Quote 1
          • 1 / 1
          • First post
            Last post
          Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors