-
Notifications
You must be signed in to change notification settings - Fork 169
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
Encapsulate Trailing and Stop #43
Open
inverse
wants to merge
12
commits into
ZENALC:master
Choose a base branch
from
inverse:enum-stoptype
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
37af02d
Init
inverse 322cba1
Refactor
inverse abaa863
Update
inverse 1f7f093
Update
inverse 3184406
Refactor and add tests
inverse 45aa480
Consolidate
inverse fdd9e96
Fix up tests
inverse aa9dea7
Tidy
inverse 48dab27
Merge branch 'master' into enum-stoptype
inverse 7ad843b
More tests
inverse 3f5ad32
Fix bad refactor
inverse d424266
Fix tests
inverse File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
from typing import Dict, List, Union | ||
|
||
from algobot.enums import (BEARISH, BULLISH, ENTER_LONG, ENTER_SHORT, | ||
EXIT_LONG, EXIT_SHORT, LONG, SHORT, STOP, TRAILING) | ||
EXIT_LONG, EXIT_SHORT, LONG, SHORT, OrderType) | ||
from algobot.helpers import get_label_string, parse_strategy_name | ||
from algobot.strategies.strategy import Strategy | ||
|
||
|
@@ -224,16 +224,16 @@ def get_stop_loss(self): | |
if self.currentPosition == SHORT: | ||
if self.smartStopLossEnter and self.previousStopLoss > self.currentPrice: | ||
self.stopLoss = self.previousStopLoss | ||
elif self.lossStrategy == TRAILING: | ||
elif self.lossStrategy == OrderType.TRAILING: | ||
self.stopLoss = self.shortTrailingPrice * (1 + self.lossPercentageDecimal) | ||
elif self.lossStrategy == STOP: | ||
elif self.lossStrategy == OrderType.STOP: | ||
self.stopLoss = self.sellShortPrice * (1 + self.lossPercentageDecimal) | ||
elif self.currentPosition == LONG: | ||
if self.smartStopLossEnter and self.previousStopLoss < self.currentPrice: | ||
self.stopLoss = self.previousStopLoss | ||
elif self.lossStrategy == TRAILING: | ||
elif self.lossStrategy == OrderType.TRAILING: | ||
self.stopLoss = self.longTrailingPrice * (1 - self.lossPercentageDecimal) | ||
elif self.lossStrategy == STOP: | ||
elif self.lossStrategy == OrderType.STOP: | ||
self.stopLoss = self.buyLongPrice * (1 - self.lossPercentageDecimal) | ||
|
||
if self.stopLoss is not None: # This is for the smart stop loss to reenter position. | ||
|
@@ -246,9 +246,9 @@ def get_stop_loss_strategy_string(self) -> str: | |
Returns stop loss strategy in string format, instead of integer enum. | ||
:return: Stop loss strategy in string format. | ||
""" | ||
if self.lossStrategy == STOP: | ||
if self.lossStrategy == OrderType.STOP: | ||
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. We should probably deprecate this function and add a kwarg for suffix in the to_str method |
||
return 'Stop Loss' | ||
elif self.lossStrategy == TRAILING: | ||
elif self.lossStrategy == OrderType.TRAILING: | ||
return 'Trailing Loss' | ||
elif self.lossStrategy is None: | ||
return 'None' | ||
|
@@ -320,28 +320,6 @@ def get_profit_percentage(initialNet: float, finalNet: float) -> float: | |
else: | ||
return -1 * (100 - finalNet / initialNet * 100) | ||
|
||
@staticmethod | ||
def get_trailing_or_stop_type_string(stopType: Union[int, None]) -> str: | ||
""" | ||
Returns stop type in string format instead of integer enum. | ||
:return: Stop type in string format. | ||
""" | ||
if stopType == STOP: | ||
return 'Stop' | ||
elif stopType == TRAILING: | ||
return 'Trailing' | ||
elif stopType is None: | ||
return 'None' | ||
else: | ||
raise ValueError("Unknown type of exit position type.") | ||
|
||
@staticmethod | ||
def get_enum_from_str(string): | ||
if string.lower() == "trailing": | ||
return TRAILING | ||
elif string.lower() == 'stop': | ||
return STOP | ||
|
||
@staticmethod | ||
def get_trend_string(trend) -> str: | ||
""" | ||
|
@@ -436,12 +414,12 @@ def get_take_profit(self) -> Union[float, None]: | |
return None | ||
|
||
if self.currentPosition == SHORT: | ||
if self.takeProfitType == STOP: | ||
if self.takeProfitType == OrderType.STOP: | ||
self.takeProfitPoint = self.sellShortPrice * (1 - self.takeProfitPercentageDecimal) | ||
else: | ||
raise ValueError("Invalid type of take profit type provided.") | ||
elif self.currentPosition == LONG: | ||
if self.takeProfitType == STOP: | ||
if self.takeProfitType == OrderType.STOP: | ||
self.takeProfitPoint = self.buyLongPrice * (1 + self.takeProfitPercentageDecimal) | ||
else: | ||
raise ValueError("Invalid type of take profit type provided.") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import unittest | ||
|
||
from algobot.enums import OrderType | ||
|
||
|
||
class OrderTypeTest(unittest.TestCase): | ||
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. let's use pytest? We should soon start rewriting all unit test tests to leverage pytest :p |
||
def test_from_str(self): | ||
self.assertEqual(OrderType.from_str("Stop"), OrderType.STOP) | ||
self.assertEqual(OrderType.from_str("Trailing"), OrderType.TRAILING) | ||
|
||
def test_from_str_unsupported(self): | ||
with self.assertRaises(ValueError): | ||
OrderType.from_str("Random") | ||
|
||
def test_to_str(self): | ||
self.assertEqual(OrderType.to_str(OrderType.STOP), "Stop") | ||
self.assertEqual(OrderType.to_str(OrderType.TRAILING), "Trailing") | ||
self.assertEqual(OrderType.to_str(None), "None") | ||
|
||
def test_to_str_unsupported(self): | ||
with self.assertRaises(ValueError): | ||
OrderType.to_str(100) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Still could be improved but it's a step in the right direction. by improvements we could use real enums and get read of the whole
from_str
but that would require a lot more mapping ;)Like mapping the UI element to string -> enum value :)
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.
nice