From d0045673faafeb4d6aa4c83f18cb65e192007153 Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 3 Mar 2023 19:56:16 +0100 Subject: [PATCH] Add explicit test for stoploss_from_open --- tests/strategy/test_strategy_helpers.py | 30 +++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/strategy/test_strategy_helpers.py b/tests/strategy/test_strategy_helpers.py index 36e997f7b..cb79ac171 100644 --- a/tests/strategy/test_strategy_helpers.py +++ b/tests/strategy/test_strategy_helpers.py @@ -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