IV rank

View as MarkdownOpen the editor

This rank sets today's option prices between the past year's cheapest and priciest.

It ranks implied volatility (how big a move option prices expect): high means traders expect big swings. A pane draws the rank, where 0 is the year's lowest implied volatility and 100 its highest. A high rank means expensive options and nervous traders, a low rank cheap options and a calm market. Check it before you buy or sell options, once there are 60 trading days of daily options data.

Under the hood: the recipe reads the 30-day at-the-money IV from option_daily with missing: "nan", so each session lands on one bar (Data sources). It ranks that IV over the window in the options kit's DayHistory (Options kit). It draws two step lines and two guides (Plotting). A render.hud card in the chart's default look sits beside them (HUD cards, The default look). It is also the iv-rank template, the IV Rank card under Beyond the time axis in the editor's starter list, and it compiles as written.

The wrun indicator

// IV Rank: where today's option prices sit between the past year's cheapest and priciest. It ranks implied volatility
// (how big a move option prices expect): high means traders expect big swings. A pane under the chart draws the rank: 0
// is the year's lowest implied volatility, 100 its highest. A dotted line beside it draws the percentile, the share of
// past days with a lower reading. A card at the bottom right names the verdict. A high rank means expensive options and
// nervous traders, a low rank cheap options and a calm market. Check it before you buy or sell options.
//
// Technical notes: the 30-day IV is the at-the-money IV at a constant 30-day maturity from the daily options
// statistics (option_daily). The IV rank is where the session's 30-day IV sits between the window's low (0) and high
// (100); the IV percentile is the share of the window's earlier sessions whose IV was lower. Both are step lines in a
// pane under the chart, with guides at the two bands, and the card reads the verdict, the IV, both readings, the
// window's low and high and the sessions behind them. Each session's reading is the statistics job's close of that
// session: it lands on its own bar on a daily chart and on the first bar of its day on an intraday chart, and the
// lines hold it until the next session's lands (the live session's row arrives after its close). The window is 252
// sessions, about a year, and the history is short until the data plane has kept the statistics that long: below
// Minimum history (60 sessions) the card says how many sessions there are and the lines stay empty, so a few days are
// never ranked as a year. Between the minimum and a full window the card names the sessions it read. A chart coarser
// than a day reads one line saying so.

param.int("lookback", 252, { min: 20, max: 520, label: "Window, sessions", description: "How many past trading days (sessions) today is ranked against. 252 is about a year." });
param.int("min_sessions", 60, { min: 10, max: 520, label: "Minimum history, sessions", description: "Trading days of data needed before a rank shows. Until then the card counts the days so far." });
param.number("high_band", 80, { min: 50, max: 100, step: 1, label: "High band", description: "A rank at or above this reads as high IV: options are expensive." });
param.number("low_band", 20, { min: 0, max: 50, step: 1, label: "Low band", description: "A rank at or below this reads as low IV: options are cheap." });
input("close", ohlcv.close); // the chart's own close: the grid
input("iv30", option_daily.atm_iv_30d, { missing: "nan", description: "The 30-day implied volatility (IV) of this symbol's options, in percent, one reading per trading day" }); // lands on the first bar of its day, NaN elsewhere
output("iv_rank", line, lower, { color: "theme.text", width: 2, step: true, label: "IV rank", format: "0.0", description: "Where today's 30-day IV sits between the window's lowest (0) and highest (100). Empty until Minimum history is met." });
output("iv_pct", line, lower, { color: "theme.text", opacity: 0.55, width: 1.5, step: true, line_style: "dotted", label: "IV percentile", format: "0.0", description: "The share of past days in the window with a lower 30-day IV, in percent. Empty until Minimum history is met." });
output("high_guide", line, lower, { role: "guide", color: "theme.muted", width: 1, label: "High band", align: "right", description: "The line where the rank turns high" });
output("low_guide", line, lower, { role: "guide", color: "theme.muted", width: 1, label: "Low band", align: "right", description: "The line where the rank turns low" });
output("band", none, lower, { description: "0 low (rank at or below Low band), 1 between, 2 high (at or above High band). It reads 1 while there is no rank." });
output("iv", none, lower, { description: "The newest 30-day IV, in percent" });
output("iv_low", none, lower, { description: "The lowest 30-day IV in the window, in percent" });
output("iv_high", none, lower, { description: "The highest 30-day IV in the window, in percent" });
output("sessions", none, lower, { description: "How many trading days of data the window holds" });
output("status", none, lower, { description: "0 not enough history yet, 1 the rank shows, 2 bars longer than a day. Empty before the first trading day." });
string("read", { max_bytes: 64 }); // the headline: the verdict, or why there is none
string("title", { max_bytes: 64 }); // the card's title: the session the readings are as of

