Page 2
risk.py — Risk management module
risk.py — Risk management moduleOverview
risk.py is the final gate before trade execution: circuit breaker, daily loss limits, minimum gas reserve, concurrent position caps, and related state. It works alongside wallet_router.py, which owns Kelly-style / phase-based position sizing. This module does not recalculate sizes from scratch when wallet_router passes them through; it validates them and applies hard limits.
Final gates (design intent)
Circuit breaker
If portfolio drawdown exceeds settings.CIRCUIT_BREAKER_PERCENT (see config), trading halts until reset.
Gas ceiling
MAX_GAS_GWEI in config.settings / chains; enforced before execution in executor.py (_check_gas_ceiling), not inside RiskManager.check_trade.
Minimum gas reserve
Solana: keep 0.01 SOL; EVM: keep 0.001 native (ETH/BNB/MATIC, etc.).
Daily loss limit
Per-wallet, USD-denominated (uses wallet.daily_loss_limit_eth × native price as the limit scale).
Max concurrent positions
Per-wallet cap from wallet.max_concurrent_positions.
Sizing and RiskCheck
RiskCheckRiskCheck.position_size_eth, position_size_usdc, and related fields are intended to reflect wallet_router.route_trade() output (TradeAllocation), passed into check_trade, with sanity caps (e.g. 90% of balance) and optional USDC-first capital routing when balance allows.
Global drawdown sleep (RiskManager)
RiskManager)GLOBAL_DRAWDOWN_SLEEP_PCT
20.0
Portfolio down ≥20% can trigger sleep.
GLOBAL_DRAWDOWN_SLEEP_HOURS
48.0
48h halt on new entries across all wallets.
Use check_global_drawdown_sleep, is_global_sleep_active, global_sleep_reason, and reset_global_sleep for this path.
Index — module symbol
Module singleton
risk_manager— GlobalRiskManager()instance.
Classes
RiskCheck (dataclass)
Fields:
approved,reason,position_size_eth,position_size_usdc,position_size_pct,use_usdc,gas_gwei,chain,native_token
RiskManager
Class constants:
GLOBAL_DRAWDOWN_SLEEP_PCT,GLOBAL_DRAWDOWN_SLEEP_HOURS
Public API
check_traderecord_trade_openrecord_trade_closerecord_trade_close_ethcheck_circuit_breakerreset_circuit_breakeris_circuit_breaker_tripped(property)check_global_drawdown_sleepis_global_sleep_active(property)global_sleep_reason(property)reset_global_sleep
Private / internal
_get_daily_loss_usd_add_daily_loss_usd_get_daily_loss_add_daily_loss
Related config (not defined in this file)
settings.CIRCUIT_BREAKER_PERCENTsettings.MAX_GAS_GWEIWalletConfig—config.wallets
Links — open the code
Sizing (wallet_router)
Gas ceiling (executor)
If the repository or Shamrock2245 org is private, only people with access (for example org members or paying collaborators you invite) can open those links in the browser.
Local path in a checkout: core/risk.py.
Last updated