-
Notifications
You must be signed in to change notification settings - Fork 181
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
base: master
Are you sure you want to change the base?
Changes from all commits
37af02d
322cba1
abaa863
1f7f093
3184406
45aa480
fdd9e96
aa9dea7
48dab27
7ad843b
3f5ad32
d424266
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 |
---|---|---|
|
@@ -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.") | ||
|
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) |
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