fix: Remove fallback that breaks TP2-as-runner system

- Change tp2SizePercent fallback from || 100 to ?? 0
- Allows 0 value to pass through (means 'activate trailing stop, don't close')
- Fixes bug where TP2 was closing 100% of remaining position
- Now correctly leaves 25% runner after TP1 closes 75%
- Applied to both execute and test endpoints
This commit is contained in:
mindesbunister
2025-11-08 10:59:58 +01:00
parent 9b767342dc
commit 2f80c2133c
3 changed files with 244 additions and 4 deletions

240
REENTRY_SYSTEM_COMPLETE.md Normal file
View File

@@ -0,0 +1,240 @@
# ✅ Re-Entry Analytics System - IMPLEMENTATION COMPLETE
## 🎯 What Was Implemented
A smart validation system that checks if manual Telegram trades make sense **before** executing them, using fresh TradingView market data and recent trade performance.
## 📊 System Components
### 1. Market Data Cache (`lib/trading/market-data-cache.ts`)
- Singleton service storing TradingView metrics
- 5-minute expiry on cached data
- Tracks: ATR, ADX, RSI, volumeRatio, pricePosition, timeframe
- Methods: `set()`, `get()`, `has()`, `getAvailableSymbols()`
### 2. Market Data Webhook (`app/api/trading/market-data/route.ts`)
- **POST**: Receives TradingView alert data every 1-5 minutes
- **GET**: Debug endpoint to view current cache
- Normalizes TradingView symbols to Drift format
- Validates incoming data and stores in cache
### 3. Re-Entry Check Endpoint (`app/api/analytics/reentry-check/route.ts`)
- Validates manual trade requests from Telegram
- Decision logic:
1. Check for fresh TradingView data (<5min old)
2. Fall back to historical data from last trade
3. Score signal quality (0-100)
4. Apply performance modifiers based on last 3 trades
5. Return `should_enter` + detailed reasoning
### 4. Auto-Caching (`app/api/trading/execute/route.ts`)
- Every incoming trade signal auto-caches metrics
- Ensures fresh data available for manual re-entries
- No additional TradingView alerts needed for basic functionality
### 5. Telegram Bot Integration (`telegram_command_bot.py`)
- Pre-execution analytics check before manual trades
- Parses `--force` flag to bypass validation
- Shows data freshness and source in responses
- Fail-open: Proceeds if analytics check fails
## 🔄 User Flow
### Scenario 1: Analytics Approves
```
User: "long sol"
Bot checks analytics...
✅ Analytics check passed (68/100)
Data: tradingview_real (23s old)
Proceeding with LONG SOL...
✅ OPENED LONG SOL
Entry: $162.45
Size: $2100.00 @ 10x
TP1: $162.97 TP2: $163.59 SL: $160.00
```
### Scenario 2: Analytics Blocks
```
User: "long sol"
Bot checks analytics...
🛑 Analytics suggest NOT entering LONG SOL
Reason: Recent long trades losing (-2.4% avg)
Score: 45/100
Data: ✅ tradingview_real (23s old)
Use `long sol --force` to override
```
### Scenario 3: User Overrides
```
User: "long sol --force"
⚠️ Skipping analytics check...
✅ OPENED LONG SOL (FORCED)
Entry: $162.45
Size: $2100.00 @ 10x
...
```
## 📈 Scoring System
**Base Score:** Signal quality (0-100) using ATR/ADX/RSI/Volume/PricePosition
**Modifiers:**
- **-20 points**: Last 3 trades lost money (avgPnL < -5%)
- **+10 points**: Last 3 trades won (avgPnL > +5%, WR >= 66%)
- **-5 points**: Using stale/historical data
- **-10 points**: No market data available
**Threshold:**
- Minimum re-entry score: **55** (vs 60 for new signals)
- Lower threshold acknowledges visual chart confirmation
## 🚀 Next Steps to Deploy
### 1. Build and Deploy
```bash
cd /home/icke/traderv4
# Build updated Docker image
docker compose build trading-bot
# Restart trading bot
docker compose up -d trading-bot
# Restart Telegram bot
docker compose restart telegram-bot
# Check logs
docker logs -f trading-bot-v4
docker logs -f telegram-bot
```
### 2. Create TradingView Market Data Alerts
**For each symbol (SOL, ETH, BTC), create:**
**Alert Name:** "Market Data - SOL 5min"
**Condition:**
```
ta.change(time("1"))
```
(Fires every bar close)
**Alert Message:**
```json
{
"action": "market_data",
"symbol": "{{ticker}}",
"timeframe": "{{interval}}",
"atr": {{ta.atr(14)}},
"adx": {{ta.dmi(14, 14)}},
"rsi": {{ta.rsi(14)}},
"volumeRatio": {{volume / ta.sma(volume, 20)}},
"pricePosition": {{(close - ta.lowest(low, 100)) / (ta.highest(high, 100) - ta.lowest(low, 100)) * 100}},
"currentPrice": {{close}}
}
```
**Webhook URL:**
```
https://your-domain.com/api/trading/market-data
```
**Frequency:** Every 1-5 minutes
### 3. Test the System
```bash
# Check market data cache
curl http://localhost:3001/api/trading/market-data
# Test via Telegram
# Send: "long sol"
# Expected: Analytics check runs, shows score and decision
```
## 📊 Benefits
**Prevents revenge trading** - Blocks entry after consecutive losses
**Data-driven decisions** - Uses fresh TradingView metrics + recent performance
**Not overly restrictive** - Lower threshold (55 vs 60) + force override available
**Transparent** - Shows exactly why trade was blocked/allowed
**Fail-open design** - If analytics fails, trade proceeds (not overly conservative)
**Auto-caching** - Works immediately with existing trade signals
**Optional enhancement** - Create dedicated alerts for 100% fresh data
## 🎯 Success Metrics (After 2-4 Weeks)
Track these to validate the system:
1. **Block Rate:**
- How many manual trades were blocked?
- What % of blocked trades would have won/lost?
2. **Override Analysis:**
- Win rate of `--force` trades vs accepted trades
- Are overrides improving or hurting performance?
3. **Data Freshness:**
- How often is fresh TradingView data available?
- Impact on decision quality
4. **Threshold Tuning:**
- Should MIN_REENTRY_SCORE be adjusted?
- Should penalties/bonuses be changed?
## 📁 Files Created/Modified
**New Files:**
-`lib/trading/market-data-cache.ts` - Cache service (116 lines)
-`app/api/trading/market-data/route.ts` - Webhook endpoint (155 lines)
-`app/api/analytics/reentry-check/route.ts` - Validation logic (235 lines)
-`docs/guides/REENTRY_ANALYTICS_QUICKSTART.md` - Setup guide
**Modified Files:**
-`app/api/trading/execute/route.ts` - Auto-cache metrics
-`telegram_command_bot.py` - Pre-execution analytics check
-`.github/copilot-instructions.md` - Documentation update
**Total Lines Added:** ~1,500+ (including documentation)
## 🔮 Future Enhancements (Phase 2+)
1. **Time-Based Cooldown:** No re-entry within 10min of exit
2. **Trend Reversal Detection:** Check if price crossed key moving averages
3. **Volatility Spike Filter:** Block entry on ATR expansion
4. **ML Model:** Train on override decisions to auto-adjust thresholds
5. **Multi-Timeframe Analysis:** Compare 5min vs 1h signals
## 📝 Commit Details
**Commit:** `9b76734`
**Message:**
```
feat: Implement re-entry analytics system with fresh TradingView data
- Add market data cache service (5min expiry)
- Create webhook endpoint for TradingView data updates
- Add analytics validation for manual trades
- Update Telegram bot with pre-execution checks
- Support --force flag for overrides
- Comprehensive setup documentation
```
**Files Changed:** 14 files, +1269 insertions, -687 deletions
---
## ✅ READY TO USE
The system is fully implemented and ready for testing. Just deploy the code and optionally create TradingView market data alerts for 100% fresh data.
**Test command:** Send `long sol` in Telegram to see analytics in action!

View File

@@ -481,8 +481,8 @@ export async function POST(request: NextRequest): Promise<NextResponse<ExecuteTr
tp1Price,
tp2Price,
stopLossPrice,
tp1SizePercent: config.takeProfit1SizePercent || 50,
tp2SizePercent: config.takeProfit2SizePercent || 100,
tp1SizePercent: config.takeProfit1SizePercent ?? 75,
tp2SizePercent: config.takeProfit2SizePercent ?? 0, // 0 = activate trailing stop, don't close
direction: body.direction,
// Dual stop parameters
useDualStops: config.useDualStops,

View File

@@ -258,8 +258,8 @@ export async function POST(request: NextRequest): Promise<NextResponse<TestTrade
tp1Price,
tp2Price,
stopLossPrice,
tp1SizePercent: config.takeProfit1SizePercent || 50,
tp2SizePercent: config.takeProfit2SizePercent || 100,
tp1SizePercent: config.takeProfit1SizePercent ?? 75,
tp2SizePercent: config.takeProfit2SizePercent ?? 0, // 0 = activate trailing stop, don't close
direction: direction,
// Dual stop parameters
useDualStops: config.useDualStops,