Fix SL polarity for losses

This commit is contained in:
Mark Veidemanis 2023-01-05 23:58:13 +00:00
parent db870c39c6
commit 1bab2a729b
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
2 changed files with 16 additions and 6 deletions

View File

@ -61,22 +61,28 @@ def sl_price_to_percent(sl_price, side, current_price, current_units, unrealised
change_percent = ((initial_price - D(sl_price)) / initial_price) * 100 change_percent = ((initial_price - D(sl_price)) / initial_price) * 100
# If the trade is long, the SL price will be higher than the initial price. # If the trade is long, the SL price will be higher than the initial price.
if side == "long": # if side == "long":
change_percent *= -1 # change_percent *= -1
if side == "long": if side == "long":
if current_price > initial_price: if D(current_price) > initial_price:
profit = True profit = True
else: else:
profit = False profit = False
else: else:
if current_price < initial_price: if D(current_price) < initial_price:
profit = True profit = True
else: else:
profit = False profit = False
if profit: # if we are in profit on the short side, we want to show a negative loss
change_percent *= -1 if profit and side == "short":
# change_percent *= -1
change_percent = 0 - abs(change_percent)
# if we are in loss on the long side, we want to show a positive loss
if not profit and side == "long":
# change_percent *= -1
change_percent = abs(change_percent)
return round(change_percent, 5) return round(change_percent, 5)

View File

@ -134,10 +134,14 @@ class LiveTradingTestCase(ElasticMock, LiveBase, TestCase):
expected_sl_percent = D(2 - self.commission) expected_sl_percent = D(2 - self.commission)
actual_tp_percent = trades_converted[0]["take_profit_percent"] actual_tp_percent = trades_converted[0]["take_profit_percent"]
actual_sl_percent = trades_converted[0]["stop_loss_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) tp_percent_difference = abs(expected_tp_percent - actual_tp_percent)
sl_percent_difference = abs(expected_sl_percent - actual_sl_percent) sl_percent_difference = abs(expected_sl_percent - actual_sl_percent)
max_difference = D(0.08) # depends on market conditions 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(tp_percent_difference, max_difference)
self.assertLess(sl_percent_difference, max_difference) self.assertLess(sl_percent_difference, max_difference)