> For the complete documentation index, see [llms.txt](https://docs.dexlens.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.dexlens.io/projects/page-2.md).

# Page 2

## `risk.py` — Risk management module

### Overview

`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)

| Gate                         | Behavior                                                                                                                                                                     |
| ---------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **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`

**`RiskCheck.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`)

| Class attribute                   | Default | Effect                                              |
| --------------------------------- | ------- | --------------------------------------------------- |
| **`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`** — Global **`RiskManager()`** 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_trade`
* `record_trade_open`
* `record_trade_close`
* `record_trade_close_eth`
* `check_circuit_breaker`
* `reset_circuit_breaker`
* `is_circuit_breaker_tripped` (property)
* `check_global_drawdown_sleep`
* `is_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_PERCENT`**
* **`settings.MAX_GAS_GWEI`**
* **`WalletConfig`** — `config.wallets`

### Links — open the code

| What                         | URL                                                                                                                                                                   |
| ---------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **Implementation**           | [github.com/Shamrock2245/shamrock-trading-bot/blob/main/core/risk.py](https://github.com/Shamrock2245/shamrock-trading-bot/blob/main/core/risk.py)                    |
| **This doc**                 | [github.com/Shamrock2245/shamrock-trading-bot/blob/main/core/README\_risk.md](https://github.com/Shamrock2245/shamrock-trading-bot/blob/main/core/README_risk.md)     |
| **Sizing (`wallet_router`)** | [github.com/Shamrock2245/shamrock-trading-bot/blob/main/core/wallet\_router.py](https://github.com/Shamrock2245/shamrock-trading-bot/blob/main/core/wallet_router.py) |
| **Gas ceiling (`executor`)** | [github.com/Shamrock2245/shamrock-trading-bot/blob/main/core/executor.py](https://github.com/Shamrock2245/shamrock-trading-bot/blob/main/core/executor.py)            |

If the repository or [`Shamrock2245`](https://github.com/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`.
