Commit Graph

96 Commits

Author SHA1 Message Date
mindesbunister
595a0ac7a2 docs: Update DRIFT SDK MEMORY LEAK section with Nov 24 health monitoring
- Replaced blind 2-hour timer documentation with error-based health monitoring
- Documented DriftHealthMonitor class with 30-second sliding window
- Added 50 error threshold and flag file restart mechanism
- Documented /api/drift/health endpoint
- Explained benefits: no unnecessary restarts, faster response (30s vs 2h)
- Preserved accurate symptom/root cause/manifestation information
- References implementation commit dc197f5
2025-11-24 16:55:10 +01:00
mindesbunister
594f547a87 docs: Add biggest losers analysis validating quality thresholds
Nov 24, 2025 Analysis: Top 15 losing trades validation

Key Findings:
- Top 2 losses: Quality 90 SHORTS (-86.62, -38.35) = -24.97
- Quality 80 trades: 4 losses totaling -12.76
- Total prevented by current thresholds: -47.07
- Missed winners from blocked signals: ~10
- NET BENEFIT: +37 (3:1 loss avoidance ratio)

Validates direction-specific thresholds:
- LONG 90+: Allows profitable quality 90-94 longs
- SHORT 95+: Blocks catastrophic quality 80-90 shorts

Trade-off confirmed: Sacrifice 1-2 winners/week to eliminate
disaster trades that caused user's ,380 → 04 drawdown.

Updated: .github/copilot-instructions.md (Direction-Specific Quality Thresholds section)
2025-11-24 11:39:13 +01:00
mindesbunister
c09201ccde docs: Document TRAILING_SL exit reason distinction in copilot instructions
Added comprehensive documentation for the trailing SL feature:
- Exit reason tracking: Regular SL vs TRAILING_SL distinction
- Detection logic: tp2Hit + trailingStopActive + price pullback check
- Database storage: TRAILING_SL as separate exitReason value
- Analytics UI: Purple styling with runner emoji for trailing stops
- Code locations: Position Manager (3 locations) + Analytics frontend
- Implementation date: Nov 24, 2025 (commit 9d7932f)

Purpose: Enable separate analysis of runner trailing stop performance
vs early hard stop losses for optimization insights.
2025-11-24 08:50:13 +01:00
mindesbunister
9d7932ff2f feat: Add distinction between regular SL and trailing SL
User Request: Distinguish between SL and Trailing SL in analytics overview

Changes:
1. Position Manager:
   - Updated ExitResult interface to include 'TRAILING_SL' exit reason
   - Modified trailing stop exit (line 1457) to use 'TRAILING_SL' instead of 'SL'
   - Enhanced external closure detection (line 937) to identify trailing stops
   - Updated handleManualClosure to detect trailing SL at price target

2. Database:
   - Updated UpdateTradeExitParams interface to accept 'TRAILING_SL'

3. Frontend Analytics:
   - Updated last trade display to show 'Trailing SL' with special formatting
   - Purple background/border for TRAILING_SL vs blue for regular SL
   - Runner emoji (🏃) prefix for trailing stops

Impact:
- Users can now see when trades exit via trailing stop vs regular SL
- Better understanding of runner system performance
- Trailing stops visually distinct in analytics dashboard

