Premium by expiry

View as MarkdownOpen the editor

Bars under the chart show today's options money by expiry (the day the option ends), one bar per expiry.

A bar above zero means premium (money paid for options) leaned to a rise, below zero to a fall. Each bar is labelled with its date and days to go. Tall bars on near dates point to short-term bets, tall bars on far dates to longer views. Use it to see when traders expect a move, on coins, CME (Chicago futures exchange) futures or US stocks.

Under the hood: it reads option_strike_flow.cells grouped by expiry (group: "expiry"). It holds one entry per expiry and option type that traded in each bar, with its present mask last (Data sources). It takes each expiry's days to go from optionsTradeDay (the options functions) and its label from fbExpiryLabel (Panel frames). It draws a panel.bars below the chart with a caption written on each run (Cards, frames and panels). This is also the premium-by-expiry template: the Premium by Expiry card under Beyond the time axis in the editor's starter list. It compiles as written.

The wrun indicator

// Premium by Expiry: which expiry dates today's options money went into, as one bar per expiry under the chart. An
// expiry is the date an option ends. Premium is the money paid for options. Calls (bets on a rise) bought at the ask
// (buyers paying the seller's price) count as bullish. Puts (bets on a fall) sold at the bid (sellers taking the
// buyer's price) count as bullish too. Puts bought at the ask and calls sold at the bid count as bearish. Net, the
// default view, shows bullish minus bearish premium for each expiry. A bar above zero, in the up colour, means that
// expiry's money leaned to a rise. A bar below zero, in the down colour, means it leaned to a fall. Sides shows the two
// apart, bullish up and bearish down. Each bar is labelled with its date and its days to go. Tall bars on near dates
// point to short-term bets, and tall bars on far dates to longer views. Use it to see when traders expect the move.
//
// Under the hood: each contract that traded adds its premium by side, calls bought at the ask and puts sold at the bid
// as bullish premium, puts bought at the ask and calls sold at the bid as bearish. Net (the default) draws bullish
// minus bearish per expiry, in the up colour above zero and the down colour below; Sides draws the two apart, bullish
// up and bearish down. The sums run from the session's first bar (each trade date starts again), over the nearest
// expiries that traded, labelled by date and days to go. Premium at the mid or with no quote has no side and counts in
// neither. The cells come from option_strike_flow, the data plane's sums of each bar's flow per expiry, calls apart
// from puts, added bar by bar: a bar served in part (the partial bit, 14) is a floor, and the note says so; a row that
// is not whole (no expiry, a side other than call or put) is skipped and counted. 1-minute cells reach back 7 days and
// 5-minute cells 30 days. Every print takes a side, cross trades and late prints too: the panel's caption says so.

section("Expiries");
param.choice("view", ["net", "sides"], "net", { label: "View", description: "The net view shows bullish minus bearish premium (money paid for options) for each expiry date. The sides view shows bullish up and bearish down." });
param.int("expiries", 12, { min: 1, max: 40, label: "Expiries shown", description: "How many expiry dates to show, nearest first, among those that traded today" });
input("close", ohlcv.close); // the chart's own candles: the bar grid
input("trade_date", time.trade_date); // the bar's trade date, the New York date on a US stock: the sums start again on each
input("flow", option_strike_flow.cells, { max_cells: 256, group: "expiry", description: "Each bar's flow per expiry, calls apart from puts: [expiry_ms, strike, side_cp, dte_class, vol_ask, vol_bid, vol_mid, vol_none, vol_above_ask, vol_below_bid, prem_ask, prem_bid, prem_mid, prem_none, present]" }); // every expiry of the chart's underlying
market.zone(); // the market's clock: each expiry's date and days to go
// The readings, data-only: the Console reads them.
output("session_net", none, overlay, { description: "Today's bullish minus bearish premium (money paid for options) over every expiry date, in USD. Includes cross and late trades" });
output("top_days", none, overlay, { description: "Days left until the expiry date holding today's largest net premium, up or down" });
output("top_net", none, overlay, { description: "That expiry date's net premium (money paid for options) today, in USD. Includes cross and late trades" });
output("partial_bars", none, overlay, { description: "Bars today whose options data arrived only in part. While this is above 0, the totals are a minimum." });
output("skipped", none, overlay, { description: "Entries today left out because they were incomplete" });
string("note", { max_bytes: 72 }); // the one line while there is nothing to draw, or the sums are a floor
const net_rows = frame("net_rows", { max_bytes: 8192 });
const side_rows = frame("side_rows", { max_bytes: 12288 });
panel.bars({ name: "net_by_expiry", title: "Net premium by expiry", x: "category", place: "below", frame: net_rows, orientation: "vertical", color_mode: "sign", positive_color: "theme.up", negative_color: "theme.down", series: [{ name: "Net" }], format: "usd", hover_card: true, legend_style: "title", height_frac: 0.2 });
panel.bars({ name: "sides_by_expiry", title: "Bullish and bearish premium by expiry", x: "category", place: "below", frame: side_rows, orientation: "vertical", color_mode: "series", series: [{ name: "Bullish", color: "theme.up" }, { name: "Bearish", color: "theme.down" }], format: "usd", hover_card: true, legend_style: "title", height_frac: 0.2 });
render.label("note_label", { position: "top_center", text: "note", color: "theme.muted", style: "plain", offset: [0, 8] });

