Fix TP/SL calculation and make more tests for profit/loss
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
from decimal import Decimal as D
|
||||
|
||||
from django.test import TestCase
|
||||
|
||||
from core.exchanges.common import convert_open_trades
|
||||
@@ -18,6 +20,7 @@ class LiveTradingTestCase(ElasticMock, LiveBase, TestCase):
|
||||
amount=10,
|
||||
direction="buy",
|
||||
)
|
||||
self.commission = 0.025
|
||||
|
||||
def test_account_functional(self):
|
||||
"""
|
||||
@@ -123,11 +126,19 @@ class LiveTradingTestCase(ElasticMock, LiveBase, TestCase):
|
||||
# trailing_stop_loss=trade_tsl,
|
||||
)
|
||||
|
||||
posted = self.open_trade(complex_trade)
|
||||
print("OPENED", posted)
|
||||
self.open_trade(complex_trade)
|
||||
trades = self.account.client.get_all_open_trades()
|
||||
print("TRADES", trades)
|
||||
trades_converted = convert_open_trades(trades["itemlist"])
|
||||
print("TRADES CONVERTED", trades_converted)
|
||||
closed = self.close_trade(complex_trade)
|
||||
print("CLOSED", closed)
|
||||
self.assertEqual(len(trades_converted), 1)
|
||||
expected_tp_percent = D(1 - self.commission)
|
||||
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"]
|
||||
|
||||
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
|
||||
|
||||
self.assertLess(tp_percent_difference, max_difference)
|
||||
self.assertLess(sl_percent_difference, max_difference)
|
||||
self.close_trade(complex_trade)
|
||||
|
||||
Reference in New Issue
Block a user