MCPcopy Create free account
hub / github.com/kernc/backtesting.py / set_trailing_pct

Method set_trailing_pct

backtesting/lib.py:483–494  ·  view source on GitHub ↗

Set the future trailing stop-loss as some percent (`0 < pct < 1`) below the current price (default 5% below). .. note:: Stop-loss set by `pct` is inexact Stop-loss set by `set_trailing_pct` is converted to units of ATR with `mean(Close * pct / atr)`

(self, pct: float = .05)

Source from the content-addressed store, hash-verified

481 self.__n_atr = n_atr
482
483 def set_trailing_pct(self, pct: float = .05):
484 """
485 Set the future trailing stop-loss as some percent (`0 < pct < 1`)
486 below the current price (default 5% below).
487
488 .. note:: Stop-loss set by `pct` is inexact
489 Stop-loss set by `set_trailing_pct` is converted to units of ATR
490 with `mean(Close * pct / atr)` and set with `set_trailing_sl`.
491 """
492 assert 0 < pct < 1, 'Need pct= as rate, i.e. 5% == 0.05'
493 pct_in_atr = np.mean(self.data.Close * pct / self.__atr) # type: ignore
494 self.set_trailing_sl(pct_in_atr)
495
496 def next(self):
497 super().next()

Callers 1

initMethod · 0.80

Calls 1

set_trailing_slMethod · 0.95

Tested by 1

initMethod · 0.64