From 7d0f979a96965a978c88a645155e4a7c09706b36 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Wed, 11 Jan 2023 20:48:17 +0000 Subject: [PATCH] Check risk management when opening trades with strategies --- core/trading/market.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/core/trading/market.py b/core/trading/market.py index fe56215..29a2d07 100644 --- a/core/trading/market.py +++ b/core/trading/market.py @@ -5,6 +5,7 @@ from core.exchanges import GenericAPIError, common from core.lib.notify import sendmsg from core.models import Account, Strategy, Trade from core.trading.crossfilter import crossfilter +from core.trading.risk import check_risk from core.util import logs log = logs.get_logger(__name__) @@ -488,6 +489,19 @@ def execute_strategy(callback, strategy, func): ) new_trade.save() + if account.risk_model is not None: + allowed = check_risk(account.risk_model, account, new_trade) + if not allowed["allowed"]: + new_trade.status = "rejected" + new_trade.information = allowed["reason"] + new_trade.save() + sendmsg( + user, + f"Trade rejected due to risk model: {allowed['reason']}", + title="Trade rejected", + ) + return + # Run the crossfilter to ensure we don't trade the same pair in opposite directions filtered = crossfilter(account, symbol, direction, func)