backtesting.py icon indicating copy to clipboard operation
backtesting.py copied to clipboard

ATR introduces look-ahead bias

Open EladioRocha opened this issue 2 years ago • 6 comments

Expected Behavior

When we trade with trailing stop loss the ATR is calculated. Fo example suppose that we calculate the ATR for 100 periods the first 100 periods should be removed because they don't have values and if you try to open a trade in the period 10 there isn't an available ATR value therefore isn't possible to determine the stop loss. So in that case all the values that contains NaN values should be removed.

Actual Behavior

In the code when the ATR is calculated the values are backward filled with the future values and according to our previous example if a trade is opened in the period 10, then the ATR used is the ATR in the period 101

def set_atr_periods(self, periods: int = 100):
    """
    Set the lookback period for computing ATR. The default value
    of 100 ensures a _stable_ ATR.
    """
    h, l, c_prev = self.data.High, self.data.Low, pd.Series(self.data.Close).shift(1)
    tr = np.max([h - l, (c_prev - h).abs(), (c_prev - l).abs()], axis=0)
    atr = pd.Series(tr).rolling(periods).mean().bfill().values  # Is look-ahead bias introduced using bfill?
    
    self.__atr = atr

Additional info

image

It maintins the ATR calculation and changes until the period 101

My code is the following

class MyStrategy(TrailingStrategy):
    size = 0.3
    atr = 3
    atr_period = 100

    def init(self):
        super().init()
        self.set_trailing_sl(n_atr=self.atr)
        self.set_atr_periods(periods=self.atr_period)

    def next(self):
        super().next()
        
        if not self.position.is_long:
            self.buy(size=self.size)

EladioRocha avatar Apr 15 '23 01:04 EladioRocha

True, the .bfill() call here introduces a brief look-ahead bias.

https://github.com/kernc/backtesting.py/blob/0ce24d80b1bcb8120d95d31dc3bb351b1052a27d/backtesting/lib.py#L466

The assumption was that the ATR for a trailing strategy would be fairly stable.

PR fix welcome!

kernc avatar May 03 '23 15:05 kernc