added RSI crossover detection in historical crossovers for timestamp

This commit is contained in:
DiTus
2026-03-02 09:09:20 +01:00
parent dd1cc521e9
commit 3ffec846dc

View File

@ -110,6 +110,10 @@ function calculateHistoricalCrossovers(activeIndicators, candles) {
let lastCrossoverTimestamp = null;
let lastSignalType = null;
// Get indicator-specific parameters
const overbought = indicator.params?.overbought || 70;
const oversold = indicator.params?.oversold || 30;
for (let i = candles.length - 1; i > 0; i--) {
const candle = candles[i]; // newer candle
const prevCandle = candles[i-1]; // older candle
@ -119,7 +123,28 @@ function calculateHistoricalCrossovers(activeIndicators, candles) {
if (!result || !prevResult) continue;
// Get MA value (handle both object and number formats)
// Handle different indicator types
if (indicatorType === 'rsi' || indicatorType === 'stoch') {
// RSI/Stochastic: check crossing overbought/oversold levels
const rsi = result.rsi !== undefined ? result.rsi : result;
const prevRsi = prevResult.rsi !== undefined ? prevResult.rsi : prevResult;
if (rsi === undefined || prevRsi === undefined) continue;
// SELL: crossed down through overbought (was above, now below)
if (prevRsi > overbought && rsi <= overbought) {
lastCrossoverTimestamp = candle.time;
lastSignalType = 'sell';
break;
}
// BUY: crossed up through oversold (was below, now above)
if (prevRsi < oversold && rsi >= oversold) {
lastCrossoverTimestamp = candle.time;
lastSignalType = 'buy';
break;
}
} else {
// MA-style: check price crossing MA
const ma = result.ma !== undefined ? result.ma : result;
const prevMa = prevResult.ma !== undefined ? prevResult.ma : prevResult;
@ -142,6 +167,7 @@ function calculateHistoricalCrossovers(activeIndicators, candles) {
break;
}
}
}
// Always update the timestamp based on current data
// If crossover found use that time, otherwise use last candle time
@ -153,8 +179,17 @@ function calculateHistoricalCrossovers(activeIndicators, candles) {
// 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];
if (indicatorType === 'rsi' || indicatorType === 'stoch') {
// RSI/Stochastic: use RSI level to determine signal
const rsi = lastResult?.rsi !== undefined ? lastResult.rsi : lastResult;
if (rsi !== undefined) {
indicator.lastSignalType = rsi > overbought ? 'sell' : (rsi < oversold ? 'buy' : null);
indicator.lastSignalTimestamp = lastCandleTime;
}
} else {
// MA-style: use price vs MA
const ma = lastResult?.ma !== undefined ? lastResult.ma : lastResult;
if (ma !== undefined) {
const isAbove = candles[candles.length - 1].close > ma;
@ -163,6 +198,7 @@ function calculateHistoricalCrossovers(activeIndicators, candles) {
}
}
}
}
});
}