Files Modified:
- lib/trading/position-manager.ts (4 locations)
- lib/database/trades.ts (UpdateTradeExitParams interface)
- app/analytics/page.tsx (exit reason display)
- .github/copilot-instructions.md (Common Pitfalls #61, #62)
2025-11-24 08:40:09 +01:00
mindesbunister
1176df4299 docs: Document adaptive leverage system in copilot instructions
- Added Adaptive Leverage System section in Architecture Overview
- Documented quality-based leverage tiers (95+ = 15x, 90-94 = 10x)
- Added configuration details and helper function usage
- Updated Configuration System with adaptive leverage integration
- Modified Execute Trade workflow to show early quality calculation
- Added critical execution order note (quality MUST be calculated before sizing)
- Added item #13 in When Making Changes section for adaptive leverage modifications
- Fixed numbering sequence (was duplicate 11s, now sequential 1-18)
- Cross-referenced ADAPTIVE_LEVERAGE_SYSTEM.md for complete details
2025-11-24 00:56:39 +01:00
mindesbunister
4f6a4b76cf docs: Document direction-specific quality thresholds in copilot instructions
Architecture Overview Updates:
- Updated Signal Quality System section with direction-specific thresholds
- Added detailed subsection explaining Nov 23, 2025 implementation
- Data-driven rationale: 227 trades analysis showed $778.52 P&L gap
- Configuration: LONG=90 (71.4% WR), SHORT=95 (28.6% WR toxic)
- Helper function, fallback logic, and expected impact documented
- Reference to complete analysis in docs/DIRECTION_SPECIFIC_QUALITY_THRESHOLDS.md

Context for Future AI Agents:
- Why different thresholds: historical data shows clear directional bias
- Quality 90-94 longs profitable (+$44.77 on 7 trades)
- Quality 90-94 shorts toxic (-$553.76 on 7 trades)
- System now captures profitable longs while blocking toxic shorts
2025-11-23 15:10:34 +01:00
mindesbunister
01aaa0932a feat: Direction-specific quality thresholds (long=90, short=95)
- DATA-DRIVEN: 227 trades analysis showed longs 71.4% WR vs shorts 28.6% WR at quality 90-94
- LONG threshold: 90 (captures profitable 90-94 signals: +4.77 total, +.40 avg)
- SHORT threshold: 95 (blocks toxic 90-94 signals: -53.76 total, -9.11 avg)
- Historical validation: Quality 90+ longs +00.62 vs shorts -77.90

Modified files:
- config/trading.ts: Added minSignalQualityScoreLong/Short fields + getMinQualityScoreForDirection()
- lib/trading/signal-quality.ts: Accept direction-specific minScore parameter
- app/api/trading/check-risk/route.ts: Use direction-specific thresholds
- .env: Added MIN_SIGNAL_QUALITY_SCORE_LONG=90 and _SHORT=95

Fallback logic: direction-specific → global → 60 default
Backward compatible with existing code
2025-11-23 15:01:56 +01:00
mindesbunister
187a123a25 docs: Add Common Pitfall #60 (stale array snapshot duplicate processing)
Documented Nov 23, 2025 bug where monitoring loop created array snapshot
before async processing, causing removed trades to be processed twice.

Real incident:
- Trade cmibdii4k0004pe07nzfmturo (manual closure)
- 97% size reduction detected
- First iteration removed trade from Map
- Second iteration processed stale reference
- Result: Duplicate Telegram notifications

Fix: Added activeTrades.has() guard at start of checkTradeConditions()
Prevents duplicate processing when trade removed during loop iteration

Also documented:
- Quality threshold .env discrepancy (81 vs 91)
- Settings UI restart requirement
- Why Next.js modules need container restart for env changes

Related to Common Pitfall #59 (Layer 2 ghost detection duplicates)
but different trigger - normal monitoring vs rate limit storm detection
2025-11-23 11:03:02 +01:00
mindesbunister
d71254a57e docs: Document quality-blocked signal tracking discovery
Updates to copilot-instructions.md:
- Multi-Timeframe Price Tracking System section enhanced
- BlockedSignalTracker purpose clarified: validates quality 91 threshold
- Current Status updated with Nov 22 enhancement details
- First false negative result documented (quality 80, +0.52% missed profit)

Signal Quality Scoring section:
- Added 'Threshold Validation In Progress' subsection
- User observation documented: 'green dots shot up'
- Data collection criteria defined (20-30 blocked signals)
- Decision framework added: Keep 91 vs lower to 85 vs adjust weights
- Possible outcomes listed for data-driven optimization

Next Steps:
- Continue collecting quality-blocked signal data (2-4 weeks)
- Target: 20-30 signals with complete price tracking
- SQL analysis: Compare blocked signal win rate vs executed trades
- Decision: Validate quality 91 threshold or adjust based on data

Purpose: Complete documentation of missed opportunity discovery and
validation plan for quality threshold optimization.
2025-11-22 16:15:18 +01:00
mindesbunister
bba91c1df8 feat: Archive old indicator versions, focus on v8 production system
Version Management:
- v8 Money Line: PRODUCTION (8 trades, 57.1% WR, +$262.70, quality ≥95 = 100% wins)
- v5/v6/v7: ARCHIVED (historical baseline for future v9 comparison)

API Changes (app/api/analytics/version-comparison/route.ts):
- Added 'archived' flag to version stats
- Added 'production' field pointing to v8
- Updated sort order: v8 first, then archived versions
- Enhanced descriptions with PRODUCTION/ARCHIVED labels

UI Changes (app/analytics/page.tsx):
- v8 highlighted with blue gradient + 🚀 PRODUCTION badge
- Archived versions greyed out (60% opacity) + ARCHIVED badge
- Updated header: 'Indicator Versions (v8 Production)'
- Data collection notice: v8 shows 8/50 trades progress
- Kept comparison infrastructure for future v9 development

Documentation (.github/copilot-instructions.md):
- Updated Indicator Version Tracking section
- Documented perfect quality separation (≥95 = 100% wins)
- Clarified v8 production status, archived versions purpose
- Analytics UI behavior documented

Purpose: Keep comparison infrastructure for statistical validation
and future v9 testing while focusing user attention on v8 results.
2025-11-22 14:45:48 +01:00
mindesbunister
81926c24b9 docs: Add Common Pitfall #59 - Layer 2 duplicate Telegram notifications
Bug: Trade #8 (SHORT SOL-PERP, Nov 22, 04:05) sent 13 duplicate notifications
- P&L compounded $11.50 → $155.05 (8.2× actual $18.79)
- Root cause: Layer 2 ghost detection ignored closingInProgress flag
- Rate limit storm: 6,581 failures → Layer 2 triggered 13 times
- Each call sent notification before async DB update completed

Real incident details:
- TP1: +$32.63 (60% closed)
- Runner: -$13.84 (40% closed at breakeven)
- Net: +$18.79 (Drift confirmed)
- Database showed $155.05 (manually corrected)

Fix: Added closingInProgress check before Layer 2 ghost detection
Related: Pitfalls #40 (death spiral), #48 (closingInProgress), #49 (compounding)
2025-11-22 14:13:26 +01:00
mindesbunister
c9972ffd99 docs: Add perfect quality score separation data (validates 91 threshold)
Critical finding from 7 v8 trades analysis:
- ALL 4 winners: quality ≥95 (95, 95, 100, 105)
- ALL 3 losers: quality ≤90 (80, 90, 90)
- Perfect separation validates 91 threshold decision
- Would have prevented 100% of losses (-$624.90 total)

Updated:
- SIGNAL_QUALITY_SETUP_GUIDE.md (overview + threshold history)
- SIGNAL_QUALITY_OPTIMIZATION_ROADMAP.md (current system)
- BLOCKED_SIGNALS_TRACKING.md (quality score analysis)
- .github/copilot-instructions.md (data validation sections)

This proves 91 is data-driven optimal, not too strict.
2025-11-21 19:16:07 +01:00
mindesbunister
5419b3f48f docs: Add quality-blocked signal tracking to copilot instructions
Enhanced Multi-Timeframe Price Tracking System documentation:
- Clarified that quality-blocked signals get tracked (not just timeframes)
- Added QUALITY_SCORE_TOO_LOW to blockReason types (Nov 21: threshold 91+)
- Included SQL query example for analyzing missed winners
- Decision Making section now covers threshold optimization

Context: User asked about 80-quality bounce play that shot up
- System correctly blocked it (ADX 16.6, quality 80 < 91 threshold)
- BlockedSignalTracker captures all price movement for 30 minutes
- Enables data-driven analysis: are we filtering too many winners?
- Stick with trend-following for now, let data accumulate for future optimization

Current tracker status: 8 tracked, 6 complete (75%), 50% TP1 hit rate
2025-11-21 19:02:25 +01:00
mindesbunister
08482b43fe critical: Raise quality threshold from 81 to 91 after trade #7 analysis
Trade #7 post-mortem (Nov 21, 2025):
- SHORT SOL-PERP: Entry $123.77, Exit $126.33
- Loss: -$386.62 (2.06%, emergency stop)
- Quality: 90, ADX: 19.0 (weak trend, below 20 threshold)
- Duration: 18 minutes
- Result: Price never moved favorably, immediate reversal

Impact on v8 performance:
- 7 trades: 4 wins, 3 losses (57.1% WR, below 60% target)
- Total P&L: $262.70 (down from $649.32)
- Avg quality: 93.6 (still excellent)

Decision rationale:
- ADX 19.0 is borderline weak/chop territory
- Quality 90 insufficient to filter weak-trend entries
- Raised threshold to 91+ to restore 60%+ win rate
- v8 avg 93.6 quality supports higher threshold
- Next blocked signal after loss: quality 80 (correctly blocked)

This implements 3-loss circuit breaker analysis - system paused
for threshold adjustment before resuming.
2025-11-21 18:55:11 +01:00
mindesbunister
33ef157a24 docs: Update Common Pitfall #56 with order filtering bug fix
Added Nov 21 false positive investigation and resolution:
- Bot showed '32 open orders' when Drift UI showed 0
- Root cause: orderId > 0 check didn't verify baseAssetAmount
- Drift's 32-slot array contained historical metadata
- Fix: Enhanced filter with baseAssetAmount.eq(new BN(0)) check
- Result: Accurate order count reporting

Updated with correct filtering code example and dual commit references.
2025-11-21 16:45:43 +01:00
mindesbunister
17071fe7ec docs: Update minimum quality score from 60 to 81 across documentation
- Updated .github/copilot-instructions.md key constraints and signal quality system description
- Updated config/trading.ts minimum score from 60 to 81 with v8 performance rationale
- Updated SIGNAL_QUALITY_SETUP_GUIDE.md intro to reflect 81 threshold
- Updated SIGNAL_QUALITY_OPTIMIZATION_ROADMAP.md current system section
- Updated BLOCKED_SIGNALS_TRACKING.md quality score requirements

Context: After v8 Money Line indicator deployed with 0.6% flip threshold,
system achieving 66.7% win rate with average quality score 94.2. Raised
minimum threshold from 60 to 81 to maintain exceptional selectivity.

Current v8 stats: 6 trades, 4 wins, $649.32 profit, 94.2 avg quality
Account growth: $540 → $1,134.92 (110% gain in 2-3 days)
2025-11-21 15:49:26 +01:00
mindesbunister
9b0b1d46ca fix: Change revenge system to 1.0x position sizing (same as original)
**ISSUE:** User operates at 100% capital allocation - no room for 1.2x sizing
- 1.2x would require 120% of capital (mathematically impossible)
- User: 'thats not gonna work. we are already using 100% of our portfolio'

**FIX:** Changed from 1.2x to 1.0x (same size as original trade)
- Focus on capturing reversal, not sizing bigger
- Maintains aggressive 15x leverage
- Example: Original ,350 → Revenge ,350 (not 0,020)

**FILES CHANGED:**
- lib/trading/stop-hunt-tracker.ts: sizingMultiplier 1.2 → 1.0
- Telegram notification: Updated to show 'same as original'
- Documentation: Updated all references to 1.0x strategy

**DEPLOYED:** Nov 20, 2025 ~20:30 CET
**BUILD TIME:** 71.8s, compiled successfully
**STATUS:** Container running stable, stop hunt tracker operational
2025-11-20 19:45:22 +01:00
mindesbunister
9cbb819c78 docs: Document Stop Hunt Revenge System (Nov 20, 2025)
Complete documentation for automated revenge trading system

Sections added:
- Purpose and architecture (4-hour window, 1.2x sizing, quality 85+)
- Revenge conditions (price reversal logic for LONG/SHORT)
- How it works (7-step process from recording to execution)
- Database schema (StopHunt table, 20 fields, 4 indexes)
- Code components (tracker service, startup integration, Position Manager)
- Telegram notification format
- Singleton pattern usage
- Startup behavior
- Common pitfalls (7 issues to avoid)
- Real-world use case (Nov 20 motivation)
- Deployment status
- Git commits

Location: Added between Telegram Notifications and Integration Points
File: .github/copilot-instructions.md
Lines: ~140 lines of comprehensive documentation
2025-11-20 19:20:13 +01:00
mindesbunister
e99bd32a9f docs: Update Telegram notifications section for TP1 partial closes
- Updated section header: Nov 16, 2025 → Nov 16, 2025 - Enhanced Nov 20, 2025
- Added TP1 partial close notification to implemented features list
- Enhanced notification format example with runner split
- Added Key Features section explaining immediate TP1 feedback
- Updated commits list: b1ca454 (Nov 16) + 79e7ffe (Nov 20)
- Clarified separate notifications for TP1 and runner closures
2025-11-20 17:44:33 +01:00
mindesbunister
9d92428c63 docs: Add MANDATORY documentation requirement to copilot instructions
**CRITICAL: Documentation is now a NON-NEGOTIABLE requirement**

- Added ironclad rule: Every git commit MUST update copilot-instructions.md
- NO EXCEPTIONS - this is a real money trading system
- Undocumented fixes = forgotten fixes = financial losses
- Future AI agents need complete context for data integrity

**What must be documented:**
- Bug fixes → Common Pitfalls (symptom, root cause, fix, lesson)
- New features → Architecture Overview, Critical Components
- Database changes → Important fields, filtering requirements
- Config changes → Configuration System section
- Breaking changes → When Making Changes section

**Examples of proper documentation:**
- Common Pitfall #56: Ghost orders (a3a6222)
- Common Pitfall #57: P&L inaccuracy (8e600c8)
- Common Pitfall #55: BlockedSignalTracker (6b00303)

**This is not optional - documentation is part of 'done'**
2025-11-20 15:37:07 +01:00
mindesbunister
9b280a4016 docs: Add Common Pitfall #57 - P&L calculation inaccuracy fix
- Documented Nov 20, 2025 fix for 36% P&L calculation errors
- External closure handler was using monitoring loop's stale currentPrice
- Real incident: Database -$101.68 vs Drift -$138.35 actual
- Fix: Query Drift's userAccount.perpPositions[].settledPnl directly
- Fallback to calculation if Drift query fails
- Updated #56 commit reference from [pending] to a3a6222
- Both fixes deployed Nov 20, 2025 15:25 CET
2025-11-20 15:34:07 +01:00
mindesbunister
a3a6222047 critical: Cancel ghost orders after external closures
- Added order cancellation to Position Manager's external closure handler
- When on-chain SL/TP orders close position, remaining orders now cancelled automatically
- Prevents ghost orders from triggering unintended positions
- Real incident: Nov 20 SHORT stop-out left 32 ghost orders on Drift
- Risk: Ghost TP1 at $140.66 could fill later, creating unwanted LONG position
- Fix: Import cancelAllOrders() and call after trade removed from monitoring
- Non-blocking: Logs errors but doesn't fail trade closure if cancellation fails
- Files: lib/trading/position-manager.ts (external closure handler ~line 920)
- Documented as Common Pitfall #56
2025-11-20 14:52:29 +01:00
mindesbunister
5db7d7cc0a docs: Add Common Pitfall #55 - BlockedSignalTracker Pyth cache bug
- Documented Nov 20, 2025 fix for multi-timeframe data collection
- Tracker was using Pyth cache (empty) instead of Drift oracle prices
- Result: 30+ hours with no price tracking, all priceAfter* fields NULL
- Fix: Changed to initializeDriftService() + getOraclePrice()
- Now working: Price tracking every 5min, analysisComplete transitions, TP/SL detection
- Impact: Multi-timeframe data collection now operational for Phase 1 analysis
2025-11-20 10:38:00 +01:00
mindesbunister
c4c5c84c24 docs: Add Common Pitfall #43 - Runner trailing stop protection
Documented critical bug where runner trailing stop never activated after TP1:
- Symptom: Runner exposed to full reversal with static SL
- Real incident: 24 profit at risk, user forced manual close
- Root cause: External closure detected before TP2 price check
- Fix: Three-part solution with TP2 pre-check, diagnostics, exit reason detection
- Impact: Runner now fully autonomous with trailing protection

Also renumbered subsequent pitfalls 43→45, 44→46, etc. for consistency.

Commit: 55582a4 "critical: Fix runner trailing stop protection after TP1"
2025-11-20 08:07:59 +01:00
mindesbunister
042fb33002 docs: Add Common Pitfall #41 - Stats API P&L recalculation bug
Documented the stats API bug where recalculating P&L from entry/exit
prices doesn't work for TP1+runner partial closes.

Issue:
- Stats showed -$26.10 when actual was +$46.97
- Recalculation used average exit price × full size
- Doesn't account for different TP1 vs runner exit prices

Fix:
- Use database realizedPnL field directly
- Database values corrected to match Drift UI TP1+runner sums
- Each trade shows as 2 lines in Drift (TP1 + runner)

Commits referenced:
- cd6f590: Corrected database P&L values
- d8b0307: Fixed stats API calculation

Lesson: Partial closes require storing combined P&L, can't
recalculate from average exit price.
2025-11-19 21:55:14 +01:00
mindesbunister
d28da02089 docs: Add withdrawal system fixes to copilot instructions
Added recent commit examples to git workflow documentation:
- ATA implementation for USDC withdrawals (c37a9a3)
- MIN_SIGNAL_QUALITY_SCORE variable name fix
- Withdrawal statistics accuracy fix (8d53c4b)

Shows proper commit message format with types (feat, fix, critical)
and detailed descriptions of changes.
2025-11-19 20:38:11 +01:00
mindesbunister
c42bf94c1f fix: Clarify 40 is from deposits, not trading profits
Corrected misleading statement that implied 5.1x trading gains.
Reality: 46 total invested (06 + 40 deposits), 40 current = - trading P&L

Changes:
- Removed '5.1x in 8 days!' claim (was deposit-driven, not trading)
- Added 'Total Invested' line showing 46 actual capital input
- Added 'Trading P&L: -' to show early testing results
- Clarified losses from v6/v7 indicator testing before v8 optimization

Honesty matters - deposits ≠ profits. System is in proof phase.
2025-11-19 17:42:38 +01:00
mindesbunister
a6794d00dc docs: Update roadmap with 40 current capital and latest system improvements
Financial Updates:
- Current capital: 7.55 → 40 USDC (with deposits)
- Starting capital: 06 (Nov 11) + 40 deposits
- Phase 1 target: ,500 (4.6x growth from current)
- Monthly return target: 20-30% → 15-20% (more achievable with larger base)
- Risk tolerance: EXTREME → HIGH (increased capital cushion)
- Notional position size: ~,463 → ~,100 (at 15x leverage)

Recent Improvements (Nov 19, 2025):
 v8 Money Line Sticky Trend (0.6% flip threshold, anti-whipsaw)
 ATR-based TP/SL system (adaptive to volatility)
 ADX-based runner positioning (trend-strength adaptive SL)
 Multi-timeframe price tracking (15min/1H/4H/Daily data collection)
 Quality score increased to 81 (filters small chop trades)
 60/40 TP1/Runner split (captures runners on strong trends)
 Automated MFE/MAE analysis (30-minute tracking per signal)

System Status:
- Readiness: READY → PRODUCTION
- Confidence: HIGH → VERY HIGH
- Multi-timeframe: Fully operational, 2 signals tracked
- Background tracker: Runs every 5 minutes autonomously
- Next milestone: ,000 account (1.85x from current)
- Days to first withdrawal: ~60 days (Early January 2026)

Updated both TRADING_GOALS.md and copilot-instructions.md
2025-11-19 17:40:54 +01:00
mindesbunister
c450a78456 docs: Add multi-timeframe tracking and Docker maintenance to copilot instructions
- Multi-Timeframe Price Tracking System section (Nov 19, 2025)
  - Purpose and architecture overview
  - BlockedSignalTracker background job details
  - Database schema with price tracking fields
  - API endpoints and integration points
  - How it works (step-by-step flow)
  - Analysis queries for cross-timeframe comparison
  - Decision-making criteria for timeframe optimization
  - Current status: 2 signals tracked, pending 4H/Daily alerts

- Docker Maintenance section (Item #13 in When Making Changes)
  - Cleanup commands: image prune, builder prune, volume prune
  - When to run: After builds, weekly, on disk warnings
  - Space savings: 2-5GB images, 40-50GB cache, 0.5-1GB volumes
  - Safety guidelines: What to delete vs keep
  - Why critical: Prevent disk full from build cache accumulation

User requested: 'document in the copilot instructions with the latest updates'
Both sections provide complete reference for future AI agents
2025-11-19 17:27:12 +01:00
mindesbunister
e1bce56065 fix: Correct v8 P&L values in database
Two P&L corrections for v8 trades:
1. First losing trade: Updated from -7.88 to -9.93 (matches Drift UI)
2. Phantom trade bug: Updated from /bin/bash.00 to 4.19 (TP1 + runner combined)

Corrected v8 stats:
- 5 trades, 80% win rate
- Total P&L: 1.06 (was 6.87 before corrections)
- Average: 4.21 per trade
- System recovered all previous losses and turned profitable

Related: Common Pitfall #49 (P&L compounding) and #53 (phantom detection)
caused the /bin/bash.00 entry. Database now reflects actual Drift results.
2025-11-19 15:52:10 +01:00
mindesbunister
57790d04d6 docs: Document ADX-based adaptive runner SL system
Updated copilot-instructions.md with Nov 19, 2025 enhancement:

Architecture Overview - Exit Strategy:
- Added runner SL section with ADX-based positioning
- Documented three-tier system: <20 (0%), 20-25 (-0.3%), >25 (-0.55%)
- Explained rationale: Entry at candle close = natural pullbacks
- Risk management: Only accept drawdown on strong trends
- Example outcomes: ADX 18 → +$38.70, ADX 29 → +$22.20 worst case

Position Manager Section:
- Replaced "Dynamic SL adjustments: breakeven" with ADX-based logic
- Implementation details: Checks trade.adxAtEntry in TP1 handler
- Logging format: "🔒 ADX-based runner SL: 29.3 → -0.55%"
- Data collection phase: 50-100 trades for threshold optimization
- Noted TP1 default is 60% (not 75% - old value)

Key points documented:
- Adaptive vs fixed approach (capital preservation on weak trends)
- Integration with ADX trailing stop multiplier (both trend-adaptive)
- Natural retracement tolerance (entry at top requires -1% room)
- Future optimization plan (adjust 20/25 thresholds based on data)

Related commits:
- 66b2922: Implementation
- 7cf00cf: Fixed runner SL to -0.55% (replaced by ADX-based)
- d09838d: ADX trailing stop multiplier
2025-11-19 13:12:16 +01:00
mindesbunister
6515e1054b docs: Document ADX-based trailing stop multiplier system
Added comprehensive documentation for Nov 19, 2025 enhancement:

Architecture Overview:
- Updated Exit Strategy section with ADX multiplier details
- Documented graduated system (>30: 1.5, 25-30: 1.25×, <25: 1.0×)
- Explained profit acceleration (>2%: 1.3× additional)
- Noted backward compatibility (trades without ADX use base)

Position Manager Section:
- Added detailed ADX multiplier calculation logic
- Documented combined effect examples (ADX 29.3 + 2% profit = 1.95×)
- Explained purpose (capture more of 38% MFE trades)
- Noted trail distance clamping for safety

When Making Changes Section:
- Added item #11 for trailing stop modifications
- Documented verification logs to watch for
- Listed ActiveTrade interface requirement (adxAtEntry field)
- Included example log output for validation

Related commit: d09838d (implementation)
2025-11-19 12:03:06 +01:00
mindesbunister
f65af9530f feat: Enhanced timeframe extraction for multi-timeframe data collection
Updated n8n Parse Signal Enhanced to support multiple timeframe formats:
- 5m, 15m timeframes (buy 5, buy 15)
- Hourly: buy 60, buy 1h → 60
- 4-hour: buy 240, buy 4h → 240
- Daily: buy D, buy 1d → D
- Weekly/Monthly: buy W, buy M

Fixes:
- Default timeframe changed from '15' to '5' (5min is production)
- Added indicator version extraction (IND:v8)
- Proper conversion of hour/day notation to minutes
- Case-insensitive matching for D/W/M

Related: Multi-timeframe data collection system (execute endpoint saves
non-5min signals to BlockedSignal for cross-timeframe analysis). Now
15min signals from TradingView will be properly parsed and saved.

Files:
- workflows/trading/parse_signal_enhanced.json (updated regex + conversion)
- .github/copilot-instructions.md (documented supported formats)
2025-11-19 10:33:12 +01:00
mindesbunister
96f5cfae77 docs: Add Common Pitfall #51 - TP1 detection for fast on-chain fills
Documents the TP1 detection bug where on-chain limit orders fill faster
than Position Manager monitoring loop can detect. When both TP1 (75%)
and runner (25%) close before next monitoring cycle, PM doesn't know
TP1 filled first, marks entire closure as 'SL' instead of 'TP1'.

Fix uses profit percentage thresholds instead of state flags:
- >1.2%: TP2 range
- 0.3-1.2%: TP1 range
- <0.3%: SL range

Simpler and more reliable than tracking order fill state.
2025-11-19 09:09:53 +01:00
mindesbunister
120a4b499e docs: Mark P&L compounding bug as RESOLVED
Common Pitfall #50 updated with resolution details:
- Bug fixed: Removed previouslyRealized from external closure calculations
- Database corrected: 3 v8 trades updated with accurate P&L values
- System operational: New trade executed successfully at 08:40 CET
- Analytics baseline established: -$8.74 total P&L, 66.7% WR, 3 trades

Historical data gap (~3 trades) explains difference between
analytics (-$8.74) and Drift UI (~-$52). All future trades
will track accurately with fixed calculation.

Ready for Phase 1: Collect 50+ v8 trades for statistical validation.
2025-11-19 08:50:22 +01:00
mindesbunister
a69d2b51a5 docs: Document Nov 19 critical findings - database/Drift mismatch and P&L inflation
- Database shows only 3 v8 trades when Drift UI shows 6 trades
- One trade has 10x inflated P&L (81 vs 9 expected)
- Bot receiving ZERO API requests after 06:51 restart despite n8n executions succeeding
- Real v8 performance: ~2 LOSS (from Drift), database shows 20 profit (WRONG)
- Two issues: P&L compounding bug still active + n8n→bot connection broken
- Need to verify n8n workflow endpoints and fix external closure P&L calculation
- Common Pitfall #50 added to copilot-instructions.md
2025-11-19 08:33:03 +01:00
mindesbunister
5146f37acc docs: Add multi-timeframe data collection documentation
- Updated copilot instructions with data collection system overview
- Documents 5min execute vs 15min/1H/4H/Daily collect pattern
- Explains zero-risk parallel data collection approach
- Notes SQL analysis capability for timeframe optimization
- References implementation location in execute endpoint
- Part of v8 indicator testing and optimization strategy
2025-11-18 20:39:08 +01:00
mindesbunister
c330c60d88 docs: Add v8 Money Line indicator documentation to copilot instructions
- Added v8 to indicator version tracking section (Common Pitfall #26)
- Created comprehensive indicator comparison section (v6/v7/v8)
- Documented v8 improvements: flip threshold, momentum confirmation, stickier multipliers
- Added A/B testing expectations and visual backtest results
- Referenced README.md lines 48-120 for user-facing documentation
- Noted v7 deprecation (no fundamental improvements over v6)
2025-11-18 15:57:14 +01:00
mindesbunister
becd5631e7 docs: Add Common Pitfall #49 - P&L exponential compounding bug
Documents the critical P&L compounding bug fixed in commit 6156c0f where
trade.realizedPnL mutation during external closure detection caused 15-20x
inflation of actual profit/loss values.

Includes:
- Root cause: Mutating trade.realizedPnL in monitoring loop
- Real incident: $6 actual profit → $92.46 in database
- Bug mechanism with code examples
- Why previous fixes (Common Pitfalls #27, #48) didn't prevent this
- Fix: Don't mutate shared state during calculations
- Related to other P&L compounding variants for cross-reference
2025-11-17 15:34:22 +01:00
mindesbunister
84bd2e27f0 docs: Add comprehensive ATR-based risk management documentation
- Document ATR × multiplier system (2.0/4.0/3.0 for TP1/TP2/SL)
- Add calculation formula and example with SOL at $140
- Document safety bounds (MIN/MAX percentages)
- Include data-driven ATR values (SOL median 0.43 from 162 trades)
- Document ENV configuration variables
- Add regime-agnostic benefits explanation
- Update Exit Strategy section with ATR-based details
- Update Telegram manual trade presets with accurate ATR
- Add TradingView integration requirements
- Update 'When Making Changes' with ATR modification guidance
- Explain performance analysis and expected improvements

Why: Major system upgrade (Nov 17, 2025) requires complete documentation
for future AI agents and developers. ATR-based targets solve bull/bear
optimization bias by adapting to actual market volatility.
2025-11-17 12:36:44 +01:00
mindesbunister
018f973609 critical: Fix P&L compounding during close verification (20x inflation bug)
Problem:
- Close transaction confirmed but Drift state takes 5-10s to propagate
- Position Manager returned needsVerification=true to keep monitoring
- BUT: Monitoring loop detected position as 'externally closed' EVERY 2 seconds
- Each detection called handleExternalClosure() and added P&L to database
- Result: .66 actual profit → 73.36 in database (20x compounding)
- Logs showed: $112.96 → $117.62 → $122.28 → ... → $173.36 (14+ updates)

Root Cause:
- Common Pitfall #47 fix introduced needsVerification flag to wait for propagation
- But NO flag to prevent external closure detection during wait period
- Monitoring loop thought position was 'closed externally' on every cycle
- Rate limiting (429 errors) made it worse by extending wait time

Fix (closingInProgress flag):
1. Added closingInProgress boolean to ActiveTrade interface
2. Set flag=true when needsVerification returned (close confirmed, waiting)
3. Skip external closure detection entirely while flag=true
4. Timeout after 60s if stuck (abnormal case - allows cleanup)

Impact:
- Every close with verification delay (most closes) had 10-20x P&L inflation
- This is variant of Common Pitfall #27 but during verification, not external closure
- Rate limited closes were hit hardest (longer wait = more compounding cycles)

Files:
- lib/trading/position-manager.ts: Added closingInProgress flag + skip logic

Incident: Nov 16, 11:50 CET - SHORT 41.64→40.08 showed 73.36 vs .66 real
Documented: Common Pitfall #48
2025-11-16 15:07:27 +01:00
mindesbunister
54815a0daa docs: Add multi-fix deployment verification to VERIFICATION MANDATE
- Extended verification checklist for sessions with multiple related fixes
- Added requirement to verify container newer than ALL commits
- Included example from Nov 16 session (3 fixes deployed together)
- Added bash commands for complete deployment verification
- Emphasized that ALL fixes must be deployed, not just latest commit
- Updated Common Pitfall #47 with deployment verification commands
- Prevents declaring fixes 'working' when only some are deployed
2025-11-16 10:33:58 +01:00
mindesbunister
84f40f3e15 docs: Document position close verification fix (Common Pitfall #47)
- Added comprehensive documentation for close verification gap bug
- Real incident: 6 hours unmonitored exposure after close confirmation
- Root cause: Transaction confirmed ≠ Drift state propagated (5-10s delay)
- Fix: 5s wait + verification + needsVerification flag for Position Manager
- Prevents premature database 'closed' marking while position still open
- TypeScript interface updated: ClosePositionResult.needsVerification
- Deployed: Nov 16, 2025 09:28:20 CET
- Commits: c607a66 (logic), b23dde0 (interface)
2025-11-16 10:31:23 +01:00
mindesbunister
9905ab4f5a docs: Add Common Pitfall #47 - position close verification gap 2025-11-16 10:05:12 +01:00
mindesbunister
673a49302a critical: Fix breakeven SL using wrong entry price after TP1
- CRITICAL BUG: Drift SDK's position.entryPrice RECALCULATES after partial closes
- After TP1, Drift returns COST BASIS of remaining position, NOT original entry
- Example: SHORT @ 38.52 → TP1 @ 70% → Drift shows entry 40.01 (runner's basis)
- Result: Breakeven SL set .50 ABOVE actual entry = guaranteed loss if triggered

Fix:
- Always use database trade.entryPrice for breakeven calculations
- Drift's position.entryPrice = current state (runner cost basis)
- Database entryPrice = original entry (authoritative for breakeven)
- Added logging to show both values for verification

Impact:
- Every TP1 → breakeven transition was using WRONG price
- Locking in losses instead of true breakeven protection
- Financial loss bug affecting every trade with TP1

Files:
- lib/trading/position-manager.ts: Line 513 - use trade.entryPrice not position.entryPrice
- .github/copilot-instructions.md: Added Common Pitfall #43, deprecated old #44

Incident: Nov 16, 02:47 CET - SHORT entry 38.52, breakeven SL set at 40.01
Position closed by ghost detection before SL could trigger (lucky)
2025-11-16 03:00:22 +01:00
mindesbunister
737e0c295f docs: Add Common Pitfall #45 - 100% position sizing InsufficientCollateral
Documents the fix for InsufficientCollateral errors when using 100% position
sizing despite having correct account leverage.

Issue: Drift's margin calculation includes fees, slippage buffers, and rounding.
Using exact 100% of collateral leaves no room, causing $0.03-0.10 shortages.

Example: $85.55 collateral
- Bot tries: 100% = $85.55
- Margin needed: $85.58 (includes fees)
- Result: Rejected

Solution: Automatically apply 99% safety buffer when configured at 100%.
Result: $85.55 × 99% = $84.69 (leaves $0.86 safety margin)

Includes real incident details, code implementation, and math proof.
User's Drift UI screenshot proved account leverage WAS set correctly to 15x.

Related commit: 7129cbf
2025-11-16 01:58:18 +01:00
mindesbunister
56b2195b88 docs: Add Common Pitfall #44 - Breakeven SL price discrepancy
Documents the fix for breakeven SL using database entry price instead of
Drift's actual on-chain entry price.

Issue: Database stored $139.18 but Drift actual was $139.07, causing
'breakeven' SL to lock in $0.11 loss per token for SHORT positions.

Solution: Query position.entryPrice from Drift SDK (calculated from
on-chain quoteAssetAmount / baseAssetAmount) when setting breakeven.

Includes real incident details, code comparison, and relationship to
Common Pitfall #33 (orphaned position restoration).

Related commit: 528a0f4
2025-11-16 01:45:48 +01:00
mindesbunister
dc6625404a docs: Document session improvements for developer continuity
Added comprehensive documentation per user request: 'is everything documentet
and pushed to git? nothing we forget to mention which is crucial for other
developers/agents?'

Updates:
- Common Pitfall #40: Added refactoring note (removed time-based Layer 1)
  - User feedback: Time-based cleanup too aggressive for long-running positions
  - Now 100% Drift API-based ghost detection (commit 9db5f85)

- Common Pitfall #41: Telegram notifications for position closures (NEW)
  - Implemented lib/notifications/telegram.ts with sendPositionClosedNotification()
  - Direct Telegram API calls (no n8n dependency)
  - Includes P&L, prices, hold time, MAE/MFE, exit reason with emojis
  - Integrated into Position Manager (commit b1ca454)

- Common Pitfall #42: Telegram bot DNS retry logic (NEW)
  - Python urllib3 transient DNS failures (same as Node.js)
  - Added retry_request() wrapper with exponential backoff
  - 3 attempts (2s → 4s → 8s), matches Node.js retryOperation pattern
  - Applied to /status and manual trade execution (commit bdf1be1)

- Common Pitfall #43: Drift account leverage UI requirement (NEW)
  - Account leverage is on-chain setting, CANNOT be changed via API
  - Must use Drift UI settings page
  - Confusion: Order leverage dropdown ≠ account leverage setting
  - Temporary workaround: Reduced position size to 6% for 1x leverage
  - User action needed: Set account leverage to 15x in Drift UI

- Telegram Notifications section: Added to main architecture documentation
  - Purpose, format, configuration, integration points
  - Reference implementation details

Session focus: Ghost detection refactoring, Telegram notifications, DNS retry,
Drift leverage diagnostics. User emphasized knowledge preservation for future
developers and AI agents working on this codebase.
2025-11-16 01:28:03 +01:00
mindesbunister
bbab693cc1 docs: Document ghost position death spiral fix as Common Pitfall #40
Added comprehensive documentation for 3-layer ghost prevention system:
- Root cause analysis (validation skipped during rate limiting)
- Death spiral explanation (ghosts → rate limits → skipped validation)
- User requirement context (must be fully autonomous)
- Real incident details (Nov 15, 2025)
- Complete solution with code examples for all 3 layers
- Impact and verification notes
- Key lesson: validation logic must never skip during errors

Files changed:
- .github/copilot-instructions.md (added Pitfall #40)
2025-11-15 23:52:39 +01:00
mindesbunister
e057cda990 fix: Settings UI .env permission error - container user writability
CRITICAL FIX: Settings UI was completely broken with EACCES permission denied

Problem:
- .env file on host owned by root:root
- Docker mounts .env as volume, retains host ownership
- Container runs as nextjs user (UID 1001) for security
- Settings API attempts fs.writeFileSync() → permission denied
- Users could NOT adjust position size, leverage, TP/SL, or any config

User escalation: "thats a major flaw. THIS NEEDS TO WORK."

Solution:
- Changed .env ownership on HOST to UID 1001 (nextjs user)
- chown 1001:1001 /home/icke/traderv4/.env
- Restarted container to pick up new permissions
- .env now writable by nextjs user inside container

Verified: Settings UI now saves successfully

Documented as Common Pitfall #39 with:
- Symptom, root cause, and impact
- Why docker exec chown fails (mounted files)
- Correct fix with UID matching
- Alternative solutions and tradeoffs
- Lesson about Docker volume mount ownership

Files changed:
- .github/copilot-instructions.md (added Pitfall #39)
- .env (ownership changed from root:root to 1001:1001)
2025-11-15 23:33:41 +01:00