fix charts

This commit is contained in:
Dobromir Popov
2025-12-10 14:14:17 +02:00
parent bfaba556ea
commit 83bb688158
5 changed files with 4013 additions and 9 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -41,6 +41,9 @@ class ChartManager {
const date = new Date(timestamp); const date = new Date(timestamp);
return date.toISOString(); // Always returns UTC with Z suffix return date.toISOString(); // Always returns UTC with Z suffix
}; };
// Throttle pivot recalculation to prevent excessive API calls
this.pivotRecalcThrottle = {}; // {timeframe: lastCallTime}
console.log('ChartManager initialized with timeframes:', timeframes); console.log('ChartManager initialized with timeframes:', timeframes);
} }
@@ -2542,9 +2545,21 @@ class ChartManager {
async recalculatePivots(timeframe, data) { async recalculatePivots(timeframe, data) {
try { try {
// Don't recalculate if we don't have enough data // Don't recalculate if we don't have enough data
if (data.timestamps.length < 50) return; if (data.timestamps.length < 50) {
console.log(`[${timeframe}] Skipping pivot recalculation: only ${data.timestamps.length} candles (need 50+)`);
return;
}
console.log(` Recalculating pivots for ${timeframe} with ${data.timestamps.length} candles...`); // Throttle pivot recalculation (max once every 5 seconds per timeframe)
const now = Date.now();
const lastCall = this.pivotRecalcThrottle[timeframe] || 0;
if (now - lastCall < 5000) {
console.log(`[${timeframe}] Throttling pivot recalculation (last call ${Math.round((now - lastCall)/1000)}s ago)`);
return;
}
this.pivotRecalcThrottle[timeframe] = now;
console.log(`[${timeframe}] Recalculating pivots with ${data.timestamps.length} candles...`);
// Optimized: Only send symbol and timeframe, backend uses its own data // Optimized: Only send symbol and timeframe, backend uses its own data
const response = await fetch('/api/recalculate-pivots', { const response = await fetch('/api/recalculate-pivots', {
@@ -2562,14 +2577,23 @@ class ChartManager {
// Update pivot markers in chart data // Update pivot markers in chart data
const chart = this.charts[timeframe]; const chart = this.charts[timeframe];
if (chart && chart.data) { if (chart && chart.data) {
const oldPivotCount = chart.data.pivot_markers ? Object.keys(chart.data.pivot_markers).length : 0;
chart.data.pivot_markers = result.pivot_markers; chart.data.pivot_markers = result.pivot_markers;
console.log(` Pivots recalculated: ${Object.keys(result.pivot_markers).length} pivot candles`); const newPivotCount = Object.keys(result.pivot_markers).length;
console.log(`[${timeframe}] Pivots updated: ${oldPivotCount}${newPivotCount} pivot candles`);
// Redraw the chart with updated pivots // Redraw the chart with updated pivots
this.redrawChartWithPivots(timeframe, chart.data); this.redrawChartWithPivots(timeframe, chart.data);
// Ensure pivots are visible (in case they were toggled off)
if (this.displayToggles.pivots) {
console.log(`[${timeframe}] Ensuring pivots are visible after recalculation`);
}
} else {
console.warn(`[${timeframe}] Chart not found for pivot update`);
} }
} else { } else {
console.warn('Failed to recalculate pivots:', result.error); console.warn(`[${timeframe}] Failed to recalculate pivots:`, result.error || 'Unknown error');
} }
} catch (error) { } catch (error) {
@@ -2582,7 +2606,12 @@ class ChartManager {
*/ */
redrawChartWithPivots(timeframe, data) { redrawChartWithPivots(timeframe, data) {
const chart = this.charts[timeframe]; const chart = this.charts[timeframe];
if (!chart) return; if (!chart) {
console.warn(`[${timeframe}] Cannot redraw pivots: chart not found`);
return;
}
console.log(`[${timeframe}] Redrawing chart with pivots...`);
// Build pivot shapes and annotations // Build pivot shapes and annotations
const shapes = []; const shapes = [];
@@ -4365,3 +4394,34 @@ class ChartManager {
this.liveMetricsOverlay = null; this.liveMetricsOverlay = null;
} }
} }
// Debug method to manually trigger pivot recalculation
debugRecalculatePivots(timeframe) {
console.log(`[DEBUG] Manually triggering pivot recalculation for ${timeframe}`);
const chart = this.charts[timeframe];
if (chart && chart.data) {
// Bypass throttling for debug
delete this.pivotRecalcThrottle[timeframe];
this.recalculatePivots(timeframe, chart.data);
} else {
console.log(`[DEBUG] No chart data available for ${timeframe}`);
}
}
// Debug method to check pivot status
debugPivotStatus() {
console.log('=== PIVOT DEBUG STATUS ===');
console.log('Display toggles:', this.displayToggles);
console.log('Pivot recalc throttle:', this.pivotRecalcThrottle);
this.timeframes.forEach(tf => {
const chart = this.charts[tf];
if (chart && chart.data) {
const pivotCount = chart.data.pivot_markers ? Object.keys(chart.data.pivot_markers).length : 0;
console.log(`${tf}: ${chart.data.timestamps.length} candles, ${pivotCount} pivot markers`);
} else {
console.log(`${tf}: No chart data`);
}
});
console.log('========================');
}

View File

@@ -74,8 +74,8 @@
<div class="mb-2"> <div class="mb-2">
<label for="primary-timeframe-select" class="form-label small text-muted">Primary Timeframe</label> <label for="primary-timeframe-select" class="form-label small text-muted">Primary Timeframe</label>
<select class="form-select form-select-sm" id="primary-timeframe-select"> <select class="form-select form-select-sm" id="primary-timeframe-select">
<option value="1s">1 Second</option> <option value="1s" selected>1 Second</option>
<option value="1m" selected>1 Minute</option> <option value="1m">1 Minute</option>
<option value="1h">1 Hour</option> <option value="1h">1 Hour</option>
<option value="1d">1 Day</option> <option value="1d">1 Day</option>
</select> </select>

View File

@@ -0,0 +1,145 @@
# Pivot Points Debugging Improvements
## Issue
Live charts are updating correctly, but pivot points stop being drawn after the first 2 pivot points are detected.
## Debugging Improvements Added
### 1. Enhanced Logging ✅
**File**: `ANNOTATE/web/static/js/chart_manager.js`
**Added detailed console logging:**
- Pivot recalculation attempts with candle counts
- Throttling messages when calls are too frequent
- Before/after pivot marker counts
- Chart redraw status messages
- Warnings when charts or data are missing
### 2. Pivot Recalculation Throttling ✅
**Problem**: Pivot recalculation might be called too frequently with live updates
**Solution**: Added 5-second throttling per timeframe
```javascript
// Throttle pivot recalculation (max once every 5 seconds per timeframe)
const now = Date.now();
const lastCall = this.pivotRecalcThrottle[timeframe] || 0;
if (now - lastCall < 5000) {
console.log(`[${timeframe}] Throttling pivot recalculation`);
return;
}
```
### 3. Debug Methods ✅
**Added manual debugging methods:**
```javascript
// Debug method to manually trigger pivot recalculation
debugRecalculatePivots(timeframe)
// Debug method to check pivot status
debugPivotStatus()
```
**Usage in browser console:**
```javascript
// Check current pivot status
window.appState.chartManager.debugPivotStatus();
// Manually trigger pivot recalculation for 1m chart
window.appState.chartManager.debugRecalculatePivots('1m');
```
### 4. API Test Script ✅
**File**: `test_pivot_api.py`
**Tests the backend pivot recalculation API:**
- Calls `/api/recalculate-pivots` directly
- Shows number of pivot markers returned
- Displays sample pivot data
- Verifies API is working correctly
## Potential Root Causes
### 1. **Throttling Too Aggressive**
- 5-second throttling might prevent updates when new pivots form
- **Check**: Look for throttling messages in console
### 2. **Backend API Issues**
- Data provider might not have enough data
- Williams Market Structure calculation might be failing
- **Check**: Run `python test_pivot_api.py` to test API directly
### 3. **Chart Redraw Issues**
- Pivot shapes/annotations might not be updating correctly
- Display toggles might be hiding pivots
- **Check**: Look for redraw messages and toggle status
### 4. **Data Synchronization**
- Live data updates might not trigger pivot recalculation
- New candles might not be detected properly
- **Check**: Look for "NEW candles" vs "updated candles" messages
## Debugging Steps
### 1. Check Console Logs
Look for these messages in browser console:
```
[1m] Recalculating pivots with 150 candles...
[1m] Pivots updated: 5 → 7 pivot candles
[1m] Redrawing chart with pivots...
```
### 2. Check Throttling
If you see throttling messages, pivots might be blocked:
```
[1m] Throttling pivot recalculation (last call 3s ago)
```
### 3. Manual Debug
Use debug methods in browser console:
```javascript
// Check status
window.appState.chartManager.debugPivotStatus();
// Force recalculation
window.appState.chartManager.debugRecalculatePivots('1m');
```
### 4. Test Backend API
Run the test script:
```bash
python test_pivot_api.py
```
### 5. Check Display Toggles
Verify pivots are enabled:
```javascript
console.log(window.appState.chartManager.displayToggles.pivots); // Should be true
```
## Expected Behavior After Fix
### Live Pivot Updates Should:
1.**Detect New Candles** - Log when new candles are added
2.**Trigger Recalculation** - Call pivot API when needed (but throttled)
3.**Update Markers** - Show new pivot count in logs
4.**Redraw Charts** - Update visual pivot lines and dots
5.**Respect Toggles** - Only show pivots if enabled
### Console Should Show:
```
[1m] Chart update: 0 updated, 1 new candles, total: 151
[1m] Recalculating pivots with 151 candles...
[1m] Pivots updated: 5 → 6 pivot candles
[1m] Redrawing chart with pivots...
```
## Next Steps
1. **Monitor Console** - Check for pivot recalculation messages
2. **Test API** - Verify backend pivot calculation works
3. **Adjust Throttling** - Reduce throttle time if needed
4. **Check Data Flow** - Ensure new candles trigger recalculation
5. **Verify Display** - Confirm pivots are visible after redraw
The debugging improvements should help identify exactly where the pivot update process is failing! 🔍

60
test_pivot_api.py Normal file
View File

@@ -0,0 +1,60 @@
#!/usr/bin/env python3
"""
Test script to verify pivot recalculation API works
"""
import requests
import json
def test_pivot_api():
"""Test the pivot recalculation API"""
print("Testing pivot recalculation API...")
print("-" * 50)
url = "http://localhost:8051/api/recalculate-pivots"
payload = {
"symbol": "ETH/USDT",
"timeframe": "1m"
}
try:
print(f"Calling: {url}")
print(f"Payload: {json.dumps(payload, indent=2)}")
response = requests.post(url, json=payload, timeout=10)
print(f"Status: {response.status_code}")
if response.status_code == 200:
data = response.json()
print(f"Success: {data.get('success')}")
if data.get('success'):
pivot_markers = data.get('pivot_markers', {})
print(f"Pivot markers: {len(pivot_markers)} timestamps")
# Show first few pivot markers
for i, (timestamp, pivots) in enumerate(list(pivot_markers.items())[:3]):
highs = len(pivots.get('highs', []))
lows = len(pivots.get('lows', []))
print(f" {timestamp}: {highs} highs, {lows} lows")
if len(pivot_markers) > 3:
print(f" ... and {len(pivot_markers) - 3} more")
print("✅ Pivot API working correctly!")
else:
print(f"❌ API returned error: {data.get('error')}")
else:
print(f"❌ HTTP Error: {response.status_code}")
print(f"Response: {response.text}")
except requests.exceptions.RequestException as e:
print(f"❌ Request failed: {e}")
except Exception as e:
print(f"❌ Error: {e}")
if __name__ == "__main__":
test_pivot_api()