Skip to content

Commit

Permalink
Merge pull request #3 from zlpatel/kernc#223
Browse files Browse the repository at this point in the history
Trailing pct instead of ATR kernc#223
  • Loading branch information
zlpatel authored Jun 13, 2021
2 parents d81ae85 + a7732f2 commit 18ecead
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 0 deletions.
3 changes: 3 additions & 0 deletions backtesting/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@ def next(self):
trade.sl = min(trade.sl or np.inf,
self.data.Close[index] + self.__atr[index] * self.__n_atr)


class PercentageTrailingStrategy(Strategy):
"""
A strategy with automatic trailing stop-loss, trailing the current
Expand All @@ -464,6 +465,7 @@ class PercentageTrailingStrategy(Strategy):
overridden methods.
"""
_sl_percent = 5.

def init(self):
super().init()

Expand All @@ -486,6 +488,7 @@ def next(self):
trade.sl = min(trade.sl or np.inf,
self.data.Close[index]*(1+(self._sl_percent/100)))


# Prevent pdoc3 documenting __init__ signature of Strategy subclasses
for cls in list(globals().values()):
if isinstance(cls, type) and issubclass(cls, Strategy):
Expand Down
1 change: 1 addition & 0 deletions backtesting/test/_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,7 @@ def next(self):
stats = Backtest(GOOG, S).run()
self.assertEqual(stats['# Trades'], 91)


class TestUtil(TestCase):
def test_as_str(self):
def func():
Expand Down

0 comments on commit 18ecead

Please sign in to comment.