Options stats

View as MarkdownOpen the editor

A card at the bottom of the chart sums up today's options trading on this symbol.

Its headline says which side leads: calls (bets on a rise) or puts (bets on a fall). Below it sit contracts, premium (money paid for options) and open interest (contracts still open), each with a put/call ratio. A put/call ratio under 1 means calls lead, and over 1 means puts lead. Use it on a coin, US stock or CME (Chicago futures exchange) future for a quick read of today's mood.

Under the hood: it sums sixteen options_flow fields over today's bars. Each bar's flow counts over every expiry and over the same-day expiries alone (dte: "0dte", Data sources). It reads option_contract_state.cells on the live bar for the open interest through its present mask (Data sources, The present mask and partial bars). It takes today's date and the same-day expiry from optionsTradeDay (the options functions). It draws a render.hud card with no look word over data-only values and two text strings (HUD cards, The default look). This is also the options-stats template: the Options Stats card under HUDs in the editor's starter list, and it compiles as written.

The wrun indicator

// Options Stats: today's options trading on this symbol in one card, calls against puts. Calls are bets on a rise, and
// puts are bets on a fall. The card first says which side leads, as in "Calls lead: put/call 0.62". The put/call ratio
// is put contracts per call contract, so below 1 means calls lead. Then it shows today's call and put contracts and
// their ratio. Next come the premium (money paid for options) on each side and its ratio. Net premium is calls bought
// minus calls sold, minus puts bought minus puts sold. The bullish share is how much of today's bought or sold premium
// bet on a rise. Last come open interest (contracts still open) on each side, its ratio and its change since yesterday.
// Use it for a quick read of whether options traders lean up or down today.
//
// Under the hood: a card at the bottom of the pane says first which side leads (Calls lead: put/call 0.62), then
// today's call and put contracts and their ratio, the premium of each side and its ratio, the net premium (calls bought
// minus calls sold, minus puts bought minus puts sold, by premium at the ask and at the bid), the bullish share of the
// premium that hit a side of the quote (calls at the ask and puts at the bid), and the open interest of each side with
// its ratio and its change since the prior day. Contracts (coins on a crypto chain) and premium are the underlying's
// option flow of each of today's bars, summed: every expiry, the same-day expiries (0DTE) or the ones after today.
// Today is the trade date on the market's own clock. A bar with no flow row adds nothing. Open interest comes from the
// chain summary on the newest bar, over the twelve nearest expiries and the strikes within 25% of the underlying (a
// whole chain can hold more contracts than one read carries), its 0DTE on crypto the next 24 hours (dailies expire at
// 08:00 UTC). Trade counts are not served. It reads the chart's own underlying (on an option contract's chart, the
// contract's). Net premium and the bullish share count every print, cross trades and late prints too: the card says so.
param.choice("expiries", ["every expiry", "0DTE only", "after today"], "every expiry", { label: "Expiries", description: "Which expiry dates count. 0DTE only keeps options that expire today, or within 24 hours on crypto. After today leaves those out." });
input("close", ohlcv.close); // the chart's own candles: the grid the flow lines up on
// The underlying's option flow of each bar (the ticker scope), every expiry, then the same-day expiries alone.
input("call_volume", options_flow.call_contracts_total, { missing: "nan" });
input("put_volume", options_flow.put_contracts_total, { missing: "nan" });
input("call_premium", options_flow.call_prem_total, { missing: "nan" });
input("put_premium", options_flow.put_prem_total, { missing: "nan" });
input("call_ask", options_flow.call_prem_ask, { missing: "nan" });
input("call_bid", options_flow.call_prem_bid, { missing: "nan" });
input("put_ask", options_flow.put_prem_ask, { missing: "nan" });
input("put_bid", options_flow.put_prem_bid, { missing: "nan" });
input("call_volume_0dte", options_flow.call_contracts_total, { dte: "0dte", missing: "nan" });
input("put_volume_0dte", options_flow.put_contracts_total, { dte: "0dte", missing: "nan" });
input("call_premium_0dte", options_flow.call_prem_total, { dte: "0dte", missing: "nan" });
input("put_premium_0dte", options_flow.put_prem_total, { dte: "0dte", missing: "nan" });
input("call_ask_0dte", options_flow.call_prem_ask, { dte: "0dte", missing: "nan" });
input("call_bid_0dte", options_flow.call_prem_bid, { dte: "0dte", missing: "nan" });
input("put_ask_0dte", options_flow.put_prem_ask, { dte: "0dte", missing: "nan" });
input("put_bid_0dte", options_flow.put_prem_bid, { dte: "0dte", missing: "nan" });
input("state", option_contract_state.cells, { max_cells: 16000, expiries: "nearest:12", strike_window_pct: 25, description: "This symbol's options summary on the newest bar: each contract's open interest and yesterday's. It covers the twelve nearest expiry dates and strikes within 25% of the price." }); // [root_code, expiry_ms, side_cp, strike, bid, ask, last, prev_close, theta, volume_day, oi, oi_prev, present] per contract
market.zone(); // the market's clock: today's trade date and the same-day expiries
// The readings, data-only: today's sums through each bar; the open interest rows on the newest bar only.
output("call_volume_today", none, overlay, { format: "si", description: "Call contracts (bets on a rise) traded today up to this bar, counted in coins on crypto" });
output("put_volume_today", none, overlay, { format: "si", description: "Put contracts (bets on a fall) traded today up to this bar, counted in coins on crypto" });
output("pc_volume", none, overlay, { format: "0.00", description: "Put contracts divided by call contracts today. Below 1 means calls lead." });
output("call_premium_today", none, overlay, { format: "usd", description: "Premium (money paid for options) traded on calls today, in USD" });
output("put_premium_today", none, overlay, { format: "usd", description: "Premium (money paid for options) traded on puts today, in USD" });
output("pc_premium", none, overlay, { format: "0.00", description: "Put premium divided by call premium today. Below 1 means more money went to calls." });
output("net_premium", none, overlay, { format: "usd", description: "Calls bought at the ask minus calls sold at the bid, less the same for puts, in USD. Includes cross and late trades" });
output("bull_share", none, overlay, { format: "%", decimals: 1, description: "The share of bought or sold premium that bet on a rise: calls bought plus puts sold, in percent" });
output("empty_bars", none, overlay, { description: "Bars today, up to this one, that had no options data" });
output("call_oi", none, overlay, { format: "si", description: "Call open interest (contracts still open) over the twelve nearest expiry dates and strikes within 25%. Counted in coins on crypto. Newest bar only." });
output("put_oi", none, overlay, { format: "si", description: "Put open interest (contracts still open) over the twelve nearest expiry dates and strikes within 25%. Counted in coins on crypto. Newest bar only." });
output("pc_oi", none, overlay, { format: "0.00", description: "Put open interest divided by call open interest. Newest bar only." });
output("oi_change", none, overlay, { format: "si", signed: true, description: "Open interest (contracts still open) now minus yesterday's, calls and puts together. Newest bar only." });
output("tone", none, overlay, { description: "Which side leads: 0 puts, 1 even or no reading, 2 calls" });
string("read", { max_bytes: 48 }); // the headline: which side leads, or why there is no reading
string("title", { max_bytes: 48 }); // the card's title: the day and the expiries read

