QuantScript — multi-tf-indicator.qs
// 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({ mode: "indicator", script_name: "Multi-Timeframe Indicator" });
candles({ symbol: "BTCUSD", resolution: "1h", bars: 200, adapter: "demotrader.net" });

// ── Inputs ───────────────────────────────────────────────────────────────────
const smaLen = input.int(50, "ETH 4h SMA Length", 10, 200, null, null, null, "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({ adapter: "demotrader.net", symbol: "ETHUSD", resolution: "4h", callback: (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 (!isNaN(prevSma)) {
    chart.plot(prevSma, "ETH 4h SMA [1]", "#9E9E9E", { style: "dashed" });
  }
});

← Back to all examples