2023-01-05 19:27:59 +00:00
|
|
|
from django.test import TestCase
|
|
|
|
|
2023-01-11 19:46:47 +00:00
|
|
|
from core.exchanges.convert import sl_price_to_percent, tp_price_to_percent
|
2023-01-05 19:27:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CommonTestCase(TestCase):
|
|
|
|
# TP
|
|
|
|
def test_tp_price_to_percent_initial_long(self):
|
|
|
|
"""
|
|
|
|
Test that the TP price to percent conversion works for long trades.
|
|
|
|
"""
|
|
|
|
tp_price = 1.1 # 10%
|
|
|
|
current_price = 1.0
|
|
|
|
current_units = 1
|
|
|
|
unrealised_pl = 0
|
|
|
|
percent = tp_price_to_percent(
|
2023-01-05 23:37:50 +00:00
|
|
|
tp_price, "long", current_price, current_units, unrealised_pl
|
2023-01-05 19:27:59 +00:00
|
|
|
)
|
|
|
|
self.assertEqual(percent, 10)
|
|
|
|
|
|
|
|
def test_tp_price_to_percent_initial_short(self):
|
|
|
|
"""
|
|
|
|
Test that the TP price to percent conversion works for short trades.
|
|
|
|
"""
|
|
|
|
tp_price = 0.9 # 10%
|
|
|
|
current_price = 1.0
|
|
|
|
current_units = 1
|
|
|
|
unrealised_pl = 0
|
|
|
|
percent = tp_price_to_percent(
|
2023-01-05 23:37:50 +00:00
|
|
|
tp_price, "short", current_price, current_units, unrealised_pl
|
2023-01-05 19:27:59 +00:00
|
|
|
)
|
|
|
|
self.assertEqual(percent, 10)
|
|
|
|
|
|
|
|
def test_tp_price_to_percent_change_long(self):
|
|
|
|
"""
|
|
|
|
Test that the TP price to percent conversion works for long trades
|
|
|
|
when the price has changed.
|
|
|
|
"""
|
|
|
|
tp_price = 1.2 # 20%
|
|
|
|
current_price = 1.1 # + 10%
|
|
|
|
current_units = 1
|
|
|
|
unrealised_pl = 0.1 # 10%
|
|
|
|
percent = tp_price_to_percent(
|
2023-01-05 23:37:50 +00:00
|
|
|
tp_price, "long", current_price, current_units, unrealised_pl
|
2023-01-05 19:27:59 +00:00
|
|
|
)
|
|
|
|
self.assertEqual(percent, 20)
|
|
|
|
|
2023-01-05 23:37:50 +00:00
|
|
|
def test_tp_price_to_percent_change_long_loss(self):
|
|
|
|
"""
|
|
|
|
Test that the TP price to percent conversion works for long trades
|
|
|
|
when the price has changed and the TP is at a loss.
|
|
|
|
"""
|
|
|
|
tp_price = 0.8 # -20%
|
|
|
|
current_price = 0.9 # - 10%
|
|
|
|
current_units = 1
|
|
|
|
unrealised_pl = -0.1 # -10%
|
|
|
|
percent = tp_price_to_percent(
|
|
|
|
tp_price, "long", current_price, current_units, unrealised_pl
|
|
|
|
)
|
|
|
|
self.assertEqual(percent, -20)
|
|
|
|
|
|
|
|
def test_tp_price_to_percent_change_short_loss(self):
|
2023-01-05 19:27:59 +00:00
|
|
|
"""
|
|
|
|
Test that the TP price to percent conversion works for short trades
|
2023-01-05 23:37:50 +00:00
|
|
|
when the price has changed and the TP is at a loss.
|
2023-01-05 19:27:59 +00:00
|
|
|
"""
|
2023-01-05 23:37:50 +00:00
|
|
|
tp_price = 1.2 # -20%
|
|
|
|
current_price = 1.1 # - 10%
|
2023-01-05 19:27:59 +00:00
|
|
|
current_units = 1
|
2023-01-05 23:37:50 +00:00
|
|
|
unrealised_pl = -0.1 # -10%
|
2023-01-05 19:27:59 +00:00
|
|
|
percent = tp_price_to_percent(
|
2023-01-05 23:37:50 +00:00
|
|
|
tp_price, "short", current_price, current_units, unrealised_pl
|
2023-01-05 19:27:59 +00:00
|
|
|
)
|
2023-01-05 23:37:50 +00:00
|
|
|
self.assertEqual(percent, -20)
|
2023-01-05 19:27:59 +00:00
|
|
|
|
|
|
|
# For multiple units
|
|
|
|
def test_tp_price_to_percent_initial_long_multi(self):
|
|
|
|
"""
|
|
|
|
Test that the TP price to percent conversion works for long trades
|
|
|
|
with multiple units.
|
|
|
|
"""
|
|
|
|
tp_price = 1.1 # 10%
|
|
|
|
current_price = 1.0
|
|
|
|
current_units = 10
|
|
|
|
unrealised_pl = 0
|
|
|
|
percent = tp_price_to_percent(
|
2023-01-05 23:37:50 +00:00
|
|
|
tp_price, "long", current_price, current_units, unrealised_pl
|
2023-01-05 19:27:59 +00:00
|
|
|
)
|
|
|
|
self.assertEqual(percent, 10)
|
|
|
|
|
|
|
|
def test_tp_price_to_percent_initial_short_multi(self):
|
|
|
|
"""
|
|
|
|
Test that the TP price to percent conversion works for short trades
|
|
|
|
with multiple units.
|
|
|
|
"""
|
|
|
|
tp_price = 0.9 # 10%
|
|
|
|
current_price = 1.0
|
|
|
|
current_units = 10
|
|
|
|
unrealised_pl = 0
|
|
|
|
percent = tp_price_to_percent(
|
2023-01-05 23:37:50 +00:00
|
|
|
tp_price, "short", current_price, current_units, unrealised_pl
|
2023-01-05 19:27:59 +00:00
|
|
|
)
|
|
|
|
self.assertEqual(percent, 10)
|
|
|
|
|
|
|
|
def test_tp_price_to_percent_change_long_multi(self):
|
|
|
|
"""
|
|
|
|
Test that the TP price to percent conversion works for long trades
|
|
|
|
when the price has changed, with multiple units.
|
|
|
|
"""
|
2023-01-05 23:37:50 +00:00
|
|
|
tp_price = 1.2 # +20%
|
|
|
|
current_price = 1.1 # +10%
|
2023-01-05 19:27:59 +00:00
|
|
|
current_units = 10
|
|
|
|
unrealised_pl = 1 # 10%
|
|
|
|
percent = tp_price_to_percent(
|
2023-01-05 23:37:50 +00:00
|
|
|
tp_price, "long", current_price, current_units, unrealised_pl
|
2023-01-05 19:27:59 +00:00
|
|
|
)
|
|
|
|
self.assertEqual(percent, 20)
|
|
|
|
|
|
|
|
def test_tp_price_to_percent_change_short_multi(self):
|
|
|
|
"""
|
|
|
|
Test that the TP price to percent conversion works for short trades
|
|
|
|
when the price has changed, with multiple units.
|
|
|
|
"""
|
2023-01-05 23:37:50 +00:00
|
|
|
tp_price = 0.8 # -20%
|
|
|
|
current_price = 0.9 # -10%
|
2023-01-05 19:27:59 +00:00
|
|
|
current_units = 10
|
|
|
|
unrealised_pl = 1 # 10%
|
|
|
|
percent = tp_price_to_percent(
|
2023-01-05 23:37:50 +00:00
|
|
|
tp_price, "short", current_price, current_units, unrealised_pl
|
2023-01-05 19:27:59 +00:00
|
|
|
)
|
|
|
|
self.assertEqual(percent, 20)
|
|
|
|
|
2023-01-05 23:37:50 +00:00
|
|
|
def test_tp_price_to_percent_change_long_multi_loss(self):
|
|
|
|
"""
|
|
|
|
Test that the TP price to percent conversion works for long trades
|
|
|
|
when the price has changed, with multiple units, and the TP is at a loss.
|
|
|
|
"""
|
|
|
|
tp_price = 0.8 # -20%
|
|
|
|
current_price = 0.9 # -10%
|
|
|
|
current_units = 10
|
|
|
|
unrealised_pl = -1 # -10%
|
|
|
|
percent = tp_price_to_percent(
|
|
|
|
tp_price, "long", current_price, current_units, unrealised_pl
|
|
|
|
)
|
|
|
|
self.assertEqual(percent, -20)
|
|
|
|
|
|
|
|
def test_tp_price_to_percent_change_short_multi_loss(self):
|
|
|
|
"""
|
|
|
|
Test that the TP price to percent conversion works for short trades
|
|
|
|
when the price has changed, with multiple units, and the TP is at a loss.
|
|
|
|
"""
|
|
|
|
tp_price = 1.2 # -20%
|
|
|
|
current_price = 1.1 # -10%
|
|
|
|
current_units = 10
|
|
|
|
unrealised_pl = -1 # 10%
|
|
|
|
percent = tp_price_to_percent(
|
|
|
|
tp_price, "short", current_price, current_units, unrealised_pl
|
|
|
|
)
|
|
|
|
self.assertEqual(percent, -20)
|
|
|
|
|
2023-01-05 19:27:59 +00:00
|
|
|
# SL
|
2023-01-05 23:37:50 +00:00
|
|
|
def test_sl_price_to_percent_initial_long(self):
|
2023-01-05 19:27:59 +00:00
|
|
|
"""
|
|
|
|
Test that the SL price to percent conversion works for long trades.
|
|
|
|
"""
|
2023-01-06 08:52:15 +00:00
|
|
|
sl_price = 0.9 # 10%
|
2023-01-05 19:27:59 +00:00
|
|
|
current_price = 1.0
|
|
|
|
current_units = 1
|
|
|
|
unrealised_pl = 0
|
|
|
|
percent = sl_price_to_percent(
|
2023-01-05 23:37:50 +00:00
|
|
|
sl_price, "long", current_price, current_units, unrealised_pl
|
2023-01-05 19:27:59 +00:00
|
|
|
)
|
|
|
|
self.assertEqual(percent, 10)
|
|
|
|
|
|
|
|
def test_sl_price_to_percent_initial_short(self):
|
|
|
|
"""
|
|
|
|
Test that the SL price to percent conversion works for short trades.
|
|
|
|
"""
|
2023-01-06 08:52:15 +00:00
|
|
|
sl_price = 1.1 # 10%
|
2023-01-05 19:27:59 +00:00
|
|
|
current_price = 1.0
|
|
|
|
current_units = 1
|
|
|
|
unrealised_pl = 0
|
|
|
|
percent = sl_price_to_percent(
|
2023-01-05 23:37:50 +00:00
|
|
|
sl_price, "short", current_price, current_units, unrealised_pl
|
2023-01-05 19:27:59 +00:00
|
|
|
)
|
|
|
|
self.assertEqual(percent, 10)
|
|
|
|
|
2023-01-05 23:37:50 +00:00
|
|
|
def test_sl_price_to_percent_change_long_profit(self):
|
2023-01-05 19:27:59 +00:00
|
|
|
"""
|
|
|
|
Test that the SL price to percent conversion works for long trades
|
2023-01-05 23:37:50 +00:00
|
|
|
when the price has changed and the SL is at a profit.
|
2023-01-05 19:27:59 +00:00
|
|
|
"""
|
2023-01-05 23:37:50 +00:00
|
|
|
sl_price = 1.2 # +20%
|
|
|
|
current_price = 1.1 # +10%
|
2023-01-05 19:27:59 +00:00
|
|
|
current_units = 1
|
2023-01-05 23:37:50 +00:00
|
|
|
unrealised_pl = 0.1 # +10%
|
2023-01-05 19:27:59 +00:00
|
|
|
percent = sl_price_to_percent(
|
2023-01-05 23:37:50 +00:00
|
|
|
sl_price, "long", current_price, current_units, unrealised_pl
|
2023-01-05 19:27:59 +00:00
|
|
|
)
|
2023-01-05 23:37:50 +00:00
|
|
|
self.assertEqual(percent, -20)
|
2023-01-05 19:27:59 +00:00
|
|
|
|
2023-01-05 23:37:50 +00:00
|
|
|
def test_sl_price_to_percent_change_short_profit(self):
|
2023-01-05 19:27:59 +00:00
|
|
|
"""
|
|
|
|
Test that the SL price to percent conversion works for short trades
|
2023-01-05 23:37:50 +00:00
|
|
|
when the price has changed and the SL is at a profit.
|
2023-01-05 19:27:59 +00:00
|
|
|
"""
|
2023-01-05 23:37:50 +00:00
|
|
|
sl_price = 0.8 # +20%
|
|
|
|
current_price = 0.9 # +10%
|
2023-01-05 19:27:59 +00:00
|
|
|
current_units = 1
|
2023-01-05 23:37:50 +00:00
|
|
|
unrealised_pl = 0.1 # +10%
|
2023-01-05 19:27:59 +00:00
|
|
|
percent = sl_price_to_percent(
|
2023-01-05 23:37:50 +00:00
|
|
|
sl_price, "short", current_price, current_units, unrealised_pl
|
2023-01-05 19:27:59 +00:00
|
|
|
)
|
2023-01-05 23:37:50 +00:00
|
|
|
self.assertEqual(percent, -20)
|
2023-01-05 19:27:59 +00:00
|
|
|
|
|
|
|
# For multiple units
|
|
|
|
def test_sl_price_to_percent_initial_long_multi(self):
|
|
|
|
"""
|
|
|
|
Test that the SL price to percent conversion works for long trades
|
|
|
|
with multiple units.
|
|
|
|
"""
|
2023-01-06 08:52:15 +00:00
|
|
|
sl_price = 0.9 # -10%
|
2023-01-05 19:27:59 +00:00
|
|
|
current_price = 1.0
|
|
|
|
current_units = 10
|
|
|
|
unrealised_pl = 0
|
|
|
|
percent = sl_price_to_percent(
|
2023-01-05 23:37:50 +00:00
|
|
|
sl_price, "long", current_price, current_units, unrealised_pl
|
2023-01-05 19:27:59 +00:00
|
|
|
)
|
|
|
|
self.assertEqual(percent, 10)
|
|
|
|
|
|
|
|
def test_sl_price_to_percent_initial_short_multi(self):
|
|
|
|
"""
|
|
|
|
Test that the SL price to percent conversion works for short trades
|
|
|
|
with multiple units.
|
|
|
|
"""
|
2023-01-06 08:52:15 +00:00
|
|
|
sl_price = 1.2 # -20%
|
2023-01-05 19:27:59 +00:00
|
|
|
current_price = 1.0
|
|
|
|
current_units = 10
|
|
|
|
unrealised_pl = 0
|
|
|
|
percent = sl_price_to_percent(
|
2023-01-05 23:37:50 +00:00
|
|
|
sl_price, "short", current_price, current_units, unrealised_pl
|
2023-01-05 19:27:59 +00:00
|
|
|
)
|
2023-01-06 08:52:15 +00:00
|
|
|
self.assertEqual(percent, 20)
|
2023-01-05 19:27:59 +00:00
|
|
|
|
2023-01-05 23:37:50 +00:00
|
|
|
def test_sl_price_to_percent_change_long_multi_profit(self):
|
2023-01-05 19:27:59 +00:00
|
|
|
"""
|
|
|
|
Test that the SL price to percent conversion works for long trades
|
2023-01-05 23:37:50 +00:00
|
|
|
when the price has changed, with multiple units, and the SL is at a profit.
|
2023-01-05 19:27:59 +00:00
|
|
|
"""
|
2023-01-05 23:37:50 +00:00
|
|
|
sl_price = 1.2 # +20%
|
|
|
|
current_price = 1.1 # +10%
|
2023-01-05 19:27:59 +00:00
|
|
|
current_units = 10
|
2023-01-05 23:37:50 +00:00
|
|
|
unrealised_pl = 1 # +10%
|
2023-01-05 19:27:59 +00:00
|
|
|
percent = sl_price_to_percent(
|
2023-01-05 23:37:50 +00:00
|
|
|
sl_price, "long", current_price, current_units, unrealised_pl
|
2023-01-05 19:27:59 +00:00
|
|
|
)
|
2023-01-05 23:37:50 +00:00
|
|
|
self.assertEqual(percent, -20)
|
2023-01-05 19:27:59 +00:00
|
|
|
|
2023-01-05 23:37:50 +00:00
|
|
|
def test_sl_price_to_percent_change_short_multi_profit(self):
|
2023-01-05 19:27:59 +00:00
|
|
|
"""
|
|
|
|
Test that the SL price to percent conversion works for short trades
|
2023-01-05 23:37:50 +00:00
|
|
|
when the price has changed, with multiple units, and the SL is at a profit.
|
2023-01-05 19:27:59 +00:00
|
|
|
"""
|
2023-01-05 23:37:50 +00:00
|
|
|
sl_price = 0.8 # -20%
|
|
|
|
current_price = 0.9 # +10%
|
2023-01-05 19:27:59 +00:00
|
|
|
current_units = 10
|
2023-01-05 23:37:50 +00:00
|
|
|
unrealised_pl = 1 # +10%
|
2023-01-05 19:27:59 +00:00
|
|
|
percent = sl_price_to_percent(
|
2023-01-05 23:37:50 +00:00
|
|
|
sl_price, "short", current_price, current_units, unrealised_pl
|
2023-01-05 19:27:59 +00:00
|
|
|
)
|
2023-01-05 23:37:50 +00:00
|
|
|
self.assertEqual(percent, -20)
|