From 2dfaef324c6ffed4a8355e3809a3e8af2e9d8a4a Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Thu, 5 Jan 2023 19:48:56 +0000 Subject: [PATCH] Add more tests for risk checking --- core/tests/trading/test_risk.py | 49 +++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/core/tests/trading/test_risk.py b/core/tests/trading/test_risk.py index 50f29aa..6f23d1b 100644 --- a/core/tests/trading/test_risk.py +++ b/core/tests/trading/test_risk.py @@ -274,3 +274,52 @@ class RiskModelTestCase(TestCase): converted = convert_open_trades([trade, trade]) max_risk_check = risk.check_max_risk(self.risk_model, converted) self.assertFalse(max_risk_check) # 10% risk is too much + + def check_max_risk_market_data_fail_multiple_mixed(self): + """ + Check that we can not open a trade outside the max risk limit with market data + and multiple trades, mixing SL and TSL. + """ + trade = { + "id": "abd123", + "symbol": "EUR_USD", + "currentUnits": 100, + "side": "long", + "state": "open", + "price": 1.0, # initial + "unrealizedPL": 0, # price == initial + "stopLossOrder": { + "price": 0.95, # down by 5%, 5% risk + }, + } + trade2 = trade.copy() + trade2["trailingStopLossOrder"] = {"price": 0.95} + del trade2["stopLossOrder"] + + converted = convert_open_trades([trade, trade2]) + max_risk_check = risk.check_max_risk(self.risk_model, converted) + self.assertFalse(max_risk_check) # 10% risk is too much + + def check_max_risk_market_data_fail_multiple_mixed_both(self): + """ + Check that we can not open a trade outside the max risk limit with market data + and multiple trades, mixing SL and TSL, where both are set. + """ + trade = { + "id": "abd123", + "symbol": "EUR_USD", + "currentUnits": 100, + "side": "long", + "state": "open", + "price": 1.0, # initial + "unrealizedPL": 0, # price == initial + "stopLossOrder": { + "price": 0.95, # down by 5%, 5% risk + }, + } + trade2 = trade.copy() + trade2["trailingStopLossOrder"] = {"price": 0.951} + + converted = convert_open_trades([trade, trade2]) + max_risk_check = risk.check_max_risk(self.risk_model, converted) + self.assertFalse(max_risk_check) # 10% risk is too much