2023-01-05 23:37:50 +00:00
|
|
|
from decimal import Decimal as D
|
|
|
|
|
2023-01-01 21:52:43 +00:00
|
|
|
from django.test import TestCase
|
|
|
|
|
2023-01-05 19:27:59 +00:00
|
|
|
from core.exchanges.common import convert_open_trades
|
2023-01-02 18:42:55 +00:00
|
|
|
from core.models import Trade
|
2023-01-01 21:52:43 +00:00
|
|
|
from core.tests.helpers import ElasticMock, LiveBase
|
2023-01-05 19:27:59 +00:00
|
|
|
from core.trading.market import get_precision, get_price, get_sl, get_tp
|
2023-01-01 21:52:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
class LiveTradingTestCase(ElasticMock, LiveBase, TestCase):
|
2023-01-02 18:42:55 +00:00
|
|
|
def setUp(self):
|
|
|
|
super(LiveTradingTestCase, self).setUp()
|
|
|
|
self.trade = Trade.objects.create(
|
|
|
|
user=self.user,
|
|
|
|
account=self.account,
|
|
|
|
symbol="EUR_USD",
|
|
|
|
time_in_force="FOK",
|
|
|
|
type="market",
|
|
|
|
amount=10,
|
|
|
|
direction="buy",
|
|
|
|
)
|
2023-01-05 23:37:50 +00:00
|
|
|
self.commission = 0.025
|
2023-01-02 18:42:55 +00:00
|
|
|
|
2023-01-01 21:52:43 +00:00
|
|
|
def test_account_functional(self):
|
|
|
|
"""
|
|
|
|
Test that the account is functional.
|
|
|
|
"""
|
|
|
|
balance = self.account.client.get_balance()
|
2023-01-02 18:42:55 +00:00
|
|
|
# We need some money to place trades
|
|
|
|
self.assertTrue(balance > 1000)
|
|
|
|
|
2023-01-05 19:27:59 +00:00
|
|
|
def open_trade(self, trade=None):
|
|
|
|
if trade:
|
|
|
|
posted = trade.post()
|
|
|
|
else:
|
|
|
|
posted = self.trade.post()
|
2023-01-02 18:42:55 +00:00
|
|
|
# Check the opened trade
|
|
|
|
self.assertEqual(posted["type"], "MARKET_ORDER")
|
|
|
|
self.assertEqual(posted["symbol"], "EUR_USD")
|
|
|
|
self.assertEqual(posted["units"], "10")
|
|
|
|
self.assertEqual(posted["timeInForce"], "FOK")
|
|
|
|
|
|
|
|
return posted
|
|
|
|
|
2023-01-05 19:27:59 +00:00
|
|
|
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)
|
2023-01-02 18:42:55 +00:00
|
|
|
|
|
|
|
# Check the feedback from closing the trade
|
|
|
|
self.assertEqual(closed["type"], "MARKET_ORDER")
|
|
|
|
self.assertEqual(closed["symbol"], "EUR_USD")
|
|
|
|
self.assertEqual(closed["units"], "-10")
|
|
|
|
self.assertEqual(closed["timeInForce"], "FOK")
|
|
|
|
self.assertEqual(closed["reason"], "TRADE_CLOSE")
|
|
|
|
|
|
|
|
return closed
|
|
|
|
|
|
|
|
def test_place_close_trade(self):
|
|
|
|
"""
|
|
|
|
Test placing a trade.
|
|
|
|
"""
|
|
|
|
self.open_trade()
|
|
|
|
self.close_trade()
|
|
|
|
|
|
|
|
def test_get_all_open_trades(self):
|
|
|
|
"""
|
|
|
|
Test getting all open trades.
|
|
|
|
"""
|
|
|
|
|
|
|
|
self.open_trade()
|
|
|
|
|
|
|
|
trades = self.account.client.get_all_open_trades()
|
|
|
|
self.trade.refresh_from_db()
|
|
|
|
found = False
|
|
|
|
for trade in trades["itemlist"]:
|
|
|
|
if trade["id"] == self.trade.order_id:
|
|
|
|
self.assertEqual(trade["symbol"], "EUR_USD")
|
|
|
|
self.assertEqual(trade["currentUnits"], "10")
|
|
|
|
self.assertEqual(trade["initialUnits"], "10")
|
|
|
|
self.assertEqual(trade["state"], "OPEN")
|
|
|
|
found = True
|
|
|
|
break
|
|
|
|
self.close_trade()
|
|
|
|
|
|
|
|
if not found:
|
|
|
|
self.fail("Could not find the trade in the list of open trades")
|
2023-01-05 17:25:22 +00:00
|
|
|
|
|
|
|
def test_convert_open_trades(self):
|
|
|
|
"""
|
|
|
|
Test converting open trades response to Trade-like format.
|
|
|
|
"""
|
2023-01-05 19:27:59 +00:00
|
|
|
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,
|
|
|
|
)
|
|
|
|
|
2023-01-05 23:37:50 +00:00
|
|
|
self.open_trade(complex_trade)
|
2023-01-05 19:27:59 +00:00
|
|
|
trades = self.account.client.get_all_open_trades()
|
|
|
|
trades_converted = convert_open_trades(trades["itemlist"])
|
2023-01-05 23:37:50 +00:00
|
|
|
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)
|