Fix P&L calculation and signal flip detection

- Fix external closure P&L using tp1Hit flag instead of currentSize
- Add direction change detection to prevent false TP1 on signal flips
- Signal flips now recorded with accurate P&L as 'manual' exits
- Add retry logic with exponential backoff for Solana RPC rate limits
- Create /api/trading/cancel-orders endpoint for manual cleanup
- Improves data integrity for win/loss statistics
This commit is contained in:
mindesbunister
2025-11-09 17:59:50 +01:00
parent 4d533ccb53
commit 22195ed34c
15 changed files with 2166 additions and 17 deletions

View File

@@ -0,0 +1,243 @@
# TradingView Market Data Alert Setup
## Quick Copy-Paste Alert Configuration
### Alert 1: SOL Market Data (5-minute bars)
**Symbol:** SOLUSDT
**Timeframe:** 5 minutes
**Alert Name:** Market Data - SOL 5min
**Condition:**
```pinescript
ta.change(time("1"))
```
(This 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:** (Choose one based on your setup)
```
Option 1 (if bot is publicly accessible):
https://YOUR-DOMAIN.COM:3001/api/trading/market-data
Option 2 (if using n8n as proxy):
https://flow.egonetix.de/webhook/market-data
Option 3 (local testing):
http://YOUR-SERVER-IP:3001/api/trading/market-data
```
**Settings:**
- ✅ Webhook URL (enable and enter URL above)
- ✅ Once Per Bar Close
- Expiration: Never
- Name on chart: Market Data - SOL 5min
---
### Alert 2: ETH Market Data (5-minute bars)
**Symbol:** ETHUSDT
**Timeframe:** 5 minutes
**Alert Name:** Market Data - ETH 5min
**Condition:**
```pinescript
ta.change(time("1"))
```
**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:** (Same as SOL above)
**Settings:**
- ✅ Webhook URL (same as SOL)
- ✅ Once Per Bar Close
- Expiration: Never
---
### Alert 3: BTC Market Data (5-minute bars)
**Symbol:** BTCUSDT
**Timeframe:** 5 minutes
**Alert Name:** Market Data - BTC 5min
**Condition:**
```pinescript
ta.change(time("1"))
```
**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:** (Same as SOL above)
**Settings:**
- ✅ Webhook URL (same as SOL)
- ✅ Once Per Bar Close
- Expiration: Never
---
## Verification Steps
### Step 1: Check Webhook Endpoint is Accessible
```bash
# From your server
curl http://localhost:3001/api/trading/market-data
# Should return:
# {"success":true,"availableSymbols":[],"count":0,"cache":{}}
```
### Step 2: Wait 5 Minutes for First Alert
After creating alerts, wait for next bar close (5 minutes max)
### Step 3: Verify Cache is Populated
```bash
curl http://localhost:3001/api/trading/market-data
# Should now show:
# {
# "success": true,
# "availableSymbols": ["SOL-PERP", "ETH-PERP", "BTC-PERP"],
# "count": 3,
# "cache": {
# "SOL-PERP": {
# "atr": 0.45,
# "adx": 32.1,
# "rsi": 58.3,
# "ageSeconds": 23
# },
# ...
# }
# }
```
### Step 4: Test Telegram Trade
```
You: "long sol"
# Should see:
✅ Analytics check passed (68/100)
Data: tradingview_real (23s old) ← FRESH DATA!
Proceeding with LONG SOL...
```
---
## Troubleshooting
### Problem: Cache still empty after 10 minutes
**Check:**
1. TradingView alerts show "Active" status
2. Webhook URL is correct (check for typos)
3. Port 3001 is accessible (firewall rules)
4. Docker container is running: `docker ps | grep trading-bot`
5. Check logs: `docker logs -f trading-bot-v4`
### Problem: Alerts not firing
**Check:**
1. TradingView plan supports webhooks (Pro/Premium/Pro+)
2. Chart is open (alerts need chart loaded to fire)
3. Condition `ta.change(time("1"))` is correct
4. Timeframe matches (5-minute chart)
### Problem: JSON parse errors in logs
**Check:**
1. Alert message is valid JSON (no trailing commas)
2. TradingView placeholders use `{{ticker}}` not `{ticker}`
3. No special characters breaking JSON
---
## Alert Cost Optimization
**Current setup:** 3 alerts firing every 5 minutes = ~864 alerts/day
**TradingView Alert Limits:**
- Free: 1 alert
- Pro: 20 alerts
- Pro+: 100 alerts
- Premium: 400 alerts
**If you need to reduce alerts:**
1. Use 15-minute bars instead of 5-minute (reduces by 67%)
2. Only enable alerts for symbols you actively trade
3. Use same alert for multiple symbols (requires script modification)
---
## Advanced: n8n Proxy Setup (Optional)
If your bot is not publicly accessible, use n8n as webhook proxy:
**Step 1:** Create n8n webhook
- Webhook URL: `https://flow.egonetix.de/webhook/market-data`
- Method: POST
- Response: Return text
**Step 2:** Add HTTP Request node
- URL: `http://trading-bot-v4:3000/api/trading/market-data`
- Method: POST
- Body: `{{ $json }}`
- Headers: None needed (internal network)
**Step 3:** Use n8n URL in TradingView alerts
```
https://flow.egonetix.de/webhook/market-data
```
---
## Next: Enable Market Data Alerts
1. **Copy alert message JSON** from above
2. **Open TradingView** → SOL/USD 5-minute chart
3. **Click alert icon** (top right)
4. **Paste condition and message**
5. **Save alert**
6. **Repeat for ETH and BTC**
7. **Wait 5 minutes and verify cache**
**Once verified, proceed to SQL analysis!**