Basic PoG PM Risk Management

# Risk Management Architecture

## Overview

PM (Portfolio Manager) serves as the **Master Controller** for all risk settings across the trading ecosystem. Risk settings are centralized in PM and propagated to both Trader (HyperLiquid) and TraderLighter (Lighter Exchange) services.

## Architecture

“`
+——————+
| PM (Master) |
| Risk Management |
+——–+———+
|
+————–+————–+
| |
+——–v——–+ +———v——–+
| Trader | | TraderLighter |
| (HyperLiquid) | | (Lighter) |
+—————–+ +——————+
“`

## Risk Settings Per Strategy

Each strategy has the following configurable risk parameters:

| Setting | Description | Default |
|———|————-|———|
| **Risk/Trade (%)** | Percentage of equity used as margin per trade | 1% |
| **Stop Loss ($)** | Maximum dollar loss per trade | $5 |
| **Max DD ($)** | Maximum strategy drawdown before kill switch triggers | $50 |

## Data Flow

### Reading Risk Settings

1. PM frontend calls `GET /api/risk/strategies`
2. PM backend aggregates data from both Trader and TraderLighter via their `/api/strategies` endpoints
3. Response includes: `strategy_id`, `symbol`, `enabled`, `risk_per_trade`, `stop_loss_dollars`, `max_strategy_loss`, `is_killed`, `total_pnl`, `model_reward`

### Updating Risk Settings

1. User edits values in PM Risk Management table
2. User clicks “Save” button for that row
3. PM sends `PUT /api/risk/{source}/{strategy_id}/{symbol}` with new settings
4. PM backend forwards to appropriate trader service via `PUT /api/strategies/{strategy_id}/{symbol}/settings`
5. Trader service updates in-memory settings and persists to database
6. PM refreshes the table to confirm changes

### Toggle Enable/Disable

1. User toggles the Enabled switch
2. PM calls `POST /api/risk/{source}/{strategy_id}/{symbol}/enable` or `/disable`
3. Trader service updates strategy state

### Reset Kill Switch

1. User clicks “Reset” button on a killed strategy
2. PM calls `POST /api/risk/{source}/{strategy_id}/{symbol}/reset-kill-switch`
3. Trader service resets kill state and total_pnl

## API Endpoints (PM)

| Method | Endpoint | Description |
|——–|———-|————-|
| GET | `/api/risk/strategies` | Get all strategies with risk settings from all traders |
| PUT | `/api/risk/{source}/{strategy_id}/{symbol}` | Update risk settings for a strategy |
| POST | `/api/risk/{source}/{strategy_id}/{symbol}/enable` | Enable a strategy |
| POST | `/api/risk/{source}/{strategy_id}/{symbol}/disable` | Disable a strategy |
| POST | `/api/risk/{source}/{strategy_id}/{symbol}/reset-kill-switch` | Reset kill switch |
| POST | `/api/risk/bulk-update` | Bulk update multiple strategies |

## Files Modified

### PM (Portfolio Manager)

– `frontend/index.html` – Risk Management section with editable table
– `frontend/static/app.js` – `renderRiskTable()`, `saveRiskSettings()`, `toggleStrategy()`, `resetKillSwitch()`
– `frontend/static/styles.css` – Risk table styling, input groups, toggle switches, kill badges
– `backend/services/trader_client.py` – `get_risk_settings()` now uses `/api/strategies` for full data
– `backend/services/trader_lighter_client.py` – Same update as trader_client

### Trader (HyperLiquid)

– `frontend/index.html` – Removed Strategy Settings Modal
– `frontend/static/app.js` – Removed `openStrategyModal()`, `closeStrategyModal()`, `saveStrategySettings()`
– `frontend/static/styles.css` – Changed strategy-item cursor to default, added reward display styles

### TraderLighter (Lighter Exchange)

– `frontend/index.html` – Removed Strategy Settings Modal
– `frontend/static/app.js` – Same removals as Trader
– `frontend/static/styles.css` – Same styling changes as Trader

## Sidebar Display (Trader/TraderLighter)

Strategies in the left sidebar now display:
– Strategy name
– Model Reward value (R: xxx.x)
– Status badge (Active/Disabled/Killed)

Clicking on strategies no longer opens a modal – all risk configuration is done via PM.

## Kill Switch Behavior

When a strategy’s cumulative loss (`total_pnl`) exceeds `max_strategy_loss`:
1. Strategy is marked as `is_killed = true`
2. Trading is halted for that strategy
3. Kill badge shows “KILLED” in PM Risk Management
4. User must manually reset via PM to resume trading

## Security

– All API endpoints validate path parameters to prevent injection
– Settings are persisted to SQLite database on each update
– WebSocket broadcasts settings changes to connected clients