From e0ea4c86fa1d59093e53eefc64c959448f84b129 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Sun, 1 Jan 2023 21:52:51 +0000 Subject: [PATCH] Implement check risk function --- core/trading/risk.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/core/trading/risk.py b/core/trading/risk.py index 59874b2..1ad20e2 100644 --- a/core/trading/risk.py +++ b/core/trading/risk.py @@ -44,3 +44,35 @@ def check_max_open_trades_per_symbol(risk_model, account_trades): if count >= risk_model.max_open_trades_per_symbol: return False return True + + +def check_risk(risk_model, account, proposed_trade): + """ + Run the risk checks on the account and the proposed trade. + """ + # Check that the max loss is not exceeded + max_loss_check = check_max_loss( + risk_model, account.initial_balance, account.client.get_balance() + ) + if not max_loss_check: + return {"allowed": False, "reason": "Maximum loss exceeded."} + + # Check that the account max trades is not exceeded + account_trades = account.client.get_all_open_trades() # TODO + account_trades.append(proposed_trade) # TODO + + max_open_trades_check = check_max_open_trades(risk_model, account_trades) + if not max_open_trades_check: + return {"allowed": False, "reason": "Maximum open trades exceeded."} + + # Check that the max trades per symbol is not exceeded + max_open_trades_per_symbol_check = check_max_open_trades_per_symbol( + risk_model, account_trades + ) + if not max_open_trades_per_symbol_check: + return {"allowed": False, "reason": "Maximum open trades per symbol exceeded."} + + # Check that the max risk is not exceeded + max_risk_check = check_max_risk(risk_model, account_trades) + if not max_risk_check: + return {"allowed": False, "reason": "Maximum risk exceeded."}