Add support for shorts in strategy.stoploss_from_open

Signed-off-by: Guillermo Rodríguez <guillebep@gmail.com>
This commit is contained in:
Guillermo Rodríguez
2022-01-15 04:30:30 +01:00
parent dd37e5cfb8
commit d28287880c
2 changed files with 45 additions and 36 deletions

View File

@@ -1,6 +1,5 @@
import pandas as pd
from freqtrade.exceptions import OperationalException
from freqtrade.exchange import timeframe_to_minutes
@@ -81,30 +80,26 @@ def stoploss_from_open(
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.
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 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
"""
# formula is undefined for current_profit -1, return maximum value
if current_profit == -1:
# formula is undefined for current_profit -1 (longs) or 1 (shorts), return maximum value
if (current_profit == -1 and not for_short) or (for_short and current_profit == 1):
return 1
if for_short is True:
# TODO-lev: How would this be calculated for short
raise OperationalException(
"Freqtrade hasn't figured out how to calculated stoploss on shorts")
# stoploss = 1-((1+open_relative_stop)/(1+current_profit))
stoploss = -1+((1-open_relative_stop)/(1-current_profit))
else:
stoploss = 1-((1+open_relative_stop)/(1+current_profit))
# negative stoploss values indicate the requested stop price is higher than the current price
if for_short:
return min(stoploss, 0.0)
else:
return max(stoploss, 0.0)
# negative stoploss values indicate the requested stop price is higher/lower
# (long/short) than the current price
return max(stoploss, 0.0)
def stoploss_from_absolute(stop_rate: float, current_rate: float) -> float: