From 1313031acdc2048fc0c8f373f3effb4b04c5f175 Mon Sep 17 00:00:00 2001 From: mindesbunister Date: Mon, 3 Nov 2025 13:33:07 +0100 Subject: [PATCH] docs: Update copilot instructions with per-symbol settings - Updated TP/SL percentages to current values (0.4%/0.7%) - Added Per-Symbol Configuration section with SOL/ETH controls - Documented getPositionSizeForSymbol() usage pattern - Added per-symbol settings to API endpoints documentation - Updated execute workflow to include symbol enabled checks - Corrected ETH minimum size (0.01 ETH, not 0.002) - Added comprehensive Per-Symbol Trading Controls section --- .github/copilot-instructions.md | 63 +++++++++++++++++++++++++++++---- 1 file changed, 56 insertions(+), 7 deletions(-) diff --git a/.github/copilot-instructions.md b/.github/copilot-instructions.md index 52f4a25..f73eb20 100644 --- a/.github/copilot-instructions.md +++ b/.github/copilot-instructions.md @@ -9,10 +9,16 @@ **Key Design Principle:** Dual-layer redundancy - every trade has both on-chain orders (Drift) AND software monitoring (Position Manager) as backup. **Exit Strategy:** Three-tier scaling system: -- TP1 at +1.5%: Close 75% (configurable via `TAKE_PROFIT_1_SIZE_PERCENT`) -- TP2 at +3.0%: Close 80% of remaining = 20% total (configurable via `TAKE_PROFIT_2_SIZE_PERCENT`) +- TP1 at +0.4%: Close 75% (configurable via `TAKE_PROFIT_1_SIZE_PERCENT`) +- TP2 at +0.7%: Close 80% of remaining = 20% total (configurable via `TAKE_PROFIT_2_SIZE_PERCENT`) - Runner: 5% remaining with 0.3% trailing stop (configurable via `TRAILING_STOP_PERCENT`) +**Per-Symbol Configuration:** SOL and ETH have independent enable/disable toggles and position sizing: +- `SOLANA_ENABLED`, `SOLANA_POSITION_SIZE`, `SOLANA_LEVERAGE` (defaults: true, $210, 10x) +- `ETHEREUM_ENABLED`, `ETHEREUM_POSITION_SIZE`, `ETHEREUM_LEVERAGE` (defaults: true, $4, 1x) +- BTC and other symbols fall back to global settings (`MAX_POSITION_SIZE_USD`, `LEVERAGE`) +- **Priority:** Per-symbol ENV → Market config → Global ENV → Defaults + **Signal Quality System:** Filters trades based on 5 metrics (ATR, ADX, RSI, volumeRatio, pricePosition) scored 0-100. Only trades scoring 60+ are executed. Scores stored in database for future optimization. **MAE/MFE Tracking:** Every trade tracks Maximum Favorable Excursion (best profit %) and Maximum Adverse Excursion (worst loss %) updated every 2s. Used for data-driven optimization of TP/SL levels. @@ -118,6 +124,14 @@ Without this, the SDK returns signatures for transactions that never execute, ca **Always use:** `getMergedConfig()` to get final config - never read env vars directly in business logic +**Per-symbol position sizing:** Use `getPositionSizeForSymbol(symbol, config)` which returns `{ size, leverage, enabled }` +```typescript +const { size, leverage, enabled } = getPositionSizeForSymbol('SOL-PERP', config) +if (!enabled) { + return NextResponse.json({ success: false, error: 'Symbol trading disabled' }, { status: 400 }) +} +``` + **Symbol normalization:** TradingView sends "SOLUSDT" → must convert to "SOL-PERP" for Drift ```typescript const driftSymbol = normalizeTradingViewSymbol(body.symbol) @@ -138,11 +152,11 @@ const driftSymbol = normalizeTradingViewSymbol(body.symbol) **Key endpoints:** - `/api/trading/execute` - Main entry point from n8n (production, requires auth) -- `/api/trading/check-risk` - Pre-execution validation (duplicate check, quality score, **per-symbol cooldown**, rate limits) -- `/api/trading/test` - Test trades from settings UI (no auth required) +- `/api/trading/check-risk` - Pre-execution validation (duplicate check, quality score, **per-symbol cooldown**, rate limits, **symbol enabled check**) +- `/api/trading/test` - Test trades from settings UI (no auth required, **respects symbol enable/disable**) - `/api/trading/close` - Manual position closing - `/api/trading/positions` - Query open positions from Drift -- `/api/settings` - Get/update config (writes to .env file) +- `/api/settings` - Get/update config (writes to .env file, **includes per-symbol settings**) - `/api/analytics/last-trade` - Fetch most recent trade details for dashboard (includes quality score) - `/api/restart` - Create restart flag for watch-restart.sh script @@ -151,10 +165,11 @@ const driftSymbol = normalizeTradingViewSymbol(body.symbol) ### Execute Trade (Production) ``` TradingView alert → n8n Parse Signal Enhanced (extracts metrics) - ↓ /api/trading/check-risk [validates quality score ≥60, checks duplicates] + ↓ /api/trading/check-risk [validates quality score ≥60, checks duplicates, per-symbol cooldown] ↓ /api/trading/execute ↓ normalize symbol (SOLUSDT → SOL-PERP) ↓ getMergedConfig() + ↓ getPositionSizeForSymbol() [check if symbol enabled + get sizing] ↓ openPosition() [MARKET order] ↓ calculate dual stop prices if enabled ↓ placeExitOrders() [on-chain TP1/TP2/SL orders] @@ -309,7 +324,7 @@ docker exec trading-bot-postgres psql -U postgres -d trading_bot_v4 -c "\dt" 12. **Drift minimum position sizes:** Actual minimums differ from documentation: - SOL-PERP: 0.1 SOL (~$5-15 depending on price) - - ETH-PERP: 0.002 ETH (~$7-8 at $4000/ETH) - NOT 0.01 ETH + - ETH-PERP: 0.01 ETH (~$38-40 at $4000/ETH) - BTC-PERP: 0.0001 BTC (~$10-12 at $100k/BTC) Always calculate: `minOrderSize × currentPrice` must exceed Drift's $4 minimum. Add buffer for price movement. @@ -326,6 +341,40 @@ docker exec trading-bot-postgres psql -U postgres -d trading_bot_v4 -c "\dt" - **Types:** Define interfaces in same file as implementation (not separate types directory) - **Console logs:** Use emojis for visual scanning: 🎯 🚀 ✅ ❌ 💰 📊 🛡️ +## Per-Symbol Trading Controls + +**Purpose:** Independent enable/disable toggles and position sizing for SOL and ETH to support different trading strategies (e.g., ETH for data collection at minimal size, SOL for profit generation). + +**Configuration Priority:** +1. **Per-symbol ENV vars** (highest priority) + - `SOLANA_ENABLED`, `SOLANA_POSITION_SIZE`, `SOLANA_LEVERAGE` + - `ETHEREUM_ENABLED`, `ETHEREUM_POSITION_SIZE`, `ETHEREUM_LEVERAGE` +2. **Market-specific config** (from `MARKET_CONFIGS` in config/trading.ts) +3. **Global ENV vars** (fallback for BTC and other symbols) + - `MAX_POSITION_SIZE_USD`, `LEVERAGE` +4. **Default config** (lowest priority) + +**Settings UI:** `app/settings/page.tsx` has dedicated sections: +- 💎 Solana section: Toggle + position size + leverage + risk calculator +- ⚡ Ethereum section: Toggle + position size + leverage + risk calculator +- 💰 Global fallback: For BTC-PERP and future symbols + +**Example usage:** +```typescript +// In execute/test endpoints +const { size, leverage, enabled } = getPositionSizeForSymbol(driftSymbol, config) +if (!enabled) { + return NextResponse.json({ + success: false, + error: 'Symbol trading disabled' + }, { status: 400 }) +} +``` + +**Test buttons:** Settings UI has symbol-specific test buttons: +- 💎 Test SOL LONG/SHORT (disabled when `SOLANA_ENABLED=false`) +- ⚡ Test ETH LONG/SHORT (disabled when `ETHEREUM_ENABLED=false`) + ## When Making Changes 1. **Adding new config:** Update DEFAULT_TRADING_CONFIG + getConfigFromEnv() + .env file