render.hud("stats", {
  position: "bottom_center",
  title: "{{title}}",
  columns: 2,
  tiles: [
    tile.pill("Who leads", "read", { headline: true }),
    tile.value("Call contracts", "call_volume_today", { format: "si" }),
    tile.value("Put contracts", "put_volume_today", { format: "si" }),
    tile.rows([
      ["Put/call, contracts", "pc_volume", "0.00"],
      ["Call premium", "call_premium_today", "usd"],
      ["Put premium", "put_premium_today", "usd"],
      ["Put/call, premium", "pc_premium", "0.00"],
    ]),
    tile.rows("Includes cross and late trades", [
      ["Net premium", "net_premium", "usd"],
      ["Bullish share", "bull_share", "%"],
    ]),
    tile.rows([
      ["Open calls, 12 expiries", "call_oi", "si"],
      ["Open puts, 12 expiries", "put_oi", "si"],
      ["Put/call, open interest", "pc_oi", "0.00"],
      ["Open interest change", "oi_change", "si"],
    ]),
  ],
});

const STATE = 13; // f64s per contract: OPTION_CONTRACT_STATE_TUPLE
const EVEN_BAND = 0.1; // a put/call ratio within 10% of 1 reads even
const MONTHS: StaticArray<string> = ["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"];
const SCOPE_WORDS: StaticArray<string> = ["", ", 0DTE", ", after today"];
const dateClock = new Clock("UTC"); // a trade date's day and month

