modify test_handle_trade to test several cases of calc_profit_ratio

This commit is contained in:
Stefano
2025-08-23 17:23:56 +09:00
parent 030bc8641b
commit 24f5096ec6
2 changed files with 21 additions and 2 deletions

View File

@@ -1181,7 +1181,7 @@ class LocalTrade:
""" """
Calculates the profit as ratio (including fee). Calculates the profit as ratio (including fee).
:param rate: rate to compare with. :param rate: rate to compare with.
:param amount: Amount to use for the calculation. Falls back to self.amount if not set. :param amount: Amount to use for the calculation. Falls back to trade.amount if not set.
:param open_rate: open_rate to use. Defaults to self.open_rate if not provided. :param open_rate: open_rate to use. Defaults to self.open_rate if not provided.
:return: profit ratio as float :return: profit ratio as float
""" """

View File

@@ -1528,6 +1528,25 @@ def test_handle_trade(
assert trade.close_date is not None assert trade.close_date is not None
assert trade.exit_reason == "sell_signal1" assert trade.exit_reason == "sell_signal1"
correct_profit_ratio = trade.calc_profit_ratio(
rate=trade.close_rate, amount=trade.amount, open_rate=trade.open_rate
)
profit_ratio_1 = trade.calc_profit_ratio(rate=trade.close_rate, open_rate=trade.open_rate)
profit_ratio_2 = trade.calc_profit_ratio(
rate=trade.close_rate, open_rate=trade.open_rate * 1.02
)
profit_ratio_3 = trade.calc_profit_ratio(rate=trade.close_rate, amount=trade.amount)
profit_ratio_4 = trade.calc_profit_ratio(rate=trade.close_rate)
profit_ratio_5 = trade.calc_profit_ratio(
rate=trade.close_rate, amount=trade.amount, open_rate=trade.open_rate * 1.02
)
assert correct_profit_ratio == close_profit
assert correct_profit_ratio == profit_ratio_1
assert correct_profit_ratio != profit_ratio_2
assert correct_profit_ratio == profit_ratio_3
assert correct_profit_ratio == profit_ratio_4
assert correct_profit_ratio != profit_ratio_5
@pytest.mark.parametrize("is_short", [False, True]) @pytest.mark.parametrize("is_short", [False, True])
def test_handle_overlapping_signals( def test_handle_overlapping_signals(
@@ -5729,7 +5748,7 @@ def test_position_adjust2(mocker, default_conf_usdt, fee) -> None:
@pytest.mark.parametrize( @pytest.mark.parametrize(
"data", "data",
[ [
# tuple 1 - side amount, price # tuple 1 - side, amount, price
# tuple 2 - amount, open_rate, stake_amount, cumulative_profit, realized_profit, rel_profit # tuple 2 - amount, open_rate, stake_amount, cumulative_profit, realized_profit, rel_profit
( (
(("buy", 100, 10), (100.0, 10.0, 1000.0, 0.0, None, None)), (("buy", 100, 10), (100.0, 10.0, 1000.0, 0.0, None, None)),