Separate live tests for active management

This commit is contained in:
2023-02-22 07:20:21 +00:00
parent 9c537187f0
commit 682c42c0e8
3 changed files with 153 additions and 102 deletions

View File

@@ -3,7 +3,16 @@ from decimal import Decimal as D
from os import getenv
from unittest.mock import Mock, patch
from core.models import Account, OrderSettings, RiskModel, Strategy, TradingTime, User
from core.models import (
Account,
OrderSettings,
RiskModel,
Strategy,
Trade,
TradingTime,
User,
)
from core.trading import market
# Create patch mixin to mock out the Elastic client
@@ -110,6 +119,75 @@ If you have done this, please see the following line for more information:
if self.fail:
self.skipTest("Live tests aborted")
def open_trade(self, trade=None):
if trade:
posted = trade.post()
else:
trade = self.trade
posted = self.trade.post()
# Check the opened trade
self.assertEqual(posted["type"], "MARKET_ORDER")
self.assertEqual(posted["symbol"], trade.symbol)
if trade.direction == "sell":
self.assertEqual(posted["units"], str(0 - trade.amount))
else:
self.assertEqual(posted["units"], str(trade.amount))
self.assertEqual(posted["timeInForce"], "FOK")
return posted
def close_trade(self, trade=None):
if trade:
trade.refresh_from_db()
closed = self.account.client.close_trade(trade.order_id)
else:
trade = self.trade
# 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")
self.assertEqual(closed["symbol"], trade.symbol)
self.assertEqual(closed["units"], str(0 - int(trade.amount)))
self.assertEqual(closed["timeInForce"], "FOK")
self.assertEqual(closed["reason"], "TRADE_CLOSE")
return closed
def create_complex_trade(self, direction, amount, symbol, tp_percent, sl_percent):
eur_usd_price = market.get_price(self.account, direction, symbol)
trade_tp = market.get_tp(direction, tp_percent, eur_usd_price)
trade_sl = market.get_sl(direction, sl_percent, eur_usd_price)
# trade_tsl = market.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 = market.get_precision(self.account, symbol)
# 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=symbol,
time_in_force="FOK",
type="market",
amount=amount,
direction=direction,
take_profit=trade_tp,
stop_loss=trade_sl,
# trailing_stop_loss=trade_tsl,
)
return complex_trade
class StrategyMixin:
def setUp(self):