fisk/core/trading/risk.py

47 lines
1.5 KiB
Python

def check_max_loss(risk_model, initial_balance, account_balance):
"""
Check that the account balance is within the max loss limit.
"""
max_loss_percent = risk_model.max_loss_percent
max_loss = initial_balance * (max_loss_percent / 100)
return account_balance > max_loss
def check_max_risk(risk_model, 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
total_risk = 0
for trade in account_trades:
max_tmp = []
if "sl_percent" in trade:
max_tmp.append(trade["sl_percent"])
if "tsl_percent" in trade:
max_tmp.append(trade["tsl_percent"])
total_risk += max(max_tmp)
return total_risk < max_risk_percent
def check_max_open_trades(risk_model, account_trades):
"""
Check that the number of trades in the account is within the max open trades limit.
"""
return len(account_trades) < risk_model.max_open_trades
def check_max_open_trades_per_symbol(risk_model, account_trades):
"""
Check we cannot open more trades per symbol than permissible.
"""
symbol_map = {}
for trade in account_trades:
symbol = trade["symbol"]
if symbol not in symbol_map:
symbol_map[symbol] = 0
symbol_map[symbol] += 1
for symbol, count in symbol_map.items():
if count >= risk_model.max_open_trades_per_symbol:
return False
return True