PROBLEM:
- Trades with quality score 35 and 45 were executed (threshold: 60)
- Position opened without risk management after signal flips
- "Parse Signal" node didn't extract ATR/ADX/RSI/volumeRatio/pricePosition
- "Check Risk" node only sent symbol+direction, skipped quality validation
- "Execute Trade" node didn't forward metrics to backend
ROOT CAUSE:
n8n workflow had TWO paths:
1. NEW: Parse Signal Enhanced → Check Risk1 → Execute Trade1 (working)
2. OLD: Parse Signal → Check Risk → Execute Trade (broken)
Old path bypassed quality check because check-risk endpoint saw
hasContextMetrics=false and allowed trade without validation.
FIX:
1. Changed "Parse Signal" from 'set' to 'code' node with metric extraction
2. Updated "Check Risk" to send atr/adx/rsi/volumeRatio/pricePosition
3. Updated "Execute Trade" to forward all metrics to backend
IMPACT:
- All trades now validated against quality score threshold (60)
- Low-quality signals properly blocked before execution
- Prevents positions opening without proper risk management
Evidence from database showed 3 trades in 2 hours with scores <60:
- 10:00:31 SOL LONG - Score 35 (phantom detected)
- 09:55:30 SOL SHORT - Score 35 (executed)
- 09:35:14 SOL LONG - Score 45 (executed)
All three should have been blocked. Fix prevents future bypasses.
- Detect position size mismatches (>50% variance) after opening
- Save phantom trades to database with expectedSizeUSD, actualSizeUSD, phantomReason
- Return error from execute endpoint to prevent Position Manager tracking
- Add comprehensive documentation of phantom trade issue and solution
- Enable data collection for pattern analysis and future optimization
Fixes oracle price lag issue during volatile markets where transactions
confirm but positions don't actually open at expected size.
- Remove trade from Position Manager BEFORE closing Drift position (prevents race condition)
- Explicitly save closure to database with proper P&L calculation
- Mark flipped positions as 'manual' exit reason
- Increase delay from 1s to 2s for better on-chain confirmation
- Preserve MAE/MFE data in closure records
Fixes issue where SHORT signal would close LONG but not properly track the new SHORT position.
Database now correctly records both old position closure and new position opening.
- Position Manager was calculating P&L using tracked size instead of actual on-chain size
- Example: Tracked 100, actual 0.04 SOL () = -99.63% false loss instead of -0.32%
- Fixed external closure detection to use position.size * currentPrice as lastKnownSize
- Manually corrected phantom trade P&L from -092.25 to /bin/bash
- Total P&L corrected: -013.92 → +8.33 (accurate)
- Prevents all future phantom/mismatch trades from wildly incorrect P&L
Modified:
- lib/trading/position-manager.ts lines 421-445 (external closure P&L calculation)
**Problem:**
Config had minOrderSize: 0.01 ETH for ETH-PERP, but user successfully opens positions as small as $4-8 (0.001-0.002 ETH at ~$4000/ETH).
Database shows successful ETH trades:
- $8 positions = 0.002 ETH at $4000/ETH
- $4 positions = 0.001 ETH at $4000/ETH
**Actual Drift Minimum:**
0.001 ETH (~$4 at $4000/ETH), NOT 0.01 ETH
**Fix:**
Updated config/trading.ts:
- minOrderSize: 0.01 → 0.001 ETH
- Updated comment to reflect actual minimum
**Impact:**
- ✅ Accurate minimum validation
- ✅ Small runner positions (0.0005-0.001 ETH) won't be falsely flagged
- ✅ Prevents incorrect "forcing 100% close" on valid sizes
- ✅ Allows proper data collection at $4 position size
**Note:**
The previous fix for checking minOrderSize before close is still valid and needed - it just now uses the correct minimum (0.001 instead of 0.01).
**Problem:**
When closing small runner positions (5% after TP1+TP2), the calculated size could be below Drift's minimum order size:
- ETH minimum: 0.01 ETH
- After TP1 (75%): 0.0025 ETH left
- After TP2 (80%): 0.0005 ETH runner
- Trailing stop tries to close 0.0005 ETH → ERROR: Below minimum 0.01
n8n showed: "Order size 0.0011 is below minimum 0.01"
**Root Cause:**
closePosition() calculated: sizeToClose = position.size * (percentToClose / 100)
No validation against marketConfig.minOrderSize before submitting to Drift.
**Solution:**
Added minimum size check in closePosition() (lib/drift/orders.ts):
1. Calculate intended close size
2. If below minOrderSize → force 100% close instead
3. Log warning when this happens
4. Prevents Drift API rejection
**Code Change:**
```typescript
let sizeToClose = position.size * (params.percentToClose / 100)
// If calculated size is below minimum, close 100%
if (sizeToClose < marketConfig.minOrderSize) {
console.log('⚠️ Calculated size below minimum - forcing 100% close')
sizeToClose = position.size
}
```
**Impact:**
- ✅ Small runner positions close successfully
- ✅ No more "below minimum" errors from Drift
- Trades complete cleanly
- ⚠️ Runner may close slightly earlier than intended (but better than error)
**Example:**
ETH runner at 0.0005 ETH → tries to close → detects <0.01 → closes entire 0.0005 ETH position at once instead of rejecting.
This is the correct behavior - if the position is already too small, we should close it entirely.
**UI Updates (settings page):**
Added new '📈 Position Scaling' section with:
- Enable/disable toggle (defaults to OFF)
- Min quality score slider (60-90, default 75)
- Min profit to scale (0-2%, default 0.4%)
- Scale size percent (10-100%, default 50%)
- Max position multiplier (1-3x, default 2.0x)
- Min ADX increase (0-15, default 5)
- Max price position for scale (50-90%, default 70%)
**Visual Feedback:**
- Purple-themed section with warning banner
- Real-time risk calculator showing:
* Original position size (SOL example)
* Scale addition amount
* Total after 1 scale
* Maximum possible position size
- Dynamic descriptions explain each parameter
- Warning: 'DISABLED by default' with red indicator
**API Updates:**
Extended /api/settings GET/POST to handle 7 new fields:
- ENABLE_POSITION_SCALING
- MIN_SCALE_QUALITY_SCORE
- MIN_PROFIT_FOR_SCALE
- MAX_SCALE_MULTIPLIER
- SCALE_SIZE_PERCENT
- MIN_ADX_INCREASE
- MAX_PRICE_POSITION_FOR_SCALE
**User Flow:**
1. Navigate to http://localhost:3001/settings
2. Scroll to '📈 Position Scaling' section
3. Toggle 'Enable Position Scaling' to 1
4. Adjust thresholds (defaults are conservative)
5. See live calculation of scaling impact
6. Click 'Save Settings'
7. Click 'Restart Bot' to apply
**Safety:**
- Feature OFF by default (requires explicit opt-in)
- Warning banner explains scaling behavior
- Risk calculator shows maximum exposure
- Conservative defaults prevent aggressive scaling
- All parameters adjustable via sliders
**Example:**
With defaults (SOL $210×10x = $2100):
- Scale adds: $1050 (50% of $2100)
- Total after 1 scale: $3150
- Max position (2x): $4200
User can now enable and configure position scaling without touching .env file!
**Feature: Position Scaling**
Allows adding to existing profitable positions when high-quality signals confirm trend strength.
**Configuration (config/trading.ts):**
- enablePositionScaling: false (disabled by default - enable after testing)
- minScaleQualityScore: 75 (higher bar than initial 60)
- minProfitForScale: 0.4% (must be at/past TP1)
- maxScaleMultiplier: 2.0 (max 200% of original size)
- scaleSizePercent: 50% (add 50% of original position)
- minAdxIncrease: 5 (ADX must strengthen)
- maxPricePositionForScale: 70% (don't chase resistance)
**Validation Logic (check-risk endpoint):**
Same-direction signal triggers scaling check if enabled:
1. Quality score ≥75 (stronger than initial entry)
2. Position profitable ≥0.4% (at/past TP1)
3. ADX increased ≥5 points (trend strengthening)
4. Price position <70% (not near resistance)
5. Total size <2x original (risk management)
6. Returns 'allowed: true, reason: Position scaling' if all pass
**Execution (execute endpoint):**
- Opens additional position at scale size (50% of original)
- Updates ActiveTrade: timesScaled, totalScaleAdded, currentSize
- Tracks originalAdx from first entry for comparison
- Returns 'action: scaled' with scale details
**ActiveTrade Interface:**
Added fields:
- originalAdx?: number (for scaling validation)
- timesScaled?: number (track scaling count)
- totalScaleAdded?: number (total USD added)
**Example Scenario:**
1. LONG SOL at $176 (quality: 45, ADX: 13.4) - weak but entered
2. Price hits $176.70 (+0.4%) - at TP1
3. New LONG signal (quality: 78, ADX: 19) - strong confirmation
4. Scaling validation: ✅ Quality 78 ✅ Profit +0.4% ✅ ADX +5.6 ✅ Price 68%
5. Adds 50% more position at $176.70
6. Total position: 150% of original size
**Conservative Design:**
- Disabled by default (requires manual enabling)
- Only scales INTO profitable positions (never averaging down)
- Requires significant quality improvement (75 vs 60)
- Requires trend confirmation (ADX increase)
- Hard cap at 2x original size
- Won't chase near resistance levels
**Next Steps:**
1. Enable in settings: ENABLE_POSITION_SCALING=true
2. Test with small positions first
3. Monitor data: do scaled positions outperform?
4. Adjust thresholds based on results
**Safety:**
- All existing duplicate prevention logic intact
- Flip logic unchanged (still requires quality check)
- Position Manager tracks scaling state
- Can be toggled on/off without code changes
**Problem:**
- Signal flips (SHORT→LONG or LONG→SHORT) were auto-approved
- Bypassed signal quality scoring, cooldown, drawdown checks
- User wanted flips ONLY if new signal has strong quality (score ≥60)
**Solution:**
- Removed early return for opposite-direction signals in check-risk
- Flips now go through FULL validation: quality score, cooldown, limits
- Execute endpoint still handles flip logic (close opposite + open new)
**New Flow:**
1. n8n sends flip signal → check-risk endpoint
2. Detects potential flip, logs 'checking quality score'
3. Continues to quality checks (not early return)
4. If score ≥60 AND all checks pass → execute handles flip
5. If score <60 → BLOCKS flip with 'Signal quality too low'
**Result:**
Flips now require signal strength, not just direction change
**Root Causes:**
1. Auto-flip logic could create phantom trades if close failed
2. Position size mismatches (0.01 SOL vs 11.92 SOL expected) not caught
3. Multiple trades for same symbol+direction in database
**Preventive Measures:**
1. **Startup Validation (lib/startup/init-position-manager.ts)**
- Validates all open trades against Drift positions on startup
- Auto-closes phantom trades with <50% expected size
- Logs size mismatches for manual review
- Prevents Position Manager from tracking ghost positions
2. **Duplicate Position Prevention (app/api/trading/execute/route.ts)**
- Blocks opening same-direction position on same symbol
- Returns 400 error if duplicate detected
- Only allows auto-flip (opposite direction close + open)
3. **Runtime Phantom Detection (lib/trading/position-manager.ts)**
- Checks position size every 2s monitoring cycle
- Auto-closes if size ratio <50% (extreme mismatch)
- Logs as 'manual' exit with AUTO_CLEANUP tx
- Removes from monitoring immediately
4. **Quality Score Fix (app/api/trading/check-risk/route.ts)**
- Hardcoded minScore=60 (removed non-existent config reference)
**Prevention Summary:**
- ✅ Startup validation catches historical phantoms
- ✅ Duplicate check prevents new phantoms
- ✅ Runtime detection catches size mismatches <30s after they occur
- ✅ All three layers work together for defense-in-depth
Issue: User had LONG (phantom) + SHORT (undersized 0.01 SOL vs 11.92 expected)
Fix: Both detected and closed, bot now clean with 0 active trades
- Add SymbolSettings interface with enabled/positionSize/leverage fields
- Implement per-symbol ENV variables (SOLANA_*, ETHEREUM_*)
- Add SOL and ETH sections to settings UI with enable/disable toggles
- Add symbol-specific test buttons (SOL LONG/SHORT, ETH LONG/SHORT)
- Update execute and test endpoints to check symbol enabled status
- Add real-time risk/reward calculator per symbol
- Rename 'Position Sizing' to 'Global Fallback' for clarity
- Fix position manager P&L calculation for externally closed positions
- Fix zero P&L bug affecting 12 historical trades
- Add SQL scripts for recalculating historical P&L data
- Move archive TypeScript files to .archive to fix build
Defaults:
- SOL: 10 base × 10x leverage = 100 notional (profit trading)
- ETH: base × 1x leverage = notional (data collection)
- Global: 10 × 10x for BTC and other symbols
Configuration priority: Per-symbol ENV > Market config > Global ENV > Defaults
Added documentation for recent improvements:
- MAE/MFE tracking for trade optimization
- On-chain order synchronization after TP1 hits
- Exit reason detection using trade state flags (not current price)
- Per-symbol cooldown to avoid missing opportunities
- Quality score integration in analytics dashboard
Updated workflows and pitfalls sections with lessons learned from debugging session
CRITICAL: Cooldown was global across ALL symbols, causing missed opportunities
Example: ETH trade at 10:00 blocked SOL trade at 10:04 (5min cooldown)
Changes:
- Added getLastTradeTimeForSymbol() function to query last trade per symbol
- Updated check-risk endpoint to use symbol-specific cooldown
- Each coin (SOL/ETH/BTC) now has independent cooldown timer
- Cooldown message shows symbol: 'Must wait X min before next SOL-PERP trade'
Result: Can trade ETH and SOL simultaneously without interference
Example: ETH LONG at 10:00, SOL SHORT at 10:01 = both allowed
CRITICAL BUG: Position Manager was using current price to determine exit reason,
but on-chain orders filled at a DIFFERENT price in the past!
Example: LONG entry $184.55, TP1 filled at $184.66, but when Position Manager
checked later (price dropped), it saw currentPrice < TP1 and defaulted to 'SL'
Result: Profitable trades incorrectly labeled as SL exits in database
Fix:
- Use trade.tp1Hit and trade.tp2Hit flags to determine exit reason
- If no TP flags set, use realized P&L to distinguish:
- Profit >0.5% = TP1 filled
- Negative P&L = SL filled
- Remove duplicate P&L calculation
This ensures exit reasons match actual on-chain order fills
- Added qualityScore field to Execute Trade node JSON body
- Pulls value from Check Risk response: .item.json.qualityScore
- This enables quality score to be saved in database and displayed on analytics dashboard
Added Maximum Favorable/Adverse Excursion tracking:
- Track maxFavorableExcursion: best profit % reached during trade
- Track maxAdverseExcursion: worst loss % reached during trade
- Track maxFavorablePrice and maxAdversePrice
- Update every price check (2s interval)
- Save to database on trade exit for optimization analysis
Benefits:
- Identify if TP levels are too conservative (MFE consistently higher)
- Determine if SL is too tight (MAE < SL but trade recovers)
- Optimize runner size based on how often MFE >> TP2
- Data-driven exit strategy tuning after collecting 10-20 trades
Display in monitoring logs: Shows MFE/MAE % every 20 seconds
CRITICAL: After TP1 closes 75%, the on-chain stop loss orders were NOT being updated
- Position Manager was tracking new SL price internally but not updating Drift orders
- Old SL orders (e.g., $181.69) remained active even after TP1 at $185.28
- This prevented the 'move SL to breakeven after TP1' logic from working
Fix:
- After TP1 hits, cancel ALL old orders on-chain
- Place new SL orders at updated price (breakeven + configured %)
- Place remaining TP2 order for the 25% runner position
- Maintains dual-stop system if enabled
Result: SL will now actually move up on Drift UI after TP1 fires
- Convert closePosition.closedSize (base asset) to USD when updating trade.currentSize
- Fix conversion when position.size detected from Drift: set currentSize = position.size * currentPrice
- Prevent trade.currentSize from being reduced to tiny values due to unit mismatch
- Execute Trade node was trying to access qualityScore from Check Risk node
- This caused syntax error in n8n when Check Risk blocks the trade
- Backend API calculates qualityScore from the provided metrics (atr, adx, rsi, etc.)
- No need to pass it explicitly in the request body
- Lower ATR threshold from 0.6% to 0.15% (allows low volatility breakouts)
- Increase volume bonus: +15 for very strong volume (1.5x+), was +10 for 1.2x+
- Add volume breakout logic: High volume (1.4x+) at 95%+ range gets +5 instead of -15 penalty
- Add volume compensation: +10 bonus when volume >1.8x and ATR <0.6%
- Example: SOL signal with 0.18% ATR, 1.74x volume at 95.6% range now scores 70/100 (PASS) instead of 25/100 (BLOCK)
- This signal moved +0.97% and would have hit TP1 (+1.5%) - proves quality scoring was too conservative
- Changes apply globally to all symbols (SOL, ETH, BTC) using same scoring algorithm
- Position Manager now detects TP1/TP2 fills by monitoring position size reductions instead of entry price mismatches
- When position size reduces by ~75%, marks TP1 as filled and updates currentSize
- When position size reduces by ~95%, marks TP2 as filled and activates trailing stop for 5% runner
- Entry price mismatch check now skipped after TP fills (Drift shows weighted average entry price after partial closes)
- Fixes bug where runners were incorrectly closed after TP1/TP2 fired on-chain
- Adds grace period for new trades (<30s) to avoid false positives during blockchain propagation delays
- This unblocks Phase 1 data collection for signal quality optimization (need 10+ trades with MAE/MFE data)
- Add qualityScore to ExecuteTradeResponse interface and response object
- Update analytics page to always show Signal Quality card (N/A if unavailable)
- Fix n8n workflow to pass context metrics and qualityScore to execute endpoint
- Fix timezone in Telegram notifications (Europe/Berlin)
- Fix symbol normalization in /api/trading/close endpoint
- Update Drift ETH-PERP minimum order size (0.002 ETH not 0.01)
- Add transaction confirmation to closePosition() to prevent phantom closes
- Add 30-second grace period for new trades in Position Manager
- Fix execution order: database save before Position Manager.addTrade()
- Update copilot instructions with transaction confirmation pattern
- Changed regex from /\.P\s+(\d+)/ to /\b(buy|sell)\s+(\d+|D|W|M)\b/i
- Matches new format: 'ETH buy 15' instead of 'SOL buy .P 15'
- Supports all timeframes: 5, 15, 60, D (daily), W (weekly), M (monthly)
- Updated comment to reflect new format example
- Changed 'SOL buy .P 15' to 'SOL buy 15' (cleaner format)
- timeframe.period is already dynamic (no conversion needed)
- Works for any timeframe: 5, 15, 60, 240, D, etc.
- Format: 'ETH buy 15' or 'BTC sell 5' or 'SOL buy 60'
- Use syminfo.ticker to dynamically get symbol name
- Strip USD/USDT/PERP suffixes to get base currency
- Works for ETH, SOL, BTC, and any other symbol
- Alerts now correctly show 'ETH buy' for Ethereum, 'BTC buy' for Bitcoin, etc.
This fixes the bug where ETH triggers sent 'SOL buy' alerts.
- Updated to Set node v3.4 with proper assignments format
- Explicitly reference Parse Signal Enhanced for rawMessage
- Use $json for Check Risk output (reason, details, score, reasons)
- Properly formatted message with all data fields populated
- Added seconds to timestamp for better tracking
- Removed ugly escaped syntax with $('Parse Signal').item.json references
- Use $json directly (cleaner and works correctly)
- Issues now display as bullet points instead of comma-separated
- Proper line breaks and formatting
- Professional looking blocked trade notifications
- New button in analytics page to clear orphaned trades
- API endpoint /api/trading/clear-manual-closes
- Intelligently checks Drift positions before deleting
- Only removes trades with no matching position or mismatched entry price
- Safe operation: keeps trades on error (false positives better than deletions)
- User-friendly confirmation dialog
- Added getConnection() method to DriftService
- Added proper transaction confirmation in openPosition()
- Check confirmation.value.err to detect on-chain failures
- Return error if transaction fails instead of assuming success
- Prevents phantom trades that never actually execute
This fixes the issue where bot was recording trades with transaction
signatures that don't exist on-chain (like 2gqrPxnvGzdRp56...).
- Added minQualityScore to TradingConfig (default: 60)
- Updated settings UI with slider control (0-100, step 5)
- Updated check-risk endpoint to use config value
- Made scoreSignalQuality function accept minScore parameter
- Updated API to read/write MIN_QUALITY_SCORE env variable
- Allows users to adjust quality threshold from settings page
- Extended MarketConfig with optional positionSize and leverage fields
- Configured ETH-PERP at @ 1x leverage for minimal-risk data collection
- Created getPositionSizeForSymbol() helper function in config/trading.ts
- Integrated symbol-specific sizing into execute endpoint
- Added comprehensive guide in docs/guides/SYMBOL_SPECIFIC_SIZING.md
Purpose: Enable ETH trading for faster signal quality data collection
while preserving SOL's profit-generation sizing (0 @ 10x)
Next: Create ETH alert in TradingView and restart bot
Enhanced 'Format Risk' node in n8n workflow to display:
- Specific blocking reason (duplicate, drawdown, cooldown, quality, etc.)
- Details about what triggered the block
- Quality score if low quality was the reason
- Quality issues breakdown (ATR too low, weak ADX, etc.)
Example output:
TRADE BLOCKED
SHORT | ATR:0.30 | ADX:19.1 | RSI:46
Issues: ATR too low (0.30% - dead market), Moderate trend (ADX 19.1), RSI supports short (46.0)
14:23
Comprehensive guide covering:
- How ATR is captured and stored (entry value frozen)
- Static ATR approach (Phases 1-3): Use entry ATR for entire trade
- Dynamic ATR approach (Phase 5+): Real-time updates via TradingView or bot calculation
- Use cases: Dynamic TP/SL, trailing stops, scaling in/out decisions
- Implementation path: Start simple with entry ATR, add real-time later if data supports
- Code examples for all approaches
- Troubleshooting common ATR issues
- Database schema considerations
Explains why waiting for data is critical before implementing advanced ATR features.
Corrections:
- Runner system already exists (5% with 0.3% trailing stop)
- Current +41% trade is the runner in action!
- Phase 5 reframed: Optimize runner size (5% → 10-25% for high quality) and make trailing stop ATR-based
- Updated current state and trade example to reflect actual implementation