fixed: always update timestamp from current data, not stale cache

This commit is contained in:
DiTus
2026-03-01 22:25:59 +01:00
parent af79268621
commit 28abdb15a2

View File

@ -107,6 +107,7 @@ function calculateHistoricalCrossovers(activeIndicators, candles) {
// Find the most recent crossover by going backwards from the newest candle // Find the most recent crossover by going backwards from the newest candle
// candles are sorted oldest first, newest last // candles are sorted oldest first, newest last
let lastCrossoverTimestamp = null; let lastCrossoverTimestamp = null;
let lastSignalType = null;
for (let i = candles.length - 1; i > 0; i--) { for (let i = candles.length - 1; i > 0; i--) {
const candle = candles[i]; // newer candle const candle = candles[i]; // newer candle
@ -130,18 +131,36 @@ function calculateHistoricalCrossovers(activeIndicators, candles) {
// SELL signal: price crossed from above to below MA // SELL signal: price crossed from above to below MA
if (priceAbovePrev && !priceAboveNow) { if (priceAbovePrev && !priceAboveNow) {
lastCrossoverTimestamp = candle.time; lastCrossoverTimestamp = candle.time;
lastSignalType = 'sell';
break; break;
} }
// BUY signal: price crossed from below to above MA // BUY signal: price crossed from below to above MA
if (!priceAbovePrev && priceAboveNow) { if (!priceAbovePrev && priceAboveNow) {
lastCrossoverTimestamp = candle.time; lastCrossoverTimestamp = candle.time;
lastSignalType = 'buy';
break; break;
} }
} }
// Always update the timestamp based on current data
// If crossover found use that time, otherwise use last candle time
if (lastCrossoverTimestamp) { if (lastCrossoverTimestamp) {
console.log(`[HistoricalCross] ${indicatorType}: Found crossover at ${new Date(lastCrossoverTimestamp * 1000).toLocaleString()}`); console.log(`[HistoricalCross] ${indicatorType}: Found ${lastSignalType} crossover at ${new Date(lastCrossoverTimestamp * 1000).toLocaleString()}`);
indicator.lastSignalTimestamp = lastCrossoverTimestamp; indicator.lastSignalTimestamp = lastCrossoverTimestamp;
indicator.lastSignalType = lastSignalType;
} else {
// No crossover found - use last candle time
const lastCandleTime = candles[candles.length - 1]?.time;
if (lastCandleTime) {
// Determine current signal direction
const lastResult = results[results.length - 1];
const ma = lastResult?.ma !== undefined ? lastResult.ma : lastResult;
if (ma !== undefined) {
const isAbove = candles[candles.length - 1].close > ma;
indicator.lastSignalType = isAbove ? 'buy' : 'sell';
indicator.lastSignalTimestamp = lastCandleTime;
}
}
} }
}); });
} }