let expiries = 0; // the setting, read in onStart()
let zone = 0; // the market's clock, read in onStart()
let day: f64 = NaN; // today's trade date on the market's clock, as a day number
let callVolume = 0.0; // today's sums
let putVolume = 0.0;
let callPremium = 0.0;
let putPremium = 0.0;
let callAsk = 0.0;
let callBid = 0.0;
let putAsk = 0.0;
let putBid = 0.0;
let emptyBars = 0;

function onStart(): void {
  expiries = i32(p_expiries());
  zone = i32(p_market_zone());
}

function ratio(top: f64, bottom: f64): f64 {
  return bottom > 0.0 ? top / bottom : NaN;
}

// A field under the expiries setting: every expiry, the same-day expiries, or every expiry minus them; a field the bar
// carries no row for counts 0.
function scoped(every: f64, sameDay: f64): f64 {
  const all = isNaN(every) ? 0.0 : every;
  const zero = isNaN(sameDay) ? 0.0 : sameDay;
  if (expiries == 1) return zero;
  if (expiries == 2) return all - zero > 0.0 ? all - zero : 0.0;
  return all;
}

// Whether a contract expiring at expiryMs counts: its same day is `today`, on crypto the next 24 hours from nowMs.
function counts(expiryMs: f64, today: f64, nowMs: f64): bool {
  if (expiries == 0) return true;
  const sameDay = zone == 0 ? expiryMs > nowMs && expiryMs - nowMs <= 86400000.0 : optionsTradeDay(expiryMs, zone) == today;
  return expiries == 1 ? sameDay : !sameDay;
}

// The open interest rows from the chain summary on the newest bar.
function writeOpenInterest(today: f64, nowMs: f64): void {
  const n = in_state_cells();
  let callOi = 0.0;
  let putOi = 0.0;
  let change = 0.0;
  let changeSeen = false;
  let seen = false;
  if (n > 0) {
    const cells = in_state_view();
    for (let i = 0; i + STATE - 1 < n; i += STATE) {
      const present = cells[i + 12];
      if (!(cells[i + 3] > 0.0) || !counts(cells[i + 1], today, nowMs) || !hasBit(present, 10)) continue; // bit 10: the open interest
      seen = true;
      if (cells[i + 2] > 0.0) callOi += cells[i + 10];
      else putOi += cells[i + 10];
      if (hasBit(present, 11)) { // bit 11: the prior day's
        change += cells[i + 10] - cells[i + 11];
        changeSeen = true;
      }
    }
  }
  out_call_oi(seen ? callOi : NaN);
  out_put_oi(seen ? putOi : NaN);
  out_pc_oi(seen ? ratio(putOi, callOi) : NaN);
  out_oi_change(changeSeen ? change : NaN);
}