const TUPLE = 15; // f64s per row: OPTION_STRIKE_FLOW_TUPLE
const MAX_EXPIRIES = 96; // expiries kept a session, the nearest when more trade
const expiryAt = new StaticArray<f64>(MAX_EXPIRIES); // the session's expiries, ascending
const bullAt = new StaticArray<f64>(MAX_EXPIRIES);
const bearAt = new StaticArray<f64>(MAX_EXPIRIES);

let sides = false; // settings, read in onStart()
let shown = 12;
let zone = 0; // the market's calendar, read in onStart(): an expiry's date and days to go
let day: f64 = NaN; // the trade date the sums run over
let expiryCount = 0;
let partialBars = 0;
let skipped = 0;
let runCells = 0; // rows read over the whole run

function onStart(): void {
  sides = p_view() == 1.0;
  shown = i32(p_expiries());
  zone = i32(p_market_zone());
}

// A row counts when it names an expiry and a call or a put and its present mask is a whole number from 0 up (the
// all-zero row of a bar served in part with no flow does not).
function rowOk(cells: StaticArray<f64>, i: i32): bool {
  const present = cells[i + 14];
  const side = cells[i + 2];
  return cells[i] > 0.0 && (side == 1.0 || side == -1.0) && present >= 0.0 && present == Math.floor(present);
}

// The row of an expiry, inserted when new; -1 when the table is full and the expiry is later than every kept one.
function expirySlot(ms: f64): i32 {
  let lo = 0;
  let hi = expiryCount;
  while (lo < hi) {
    const mid = (lo + hi) >> 1;
    if (expiryAt[mid] < ms) lo = mid + 1;
    else hi = mid;
  }
  if (lo < expiryCount && expiryAt[lo] == ms) return lo;
  if (expiryCount >= MAX_EXPIRIES) {
    if (lo >= expiryCount) return -1;
    expiryCount -= 1; // the latest makes room
  }
  for (let k = expiryCount; k > lo; k -= 1) {
    expiryAt[k] = expiryAt[k - 1];
    bullAt[k] = bullAt[k - 1];
    bearAt[k] = bearAt[k - 1];
  }
  expiryAt[lo] = ms;
  bullAt[lo] = 0.0;
  bearAt[lo] = 0.0;
  expiryCount += 1;
  return lo;
}

// One bar's rows into the session's sums.
function fold(n: i32): void {
  const cells = in_flow_view();
  let partial = false;
  for (let i = 0; i + TUPLE - 1 < n; i += TUPLE) {
    if (!rowOk(cells, i)) {
      skipped += 1;
      continue;
    }
    if (isPartial(cells[i + 14])) partial = true;
    runCells += 1;
    const slot = expirySlot(cells[i]);
    if (slot < 0) continue;
    const call = cells[i + 2] > 0.0;
    const premAsk = cells[i + 10];
    const premBid = cells[i + 11];
    bullAt[slot] += call ? premAsk : premBid; // calls bought, puts sold
    bearAt[slot] += call ? premBid : premAsk; // puts bought, calls sold
  }
  if (partial) partialBars += 1;
}

function fbLabel(ms: f64, today: f64): void { // "16 Oct 0d", the expiry 15 Jan '27 in another year
  fb_text("\"");
  fbExpiryLabel(ms, today * 86400000.0, zone);
  fb_text(" ");
  fb_int(i64(optionsTradeDay(ms, zone) - today));
  fb_text("d\"");
}

// The panel the View setting picks, on the live bar: the nearest expiries, ascending.
function writePanel(today: f64): void {
  const last = expiryCount < shown ? expiryCount : shown;
  fb_clear();
  fb_text("{\"caption\":\"Includes cross and late trades\","); // every print takes a side
  fb_text("\"rows\":[");
  for (let e = 0; e < last; e += 1) {
    if (e > 0) fb_text(",");
    fb_text("[");
    fbLabel(expiryAt[e], today);
    fb_text(",");
    if (sides) {
      fb_f64(bullAt[e], 0);
      fb_text(",");
      fb_f64(-bearAt[e], 0);
    } else fb_f64(bullAt[e] - bearAt[e], 0);
    fb_text("]");
  }
  fb_text("]}");
  if (sides) writeFrameBuffer(side_rows);
  else writeFrameBuffer(net_rows);
}

