Check risk management when opening trades with strategies

This commit is contained in:
Mark Veidemanis 2023-01-11 20:48:17 +00:00
parent 3f05553c71
commit 7d0f979a96
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
1 changed files with 14 additions and 0 deletions

View File

@ -5,6 +5,7 @@ from core.exchanges import GenericAPIError, common
from core.lib.notify import sendmsg from core.lib.notify import sendmsg
from core.models import Account, Strategy, Trade from core.models import Account, Strategy, Trade
from core.trading.crossfilter import crossfilter from core.trading.crossfilter import crossfilter
from core.trading.risk import check_risk
from core.util import logs from core.util import logs
log = logs.get_logger(__name__) log = logs.get_logger(__name__)
@ -488,6 +489,19 @@ def execute_strategy(callback, strategy, func):
) )
new_trade.save() 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 # Run the crossfilter to ensure we don't trade the same pair in opposite directions
filtered = crossfilter(account, symbol, direction, func) filtered = crossfilter(account, symbol, direction, func)