This post is the complete technical reference for AI Smart Market Analyzer — every component in the pipeline, from data fetch to final signal, in one place.
Stage 1: Data Fetch
fetchLiveCandles(asset, tf, apiKey, polygonKey) is called with the selected asset and timeframe. Priority order:
- Polygon.io (if key provided and asset is Forex/Crypto/OTC)
- Twelve Data (if key provided, for Forex/Commodities)
- Binance (for Crypto, always available without a key)
Returns: Array<{open, high, low, close, volume}> in chronological order (oldest first).
Stage 2: Candle Grouping
If the selected timeframe is 2m (using 1m data) or 10m (using 5m data), groupCandles(candles, 2) merges consecutive pairs into proper OHLCV candles with true high/low and summed volume. The analysis always operates on the correctly grouped candle array.
Stage 3: Indicator Calculation
On a closes array (the close prices of all candles):
calculateEMAArray(closes, 20/50/200)→ EMA arrays at three periodscalculateRSIArray(closes, 14)→ Wilder's RSI arraycalculateStochRSI(rsiArray, 14)→ current Stochastic RSI valuecalculateEMAArray(macdLine, 9)→ signal line (where macdLine = EMA12 - EMA26)- Bollinger Bands: 20-period SMA ± 2 standard deviations on
closes.slice(-20) calculateADX(candles, 14)→ ADX via True Range and Directional Movement smoothingcalculateATR(candles, 14)→ Average True Range for volatility measurement
Stage 4: Pattern Recognition
Compares the last two candles for five patterns: Bullish Engulfing, Bearish Engulfing, Hammer, Shooting Star, Doji. Assigns pb (+1, -1, or 0) for use in the signal score.
Stage 5: MTF Macro Trend
If macroCandles (4H data) are provided and have ≥ 20 candles, calculates EMA 50 on the macro dataset. Sets macroTrend = "BULLISH" | "BEARISH" | "NEUTRAL".
Stage 6: Signal Scoring
Nine dimensions each score +1 / -1 / 0. Total determines direction (threshold: ±2). Confidence mapped from total magnitude, clamped to 55–97%.
Stage 7: MTF Confluence Override
If macro trend opposes the signal: override to NO TRADE, confidence → 45. If aligned: confidence += 5, clamped at 98.
Stage 8: Dynamic Reasoning Generation
Based on final signal direction, one of three reasoning pools (bullR / bearR / neutralR) is populated. Each pool is an array of {icon, text} objects with dynamically interpolated indicator values — exact RSI numbers, EMA relationships, MACD crossover state, support/resistance levels, Stochastic RSI readings, and pattern names. The reasoning is assembled fresh on every analysis run.
Stage 9: AI Overlay
The animated processing overlay fires after Stage 8. If the user has live API keys configured in Firebase Firestore, Gemini or Claude are called with a structured prompt containing the indicator summary. The AI commentary is appended to the reasoning panel. Without keys, the overlay shows the animation only.
Output
A single analysis object:
{
signal: "BUY" | "SELL" | "NO TRADE",
conf: Number, // 40–98
rsi: Number,
stochRSI: Number,
macdHist: Number,
adxVal: Number,
atr: Number,
ema20/50/200: Number,
bbUpper/Lower/Mean: Number,
support: Number,
resistance: Number,
pattern: String,
macroTrend: "BULLISH" | "BEARISH" | "NEUTRAL",
bullReasons: Array<{icon, text}>,
bearReasons: Array<{icon, text}>,
neutralReasons: Array<{icon, text}>,
}This object drives the entire UI: the signal badge, confidence bar, indicator table, reasoning list, and AI overlay.