// The card: the headline, then the IV and the two readings, and the window as rows.
render.hud("iv_rank_card", {
  position: "bottom_right",
  title: "{{title}}",
  columns: 2,
  tiles: [
    tile.pill("IV rank", "read", { headline: true }),
    tile.value("30-day IV", "iv", { format: "0.00" }),
    tile.value("Rank, 0 to 100", "iv_rank", { format: "0.0" }),
    tile.rows([
      ["IV percentile", "iv_pct", "0.0"],
      ["Lowest IV", "iv_low", "0.00"],
      ["Highest IV", "iv_high", "0.00"],
      ["Trading days read", "sessions", "int"],
    ]),
  ],
});

const DAY = 86400.0;
const CAPACITY = 520; // the most sessions the history keeps: the window's largest setting
const MONTHS: String[] = ["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"];
const history = new DayHistory(CAPACITY); // one 30-day IV per session
const dateClock = new Clock("UTC"); // reads a date from its day number

let lookback = 252; // settings, read in onStart()
let need = 60;
let highBand = 80.0;
let lowBand = 20.0;
let prevT: f64 = NaN; // the bar spacing: the smallest gap seen between two bars
let barSec: f64 = NaN;
let held = 0; // the readings, measured when a session lands and held until the next one
let ivNow: f64 = NaN;
let ivLow: f64 = NaN;
let ivHigh: f64 = NaN;
let rank: f64 = NaN;
let pct: f64 = NaN;

function onStart(): void {
  lookback = i32(p_lookback());
  need = i32(p_min_sessions());
  if (need > lookback) need = lookback; // the window can never hold more than itself
  highBand = p_high_band();
  lowBand = p_low_band();
}

// ── The readings, measured on the bar a session lands ──
function measure(): void {
  held = history.span(lookback);
  ivNow = history.value(0);
  ivLow = history.min(lookback);
  ivHigh = history.max(lookback);
  const ready = held >= need;
  rank = ready ? history.rank(lookback) : NaN;
  pct = ready ? history.percentile(lookback) : NaN;
}

function bandOf(r: f64): f64 { // 0 low, 1 between, 2 high; 1 with no rank
  if (isNaN(r)) return 1.0;
  return r >= highBand ? 2.0 : r <= lowBand ? 0.0 : 1.0;
}

function sbDay(day: f64): void { // "16OCT"
  dateClock.update(day * DAY);
  if (dateClock.dayOfMonth() < 10) sb_text("0");
  sb_int(dateClock.dayOfMonth());
  sb_text(MONTHS[dateClock.month() - 1]);
}

// ── The card's words, on the live bar ──
function writeWords(coarse: bool): void {
  sb_clear();
  if (coarse) sb_text("Needs a daily or finer chart");
  else if (history.count() == 0) sb_text("Waiting for the daily options statistics");
  else if (held < need) {
    sb_text("Not enough history yet: ");
    sb_int(held);
    sb_text(" of ");
    sb_int(need);
    sb_text(" sessions");
  } else if (isNaN(rank)) sb_text("No range: the IV has not moved in the window");
  else {
    const b = bandOf(rank);
    sb_text(b == 2.0 ? "High IV, options expensive: rank " : b == 0.0 ? "Low IV, options cheap: rank " : "Mid-range IV: rank ");
    sb_f64(rank, 0);
    sb_text(", percentile ");
    sb_f64(pct, 0);
  }
  str_read_sb();
  sb_clear();
  sb_text("IV rank, 30-day IV");
  if (!coarse && history.count() > 0) {
    sb_text(", as of ");
    sbDay(history.day(0));
    if (held >= need && held < lookback) {
      sb_text(", ");
      sb_int(held);
      sb_text(" sessions");
    }
  }
  str_title_sb();
}

