File tree 2 files changed +15
-1
lines changed
2 files changed +15
-1
lines changed Original file line number Diff line number Diff line change @@ -474,11 +474,24 @@ def set_atr_periods(self, periods: int = 100):
474
474
475
475
def set_trailing_sl (self , n_atr : float = 6 ):
476
476
"""
477
- Sets the future trailing stop-loss as some multiple (`n_atr`)
477
+ Set the future trailing stop-loss as some multiple (`n_atr`)
478
478
average true bar ranges away from the current price.
479
479
"""
480
480
self .__n_atr = n_atr
481
481
482
+ def set_trailing_pct (self , pct : float = .05 ):
483
+ """
484
+ Set the future trailing stop-loss as some percent (`0 < pct < 1`)
485
+ below the current price (default 5% below).
486
+
487
+ .. note:: Stop-loss set by `pct` is inexact
488
+ Stop-loss set by `set_trailing_pct` is converted to units of ATR
489
+ with `mean(Close * pct / atr)` and set with `set_trailing_sl`.
490
+ """
491
+ assert 0 < pct < 1 , 'Need pct= as rate, i.e. 5% == 0.05'
492
+ pct_in_atr = np .mean (self .data .Close * pct / self .__atr ) # type: ignore
493
+ self .set_trailing_sl (pct_in_atr )
494
+
482
495
def next (self ):
483
496
super ().next ()
484
497
# Can't use index=-1 because self.__atr is not an Indicator type
Original file line number Diff line number Diff line change @@ -926,6 +926,7 @@ class S(TrailingStrategy):
926
926
def init (self ):
927
927
super ().init ()
928
928
self .set_atr_periods (40 )
929
+ self .set_trailing_pct (.1 )
929
930
self .set_trailing_sl (3 )
930
931
self .sma = self .I (lambda : self .data .Close .s .rolling (10 ).mean ())
931
932
You can’t perform that action at this time.
0 commit comments