Add support for shorts in strategy.stoploss_from_open
Signed-off-by: Guillermo Rodríguez <guillebep@gmail.com>
This commit is contained in:
parent
dd37e5cfb8
commit
d28287880c
@ -1,6 +1,5 @@
|
|||||||
import pandas as pd
|
import pandas as pd
|
||||||
|
|
||||||
from freqtrade.exceptions import OperationalException
|
|
||||||
from freqtrade.exchange import timeframe_to_minutes
|
from freqtrade.exchange import timeframe_to_minutes
|
||||||
|
|
||||||
|
|
||||||
@ -81,29 +80,25 @@ def stoploss_from_open(
|
|||||||
The requested stop can be positive for a stop above the open price, or negative for
|
The requested stop can be positive for a stop above the open price, or negative for
|
||||||
a stop below the open price. The return value is always >= 0.
|
a stop below the open price. The return value is always >= 0.
|
||||||
|
|
||||||
Returns 0 if the resulting stop price would be above the current price.
|
Returns 0 if the resulting stop price would be above/below (longs/shorts) the current price
|
||||||
|
|
||||||
:param open_relative_stop: Desired stop loss percentage relative to open price
|
:param open_relative_stop: Desired stop loss percentage relative to open price
|
||||||
:param current_profit: The current profit percentage
|
:param current_profit: The current profit percentage
|
||||||
|
:param for_short: When true, perform the calculation for short instead of long
|
||||||
:return: Stop loss value relative to current price
|
:return: Stop loss value relative to current price
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# formula is undefined for current_profit -1, return maximum value
|
# formula is undefined for current_profit -1 (longs) or 1 (shorts), return maximum value
|
||||||
if current_profit == -1:
|
if (current_profit == -1 and not for_short) or (for_short and current_profit == 1):
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
if for_short is True:
|
if for_short is True:
|
||||||
# TODO-lev: How would this be calculated for short
|
stoploss = -1+((1-open_relative_stop)/(1-current_profit))
|
||||||
raise OperationalException(
|
|
||||||
"Freqtrade hasn't figured out how to calculated stoploss on shorts")
|
|
||||||
# stoploss = 1-((1+open_relative_stop)/(1+current_profit))
|
|
||||||
else:
|
else:
|
||||||
stoploss = 1-((1+open_relative_stop)/(1+current_profit))
|
stoploss = 1-((1+open_relative_stop)/(1+current_profit))
|
||||||
|
|
||||||
# negative stoploss values indicate the requested stop price is higher than the current price
|
# negative stoploss values indicate the requested stop price is higher/lower
|
||||||
if for_short:
|
# (long/short) than the current price
|
||||||
return min(stoploss, 0.0)
|
|
||||||
else:
|
|
||||||
return max(stoploss, 0.0)
|
return max(stoploss, 0.0)
|
||||||
|
|
||||||
|
|
||||||
|
@ -109,30 +109,44 @@ def test_stoploss_from_open():
|
|||||||
[1, 100, 30],
|
[1, 100, 30],
|
||||||
[100, 10000, 30],
|
[100, 10000, 30],
|
||||||
]
|
]
|
||||||
current_profit_range = [-0.99, 2, 30]
|
# profit range for long is [-1, inf] while for shorts is [-inf, 1]
|
||||||
|
current_profit_range_dict = {'long': [-0.99, 2, 30], 'short': [-2.0, 0.99, 30]}
|
||||||
desired_stop_range = [-0.50, 0.50, 30]
|
desired_stop_range = [-0.50, 0.50, 30]
|
||||||
|
|
||||||
|
for side, current_profit_range in current_profit_range_dict.items():
|
||||||
for open_range in open_price_ranges:
|
for open_range in open_price_ranges:
|
||||||
for open_price in np.linspace(*open_range):
|
for open_price in np.linspace(*open_range):
|
||||||
for desired_stop in np.linspace(*desired_stop_range):
|
for desired_stop in np.linspace(*desired_stop_range):
|
||||||
|
|
||||||
|
if side == 'long':
|
||||||
# -1 is not a valid current_profit, should return 1
|
# -1 is not a valid current_profit, should return 1
|
||||||
assert stoploss_from_open(desired_stop, -1) == 1
|
assert stoploss_from_open(desired_stop, -1) == 1
|
||||||
|
else:
|
||||||
|
# 1 is not a valid current_profit for shorts, should return 1
|
||||||
|
assert stoploss_from_open(desired_stop, 1, True) == 1
|
||||||
|
|
||||||
for current_profit in np.linspace(*current_profit_range):
|
for current_profit in np.linspace(*current_profit_range):
|
||||||
|
if side == 'long':
|
||||||
current_price = open_price * (1 + current_profit)
|
current_price = open_price * (1 + current_profit)
|
||||||
expected_stop_price = open_price * (1 + desired_stop)
|
expected_stop_price = open_price * (1 + desired_stop)
|
||||||
|
|
||||||
stoploss = stoploss_from_open(desired_stop, current_profit)
|
stoploss = stoploss_from_open(desired_stop, current_profit)
|
||||||
|
stop_price = current_price * (1 - stoploss)
|
||||||
|
else:
|
||||||
|
current_price = open_price * (1 - current_profit)
|
||||||
|
expected_stop_price = open_price * (1 - desired_stop)
|
||||||
|
stoploss = stoploss_from_open(desired_stop, current_profit, True)
|
||||||
|
stop_price = current_price * (1 + stoploss)
|
||||||
|
|
||||||
assert stoploss >= 0
|
assert stoploss >= 0
|
||||||
|
# Technically the formula can yield values greater than 1 for shorts
|
||||||
|
# eventhough it doesn't make sense because the position would be liquidated
|
||||||
|
if side == 'long':
|
||||||
assert stoploss <= 1
|
assert stoploss <= 1
|
||||||
|
|
||||||
stop_price = current_price * (1 - stoploss)
|
|
||||||
|
|
||||||
# there is no correct answer if the expected stop price is above
|
# there is no correct answer if the expected stop price is above
|
||||||
# the current price
|
# the current price
|
||||||
if expected_stop_price > current_price:
|
if (side == 'long' and expected_stop_price > current_price) \
|
||||||
|
or (side == 'short' and expected_stop_price < current_price):
|
||||||
assert stoploss == 0
|
assert stoploss == 0
|
||||||
else:
|
else:
|
||||||
assert isclose(stop_price, expected_stop_price, rel_tol=0.00001)
|
assert isclose(stop_price, expected_stop_price, rel_tol=0.00001)
|
||||||
|
Loading…
Reference in New Issue
Block a user