Write tests for open trade checks
This commit is contained in:
parent
e55f903f42
commit
23faeb6f71
|
@ -64,8 +64,6 @@ class LiveTradingTestCase(ElasticMock, LiveBase, TestCase):
|
|||
closed = self.account.client.close_trade(self.trade.order_id)
|
||||
|
||||
# Check the feedback from closing the trade
|
||||
print("CLOSED", closed)
|
||||
print("TRADE AMOUNT", trade.amount)
|
||||
self.assertEqual(closed["type"], "MARKET_ORDER")
|
||||
self.assertEqual(closed["symbol"], trade.symbol)
|
||||
self.assertEqual(closed["units"], str(0 - int(trade.amount)))
|
||||
|
@ -151,7 +149,6 @@ class LiveTradingTestCase(ElasticMock, LiveBase, TestCase):
|
|||
# Should be over 2% risk
|
||||
trade = self.create_complex_trade("buy", 10, "EUR_USD", 1, 22)
|
||||
allowed = risk.check_risk(self.risk_model, self.account, trade)
|
||||
print("ALLOWED", allowed)
|
||||
self.assertFalse(allowed["allowed"])
|
||||
self.assertEqual(allowed["reason"], "Maximum risk exceeded.")
|
||||
|
||||
|
@ -167,6 +164,11 @@ class LiveTradingTestCase(ElasticMock, LiveBase, TestCase):
|
|||
@patch("core.exchanges.oanda.OANDAExchange.get_balance", return_value=100)
|
||||
def test_check_risk_max_open_trades_fail(self, mock_balance):
|
||||
# The maximum open trades is 3. Let's open 2 trades
|
||||
# This would not be allowed by the risk model but we're doing it
|
||||
# manually
|
||||
# Don't be confused by the next test. The max open trades check
|
||||
# fails before the symbol one is run, but yes, they would both
|
||||
# fail.
|
||||
trade1 = self.create_complex_trade("buy", 1, "EUR_USD", 1, 1)
|
||||
self.open_trade(trade1)
|
||||
|
||||
|
@ -184,7 +186,16 @@ class LiveTradingTestCase(ElasticMock, LiveBase, TestCase):
|
|||
|
||||
@patch("core.exchanges.oanda.OANDAExchange.get_balance", return_value=100)
|
||||
def test_check_risk_max_open_trades_per_symbol_fail(self, mock_balance):
|
||||
pass
|
||||
trade1 = self.create_complex_trade("buy", 1, "EUR_USD", 1, 1)
|
||||
self.open_trade(trade1)
|
||||
|
||||
trade2 = self.create_complex_trade("buy", 1, "EUR_USD", 1, 1)
|
||||
|
||||
allowed = risk.check_risk(self.risk_model, self.account, trade2)
|
||||
self.assertFalse(allowed["allowed"])
|
||||
self.assertEqual(allowed["reason"], "Maximum open trades per symbol exceeded.")
|
||||
|
||||
self.close_trade(trade1)
|
||||
|
||||
def test_convert_trades(self):
|
||||
"""
|
||||
|
|
|
@ -74,6 +74,8 @@ def check_max_open_trades_per_symbol(risk_model, account_trades):
|
|||
if symbol not in symbol_map:
|
||||
symbol_map[symbol] = 0
|
||||
symbol_map[symbol] += 1
|
||||
|
||||
print("SUMBOL MAP", symbol_map)
|
||||
for symbol, count in symbol_map.items():
|
||||
if count >= risk_model.max_open_trades_per_symbol:
|
||||
return False
|
||||
|
@ -94,16 +96,12 @@ def check_risk(risk_model, account, proposed_trade):
|
|||
|
||||
# Check that the account max trades is not exceeded
|
||||
account_trades = account.client.get_all_open_trades()
|
||||
print("Account trades: ", account_trades)
|
||||
if isinstance(proposed_trade, Trade):
|
||||
proposed_trade = proposed_trade.__dict__
|
||||
account_trades.append(proposed_trade)
|
||||
print("After append", account_trades)
|
||||
|
||||
account_trades = convert.convert_trades(account_trades)
|
||||
print("After convert", account_trades)
|
||||
account_trades = market.convert_trades_to_usd(account, account_trades)
|
||||
print("After convert to USD", account_trades)
|
||||
|
||||
max_open_trades_check = check_max_open_trades(risk_model, account_trades)
|
||||
print("Max open trades check: ", max_open_trades_check)
|
||||
|
|
Loading…
Reference in New Issue