// onBar() runs once per bar: a new trade date starts today's sums again; the bar's flow joins them; the readings on
// every bar, the open interest, the headline and the title on the newest bar.
function onBar(): void {
  const barMs = bar.time() * 1000.0;
  const today = optionsTradeDay(barMs, zone);
  if (today != day) {
    day = today;
    callVolume = 0.0;
    putVolume = 0.0;
    callPremium = 0.0;
    putPremium = 0.0;
    callAsk = 0.0;
    callBid = 0.0;
    putAsk = 0.0;
    putBid = 0.0;
    emptyBars = 0;
  }
  const calls = in_call_volume();
  const puts = in_put_volume();
  if (isNaN(calls) && isNaN(puts) && isNaN(in_call_volume_0dte()) && isNaN(in_put_volume_0dte())) emptyBars += 1;
  callVolume += scoped(calls, in_call_volume_0dte());
  putVolume += scoped(puts, in_put_volume_0dte());
  callPremium += scoped(in_call_premium(), in_call_premium_0dte());
  putPremium += scoped(in_put_premium(), in_put_premium_0dte());
  callAsk += scoped(in_call_ask(), in_call_ask_0dte());
  callBid += scoped(in_call_bid(), in_call_bid_0dte());
  putAsk += scoped(in_put_ask(), in_put_ask_0dte());
  putBid += scoped(in_put_bid(), in_put_bid_0dte());
  const pcVolume = ratio(putVolume, callVolume);
  const sidedPremium = callAsk + callBid + putAsk + putBid;
  out_call_volume_today(callVolume);
  out_put_volume_today(putVolume);
  out_pc_volume(pcVolume);
  out_call_premium_today(callPremium);
  out_put_premium_today(putPremium);
  out_pc_premium(ratio(putPremium, callPremium));
  out_net_premium(callAsk - callBid - (putAsk - putBid));
  out_bull_share(sidedPremium > 0.0 ? ((callAsk + putBid) / sidedPremium) * 100.0 : NaN);
  out_empty_bars(f64(emptyBars));
  if (!bar.isLast()) {
    out_call_oi(NaN);
    out_put_oi(NaN);
    out_pc_oi(NaN);
    out_oi_change(NaN);
    out_tone(NaN);
    return;
  }
  writeOpenInterest(today, barMs);
  const traded = callVolume + putVolume > 0.0;
  out_tone(!traded ? 1.0 : isNaN(pcVolume) ? 0.0 : Math.abs(pcVolume - 1.0) <= EVEN_BAND ? 1.0 : pcVolume < 1.0 ? 2.0 : 0.0); // no call traded: the puts lead
  sb_clear();
  if (!traded) sb_text("No contract traded yet today");
  else if (isNaN(pcVolume)) sb_text("Puts only: no call traded yet");
  else {
    sb_text(Math.abs(pcVolume - 1.0) <= EVEN_BAND ? "Even: put/call " : pcVolume < 1.0 ? "Calls lead: put/call " : "Puts lead: put/call ");
    sb_f64(pcVolume, 2);
  }
  str_read_sb();
  dateClock.update(today * 86400.0); // a day number is its date at 00:00 UTC
  sb_clear();
  sb_text("Options today, ");
  if (dateClock.dayOfMonth() < 10) sb_text("0");
  sb_int(dateClock.dayOfMonth());
  sb_text(MONTHS[dateClock.month() - 1]);
  sb_text(SCOPE_WORDS[expiries]);
  str_title_sb();
}

How it works

Today's sums. Each bar carries the underlying's option flow of that bar, split by calls and puts and by the side of the quote: the contracts, the premium, and the premium at the ask and at the bid, once over every expiry and once over the same-day expiries alone. Today is the trade date of the bar's open on the market's clock (market.zone(): the New York date on a US stock, the UTC day on crypto, the CME trade date that opens at 17:00 Chicago): on a new trade date every sum starts again, and each bar's flow joins them. A bar with no flow row (no print in it) adds nothing.

The expiries. Expiries reads every expiry, only the contracts expiring today (0DTE only: the same-day fields), or the ones after today (every expiry minus the same-day fields, never below zero). The open interest rows follow the same choice, the same-day expiry being the one whose trade date on the same clock is today's; on crypto, whose dailies expire at 08:00 UTC, it is the one expiring within 24 hours of the newest bar, so after 08:00 the morning's expired daily leaves 0DTE and tomorrow's joins it. The flow's same-day fields are the data plane's own 0DTE bucket.

The sides of the quote. The data plane judges each print against the national best bid and offer just before it, by the midpoint: above the mid it is at the ask, below it at the bid. On crypto and CME the venue names the taker's side instead: a print the taker bought is at the ask, one the taker sold at the bid, and none is at the mid. net_premium is the calls' premium at the ask minus at the bid, minus the puts' premium at the ask minus at the bid: calls bought and puts sold push it up. bull_share is the calls' premium at the ask plus the puts' at the bid, over all the premium at the ask or the bid, in percent; the mid and the unquoted prints count in neither.

The open interest. On the live bar the chain summary carries each contract's open interest (oi, bit 10) and the prior observation (oi_prev, bit 11), over the twelve nearest expiries and the strikes within 25% of the underlying (expiries: "nearest:12", strike_window_pct: 25, max_cells: 16000): a whole chain of a busy underlying holds more contracts than one read carries, so the rows say which expiries they cover. call_oi and put_oi sum the open interest of each side, pc_oi is their ratio, and oi_change sums the open interest now minus the prior observation over the contracts that carry both.