// onBar() runs once per bar: the bar spacing, then the session's IV when it lands (measured once, held after); every
// bar writes the held readings, so an intraday chart draws each session as a step.
function onBar(): void {
  const t = bar.time();
  if (!isNaN(prevT) && t > prevT && (isNaN(barSec) || t - prevT < barSec)) barSec = t - prevT;
  prevT = t;
  const coarse = !isNaN(barSec) && barSec > 1.5 * DAY; // a weekly bar would read one session a week as a day
  const iv = in_iv30();
  if (!coarse && !isNaN(iv) && iv > 0.0 && history.push(Math.floor(t / DAY), iv)) measure();
  out_high_guide(highBand);
  out_low_guide(lowBand);
  const ready = !coarse && held >= need;
  out_iv_rank(ready ? rank : NaN);
  out_iv_pct(ready ? pct : NaN);
  out_band(ready ? bandOf(rank) : 1.0);
  out_iv(coarse ? NaN : ivNow);
  out_iv_low(coarse ? NaN : ivLow);
  out_iv_high(coarse ? NaN : ivHigh);
  out_sessions(coarse ? NaN : f64(held));
  out_status(coarse ? 2.0 : history.count() == 0 ? NaN : ready ? 1.0 : 0.0);
  if (bar.isLast()) writeWords(coarse);
}

How it works

One reading per session. The daily options statistics carry one row per session, the statistics job's reading at that session's close. The chart lands it on the session's own bar on a daily chart and on the first bar of its day on an intraday chart; every other bar reads NaN under missing: "nan". DayHistory.push(day, iv) keeps it under the bar's UTC day, so an intraday chart and a daily chart hold the same sessions, and a revised row for the same day replaces the reading instead of adding one.

The rank and the percentile. Over the newest lookback sessions (252 by default, about a year), rank() is (IV - low) / (high - low) x 100 and percentile() is the share of the earlier sessions strictly below today's, times 100. A window whose IV never moved has no range: the rank reads NaN and the card says so.

The lines hold. The readings are measured once, on the bar a session lands, and every later bar writes them again, so an intraday chart draws each session as a step until the next one lands. The live session's own row arrives after its close, so during the session the lines read the previous session.

What the settings do

  • Window, sessions: The number of past trading days today is ranked against: 252 is about a year.
  • Minimum history, sessions: A rank shows only after this many trading days of data, so a few days never pass for a year.
  • High band: A rank at or above this reads as high IV, which means options are expensive.
  • Low band: A rank at or below this reads as low IV, which means options are cheap.
  • IV rank: The verdict in words: high, mid-range or low IV, with the rank and the percentile.
  • 30-day IV: Today's implied volatility for the next 30 days, as a yearly percent.
  • Rank, 0 to 100: Where today's IV sits between the window's lowest IV (0) and its highest (100).
  • IV percentile: The share of past days with a lower IV, which reads truer than the rank after one big spike.
  • Lowest IV: The lowest 30-day IV in the window.
  • Highest IV: The highest 30-day IV in the window.
  • Trading days read: How many trading days of data the window holds.

Where it runs

It works on coins with listed options, on a daily or intraday chart. A copy you run from the editor (the code panel beside the chart) reads the same data. On a US stock chart, every indicator that runs in your browser reads it. The data is 15 minutes behind unless you have real-time US options data. On a CME (Chicago futures exchange) futures chart, every indicator that runs in your browser reads it too, once you accept the CME market data terms (Limitations). CME data is 10 minutes behind unless you have real-time CME data. A weekly chart would count one trading day a week as a day, so it shows a note instead. Crypto trades every day, so the default Window, sessions of 252 covers about eight months there. Set it to 365 for a calendar year.

When data is missing

The history starts the day OpenMarket switched on these daily readings, so it stays short for a year. Below Minimum history (60 trading days), the lines stay empty. The card reads "Not enough history yet: 12 of 60 sessions", so a few days never pass for a year. Between the minimum and a full window, the rank uses the days there are and the title counts them. Before the first trading day, the card reads "Waiting for the daily options statistics". The recipe skips a day with no IV reading and never counts it. A chart with bars longer than a day reads "Needs a daily or finer chart".

Customize it

  • A shorter or longer year. Set lookback to 126 for half a year or 504 for two.
  • Rank sooner or later. Lower Minimum history to read a rank over fewer sessions, knowing what it is; raise it to wait for a full window.
  • Other bands. Move High band and Low band to set where the guides sit and where the verdict reads high or low IV.

Run it

  1. In the editor's Explorer, press the Templates icon ("Browse starter templates") and pick IV Rank under Beyond the time axis.
  2. Press Run on a US stock's daily chart: the pane fills from the first session the statistics hold, and the card says how many there are.
  3. At the editor's Console prompt, type iv_rank, iv_pct and sessions to read the newest readings.

Concepts used