Skip to content

Commit 8eab87c

Browse files
authored
Trailing pct instead of ATR kernc#223
Add code description and use float type for default percentage value.
1 parent 46cffe3 commit 8eab87c

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

backtesting/lib.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,11 +452,27 @@ def next(self):
452452
self.data.Close[index] + self.__atr[index] * self.__n_atr)
453453

454454
class PercentageTrailingStrategy(Strategy):
455-
_sl_percent = 5
455+
"""
456+
A strategy with automatic trailing stop-loss, trailing the current
457+
price at distance of some percentage. Call
458+
`PercentageTrailingStrategy.set_trailing_sl()` to set said percentage
459+
(`5` by default). See [tutorials] for usage examples.
460+
461+
[tutorials]: index.html#tutorials
462+
463+
Remember to call `super().init()` and `super().next()` in your
464+
overridden methods.
465+
"""
466+
_sl_percent = 5.
456467
def init(self):
457468
super().init()
458469

459470
def set_trailing_sl(self, percentage: float = 5):
471+
assert percentage > 0, "percentage must be greater than 0"
472+
"""
473+
Sets the future trailing stop-loss as some (`percentage`)
474+
percentage away from the current price.
475+
"""
460476
self._sl_percent = percentage
461477

462478
def next(self):

0 commit comments

Comments
 (0)