For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See:

Issues in run a strategy

  • 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:

    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):
        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])
                    self.order_target_percent(d, 0)

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


  • 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.

  • @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.

Log in to reply