|
|
|
@ -16,69 +16,104 @@ class CommonTestCase(TestCase):
|
|
|
|
|
"""
|
|
|
|
|
Test that the TP price to percent conversion works for long trades.
|
|
|
|
|
"""
|
|
|
|
|
tp_price = 1.1 # 10%
|
|
|
|
|
tp_price = D("1.1") # 10%
|
|
|
|
|
current_price = 1.0
|
|
|
|
|
current_units = 1
|
|
|
|
|
unrealised_pl = 0
|
|
|
|
|
expected_percent = 10
|
|
|
|
|
percent = tp_price_to_percent(
|
|
|
|
|
tp_price, "long", current_price, current_units, unrealised_pl
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(percent, 10)
|
|
|
|
|
self.assertEqual(percent, expected_percent)
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
tp_percent_to_price(
|
|
|
|
|
expected_percent, "long", current_price, current_units, unrealised_pl
|
|
|
|
|
),
|
|
|
|
|
tp_price,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
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%
|
|
|
|
|
tp_price = D("0.9") # 10%
|
|
|
|
|
current_price = 1.0
|
|
|
|
|
current_units = 1
|
|
|
|
|
unrealised_pl = 0
|
|
|
|
|
expected_percent = 10
|
|
|
|
|
percent = tp_price_to_percent(
|
|
|
|
|
tp_price, "short", current_price, current_units, unrealised_pl
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(percent, 10)
|
|
|
|
|
self.assertEqual(percent, expected_percent)
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
tp_percent_to_price(
|
|
|
|
|
expected_percent, "short", current_price, current_units, unrealised_pl
|
|
|
|
|
),
|
|
|
|
|
tp_price,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
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%
|
|
|
|
|
tp_price = D("1.2") # 20%
|
|
|
|
|
current_price = 1.1 # + 10%
|
|
|
|
|
current_units = 1
|
|
|
|
|
unrealised_pl = 0.1 # 10%
|
|
|
|
|
expected_percent = 20
|
|
|
|
|
percent = tp_price_to_percent(
|
|
|
|
|
tp_price, "long", current_price, current_units, unrealised_pl
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(percent, 20)
|
|
|
|
|
self.assertEqual(percent, expected_percent)
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
tp_percent_to_price(
|
|
|
|
|
expected_percent, "long", current_price, current_units, unrealised_pl
|
|
|
|
|
),
|
|
|
|
|
tp_price,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
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%
|
|
|
|
|
tp_price = D("0.8") # -20%
|
|
|
|
|
current_price = 0.9 # - 10%
|
|
|
|
|
current_units = 1
|
|
|
|
|
unrealised_pl = -0.1 # -10%
|
|
|
|
|
expected_percent = -20
|
|
|
|
|
percent = tp_price_to_percent(
|
|
|
|
|
tp_price, "long", current_price, current_units, unrealised_pl
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(percent, -20)
|
|
|
|
|
self.assertEqual(percent, expected_percent)
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
tp_percent_to_price(
|
|
|
|
|
expected_percent, "long", current_price, current_units, unrealised_pl
|
|
|
|
|
),
|
|
|
|
|
tp_price,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def test_tp_price_to_percent_change_short_loss(self):
|
|
|
|
|
"""
|
|
|
|
|
Test that the TP price to percent conversion works for short trades
|
|
|
|
|
when the price has changed and the TP is at a loss.
|
|
|
|
|
"""
|
|
|
|
|
tp_price = 1.2 # -20%
|
|
|
|
|
tp_price = D("1.2") # -20%
|
|
|
|
|
current_price = 1.1 # - 10%
|
|
|
|
|
current_units = 1
|
|
|
|
|
unrealised_pl = -0.1 # -10%
|
|
|
|
|
expected_percent = -20
|
|
|
|
|
percent = tp_price_to_percent(
|
|
|
|
|
tp_price, "short", current_price, current_units, unrealised_pl
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(percent, -20)
|
|
|
|
|
self.assertEqual(percent, expected_percent)
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
tp_percent_to_price(
|
|
|
|
|
expected_percent, "short", current_price, current_units, unrealised_pl
|
|
|
|
|
),
|
|
|
|
|
tp_price,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# For multiple units
|
|
|
|
|
def test_tp_price_to_percent_initial_long_multi(self):
|
|
|
|
@ -86,139 +121,209 @@ class CommonTestCase(TestCase):
|
|
|
|
|
Test that the TP price to percent conversion works for long trades
|
|
|
|
|
with multiple units.
|
|
|
|
|
"""
|
|
|
|
|
tp_price = 1.1 # 10%
|
|
|
|
|
tp_price = D("1.1") # 10%
|
|
|
|
|
current_price = 1.0
|
|
|
|
|
current_units = 10
|
|
|
|
|
unrealised_pl = 0
|
|
|
|
|
expected_percent = 10
|
|
|
|
|
percent = tp_price_to_percent(
|
|
|
|
|
tp_price, "long", current_price, current_units, unrealised_pl
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(percent, 10)
|
|
|
|
|
self.assertEqual(percent, expected_percent)
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
tp_percent_to_price(
|
|
|
|
|
expected_percent, "long", current_price, current_units, unrealised_pl
|
|
|
|
|
),
|
|
|
|
|
tp_price,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
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%
|
|
|
|
|
tp_price = D("0.9") # 10%
|
|
|
|
|
current_price = 1.0
|
|
|
|
|
current_units = 10
|
|
|
|
|
unrealised_pl = 0
|
|
|
|
|
expected_percent = 10
|
|
|
|
|
percent = tp_price_to_percent(
|
|
|
|
|
tp_price, "short", current_price, current_units, unrealised_pl
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(percent, 10)
|
|
|
|
|
self.assertEqual(percent, expected_percent)
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
tp_percent_to_price(
|
|
|
|
|
expected_percent, "short", current_price, current_units, unrealised_pl
|
|
|
|
|
),
|
|
|
|
|
tp_price,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
"""
|
|
|
|
|
tp_price = 1.2 # +20%
|
|
|
|
|
tp_price = D("1.2") # +20%
|
|
|
|
|
current_price = 1.1 # +10%
|
|
|
|
|
current_units = 10
|
|
|
|
|
unrealised_pl = 1 # 10%
|
|
|
|
|
expected_percent = 20
|
|
|
|
|
percent = tp_price_to_percent(
|
|
|
|
|
tp_price, "long", current_price, current_units, unrealised_pl
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(percent, 20)
|
|
|
|
|
self.assertEqual(percent, expected_percent)
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
tp_percent_to_price(
|
|
|
|
|
expected_percent, "long", current_price, current_units, unrealised_pl
|
|
|
|
|
),
|
|
|
|
|
tp_price,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
"""
|
|
|
|
|
tp_price = 0.8 # -20%
|
|
|
|
|
tp_price = D("0.8") # -20%
|
|
|
|
|
current_price = 0.9 # -10%
|
|
|
|
|
current_units = 10
|
|
|
|
|
unrealised_pl = 1 # 10%
|
|
|
|
|
expected_percent = 20
|
|
|
|
|
percent = tp_price_to_percent(
|
|
|
|
|
tp_price, "short", current_price, current_units, unrealised_pl
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(percent, 20)
|
|
|
|
|
self.assertEqual(percent, expected_percent)
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
tp_percent_to_price(
|
|
|
|
|
expected_percent, "short", current_price, current_units, unrealised_pl
|
|
|
|
|
),
|
|
|
|
|
tp_price,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
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%
|
|
|
|
|
tp_price = D("0.8") # -20%
|
|
|
|
|
current_price = 0.9 # -10%
|
|
|
|
|
current_units = 10
|
|
|
|
|
unrealised_pl = -1 # -10%
|
|
|
|
|
expected_percent = -20
|
|
|
|
|
percent = tp_price_to_percent(
|
|
|
|
|
tp_price, "long", current_price, current_units, unrealised_pl
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(percent, -20)
|
|
|
|
|
self.assertEqual(percent, expected_percent)
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
tp_percent_to_price(
|
|
|
|
|
expected_percent, "long", current_price, current_units, unrealised_pl
|
|
|
|
|
),
|
|
|
|
|
tp_price,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
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%
|
|
|
|
|
tp_price = D("1.2") # -20%
|
|
|
|
|
current_price = 1.1 # -10%
|
|
|
|
|
current_units = 10
|
|
|
|
|
unrealised_pl = -1 # 10%
|
|
|
|
|
expected_percent = -20
|
|
|
|
|
percent = tp_price_to_percent(
|
|
|
|
|
tp_price, "short", current_price, current_units, unrealised_pl
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(percent, -20)
|
|
|
|
|
self.assertEqual(percent, expected_percent)
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
tp_percent_to_price(
|
|
|
|
|
expected_percent, "short", current_price, current_units, unrealised_pl
|
|
|
|
|
),
|
|
|
|
|
tp_price,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# SL
|
|
|
|
|
def test_sl_price_to_percent_initial_long(self):
|
|
|
|
|
"""
|
|
|
|
|
Test that the SL price to percent conversion works for long trades.
|
|
|
|
|
"""
|
|
|
|
|
sl_price = 0.9 # 10%
|
|
|
|
|
sl_price = D("0.9") # 10%
|
|
|
|
|
current_price = 1.0
|
|
|
|
|
current_units = 1
|
|
|
|
|
unrealised_pl = 0
|
|
|
|
|
expected_percent = 10
|
|
|
|
|
percent = sl_price_to_percent(
|
|
|
|
|
sl_price, "long", current_price, current_units, unrealised_pl
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(percent, 10)
|
|
|
|
|
self.assertEqual(percent, expected_percent)
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
sl_percent_to_price(
|
|
|
|
|
expected_percent, "long", current_price, current_units, unrealised_pl
|
|
|
|
|
),
|
|
|
|
|
sl_price,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def test_sl_price_to_percent_initial_short(self):
|
|
|
|
|
"""
|
|
|
|
|
Test that the SL price to percent conversion works for short trades.
|
|
|
|
|
"""
|
|
|
|
|
sl_price = 1.1 # 10%
|
|
|
|
|
sl_price = D("1.1") # 10%
|
|
|
|
|
current_price = 1.0
|
|
|
|
|
current_units = 1
|
|
|
|
|
unrealised_pl = 0
|
|
|
|
|
expected_percent = 10
|
|
|
|
|
percent = sl_price_to_percent(
|
|
|
|
|
sl_price, "short", current_price, current_units, unrealised_pl
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(percent, 10)
|
|
|
|
|
self.assertEqual(percent, expected_percent)
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
sl_percent_to_price(
|
|
|
|
|
expected_percent, "short", current_price, current_units, unrealised_pl
|
|
|
|
|
),
|
|
|
|
|
sl_price,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def test_sl_price_to_percent_change_long_profit(self):
|
|
|
|
|
"""
|
|
|
|
|
Test that the SL price to percent conversion works for long trades
|
|
|
|
|
when the price has changed and the SL is at a profit.
|
|
|
|
|
"""
|
|
|
|
|
sl_price = 1.2 # +20%
|
|
|
|
|
sl_price = D("1.2") # +20%
|
|
|
|
|
current_price = 1.1 # +10%
|
|
|
|
|
current_units = 1
|
|
|
|
|
unrealised_pl = 0.1 # +10%
|
|
|
|
|
expected_percent = -20
|
|
|
|
|
percent = sl_price_to_percent(
|
|
|
|
|
sl_price, "long", current_price, current_units, unrealised_pl
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(percent, -20)
|
|
|
|
|
self.assertEqual(percent, expected_percent)
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
sl_percent_to_price(
|
|
|
|
|
expected_percent, "long", current_price, current_units, unrealised_pl
|
|
|
|
|
),
|
|
|
|
|
sl_price,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def test_sl_price_to_percent_change_short_profit(self):
|
|
|
|
|
"""
|
|
|
|
|
Test that the SL price to percent conversion works for short trades
|
|
|
|
|
when the price has changed and the SL is at a profit.
|
|
|
|
|
"""
|
|
|
|
|
sl_price = 0.8 # +20%
|
|
|
|
|
sl_price = D("0.8") # +20%
|
|
|
|
|
current_price = 0.9 # +10%
|
|
|
|
|
current_units = 1
|
|
|
|
|
unrealised_pl = 0.1 # +10%
|
|
|
|
|
expected_percent = -20
|
|
|
|
|
percent = sl_price_to_percent(
|
|
|
|
|
sl_price, "short", current_price, current_units, unrealised_pl
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(percent, -20)
|
|
|
|
|
self.assertEqual(percent, expected_percent)
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
sl_percent_to_price(
|
|
|
|
|
expected_percent, "short", current_price, current_units, unrealised_pl
|
|
|
|
|
),
|
|
|
|
|
sl_price,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# For multiple units
|
|
|
|
|
def test_sl_price_to_percent_initial_long_multi(self):
|
|
|
|
@ -226,35 +331,49 @@ class CommonTestCase(TestCase):
|
|
|
|
|
Test that the SL price to percent conversion works for long trades
|
|
|
|
|
with multiple units.
|
|
|
|
|
"""
|
|
|
|
|
sl_price = 0.9 # -10%
|
|
|
|
|
sl_price = D("0.9") # -10%
|
|
|
|
|
current_price = 1.0
|
|
|
|
|
current_units = 10
|
|
|
|
|
unrealised_pl = 0
|
|
|
|
|
expected_percent = 10
|
|
|
|
|
percent = sl_price_to_percent(
|
|
|
|
|
sl_price, "long", current_price, current_units, unrealised_pl
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(percent, 10)
|
|
|
|
|
self.assertEqual(percent, expected_percent)
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
sl_percent_to_price(
|
|
|
|
|
expected_percent, "long", current_price, current_units, unrealised_pl
|
|
|
|
|
),
|
|
|
|
|
sl_price,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
"""
|
|
|
|
|
sl_price = 1.2 # -20%
|
|
|
|
|
sl_price = D("1.2") # -20%
|
|
|
|
|
current_price = 1.0
|
|
|
|
|
current_units = 10
|
|
|
|
|
unrealised_pl = 0
|
|
|
|
|
expected_percent = 20
|
|
|
|
|
percent = sl_price_to_percent(
|
|
|
|
|
sl_price, "short", current_price, current_units, unrealised_pl
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(percent, 20)
|
|
|
|
|
self.assertEqual(percent, expected_percent)
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
sl_percent_to_price(
|
|
|
|
|
expected_percent, "short", current_price, current_units, unrealised_pl
|
|
|
|
|
),
|
|
|
|
|
sl_price,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def test_sl_price_to_percent_change_long_multi_profit(self):
|
|
|
|
|
"""
|
|
|
|
|
Test that the SL price to percent conversion works for long trades
|
|
|
|
|
when the price has changed, with multiple units, and the SL is at a profit.
|
|
|
|
|
"""
|
|
|
|
|
sl_price = D(1.2) # +20%
|
|
|
|
|
sl_price = D("1.2") # +20%
|
|
|
|
|
current_price = 1.1 # +10%
|
|
|
|
|
current_units = 10
|
|
|
|
|
unrealised_pl = 1 # +10%
|
|
|
|
@ -264,7 +383,7 @@ class CommonTestCase(TestCase):
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(percent, expected_percent)
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
tp_percent_to_price(
|
|
|
|
|
sl_percent_to_price(
|
|
|
|
|
expected_percent, "long", current_price, current_units, unrealised_pl
|
|
|
|
|
),
|
|
|
|
|
sl_price,
|
|
|
|
@ -275,7 +394,7 @@ class CommonTestCase(TestCase):
|
|
|
|
|
Test that the SL price to percent conversion works for short trades
|
|
|
|
|
when the price has changed, with multiple units, and the SL is at a profit.
|
|
|
|
|
"""
|
|
|
|
|
sl_price = D(0.8) # -20%
|
|
|
|
|
sl_price = D("0.8") # -20%
|
|
|
|
|
current_price = 0.9 # +10%
|
|
|
|
|
current_units = 10
|
|
|
|
|
unrealised_pl = 1 # +10%
|
|
|
|
@ -285,7 +404,7 @@ class CommonTestCase(TestCase):
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(percent, expected_percent)
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
tp_percent_to_price(
|
|
|
|
|
sl_percent_to_price(
|
|
|
|
|
expected_percent, "short", current_price, current_units, unrealised_pl
|
|
|
|
|
),
|
|
|
|
|
sl_price,
|
|
|
|
|