Simplify test slightly

This commit is contained in:
Matthias 2023-02-18 18:03:07 +01:00
parent c4ec4db050
commit 2c0fbd8500

View File

@ -119,53 +119,54 @@ def test_merge_informative_pair_suffix_append_timeframe():
merge_informative_pair(data, informative, '15m', '1h', suffix="suf") merge_informative_pair(data, informative, '15m', '1h', suffix="suf")
def test_stoploss_from_open(): @pytest.mark.parametrize("side,profitrange", [
# profit range for long is [-1, inf] while for shorts is [-inf, 1]
("long", [-0.99, 2, 30]),
("short", [-2.0, 0.99, 30]),
])
def test_stoploss_from_open(side, profitrange):
open_price_ranges = [ open_price_ranges = [
[0.01, 1.00, 30], [0.01, 1.00, 30],
[1, 100, 30], [1, 100, 30],
[100, 10000, 30], [100, 10000, 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]
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(-0.50, 0.50, 30):
for desired_stop in np.linspace(*desired_stop_range):
if side == 'long':
# -1 is not a valid current_profit, should return 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(*profitrange):
if side == 'long': if side == 'long':
# -1 is not a valid current_profit, should return 1 current_price = open_price * (1 + current_profit)
assert stoploss_from_open(desired_stop, -1) == 1 expected_stop_price = open_price * (1 + desired_stop)
stoploss = stoploss_from_open(desired_stop, current_profit)
stop_price = current_price * (1 - stoploss)
else: else:
# 1 is not a valid current_profit for shorts, should return 1 current_price = open_price * (1 - current_profit)
assert stoploss_from_open(desired_stop, 1, True) == 1 expected_stop_price = open_price * (1 - desired_stop)
stoploss = stoploss_from_open(desired_stop, current_profit, True)
stop_price = current_price * (1 + stoploss)
for current_profit in np.linspace(*current_profit_range): assert stoploss >= 0
if side == 'long': # Technically the formula can yield values greater than 1 for shorts
current_price = open_price * (1 + current_profit) # eventhough it doesn't make sense because the position would be liquidated
expected_stop_price = open_price * (1 + desired_stop) if side == 'long':
stoploss = stoploss_from_open(desired_stop, current_profit) assert stoploss <= 1
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 # there is no correct answer if the expected stop price is above
# Technically the formula can yield values greater than 1 for shorts # the current price
# eventhough it doesn't make sense because the position would be liquidated if ((side == 'long' and expected_stop_price > current_price)
if side == 'long': or (side == 'short' and expected_stop_price < current_price)):
assert stoploss <= 1 assert stoploss == 0
else:
# there is no correct answer if the expected stop price is above assert pytest.approx(stop_price) == expected_stop_price
# the current price
if ((side == 'long' and expected_stop_price > current_price)
or (side == 'short' and expected_stop_price < current_price)):
assert stoploss == 0
else:
assert pytest.approx(stop_price) == expected_stop_price
def test_stoploss_from_absolute(): def test_stoploss_from_absolute():