Fix profit/loss calculation

This commit is contained in:
2023-01-06 08:52:15 +00:00
parent 1bab2a729b
commit ae42d9b223
4 changed files with 39 additions and 19 deletions

View File

@@ -163,7 +163,7 @@ class CommonTestCase(TestCase):
"""
Test that the SL price to percent conversion works for long trades.
"""
sl_price = 1.1 # 10%
sl_price = 0.9 # 10%
current_price = 1.0
current_units = 1
unrealised_pl = 0
@@ -176,7 +176,7 @@ class CommonTestCase(TestCase):
"""
Test that the SL price to percent conversion works for short trades.
"""
sl_price = 0.9 # 10%
sl_price = 1.1 # 10%
current_price = 1.0
current_units = 1
unrealised_pl = 0
@@ -219,7 +219,7 @@ class CommonTestCase(TestCase):
Test that the SL price to percent conversion works for long trades
with multiple units.
"""
sl_price = 1.1 # -10%
sl_price = 0.9 # -10%
current_price = 1.0
current_units = 10
unrealised_pl = 0
@@ -233,14 +233,14 @@ class CommonTestCase(TestCase):
Test that the SL price to percent conversion works for short trades
with multiple units.
"""
sl_price = 0.9 # -10%
sl_price = 1.2 # -20%
current_price = 1.0
current_units = 10
unrealised_pl = 0
percent = sl_price_to_percent(
sl_price, "short", current_price, current_units, unrealised_pl
)
self.assertEqual(percent, 10)
self.assertEqual(percent, 20)
def test_sl_price_to_percent_change_long_multi_profit(self):
"""

View File

@@ -134,14 +134,10 @@ class LiveTradingTestCase(ElasticMock, LiveBase, TestCase):
expected_sl_percent = D(2 - self.commission)
actual_tp_percent = trades_converted[0]["take_profit_percent"]
actual_sl_percent = trades_converted[0]["stop_loss_percent"]
print("actual_tp_percent", actual_tp_percent)
print("actual_sl_percent", actual_sl_percent)
tp_percent_difference = abs(expected_tp_percent - actual_tp_percent)
sl_percent_difference = abs(expected_sl_percent - actual_sl_percent)
max_difference = D(0.08) # depends on market conditions
print("TP percent difference: {}".format(tp_percent_difference))
print("SL percent difference: {}".format(sl_percent_difference))
self.assertLess(tp_percent_difference, max_difference)
self.assertLess(sl_percent_difference, max_difference)