**Issue 1: TP2 Runner Position Bug** ✅ FIXED - TP2 was calculated as 80% of ORIGINAL position instead of REMAINING - With TP1=75%, TP2=80%: Was closing 75%+80%=155% (capped at 100%) - Now correctly: TP1 closes 75%, TP2 closes 80% of remaining 25% = 20% - Result: 5% runner now remains for trailing stop as intended! **Issue 2: Race Condition - Orphaned SL Orders** ✅ FIXED - Orders were placed AFTER Position Manager started monitoring - If TP hit fast, PM detected 'external closure' before orders finished - Orders completed after position gone → orphaned SL orders on Drift - Now: Exit orders placed BEFORE starting monitoring - PM can now properly cancel remaining orders when position closes **Issue 3: 5min vs 15min Timeframe** ⚠️ NEEDS VERIFICATION - n8n workflow correctly filters for timeframe === '15' - Extracts timeframe with regex: /\.P\s+(\d+)/ - User needs to verify TradingView alert includes '.P 15' in message - Format should be: 'SOL buy .P 15' not just 'SOL buy' **Technical Changes:** - lib/drift/orders.ts: Fixed TP2 calculation to use remaining size - Added logging: Shows TP1, TP2, remaining, and runner amounts - app/api/trading/execute/route.ts: Reordered to place orders before monitoring - Prevents race condition where orders complete after position closed **Testing:** - Next trade will show proper runner position (5% remains) - No more orphaned SL orders after wins - Logs will show: 'Runner (if any): $X.XX' **Documentation:** - Created CRITICAL_ISSUES_FOUND.md explaining all 3 issues - Created FIXES_APPLIED.md with testing instructions
109 lines
2.9 KiB
Markdown
109 lines
2.9 KiB
Markdown
# Trading Bot v4 - Critical Issues Found & Fixes
|
||
|
||
## Issue Summary
|
||
|
||
Three critical issues discovered:
|
||
|
||
1. **5-minute chart triggered instead of 15-minute** - TradingView alert format issue
|
||
2. **SL orders not cancelled after winning trade** - Race condition + order calculation bug
|
||
3. **No runner position (20% should remain)** - TP2 size calculation bug
|
||
|
||
---
|
||
|
||
## Issue 1: Wrong Timeframe Triggered
|
||
|
||
### Problem
|
||
- Trade executed on 5-minute chart signal
|
||
- n8n workflow has correct filter for "15" timeframe
|
||
- Filter checks: `timeframe === "15"`
|
||
|
||
### Root Cause
|
||
- n8n extracts timeframe with regex: `/\.P\s+(\d+)/`
|
||
- Looks for ".P 5" or ".P 15" in TradingView message
|
||
- Defaults to '15' if no match found
|
||
|
||
### Solution
|
||
**Check your TradingView alert message format:**
|
||
|
||
Your alert should include the timeframe like this:
|
||
```
|
||
SOL buy .P 15
|
||
```
|
||
|
||
The ".P 15" tells n8n it's a 15-minute chart. If you're sending:
|
||
```
|
||
SOL buy .P 5
|
||
```
|
||
|
||
Then n8n will reject it (correctly filtering out 5-minute signals).
|
||
|
||
**Verify n8n is receiving correct format by checking n8n execution logs.**
|
||
|
||
---
|
||
|
||
## Issue 2: SL Orders Not Cancelled
|
||
|
||
### Problem
|
||
- After winning trade, 2 SL orders remain on Drift ($198.39 and $195.77)
|
||
- Bot detected "position closed externally" but found "no orders to cancel"
|
||
|
||
### Root Cause
|
||
**Race Condition in `/api/trading/execute`:**
|
||
|
||
Current order of operations:
|
||
1. Open position ✅
|
||
2. Add to Position Manager (starts monitoring immediately) ⚠️
|
||
3. Place exit orders (TP1, TP2, SL) ⏰
|
||
|
||
If TP hits very fast (< 2-3 seconds):
|
||
- Position Manager detects "external closure" while orders are still being placed
|
||
- Tries to cancel orders that don't exist yet
|
||
- Orders finish placing AFTER position is gone → orphaned orders
|
||
|
||
### Solution
|
||
**Reorder operations: Place exit orders BEFORE starting monitoring**
|
||
|
||
---
|
||
|
||
## Issue 3: No Runner Position
|
||
|
||
### Problem
|
||
- Config: `TAKE_PROFIT_2_SIZE_PERCENT=80` (should leave 20% runner)
|
||
- Expected: TP1 closes 75% → TP2 closes 80% of remaining → 5% runner remains
|
||
- Actual: Position 100% closed, no runner
|
||
|
||
### Root Cause
|
||
**BUG in `/home/icke/traderv4/lib/drift/orders.ts` lines 232-233:**
|
||
|
||
```typescript
|
||
const tp1USD = (options.positionSizeUSD * options.tp1SizePercent) / 100
|
||
const tp2USD = (options.positionSizeUSD * options.tp2SizePercent) / 100
|
||
```
|
||
|
||
Both TP1 and TP2 are calculated as **percentages of ORIGINAL position**, not remaining!
|
||
|
||
**With your settings (TP1=75%, TP2=80%, position=$80):**
|
||
- TP1: 75% × $80 = $60 ✅
|
||
- TP2: 80% × $80 = $64 ❌ (should be 80% × $20 remaining = $16)
|
||
- Total: $60 + $64 = $124 (exceeds position size!)
|
||
|
||
Drift caps at 100%, so entire position closes.
|
||
|
||
### Solution
|
||
**Fix TP2 calculation to use remaining size after TP1**
|
||
|
||
---
|
||
|
||
## Recommended Fixes
|
||
|
||
### Fix 1: TradingView Alert Format
|
||
|
||
Update your TradingView alert to include ".P 15":
|
||
```
|
||
{{ticker}} {{strategy.order.action}} .P 15
|
||
```
|
||
|
||
### Fix 2 & 3: Code Changes
|
||
|
||
See next files for implementation...
|