Check the max risk relative to the account balance

This commit is contained in:
2023-01-06 07:20:55 +00:00
parent ae42d9b223
commit 93be9e6ffe
4 changed files with 136 additions and 44 deletions

View File

@@ -1,6 +1,3 @@
from core.trading.market import to_currency
def check_max_loss(risk_model, initial_balance, account_balance):
"""
Check that the account balance is within the max loss limit.
@@ -10,23 +7,26 @@ def check_max_loss(risk_model, initial_balance, account_balance):
return account_balance > max_loss
def check_max_risk(risk_model, account_trades):
def check_max_risk(risk_model, account_balance_usd, account_trades):
"""
Check that all of the trades in the account are within the max risk limit.
"""
max_risk_percent = risk_model.max_risk_percent
# Calculate the max risk of the account in USD
max_risk_usd = account_balance_usd * (max_risk_percent / 100)
total_risk = 0
for trade in account_trades:
max_tmp = []
# Need to calculate the max risk in base account currency
# Percentages relate to the price movement, without accounting the size of the trade
if "stop_loss_percent" in trade:
max_tmp.append(trade["stop_loss_percent"])
if "trailing_stop_loss_percent" in trade:
max_tmp.append(trade["trailing_stop_loss_percent"])
# Percentages relate to the price movement, without accounting the
# size of the trade
if "stop_loss_usd" in trade:
max_tmp.append(trade["stop_loss_usd"])
if "trailing_stop_loss_usd" in trade:
max_tmp.append(trade["trailing_stop_loss_usd"])
total_risk += max(max_tmp)
print("Total risk: ", total_risk)
return total_risk < max_risk_percent
allowed = total_risk < max_risk_usd
return allowed
def check_max_open_trades(risk_model, account_trades):