The headline. The put/call ratio of contracts decides it: within 10% of 1 it reads "Even: put/call 1.03", below "Calls lead: put/call 0.62", above "Puts lead: put/call 1.43", and with puts traded but no call yet "Puts only: no call traded yet". tone carries the same answer as a number for the Console: 0 while the puts lead, 1 when even or with no reading, 2 while the calls lead. The title names today's date and the expiries read (Options today, 16OCT, 0DTE).

The card reads the outputs. The headline pill over read, two tiles (call contracts and put contracts), then ten rows: the put/call ratio of contracts, call premium, put premium, the put/call premium ratio, the net premium, the bullish share, call open interest, put open interest, the put/call open interest ratio and the open interest change. Today's sums are outputs on every bar (call_volume_today, put_volume_today, pc_volume, call_premium_today, put_premium_today, pc_premium, net_premium, bull_share, and empty_bars, today's bars with no flow row), so the Console reads them through the day; the open interest rows are numbers on the live bar only.

Cross and late trades. The data plane sides every print by the quote, cross trades and late or out-of-sequence prints included, because the feed carries no sale conditions, so a source that leaves those prints unsided shows a different net premium. The package says so where its net premium shows: the line "Includes cross and late trades" over the net premium and bullish share rows, and the description of net_premium.

What the settings do

  • Expiries: Every expiry, only 0DTE (options that expire today), or only those after today.
  • Who leads: Which side leads today by the put/call ratio of contracts, or why there is no reading yet.
  • Call contracts: Call contracts traded today, counted in coins on crypto.
  • Put contracts: Put contracts traded today, counted in coins on crypto.
  • Put/call, contracts: Put contracts divided by call contracts, so below 1 means calls lead.
  • Call premium: Money paid for calls today, in USD.
  • Put premium: Money paid for puts today, in USD.
  • Put/call, premium: Put premium divided by call premium.
  • Net premium: Calls bought minus calls sold, less the same for puts, in USD.
  • Bullish share: The share of bought or sold premium that bet on a rise, in percent.
  • Open calls, 12 expiries: Call open interest (contracts still open) over the twelve nearest expiry dates.
  • Open puts, 12 expiries: Put open interest over the twelve nearest expiry dates.
  • Put/call, open interest: Put open interest divided by call open interest.
  • Open interest change: Open interest now minus yesterday's, calls and puts together.

Where it runs

On crypto, it reads the coin's options on one exchange, usually Deribit. An exchange can list a coin's options in two sets, such as Deribit's BTC and BTC_USDC. It reads one set, the one named like the coin. A copy you run from the editor (where you write and run indicators) reads them too. On a US stock or ETF chart with US listed options, every indicator in your browser can read this data. On a futures chart from CME, the Chicago exchange, every indicator in your browser can read it too, once you accept the CME market data terms (Limitations). US options data is 15 minutes behind unless you have real-time US options data. CME data is 10 minutes behind unless you have real-time CME data. The chart's legend says which you see. Alerts, watches and Protected indicators (published ones that run on OpenMarket's servers) cannot read this data. They stop with an error that names it. On an option contract's chart, it reads the contract's stock or coin, so the card matches that symbol's chart. More than 16,000 contracts in the open interest summary stops the run rather than cutting data.

When data is missing

  • No options data. A bar with no options data adds nothing to today's totals. In the Console (the editor's prompt that shows a reading by its name), empty_bars counts it. On a busy symbol, a run of such bars inside the session is a gap in the data. On a quiet one, it is a bar without a trade.
  • No trade yet. Before the first trade of the day, the headline reads "No contract traded yet today".
  • No open interest summary. Without it on the newest bar, the card's four open interest lines read -.

Customize it

  • The same-day crowd. Set Expiries to 0DTE only on an index ETF: the ratio and the net premium of the contracts expiring today.
  • The positioning that stays. Set Expiries to after today to leave the same-day churn out of the ratios and the open interest.
  • Another threshold. Change EVEN_BAND (0.1) to widen or narrow the ratios the headline calls even.

Run it

  1. In the editor's Explorer, press the Templates icon ("Browse starter templates") and pick Options Stats under HUDs.
  2. Press Run on a BTC chart or on a US stock chart such as SPY: the card sits at the bottom centre, titled by today's date. On a US stock or a CME future the copy reads the chain at your own tier, and the legend names the tier. On a CME future, accept the CME market data terms first.
  3. At the editor's Console prompt, type pc_volume and net_premium to read today's put/call ratio and net premium through the newest bar.

Concepts used