Fix TP/SL calculation and make more tests for profit/loss

This commit is contained in:
2023-01-05 23:37:50 +00:00
parent 2dfaef324c
commit db870c39c6
3 changed files with 170 additions and 81 deletions

View File

@@ -14,7 +14,7 @@ class CommonTestCase(TestCase):
current_units = 1
unrealised_pl = 0
percent = tp_price_to_percent(
tp_price, current_price, current_units, unrealised_pl
tp_price, "long", current_price, current_units, unrealised_pl
)
self.assertEqual(percent, 10)
@@ -27,7 +27,7 @@ class CommonTestCase(TestCase):
current_units = 1
unrealised_pl = 0
percent = tp_price_to_percent(
tp_price, current_price, current_units, unrealised_pl
tp_price, "short", current_price, current_units, unrealised_pl
)
self.assertEqual(percent, 10)
@@ -41,23 +41,37 @@ class CommonTestCase(TestCase):
current_units = 1
unrealised_pl = 0.1 # 10%
percent = tp_price_to_percent(
tp_price, current_price, current_units, unrealised_pl
tp_price, "long", current_price, current_units, unrealised_pl
)
self.assertEqual(percent, 20)
def test_tp_price_to_percent_change_short(self):
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):
"""
Test that the TP price to percent conversion works for short trades
when the price has changed.
when the price has changed and the TP is at a loss.
"""
tp_price = 0.8 # 20%
current_price = 1.1 # + 10%
tp_price = 1.2 # -20%
current_price = 1.1 # - 10%
current_units = 1
unrealised_pl = 0.1 # 10%
unrealised_pl = -0.1 # -10%
percent = tp_price_to_percent(
tp_price, current_price, current_units, unrealised_pl
tp_price, "short", current_price, current_units, unrealised_pl
)
self.assertEqual(percent, 20)
self.assertEqual(percent, -20)
# For multiple units
def test_tp_price_to_percent_initial_long_multi(self):
@@ -70,7 +84,7 @@ class CommonTestCase(TestCase):
current_units = 10
unrealised_pl = 0
percent = tp_price_to_percent(
tp_price, current_price, current_units, unrealised_pl
tp_price, "long", current_price, current_units, unrealised_pl
)
self.assertEqual(percent, 10)
@@ -84,7 +98,7 @@ class CommonTestCase(TestCase):
current_units = 10
unrealised_pl = 0
percent = tp_price_to_percent(
tp_price, current_price, current_units, unrealised_pl
tp_price, "short", current_price, current_units, unrealised_pl
)
self.assertEqual(percent, 10)
@@ -93,12 +107,12 @@ class CommonTestCase(TestCase):
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%
current_price = 1.1 # + 10%
tp_price = 1.2 # +20%
current_price = 1.1 # +10%
current_units = 10
unrealised_pl = 1 # 10%
percent = tp_price_to_percent(
tp_price, current_price, current_units, unrealised_pl
tp_price, "long", current_price, current_units, unrealised_pl
)
self.assertEqual(percent, 20)
@@ -107,17 +121,45 @@ class CommonTestCase(TestCase):
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%
current_price = 1.1 # + 10%
tp_price = 0.8 # -20%
current_price = 0.9 # -10%
current_units = 10
unrealised_pl = 1 # 10%
percent = tp_price_to_percent(
tp_price, current_price, current_units, unrealised_pl
tp_price, "short", current_price, current_units, unrealised_pl
)
self.assertEqual(percent, 20)
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)
# SL
def test_SL_price_to_percent_initial_long(self):
def test_sl_price_to_percent_initial_long(self):
"""
Test that the SL price to percent conversion works for long trades.
"""
@@ -126,7 +168,7 @@ class CommonTestCase(TestCase):
current_units = 1
unrealised_pl = 0
percent = sl_price_to_percent(
sl_price, current_price, current_units, unrealised_pl
sl_price, "long", current_price, current_units, unrealised_pl
)
self.assertEqual(percent, 10)
@@ -139,37 +181,37 @@ class CommonTestCase(TestCase):
current_units = 1
unrealised_pl = 0
percent = sl_price_to_percent(
sl_price, current_price, current_units, unrealised_pl
sl_price, "short", current_price, current_units, unrealised_pl
)
self.assertEqual(percent, 10)
def test_sl_price_to_percent_change_long(self):
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.
when the price has changed and the SL is at a profit.
"""
sl_price = 1.2 # 20%
current_price = 1.1 # + 10%
sl_price = 1.2 # +20%
current_price = 1.1 # +10%
current_units = 1
unrealised_pl = 0.1 # 10%
unrealised_pl = 0.1 # +10%
percent = sl_price_to_percent(
sl_price, current_price, current_units, unrealised_pl
sl_price, "long", current_price, current_units, unrealised_pl
)
self.assertEqual(percent, 20)
self.assertEqual(percent, -20)
def test_sl_price_to_percent_change_short(self):
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.
when the price has changed and the SL is at a profit.
"""
sl_price = 0.8 # 20%
current_price = 1.1 # + 10%
sl_price = 0.8 # +20%
current_price = 0.9 # +10%
current_units = 1
unrealised_pl = 0.1 # 10%
unrealised_pl = 0.1 # +10%
percent = sl_price_to_percent(
sl_price, current_price, current_units, unrealised_pl
sl_price, "short", current_price, current_units, unrealised_pl
)
self.assertEqual(percent, 20)
self.assertEqual(percent, -20)
# For multiple units
def test_sl_price_to_percent_initial_long_multi(self):
@@ -177,12 +219,12 @@ 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 = 1.1 # -10%
current_price = 1.0
current_units = 10
unrealised_pl = 0
percent = sl_price_to_percent(
sl_price, current_price, current_units, unrealised_pl
sl_price, "long", current_price, current_units, unrealised_pl
)
self.assertEqual(percent, 10)
@@ -191,39 +233,39 @@ 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 = 0.9 # -10%
current_price = 1.0
current_units = 10
unrealised_pl = 0
percent = sl_price_to_percent(
sl_price, current_price, current_units, unrealised_pl
sl_price, "short", current_price, current_units, unrealised_pl
)
self.assertEqual(percent, 10)
def test_sl_price_to_percent_change_long_multi(self):
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.
when the price has changed, with multiple units, and the SL is at a profit.
"""
sl_price = 1.2 # 20%
current_price = 1.1 # + 10%
sl_price = 1.2 # +20%
current_price = 1.1 # +10%
current_units = 10
unrealised_pl = 1 # 10%
unrealised_pl = 1 # +10%
percent = sl_price_to_percent(
sl_price, current_price, current_units, unrealised_pl
sl_price, "long", current_price, current_units, unrealised_pl
)
self.assertEqual(percent, 20)
self.assertEqual(percent, -20)
def test_sl_price_to_percent_change_short_multi(self):
def test_sl_price_to_percent_change_short_multi_profit(self):
"""
Test that the SL price to percent conversion works for short trades
when the price has changed, with multiple units.
when the price has changed, with multiple units, and the SL is at a profit.
"""
sl_price = 0.8 # 20%
current_price = 1.1 # + 10%
sl_price = 0.8 # -20%
current_price = 0.9 # +10%
current_units = 10
unrealised_pl = 1 # 10%
unrealised_pl = 1 # +10%
percent = sl_price_to_percent(
sl_price, current_price, current_units, unrealised_pl
sl_price, "short", current_price, current_units, unrealised_pl
)
self.assertEqual(percent, 20)
self.assertEqual(percent, -20)