Open
Description
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
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)