Skip to content

Commit

Permalink
Trailing pct instead of ATR kernc#223
Browse files Browse the repository at this point in the history
  • Loading branch information
zlpatel authored Jun 12, 2021
1 parent 0a76e96 commit 46cffe3
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions backtesting/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,24 @@ def next(self):
trade.sl = min(trade.sl or np.inf,
self.data.Close[index] + self.__atr[index] * self.__n_atr)

class PercentageTrailingStrategy(Strategy):
_sl_percent = 5
def init(self):
super().init()

def set_trailing_sl(self, percentage: float = 5):
self._sl_percent = percentage

def next(self):
super().next()
index = len(self.data)-1
for trade in self.trades:
if trade.is_long:
trade.sl = max(trade.sl or -np.inf,
self.data.Close[index]*(1-(self._sl_percent/100)))
else:
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()):
Expand Down

0 comments on commit 46cffe3

Please sign in to comment.