mirror of
https://github.com/freqtrade/freqtrade.git
synced 2024-11-13 03:33:55 +00:00
Stop from open with leverage
This commit is contained in:
parent
51c15d894b
commit
027e023443
|
@ -86,7 +86,8 @@ def merge_informative_pair(dataframe: pd.DataFrame, informative: pd.DataFrame,
|
||||||
def stoploss_from_open(
|
def stoploss_from_open(
|
||||||
open_relative_stop: float,
|
open_relative_stop: float,
|
||||||
current_profit: float,
|
current_profit: float,
|
||||||
is_short: bool = False
|
is_short: bool = False,
|
||||||
|
leverage: float = 1.0
|
||||||
) -> float:
|
) -> float:
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
@ -102,21 +103,23 @@ def stoploss_from_open(
|
||||||
: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 is_short: When true, perform the calculation for short instead of long
|
:param is_short: When true, perform the calculation for short instead of long
|
||||||
|
:param leverage: Leverage to use for the calculation
|
||||||
:return: Stop loss value relative to current price
|
:return: Stop loss value relative to current price
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# formula is undefined for current_profit -1 (longs) or 1 (shorts), return maximum value
|
# formula is undefined for current_profit -1 (longs) or 1 (shorts), return maximum value
|
||||||
if (current_profit == -1 and not is_short) or (is_short and current_profit == 1):
|
_current_profit = current_profit / leverage
|
||||||
|
if (_current_profit == -1 and not is_short) or (is_short and _current_profit == 1):
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
if is_short is True:
|
if is_short is True:
|
||||||
stoploss = -1 + ((1 - open_relative_stop) / (1 - current_profit))
|
stoploss = -1 + ((1 - open_relative_stop / leverage) / (1 - _current_profit))
|
||||||
else:
|
else:
|
||||||
stoploss = 1 - ((1 + open_relative_stop) / (1 + current_profit))
|
stoploss = 1 - ((1 + open_relative_stop / leverage) / (1 + _current_profit))
|
||||||
|
|
||||||
# negative stoploss values indicate the requested stop price is higher/lower
|
# negative stoploss values indicate the requested stop price is higher/lower
|
||||||
# (long/short) than the current price
|
# (long/short) than the current price
|
||||||
return max(stoploss, 0.0)
|
return max(stoploss * leverage, 0.0)
|
||||||
|
|
||||||
|
|
||||||
def stoploss_from_absolute(stop_rate: float, current_rate: float, is_short: bool = False) -> float:
|
def stoploss_from_absolute(stop_rate: float, current_rate: float, is_short: bool = False) -> float:
|
||||||
|
|
|
@ -177,26 +177,30 @@ def test_stoploss_from_open(side, profitrange):
|
||||||
("long", 0.1, 0.2, 1, 0.08333333),
|
("long", 0.1, 0.2, 1, 0.08333333),
|
||||||
("long", 0.1, 0.5, 1, 0.266666666),
|
("long", 0.1, 0.5, 1, 0.266666666),
|
||||||
("long", 0.1, 5, 1, 0.816666666), # 500% profit, set stoploss to 10% above open price
|
("long", 0.1, 5, 1, 0.816666666), # 500% profit, set stoploss to 10% above open price
|
||||||
|
("long", 0, 5, 10, 3.3333333), # 500% profit, set stoploss break even
|
||||||
|
("long", 0.1, 5, 10, 3.26666666), # 500% profit, set stoploss to 10% above open price
|
||||||
|
("long", -0.1, 5, 10, 3.3999999), # 500% profit, set stoploss to 10% belowopen price
|
||||||
|
|
||||||
("short", 0, 0.1, 1, 0.1111111),
|
("short", 0, 0.1, 1, 0.1111111),
|
||||||
("short", -0.1, 0.1, 1, 0.2222222),
|
("short", -0.1, 0.1, 1, 0.2222222),
|
||||||
("short", 0.1, 0.2, 1, 0.125),
|
("short", 0.1, 0.2, 1, 0.125),
|
||||||
("short", 0.1, 1, 1, 1),
|
("short", 0.1, 1, 1, 1),
|
||||||
|
("short", -0.01, 5, 10, 10.01999999), # 500% profit at 10x
|
||||||
])
|
])
|
||||||
def test_stoploss_from_open_leverage(side, rel_stop, curr_profit, leverage, expected):
|
def test_stoploss_from_open_leverage(side, rel_stop, curr_profit, leverage, expected):
|
||||||
|
|
||||||
stoploss = stoploss_from_open(rel_stop, curr_profit, side == 'short')
|
stoploss = stoploss_from_open(rel_stop, curr_profit, side == 'short', leverage)
|
||||||
assert pytest.approx(stoploss) == expected
|
assert pytest.approx(stoploss) == expected
|
||||||
open_rate = 100
|
open_rate = 100
|
||||||
if stoploss != 1:
|
if stoploss != 1:
|
||||||
if side == 'long':
|
if side == 'long':
|
||||||
current_rate = open_rate * (1 + curr_profit)
|
current_rate = open_rate * (1 + curr_profit / leverage)
|
||||||
stop = current_rate * (1 - stoploss)
|
stop = current_rate * (1 - stoploss / leverage)
|
||||||
assert pytest.approx(stop) == open_rate * (1 + rel_stop)
|
assert pytest.approx(stop) == open_rate * (1 + rel_stop / leverage)
|
||||||
else:
|
else:
|
||||||
current_rate = open_rate * (1 - curr_profit)
|
current_rate = open_rate * (1 - curr_profit / leverage)
|
||||||
stop = current_rate * (1 + stoploss)
|
stop = current_rate * (1 + stoploss / leverage)
|
||||||
assert pytest.approx(stop) == open_rate * (1 - rel_stop)
|
assert pytest.approx(stop) == open_rate * (1 - rel_stop / leverage)
|
||||||
|
|
||||||
|
|
||||||
def test_stoploss_from_absolute():
|
def test_stoploss_from_absolute():
|
||||||
|
|
Loading…
Reference in New Issue
Block a user