Calculate stoploss_from_absolute for shorts
This commit is contained in:
parent
e04956be0e
commit
40cd478c6d
@ -102,9 +102,8 @@ def stoploss_from_open(
|
||||
return max(stoploss, 0.0)
|
||||
|
||||
|
||||
def stoploss_from_absolute(stop_rate: float, current_rate: float) -> float:
|
||||
def stoploss_from_absolute(stop_rate: float, current_rate: float, is_short: bool = False) -> float:
|
||||
"""
|
||||
TODO-lev: Update this method with "is_short" formula
|
||||
Given current price and desired stop price, return a stop loss value that is relative to current
|
||||
price.
|
||||
|
||||
@ -115,6 +114,7 @@ def stoploss_from_absolute(stop_rate: float, current_rate: float) -> float:
|
||||
|
||||
:param stop_rate: Stop loss price.
|
||||
:param current_rate: Current asset price.
|
||||
:param is_short: When true, perform the calculation for short instead of long
|
||||
:return: Positive stop loss value relative to current price
|
||||
"""
|
||||
|
||||
@ -123,6 +123,10 @@ def stoploss_from_absolute(stop_rate: float, current_rate: float) -> float:
|
||||
return 1
|
||||
|
||||
stoploss = 1 - (stop_rate / current_rate)
|
||||
if is_short:
|
||||
stoploss = -stoploss
|
||||
|
||||
# negative stoploss values indicate the requested stop price is higher than the current price
|
||||
return max(stoploss, 0.0)
|
||||
# negative stoploss values indicate the requested stop price is higher/lower
|
||||
# (long/short) than the current price
|
||||
# shorts can yield stoploss values higher than 1, so limit that as well
|
||||
return max(min(stoploss, 1.0), 0.0)
|
||||
|
@ -159,6 +159,13 @@ def test_stoploss_from_absolute():
|
||||
assert stoploss_from_absolute(100, 0) == 1
|
||||
assert stoploss_from_absolute(0, 100) == 1
|
||||
|
||||
assert stoploss_from_absolute(90, 100, True) == 0
|
||||
assert stoploss_from_absolute(100, 100, True) == 0
|
||||
assert stoploss_from_absolute(110, 100, True) == -(1 - (110/100))
|
||||
assert stoploss_from_absolute(100, 0, True) == 1
|
||||
assert stoploss_from_absolute(0, 100, True) == 0
|
||||
assert stoploss_from_absolute(100, 1, True) == 1
|
||||
|
||||
|
||||
# TODO-lev: @pytest.mark.parametrize('candle_type', ['mark', ''])
|
||||
def test_informative_decorator(mocker, default_conf):
|
||||
|
Loading…
Reference in New Issue
Block a user