// onBar() runs once per bar: a new trade date starts the sums again; the bar's block adds each contract's bullish
// and bearish premium to its expiry; then the readings and, on the live bar, the panel and the note.
function onBar(): void {
  const date = in_trade_date();
  if (!isNaN(date) && date != day) {
    day = date;
    expiryCount = 0;
    partialBars = 0;
    skipped = 0;
  }
  const n = in_flow_cells();
  if (n >= TUPLE) fold(n);
  let net = 0.0;
  let topDays: f64 = NaN;
  let topNet: f64 = NaN;
  const today = Math.floor(day / 86400.0); // the bar's own date as a day number (the chart fills trade_date on every bar)
  for (let e = 0; e < expiryCount; e += 1) {
    const row = bullAt[e] - bearAt[e];
    net += row;
    if (isNaN(topNet) || Math.abs(row) > Math.abs(topNet)) {
      topNet = row;
      topDays = optionsTradeDay(expiryAt[e], zone) - today;
    }
  }
  const reading = expiryCount > 0;
  out_session_net(reading ? net : NaN);
  out_top_days(topDays);
  out_top_net(topNet);
  out_partial_bars(f64(partialBars));
  out_skipped(f64(skipped));
  if (!bar.isLast()) return;
  if (reading && !isNaN(today)) writePanel(today);
  sb_clear();
  if (runCells == 0) sb_text("No contract flow on this chart yet");
  else if (!reading) sb_text("No contract flow yet this session");
  else if (partialBars > 0) {
    sb_text("Totals are a minimum: ");
    sb_int(partialBars);
    sb_text(partialBars == 1 ? " bar arrived in part" : " bars arrived in part");
  }
  str_note_sb(); // an empty text draws no label
}

How it works

An expiry's premium by side. The data plane sums each bar's prints per expiry, calls apart from puts: each tuple carries prem_ask and prem_bid, the premium of those prints at the ask and at the bid in the bar. A call row adds its ask premium to bullish and its bid premium to bearish; a put row the other way round. Premium at the mid or with no quote has no side, so neither sum holds it. On crypto and CME every print carries the taker's side: bought by the taker at the ask, sold at the bid.

By expiry, through the session. The expiries live in a small table kept in date order; a new trade date (time.trade_date) empties it. On the live bar the panel reads the nearest Expiries shown that traded this session, each labelled 16 Oct 1d (the kit's fbExpiryLabel; an expiry in another year reads its year, 15 Jan '27): the expiry's trade date on the market's clock (market.zone(): the New York date on a US stock, the UTC date on crypto, the CME trade date on a future) and its days to go from the bar's trade date, 0 on expiration day. session_net is the session's bullish minus bearish premium over every expiry, and top_days and top_net name the expiry holding the most either way.

Whole rows only. A tuple counts when it is whole: an expiry, a side of +1 or -1 and a present mask that is a whole number. Anything else is skipped and counted in skipped, never summed under a guess.

Cross and late trades. On US listed options 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 panel's caption and the descriptions of session_net and top_net.

What the settings do

  • View: Net shows bullish minus bearish premium (money paid for options) per expiry date, and sides shows both apart.
  • Expiries shown: How many expiry dates to show, nearest first, among those that traded today.

Where it runs

It runs on coins with listed options and reads the coin's own options on its exchange. A copy you run from the editor (where you write and run your own indicators) reads them too. On a US stock or ETF chart with US listed options, every indicator in your browser can read this data. The charts of that stock's option contracts work the same way. 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. It shows the last 7 days on 1-minute charts and the last 30 days on 5-minute charts.

When data is missing

When only part of a bar's options data arrives, today's totals become a minimum. A muted label at the top counts such bars, as in "Totals are a minimum: 2 bars arrived in part". A bar with no options data adds nothing. A label at the top says "No contract flow on this chart yet" until a bar has an options trade. It says "No contract flow yet this session" while today has none. The bars under the chart stay hidden until then.

Customize it

  • The sides apart. Switch View to sides to see both bets on an expiry, not only their difference.
  • More expiries. Raise Expiries shown to follow the longer-dated money; the table keeps every expiry that traded.
  • Calls and puts. Keep one sum per side and per type to split each expiry's bar into the four flows.

Run it

  1. In the editor's Explorer, press the Templates icon ("Browse starter templates") and pick Premium by Expiry under Beyond the time axis.
  2. Press Run on a BTC chart or a US stock chart: the bars draw under the chart. On a US stock or a CME future the copy reads the flow 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 session_net, top_days and top_net to read the session on the live bar.

Concepts used