Implement TP/SL price to percent conversion

This commit is contained in:
2023-01-05 19:27:59 +00:00
parent a6f9e74ee1
commit d3e2bc8648
6 changed files with 389 additions and 23 deletions

View File

View File

@@ -0,0 +1,229 @@
from django.test import TestCase
from core.exchanges.common import sl_price_to_percent, tp_price_to_percent
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(
tp_price, current_price, current_units, unrealised_pl
)
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(
tp_price, current_price, current_units, unrealised_pl
)
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(
tp_price, current_price, current_units, unrealised_pl
)
self.assertEqual(percent, 20)
def test_tp_price_to_percent_change_short(self):
"""
Test that the TP price to percent conversion works for short trades
when the price has changed.
"""
tp_price = 0.8 # 20%
current_price = 1.1 # + 10%
current_units = 1
unrealised_pl = 0.1 # 10%
percent = tp_price_to_percent(
tp_price, current_price, current_units, unrealised_pl
)
self.assertEqual(percent, 20)
# 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(
tp_price, current_price, current_units, unrealised_pl
)
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(
tp_price, current_price, current_units, unrealised_pl
)
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.
"""
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
)
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.
"""
tp_price = 0.8 # 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
)
self.assertEqual(percent, 20)
# SL
def test_SL_price_to_percent_initial_long(self):
"""
Test that the SL price to percent conversion works for long trades.
"""
sl_price = 1.1 # 10%
current_price = 1.0
current_units = 1
unrealised_pl = 0
percent = sl_price_to_percent(
sl_price, current_price, current_units, unrealised_pl
)
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.
"""
sl_price = 0.9 # 10%
current_price = 1.0
current_units = 1
unrealised_pl = 0
percent = sl_price_to_percent(
sl_price, current_price, current_units, unrealised_pl
)
self.assertEqual(percent, 10)
def test_sl_price_to_percent_change_long(self):
"""
Test that the SL price to percent conversion works for long trades
when the price has changed.
"""
sl_price = 1.2 # 20%
current_price = 1.1 # + 10%
current_units = 1
unrealised_pl = 0.1 # 10%
percent = sl_price_to_percent(
sl_price, current_price, current_units, unrealised_pl
)
self.assertEqual(percent, 20)
def test_sl_price_to_percent_change_short(self):
"""
Test that the SL price to percent conversion works for short trades
when the price has changed.
"""
sl_price = 0.8 # 20%
current_price = 1.1 # + 10%
current_units = 1
unrealised_pl = 0.1 # 10%
percent = sl_price_to_percent(
sl_price, current_price, current_units, unrealised_pl
)
self.assertEqual(percent, 20)
# 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.
"""
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
)
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.
"""
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
)
self.assertEqual(percent, 10)
def test_sl_price_to_percent_change_long_multi(self):
"""
Test that the SL price to percent conversion works for long trades
when the price has changed, with multiple units.
"""
sl_price = 1.2 # 20%
current_price = 1.1 # + 10%
current_units = 10
unrealised_pl = 1 # 10%
percent = sl_price_to_percent(
sl_price, current_price, current_units, unrealised_pl
)
self.assertEqual(percent, 20)
def test_sl_price_to_percent_change_short_multi(self):
"""
Test that the SL price to percent conversion works for short trades
when the price has changed, with multiple units.
"""
sl_price = 0.8 # 20%
current_price = 1.1 # + 10%
current_units = 10
unrealised_pl = 1 # 10%
percent = sl_price_to_percent(
sl_price, current_price, current_units, unrealised_pl
)
self.assertEqual(percent, 20)

View File

@@ -1,7 +1,9 @@
from django.test import TestCase
from core.exchanges.common import convert_open_trades
from core.models import Trade
from core.tests.helpers import ElasticMock, LiveBase
from core.trading.market import get_precision, get_price, get_sl, get_tp
class LiveTradingTestCase(ElasticMock, LiveBase, TestCase):
@@ -25,8 +27,11 @@ class LiveTradingTestCase(ElasticMock, LiveBase, TestCase):
# We need some money to place trades
self.assertTrue(balance > 1000)
def open_trade(self):
posted = self.trade.post()
def open_trade(self, trade=None):
if trade:
posted = trade.post()
else:
posted = self.trade.post()
# Check the opened trade
self.assertEqual(posted["type"], "MARKET_ORDER")
self.assertEqual(posted["symbol"], "EUR_USD")
@@ -35,10 +40,14 @@ class LiveTradingTestCase(ElasticMock, LiveBase, TestCase):
return posted
def close_trade(self):
# refresh the trade to get the trade id
self.trade.refresh_from_db()
closed = self.account.client.close_trade(self.trade.order_id)
def close_trade(self, trade=None):
if trade:
trade.refresh_from_db()
closed = self.account.client.close_trade(trade.order_id)
else:
# refresh the trade to get the trade id
self.trade.refresh_from_db()
closed = self.account.client.close_trade(self.trade.order_id)
# Check the feedback from closing the trade
self.assertEqual(closed["type"], "MARKET_ORDER")
@@ -83,3 +92,42 @@ class LiveTradingTestCase(ElasticMock, LiveBase, TestCase):
"""
Test converting open trades response to Trade-like format.
"""
eur_usd_price = get_price(self.account, "buy", "EUR_USD")
trade_tp = get_tp("buy", 1, eur_usd_price)
trade_sl = get_sl("buy", 2, eur_usd_price)
# trade_tsl = get_sl("buy", 1, eur_usd_price, return_var=True)
# # TP 1% profit
# trade_tp = eur_usd_price * D(1.01)
# # SL 2% loss
# trade_sl = eur_usd_price * D(0.98)
# # TSL 1% loss
# trade_tsl = eur_usd_price * D(0.99)
trade_precision, display_precision = get_precision(self.account, "EUR_USD")
# Round everything to the display precision
trade_tp = round(trade_tp, display_precision)
trade_sl = round(trade_sl, display_precision)
# trade_tsl = round(trade_tsl, display_precision)
complex_trade = Trade.objects.create(
user=self.user,
account=self.account,
symbol="EUR_USD",
time_in_force="FOK",
type="market",
amount=10,
direction="buy",
take_profit=trade_tp,
stop_loss=trade_sl,
# trailing_stop_loss=trade_tsl,
)
posted = self.open_trade(complex_trade)
print("OPENED", posted)
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)