Add explicit test for stoploss_from_open

This commit is contained in:
Matthias 2023-03-03 19:56:16 +01:00
parent c03c3a5706
commit d0045673fa
1 changed files with 30 additions and 0 deletions

View File

@ -169,6 +169,36 @@ def test_stoploss_from_open(side, profitrange):
assert pytest.approx(stop_price) == expected_stop_price
@pytest.mark.parametrize("side,rel_stop,curr_profit,leverage,expected", [
# profit range for long is [-1, inf] while for shorts is [-inf, 1]
("long", 0, -1, 1, 1),
("long", 0, 0.1, 1, 0.09090909),
("long", -0.1, 0.1, 1, 0.18181818),
("long", 0.1, 0.2, 1, 0.08333333),
("long", 0.1, 0.5, 1, 0.266666666),
("long", 0.1, 5, 1, 0.816666666), # 500% profit, set stoploss to 10% above open price
("short", 0, 0.1, 1, 0.1111111),
("short", -0.1, 0.1, 1, 0.2222222),
("short", 0.1, 0.2, 1, 0.125),
("short", 0.1, 1, 1, 1),
])
def test_stoploss_from_open_leverage(side, rel_stop, curr_profit, leverage, expected):
stoploss = stoploss_from_open(rel_stop, curr_profit, side == 'short')
assert pytest.approx(stoploss) == expected
open_rate = 100
if stoploss != 1:
if side == 'long':
current_rate = open_rate * (1 + curr_profit)
stop = current_rate * (1 - stoploss)
assert pytest.approx(stop) == open_rate * (1 + rel_stop)
else:
current_rate = open_rate * (1 - curr_profit)
stop = current_rate * (1 + stoploss)
assert pytest.approx(stop) == open_rate * (1 - rel_stop)
def test_stoploss_from_absolute():
assert pytest.approx(stoploss_from_absolute(90, 100)) == 1 - (90 / 100)
assert pytest.approx(stoploss_from_absolute(90, 100)) == 0.1