QuantScript — vixy-regime-gating-xauusd.qs
// VIXY Regime Gating — XAUUSD Gold Strategy
//
// Demonstrates multi-dataset loading with cross-symbol, cross-calendar data:
//   - XAUUSD 15m  primary chart (gold trading)
//   - XAUUSD 1h   higher-timeframe trend via object callback ({ sma, mom })
//   - VIXY 1D     volatility regime as a raw feed with fill_limit: 3 + .age gate
//
// VIXY is the ProShares VIX Short-Term Futures ETF (TwelveData feed) — a
// tradable proxy for the VIX volatility index. Because futures-roll decay
// drifts its price level over time, the calm threshold below is illustrative,
// not a stable long-term regime boundary.
//
// The VIXY freshness gate ensures that a stale volatility reading can never
// silently drive entries — if the VIXY feed goes stale (age becomes null),
// the strategy stops opening new positions until a fresh reading arrives.
//
// This is the classic multi-dataset use case: same-symbol higher-timeframe
// for trend, cross-symbol cross-calendar data for regime filtering.

mode({ type: "forwardtest", script_name: "VIXY Regime Gating XAUUSD" });
candles({ symbol: "XAUUSD", resolution: "15m", bars: 500, adapter: "demotrader.net" });

broker({ adapter: "sim", config: { initial_capital: 10000 } });
risk({ max_position_pct: 20, daily_loss_limit: 3 });

// ── Inputs ───────────────────────────────────────────────────────────────────
const smaLen     = input.int(20, "Primary SMA Length", 5, 100, null, null, null, "Entry");
const htfSmaLen  = input.int(20, "HTF SMA Length", 5, 100, null, null, null, "Trend");
const vixyMax    = input.float(25.0, "VIXY Calm Threshold", null, null, null, null, null, "Regime");
const lotSize    = input.float(0.1, "Lot Size", 0.01, null, 0.01, null, null, "Trade");

// ── Dataset 2: XAUUSD 1h trend (object callback → two aligned columns) ──────
// sma: trend level on native 1h bars; mom: 1h close-over-close momentum.
// Both columns are aligned to the 15m timeline with closed-bar visibility.
var htf = data.loadCandles({ adapter: "demotrader.net", symbol: "XAUUSD", resolution_type: "1h", callback: (c) => ({
  sma: ta.sma(c.close, htfSmaLen),
  mom: c.close - c.close[1]
}) });

// ── Dataset 3: VIXY 1D regime (raw feed with a staleness cap) ───────────────
// fill_limit: 3 means a daily VIXY value is carried at most 3 primary bars past
// its arrival bar, then becomes null — a stale volatility reading can never
// silently drive entries. vixy.age is 0 on the arrival bar, increments while
// carried, and is null when invisible.
var vixy = data.loadCandles({ adapter: "twelvedata", symbol: "VIXY", resolution_type: "1D", fill_limit: 3 });

// ── Strategy ─────────────────────────────────────────────────────────────────
event.onCandle((candle) => {
  const close = candle.close;
  const sma = ta.sma(close, smaLen);

  // ── Section 1: signals (named booleans over series) ────────────────────
  // HTF trend: 1h SMA rising and price above it.
  const trendUp = htf.sma !== null && htf.mom !== null && htf.mom > 0 && close > htf.sma;

  // Regime: VIXY reading visible AND within the 3-bar staleness cap AND calm.
  const vixyFresh = vixy.age !== null;
  const calmVixy = vixyFresh && vixy.close < vixyMax;

  // Entry/exit edges via ta.* — true only on the crossing bar.
  const longEntry = ta.crossover(close, sma) && trendUp && calmVixy;
  const longExit = ta.crossunder(close, sma) || !trendUp;

  // ── Plot ────────────────────────────────────────────────────────────────
  chart.plot(close, "XAU Close");
  chart.plot(htf.sma, "XAU 1h SMA", "#FF980E");
  chart.plot(sma, "15m SMA", "#58A6FF");
  chart.plot(longEntry ? candle.low * 0.998 : null, "Long Entry", "#3fb950");

  // ── Section 2: execution (mechanical signal → order mapping) ───────────
  if (!candle.isLive) return;

  // Fixed position-check boilerplate: find our long position, if any.
  const symbol = "XAUUSD";
  let longPosition = null;
  for (const pos of strategy.positions()) {
    if (pos.symbol === symbol && pos.side === "buy") longPosition = pos;
  }

  if (longPosition !== null && longExit) {
    trade.closePosition({
      positionId: longPosition.id,
      reason: "15m cross under SMA or 1h trend flipped",
      tags: ["vixy-gating", "exit"]
    });
    console.log("[Exit] long closed — exit signal at " + close);
  }

  if (longPosition === null && longEntry) {
    trade.buy({
      symbol, size: { lots: lotSize },
      reason: "SMA crossover, 1h trend up, VIXY calm (vixy.age=" + data.age(vixy) + ", vixy=" + vixy.close + ")",
      tags: ["vixy-gating", "entry"]
    });
    console.log("[Entry] long opened — entry signal at " + close);
  }
});

← Back to all examples