QuantScript multi-timeframe indicator: BTCUSD 1h chart with ETH 4h SMA overlay computed on native bars using data.loadCandles() with a callback.
// Multi-Timeframe Indicator — BTCUSD 1h with ETH 4h SMA Overlay
//
// Demonstrates data.loadCandles() with a callback to evaluate an indicator
// on a secondary timeframe's native bars. The ETH 4h SMA(50) is computed on
// actual 4h bars (not forward-filled 1h bars), giving accurate cross-timeframe
// analysis — just like Pine Script's request.security().
mode("indicator", "Multi-Timeframe Indicator");
candles("BTCUSD", timeframe: "1h", bars: 200, provider: "demotrader");
// ── Inputs ───────────────────────────────────────────────────────────────────
const smaLen = input.int(50, "ETH 4h SMA Length", minval: 10, maxval: 200, group: "Secondary");
// ── Secondary timeframe: ETH 4h SMA computed on native 4h bars ──────────────
// The callback receives raw 4h candles. ta.sma() runs over 50 real 4h bars.
// The result is forward-fill-aligned to the 1h primary timeline.
var eth4hSma = data.loadCandles("demotrader", "ETHUSD", "4h", (c) => ta.sma(c.close, smaLen));
// ── Plot ─────────────────────────────────────────────────────────────────────
event.onCandle((candle) => {
// Primary: BTC 1h close
chart.plot(candle.close, "BTC Close");
// Secondary: ETH 4h SMA overlaid (forward-filled to 1h)
chart.plot(eth4hSma, "ETH 4h SMA", "#FF9800");
// Historical lookback: previous bar's ETH 4h SMA value
const prevSma = eth4hSma[1];
if (prevSma !== null) {
chart.plot(prevSma, "ETH 4h SMA [1]", "#9E9E9E", { style: "dashed" });
}
});