# Admin Trade Control - Quick Reference ## 🎯 What It Does Allows **admin users** to pause and resume auto-trading from the dashboard. - **When PAUSED**: No new trade entries are placed - **When RUNNING**: Normal auto-trading resumes - **Always**: Existing positions continue to be managed (exits, SL, TP) --- ## πŸ” Security | Check | Implementation | |-------|----------------| | Authentication | All endpoints require valid JWT token | | Authorization | Pause/Resume require `role = 'admin'` | | UI Guards | Controls hidden for non-admin users | | Audit Logs | All actions logged with user and reason | --- ## πŸ“‘ API Endpoints ### Get Status ```bash GET /internal/trading/status Authorization: Bearer Response: { "mode": "RUNNING", "lastChangedBy": "admin@example.com", "lastChangedAt": 1708200000000, "reason": "Manual resume" } ``` ### Pause Trading ```bash POST /internal/trading/pause Authorization: Bearer Content-Type: application/json { "reason": "Market volatility" } Response: { "success": true, "status": { "mode": "PAUSED", ... } } ``` ### Resume Trading ```bash POST /internal/trading/resume Authorization: Bearer Content-Type: application/json { "reason": "Conditions normalized" } Response: { "success": true, "status": { "mode": "RUNNING", ... } } ``` --- ## πŸ–₯️ UI Components ### Header Badge (All Pages) ``` β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ ⏸️ Trading Paused β”‚ ← Orange when paused β”‚ No new entries β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ ▢️ Trading Active β”‚ ← Green when running β”‚ Entries allowed β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ ``` ### Admin Tab Controls ``` Trading Control β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ AUTO-TRADING: PAUSED β”‚ β”‚ No new positions will be opened. β”‚ β”‚ Existing positions are still managed. β”‚ β”‚ β”‚ β”‚ Last Changed: 2026-02-17 5:30 PM β”‚ β”‚ by admin@example.com β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ ⏸️ Pause Auto β”‚ β”‚ ▢️ Resume Auto β”‚ β”‚ Trading β”‚ β”‚ Trading β”‚ β”‚ (disabled) β”‚ β”‚ (enabled) β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ ⚠️ Safety Note: Pausing blocks new entries only. Existing positions continue to be managed. ``` --- ## πŸ”§ Code Integration ### Check if Paused ```typescript import { healthTracker } from './services/healthTracker'; if (healthTracker.isPaused()) { logger.info('Entry blocked: Trading is paused'); return; } ``` ### Programmatic Control ```typescript // Pause healthTracker.recordTradingControl({ mode: 'PAUSED', lastChangedBy: 'system', lastChangedAt: Date.now(), reason: 'Automated safety pause' }); // Resume healthTracker.recordTradingControl({ mode: 'RUNNING', lastChangedBy: 'system', lastChangedAt: Date.now(), reason: 'Conditions normalized' }); ``` --- ## πŸ›‘οΈ Enforcement Points | File | Line | What It Does | |------|------|--------------| | `AutoTrader.ts` | 106-109 | Blocks new entries when paused | | `TradeExecutor.ts` | 531-534 | Blocks openPosition when paused | **What Continues:** - βœ… Exit orders - βœ… Stop-loss monitoring - βœ… Take-profit monitoring - βœ… Position reconciliation - βœ… Order status sync --- ## πŸ’Ύ State Persistence ``` In-Memory (HealthTracker) ↓ Disk (bot_state.json) ↓ Database (Supabase) ``` **On Restart:** 1. Load from Supabase (preferred) 2. Fallback to `bot_state.json` 3. Default to `RUNNING` if not found --- ## πŸ§ͺ Testing Checklist ### Backend - [ ] Pause blocks new entries - [ ] Resume allows new entries - [ ] Existing positions continue when paused - [ ] State persists across restart - [ ] Non-admin gets 403 ### Frontend - [ ] Pause button disabled when paused - [ ] Resume button disabled when running - [ ] Header badge shows correct status - [ ] Error displayed on API failure - [ ] Loading state shown during API call --- ## πŸ“Š Monitoring ### Metrics to Track - Pause/resume event count - Blocked entry attempts while paused - Pause duration (time between pause/resume) - Who paused (user tracking) ### Alerts - Trading paused > 1 hour - Multiple pause/resume cycles in short time - Pause during high volatility --- ## 🚨 Troubleshooting ### Issue: Pause button not working **Check:** 1. User has `role = 'admin'` in profile 2. Valid JWT token in request 3. Backend logs for errors 4. Network tab for API response ### Issue: Status not updating in UI **Check:** 1. WebSocket connection active 2. `health_update` events received 3. Browser console for errors 4. Refresh page to force sync ### Issue: Entries still being placed when paused **Check:** 1. Backend logs show pause enforcement 2. `healthTracker.isPaused()` returns true 3. AutoTrader/TradeExecutor checking pause state 4. No cached state in trading loop ### Issue: State lost after restart **Check:** 1. `bot_state.json` exists and readable 2. Supabase connection working 3. `loadState()` called on startup 4. Logs show state restoration --- ## πŸ“š Documentation Files | File | Purpose | |------|---------| | `ADMIN_TRADE_CONTROL_SUMMARY.md` | Executive summary | | `ADMIN_TRADE_CONTROL_IMPLEMENTATION.md` | Full implementation guide | | `ADMIN_TRADE_CONTROL_TEST_PLAN.md` | Testing requirements | | `ADMIN_TRADE_CONTROL_ARCHITECTURE.md` | Visual diagrams | | `ADMIN_TRADE_CONTROL_QUICK_REF.md` | This quick reference | --- ## βœ… Pre-Production Checklist - [ ] All backend unit tests pass - [ ] All frontend component tests pass - [ ] Integration tests pass - [ ] Manual testing completed - [ ] Security review completed - [ ] Documentation reviewed - [ ] Audit logging verified - [ ] State persistence verified - [ ] Error handling verified - [ ] UI/UX reviewed --- ## πŸŽ‰ Quick Start ### For Admins 1. Login as admin user 2. Go to Admin tab (πŸ›‘οΈ) 3. Find "Trading Control" section 4. Click "Pause" or "Resume" 5. Check header badge for status ### For Developers 1. Read `ADMIN_TRADE_CONTROL_IMPLEMENTATION.md` 2. Review enforcement points in code 3. Run test suite 4. Deploy to staging 5. Verify functionality 6. Deploy to production --- ## πŸ”— Related Systems - **Health Tracker**: Stores trading control state - **API Server**: Exposes control endpoints - **AutoTrader**: Enforces pause on entries - **TradeExecutor**: Enforces pause on openPosition - **WebSocket**: Broadcasts state updates - **Dashboard**: Displays status and controls --- ## πŸ“ž Support **Issues?** Check: 1. Backend logs: `bytelyst-trading-bot-service/logs/` 2. Frontend console: Browser DevTools 3. API responses: Network tab 4. State file: `bot_state.json` **Questions?** See: - Implementation guide - Test plan - Architecture diagram