**Documentation Structure:** - Created docs/ subdirectory organization (analysis/, architecture/, bugs/, cluster/, deployments/, roadmaps/, setup/, archived/) - Moved 68 root markdown files to appropriate categories - Root directory now clean (only README.md remains) - Total: 83 markdown files now organized by purpose **New Content:** - Added comprehensive Environment Variable Reference to copilot-instructions.md - 100+ ENV variables documented with types, defaults, purpose, notes - Organized by category: Required (Drift/RPC/Pyth), Trading Config (quality/ leverage/sizing), ATR System, Runner System, Risk Limits, Notifications, etc. - Includes usage examples (correct vs wrong patterns) **File Distribution:** - docs/analysis/ - Performance analyses, blocked signals, profit projections - docs/architecture/ - Adaptive leverage, ATR trailing, indicator tracking - docs/bugs/ - CRITICAL_*.md, FIXES_*.md bug reports (7 files) - docs/cluster/ - EPYC setup, distributed computing docs (3 files) - docs/deployments/ - *_COMPLETE.md, DEPLOYMENT_*.md status (12 files) - docs/roadmaps/ - All *ROADMAP*.md strategic planning files (7 files) - docs/setup/ - TradingView guides, signal quality, n8n setup (8 files) - docs/archived/2025_pre_nov/ - Obsolete verification checklist (1 file) **Key Improvements:** - ENV variable reference: Single source of truth for all configuration - Common Pitfalls #68-71: Already complete, verified during audit - Better findability: Category-based navigation vs 68 files in root - Preserves history: All files git mv (rename), not copy/delete - Zero broken functionality: Only documentation moved, no code changes **Verification:** - 83 markdown files now in docs/ subdirectories - Root directory cleaned: 68 files → 0 files (except README.md) - Git history preserved for all moved files - Container running: trading-bot-v4 (no restart needed) **Next Steps:** - Create README.md files in each docs subdirectory - Add navigation index - Update main README.md with new structure - Consolidate duplicate deployment docs - Archive truly obsolete files (old SQL backups) See: docs/analysis/CLEANUP_PLAN.md for complete reorganization strategy
4.7 KiB
4.7 KiB
Fixes Applied - Trading Bot v4
Summary of Changes
Fixed 3 critical bugs discovered in your trading bot:
- ✅ TP2 Runner Calculation Bug - Now correctly calculates TP2 as percentage of REMAINING position
- ✅ Race Condition Fix - Exit orders now placed BEFORE Position Manager starts monitoring
- ⚠️ TradingView Timeframe - Needs verification of alert format
Fix 1: TP2 Runner Position Bug
File: lib/drift/orders.ts
Problem:
// BEFORE (WRONG):
const tp1USD = (options.positionSizeUSD * options.tp1SizePercent) / 100 // 75% of $80 = $60
const tp2USD = (options.positionSizeUSD * options.tp2SizePercent) / 100 // 80% of $80 = $64 ❌
// Total: $124 (exceeds position!) → Drift closes 100%, no runner
Fixed:
// AFTER (CORRECT):
const tp1USD = (options.positionSizeUSD * options.tp1SizePercent) / 100 // 75% of $80 = $60
const remainingAfterTP1 = options.positionSizeUSD - tp1USD // $80 - $60 = $20
const tp2USD = (remainingAfterTP1 * options.tp2SizePercent) / 100 // 80% of $20 = $16 ✅
// Remaining: $20 - $16 = $4 (5% runner!) ✅
Result:
- With
TAKE_PROFIT_2_SIZE_PERCENT=80:- TP1 closes 75% ($60)
- TP2 closes 80% of remaining ($16)
- 5% runner remains ($4) for trailing stop!
Added logging to verify:
📊 Exit order sizes:
TP1: 75% of $80.00 = $60.00
Remaining after TP1: $20.00
TP2: 80% of remaining = $16.00
Runner (if any): $4.00
Fix 2: Race Condition - Orphaned SL Orders
File: app/api/trading/execute/route.ts
Problem:
Old Flow:
1. Open position
2. Add to Position Manager → starts monitoring immediately
3. Place exit orders (TP1, TP2, SL)
If TP hits fast (< 2-3 seconds):
- Position Manager detects "external closure"
- Tries to cancel orders (finds none yet)
- Orders finish placing AFTER position gone
- Result: Orphaned SL orders on Drift!
Fixed:
New Flow:
1. Open position
2. Place exit orders (TP1, TP2, SL) ← FIRST
3. Add to Position Manager → starts monitoring
Now:
- All orders exist before monitoring starts
- If TP hits fast, Position Manager can cancel remaining orders
- No orphaned orders!
Issue 3: TradingView Timeframe Filter
Status: Needs Your Action
The n8n workflow correctly filters for 15-minute timeframe:
{
"conditions": {
"string": [{
"value1": "={{ $json.timeframe }}",
"operation": "equals",
"value2": "15"
}]
}
}
The timeframe is extracted from your TradingView alert with regex:
/\.P\s+(\d+)/ // Looks for ".P 15" or ".P 5" in message
Action Required:
Check your TradingView alert message format.
It should look like:
{{ticker}} {{strategy.order.action}} .P 15
Examples:
- ✅ Correct:
SOL buy .P 15(will be accepted) - ❌ Wrong:
SOL buy .P 5(will be rejected) - ⚠️ Missing:
SOL buy(defaults to 15, but not explicit)
To verify:
- Open your TradingView chart
- Go to Alerts
- Check the alert message format
- Ensure it includes ".P 15" for 15-minute timeframe
Testing the Fixes
Test 1: Runner Position
- Place a test trade (or wait for next signal)
- Watch position in Drift
- TP1 should hit → 75% closes
- TP2 should hit → 80% of remaining closes
- 5% runner should remain for trailing stop
Expected in Drift:
- After TP1: 0.25 SOL remaining (from 1.0 SOL)
- After TP2: 0.05 SOL remaining (runner)
Test 2: No Orphaned Orders
- Place test trade
- If TP hits quickly, check Drift "Orders" tab
- Should show: No open orders after position fully closes
- Previously: 2 SL orders remained after win
Test 3: Timeframe Filter
- Send 5-minute alert from TradingView (with ".P 5")
- Check n8n execution → Should be rejected by filter
- Send 15-minute alert (with ".P 15")
- Should be accepted and execute trade
Build & Deploy
# Build (in progress)
docker compose build trading-bot
# Restart
docker compose up -d --force-recreate trading-bot
# Verify
docker logs -f trading-bot-v4
Look for new log message:
📊 Exit order sizes:
TP1: 75% of $XX.XX = $XX.XX
Remaining after TP1: $XX.XX
TP2: 80% of remaining = $XX.XX
Runner (if any): $XX.XX
Summary
| Issue | Status | Impact |
|---|---|---|
| TP2 Runner Calculation | ✅ Fixed | 5% runner will now remain as intended |
| Orphaned SL Orders | ✅ Fixed | Orders placed before monitoring starts |
| 5min vs 15min Filter | ⚠️ Verify | Check TradingView alert includes ".P 15" |
Next Steps:
- Deploy fixes (build running)
- Verify TradingView alert format
- Test with next trade signal
- Monitor for runner position and clean order cancellation