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

Sizer based on ATR

  • Hi!
    I try to implement Sizer based on ATR using bt.indicators.ATR. As a result, I get an error which I don't know how to solve, please help to fix that.

    class AtrBasedPositionSize(bt.Sizer):
        # list of parameters which are configurable for the sizer
        params = (('period', 20),
                  ('risk', 0.05),
                  ('ATR_multiple', 1.5))
        def _getsizing(self, comminfo, cash, data, isbuy):
                return 0
            self.ticker_ATR = bt.indicators.ATR(data, period=self.p.period)
            position_size = int((self.p.risk * // (self.p.ATR_multiple * self.ticker_ATR))
            return position_size

    position_size = int((self.p.risk * // (self.p.ATR_multiple * self.ticker_ATR))
    TypeError: int() argument must be a string, a bytes-like object or a number, not 'LinesOperation'

  • either self.p.ATR_multiple, or self.ticker_ATR is a LinesOperation object. LinesOperation must have a property for the value that you are looking for.

  • Define self.ticker_ATR in the sizer's __init__(), then use self.ticker_ATR[0] in _getsizing() method. Should work.

    If you use ATR in your strategy for other needs, than you can define it on the strategy level once and call in the _getsizing() method.

  • @ab_trader thanks, that works!

Log in to reply