-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Trailing pct instead of ATR #223 #377
base: master
Are you sure you want to change the base?
Changes from all commits
46cffe3
8eab87c
d6fa851
b045c7c
3ef557a
898969c
a7732f2
f954742
baeeb5e
8bfd93e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -452,6 +452,43 @@ def next(self): | |||||||||||||||||||||||||||||
self.data.Close[index] + self.__atr[index] * self.__n_atr) | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
class PercentageTrailingStrategy(Strategy): | ||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||
A strategy with automatic trailing stop-loss, trailing the current | ||||||||||||||||||||||||||||||
price at distance of some percentage. Call | ||||||||||||||||||||||||||||||
`PercentageTrailingStrategy.set_trailing_sl()` to set said percentage | ||||||||||||||||||||||||||||||
(`5` by default). See [tutorials] for usage examples. | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
[tutorials]: index.html#tutorials | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
Remember to call `super().init()` and `super().next()` in your | ||||||||||||||||||||||||||||||
overridden methods. | ||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||
_sl_pct = 5. | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
def init(self): | ||||||||||||||||||||||||||||||
super().init() | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
def set_trailing_sl(self, percentage: float = 5): | ||||||||||||||||||||||||||||||
assert percentage > 0, "percentage must be greater than 0" | ||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||
Sets the future trailing stop-loss as some (`percentage`) | ||||||||||||||||||||||||||||||
percentage away from the current price. | ||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||
self._sl_pct = percentage/100 | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
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_pct)) | ||||||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||||||
trade.sl = min(trade.sl or np.inf, | ||||||||||||||||||||||||||||||
self.data.Close[index]*(1+self._sl_pct)) | ||||||||||||||||||||||||||||||
Comment on lines
+482
to
+489
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Before I make this change, I want to let you know that I used |
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
# Prevent pdoc3 documenting __init__ signature of Strategy subclasses | ||||||||||||||||||||||||||||||
for cls in list(globals().values()): | ||||||||||||||||||||||||||||||
if isinstance(cls, type) and issubclass(cls, Strategy): | ||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Everywhere else, the framework uses "pct" and "percent" to really mean rate (e.g. 5% == 0.05). I wonder if here shouldn't be the same.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So are you saying that a user should be inputting 0.05 instead of 5 and the code should use that value as is?