Contract sides
Contract Sides shows where option trades hit the quote, calls (bets on a rise) beside puts (bets on a fall).
A is at the ask (the seller's price, so the buyer paid up), and AA above it, paid for speed. B is at the bid (the buyer's price, so the seller gave in), BB below it, and M the middle. A column shows each bar's bullish contracts, calls bought and puts sold, minus its bearish ones. Use it to see who drove today's options on a US stock, coin or CME (Chicago futures exchange) future.
Under the hood: twenty scalar options_flow inputs read the data plane's per-bar sums by side with missing: "zero" (Data sources). A ring keeps each bar's sums. A frame feeds a panel.bars under the chart with a per-run title (Bars). It holds contracts in six groups (AA, A, M, B, BB and No side), or premium in three. The three are at the ask and above, at the bid and below, and the rest. On crypto and CME it holds Bought and Sold alone. Beside it, a histogram is coloured per bar by a data-only ladder output (Plotting). The options kit's optionsTradeDay finds today's session (Options kit). This is also the contract-sides template: the Contract Sides card under Order flow in the editor's starter list, and it compiles as written.
The wrun indicator
// Contract Sides: where this symbol's option trades hit the quote, as bars under the chart. Calls (bets on a rise) sit
// beside puts (bets on a fall). A print is one option trade. A means at the ask (the seller's price, so the buyer paid
// up), and AA above it, paid for speed. B means at the bid (the buyer's price, so the seller gave in), and BB below it.
// M is the middle, and No side had no quote. On crypto and CME (Chicago futures exchange) futures the bars read Bought
// and Sold instead. A column shows each bar's bullish contracts (calls bought, puts sold) minus its bearish ones. Use
// it to see whether buyers or sellers drove today's options.
//
// Under the hood: where the options of the chart's underlying traded against the quote, calls beside puts. The data
// plane sums each bar's prints by side, calls apart from puts, and the bars under the chart sum those over today's
// session or the last bars you pick. On a US stock, by the midpoint rule: AA above the ask, A the rest of the ask side,
// M the mid, B the rest of the bid side, BB below the bid, and no side (no quote to judge); premium splits at or above
// the ask, at or below the bid, and the rest (the mid and no side). Crypto and CME prints carry the taker's side
// instead of a quote, so there the bars read bought and sold alone, in coins on crypto. A print at the ask or above was
// most likely bought, one at the bid or below sold, and one outside the quote paid up for speed. Under the chart, each
// bar's bullish minus bearish contracts (calls bought and puts sold, minus calls sold and puts bought). It reads the
// chart's own underlying (an option contract's chart reads the contract's underlying).
section("Window");
param.choice("measure", ["contracts", "premium"], "contracts", { label: "Measure" });
param.choice("window", ["today", "last bars"], "today", { label: "Window", description: "today sums the newest bar's trading day, on the market's own clock. last bars sums the number of bars set below." });
param.int("bars", 60, { min: 1, max: 500, label: "Bars", when: "window", description: "How many recent bars last bars sums." });
input("close", ohlcv.close); // the chart's own candles: the grid the flow lines up on
// The bar's prints summed by side of the quote, calls and puts apart: contracts at the ask, at the bid, at the mid and
// with no side, the contracts above the ask and below the bid, and premium by the same four sides. A quiet side reads 0.
input("call_ask", options_flow.call_contracts_ask, { missing: "zero" });
input("call_bid", options_flow.call_contracts_bid, { missing: "zero" });
input("call_mid", options_flow.call_contracts_mid, { missing: "zero" });
input("call_none", options_flow.call_contracts_none, { missing: "zero" });
input("call_above", options_flow.call_contracts_above_ask, { missing: "zero" });
input("call_below", options_flow.call_contracts_below_bid, { missing: "zero" });
input("put_ask", options_flow.put_contracts_ask, { missing: "zero" });
input("put_bid", options_flow.put_contracts_bid, { missing: "zero" });
input("put_mid", options_flow.put_contracts_mid, { missing: "zero" });
input("put_none", options_flow.put_contracts_none, { missing: "zero" });
input("put_above", options_flow.put_contracts_above_ask, { missing: "zero" });
input("put_below", options_flow.put_contracts_below_bid, { missing: "zero" });
input("call_prem_ask", options_flow.call_prem_ask, { missing: "zero" });
input("call_prem_bid", options_flow.call_prem_bid, { missing: "zero" });
input("call_prem_mid", options_flow.call_prem_mid, { missing: "zero" });
input("call_prem_none", options_flow.call_prem_none, { missing: "zero" });
input("put_prem_ask", options_flow.put_prem_ask, { missing: "zero" });
input("put_prem_bid", options_flow.put_prem_bid, { missing: "zero" });
input("put_prem_mid", options_flow.put_prem_mid, { missing: "zero" });
input("put_prem_none", options_flow.put_prem_none, { missing: "zero" });
market.zone(); // the market's clock: the trade date today's window reads, and a US quote (zone 1) or the taker's side
output("lean", histogram, lower, { color_by: "lean_tone", colors: ["theme.down", "theme.up"], label: "Bullish minus bearish contracts", format: "si", description: "Bullish contracts minus bearish contracts in this bar, in coins on crypto. Bullish counts calls (bets on a rise) at or above the ask (the seller's price). It adds puts (bets on a fall) at or below the bid (the buyer's price)." });
output("lean_tone", none, lower, { description: "The column's colour: 0 bearish, 1 bullish." });
output("above_ask", none, lower, { format: "si", description: "Option contracts that traded above the ask (the seller's price) in this bar. Blank on crypto and CME, whose trades carry no quote." });
output("below_bid", none, lower, { format: "si", description: "Option contracts that traded below the bid (the buyer's price) in this bar. Blank on crypto and CME." });
output("call_ask_share", none, lower, { format: "%", decimals: 1, description: "Percent of the window's calls (bets on a rise) bought at the ask (the seller's price) or above. On crypto and CME, the percent bought. Live bar only." });
output("put_ask_share", none, lower, { format: "%", decimals: 1, description: "Percent of the window's puts (bets on a fall) bought at the ask (the seller's price) or above. Live bar only." });
const side_rows = frame("side_rows", { max_bytes: 2048 });
panel.bars({ name: "by_side", title: "Contracts by side", x: "category", place: "below", frame: side_rows, orientation: "vertical", labels: true, series: [{ name: "Calls", color: "theme.up" }, { name: "Puts", color: "theme.down" }], format: "si", hover_card: true, legend_style: "title", height_frac: 0.22 });
const SLOTS = 18; // per bar: calls AA, A, M, B, BB, none; puts the same; then calls' premium at the ask, at the bid, the rest; puts the same
const MAX_BARS = 500; // the bars setting's largest value
const ring = new StaticArray<f64>(MAX_BARS * SLOTS); // the last bars' sums, one slot per bar
const ringDay = new StaticArray<f64>(MAX_BARS); // each slot's trade date on the market's clock
const barSums = new StaticArray<f64>(SLOTS); // this bar's sums
const sums = new StaticArray<f64>(SLOTS); // the window's sums
const BUCKETS: StaticArray<string> = ["AA", "A", "M", "B", "BB", "No side"];
const PREMIUM_BUCKETS: StaticArray<string> = ["Ask and above", "Bid and below", "Mid and no side"];
let measure = 0; // settings, read in onStart()
let today = true;
let bars = 60;
let zone = 0; // the market's clock, read in onStart()
let head = 0; // the ring's next slot, and how many slots hold a bar
let filled = 0;
function onStart(): void {
measure = i32(p_measure());
today = p_window() == 0.0;
bars = i32(p_bars());
zone = i32(p_market_zone());
}
// One option type's sums into barSums (base 0 the calls, 6 the puts): its contracts by bucket (AA and BB are the part
// of the ask and bid sides that printed outside the quote) and its premium at the ask, at the bid and the rest (the
// mid and no side).
function add(base: i32, ask: f64, bid: f64, mid: f64, none: f64, above: f64, below: f64, premAsk: f64, premBid: f64, premMid: f64, premNone: f64): void {
const aboveAsk = Math.min(above, ask);
const belowBid = Math.min(below, bid);
barSums[base] = aboveAsk;
barSums[base + 1] = ask - aboveAsk;
barSums[base + 2] = mid;
barSums[base + 3] = bid - belowBid;
barSums[base + 4] = belowBid;
barSums[base + 5] = none;
const premiumBase = 12 + (base == 0 ? 0 : 3);
barSums[premiumBase] = premAsk;
barSums[premiumBase + 1] = premBid;
barSums[premiumBase + 2] = premMid + premNone;
}
function share(part: f64, whole: f64): f64 {
return whole > 0.0 ? (part / whole) * 100.0 : NaN;
}
// One row of the bars: its name, the calls' value and the puts' (a coin's size to four decimals, whole numbers bare).
function fbRow(name: string, calls: f64, puts: f64): void {
fb_text("[");
fb_str(name);
fb_text(",");
fb_f64(calls, measure == 0 && calls != Math.floor(calls) ? 4 : 0);
fb_text(",");
fb_f64(puts, measure == 0 && puts != Math.floor(puts) ? 4 : 0);
fb_text("]");
}
// The bars under the chart: one category per side, calls beside puts, over the window.
function writeBars(windowBars: i32): void {
fb_clear();
fb_text("{\"title\":\"");
fb_text(measure == 1 ? "Premium by side, " : zone == 0 ? "Coins by side, " : "Contracts by side, ");
if (today) fb_text("today");
else {
fb_text("last ");
fb_int(windowBars);
fb_text(windowBars == 1 ? " bar" : " bars");
}
fb_text("\",\"rows\":[");
if (zone != 1) { // crypto and CME prints carry the taker's side, never a quote: bought (the ask side), then sold
fbRow("Bought", measure == 0 ? sums[0] + sums[1] : sums[12], measure == 0 ? sums[6] + sums[7] : sums[15]);
fb_text(",");
fbRow("Sold", measure == 0 ? sums[3] + sums[4] : sums[13], measure == 0 ? sums[9] + sums[10] : sums[16]);
} else {
for (let b = 0; b < (measure == 0 ? 6 : 3); b += 1) {
if (b > 0) fb_text(",");
if (measure == 0) fbRow(BUCKETS[b], sums[b], sums[6 + b]);
else fbRow(PREMIUM_BUCKETS[b], sums[12 + b], sums[15 + b]);
}
}
fb_text("]}");
writeFrameBuffer(side_rows);
}
// onBar() runs once per bar: the bar's sums by side into its ring slot; the column and the bar's readings on every
// bar; the window's sums, the two shares and the bars on the live bar.
function onBar(): void {
const barMs = bar.time() * 1000.0;
add(0, in_call_ask(), in_call_bid(), in_call_mid(), in_call_none(), in_call_above(), in_call_below(), in_call_prem_ask(), in_call_prem_bid(), in_call_prem_mid(), in_call_prem_none());
add(6, in_put_ask(), in_put_bid(), in_put_mid(), in_put_none(), in_put_above(), in_put_below(), in_put_prem_ask(), in_put_prem_bid(), in_put_prem_mid(), in_put_prem_none());
const slot = head * SLOTS;
for (let s = 0; s < SLOTS; s += 1) ring[slot + s] = barSums[s];
ringDay[head] = optionsTradeDay(barMs, zone);
head = (head + 1) % MAX_BARS;
if (filled < MAX_BARS) filled += 1;
const bullish = barSums[0] + barSums[1] + barSums[9] + barSums[10]; // calls at the ask or above, puts at the bid or below
const bearish = barSums[3] + barSums[4] + barSums[6] + barSums[7]; // calls at the bid or below, puts at the ask or above
out_lean(bullish + bearish > 0.0 ? bullish - bearish : NaN); // a bar with no sided contract draws no column
out_lean_tone(bullish >= bearish ? 1.0 : 0.0);
out_above_ask(zone == 1 ? barSums[0] + barSums[6] : NaN); // outside the quote: a US quote's alone
out_below_bid(zone == 1 ? barSums[4] + barSums[10] : NaN);
if (!bar.isLast()) {
out_call_ask_share(NaN);
out_put_ask_share(NaN);
return;
}
// The window: today's bars, or the last `bars`, newest first from the ring.
for (let s = 0; s < SLOTS; s += 1) sums[s] = 0.0;
const day = optionsTradeDay(barMs, zone);
let windowBars = 0;
for (let k = 0; k < filled; k += 1) {
const at = (head - 1 - k + MAX_BARS) % MAX_BARS;
if (today ? ringDay[at] != day : k >= bars) break;
for (let s = 0; s < SLOTS; s += 1) sums[s] += ring[at * SLOTS + s];
windowBars += 1;
}
const calls = sums[0] + sums[1] + sums[2] + sums[3] + sums[4] + sums[5];
const puts = sums[6] + sums[7] + sums[8] + sums[9] + sums[10] + sums[11];
out_call_ask_share(share(sums[0] + sums[1], calls));
out_put_ask_share(share(sums[6] + sums[7], puts));
writeBars(windowBars);
}How it works
Each bar's flow, by side. The inputs read the data plane's own sums of the bar's prints (options_flow, the chart's underlying), calls apart from puts: the contracts at the ask, at the bid, at the mid and with no side, the contracts above the ask and below the bid, and the premium at the same four sides. Each reads missing: "zero": a side where nothing traded in the bar reads 0, as does a bar with no print at all. On a US stock 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, exactly on it at the mid, and with no valid quote it has no side. A crypto venue and CME name the taker's side instead: a print the taker bought counts at the ask, one the taker sold at the bid, and none counts at the mid, with no side or outside the quote. Sizes are contracts on a US stock and on CME and coins on crypto (0.6 BTC is 0.6, never 1).
Six buckets of contracts. A print above the ask is an ask-side print, and one below the bid a bid-side print, so the buckets split those two sides: AA is the contracts above the ask, A the rest of the ask side, B the rest of the bid side, BB the contracts below the bid; M and No side are the mid and the unquoted prints. Each bucket is summed for the calls and for the puts. Off the US quote (crypto and CME) the bars read two rows instead: Bought, the ask side, and Sold, the bid side.
Three buckets of premium. The data plane sums the premium of the bar's prints at each side of the quote, so premium splits three ways: Ask and above, Bid and below, and Mid and no side, the premium at the mid plus the premium with no side. On crypto and CME premium reads Bought and Sold alone, the premium at the ask and at the bid, since no print trades at the mid there.
The window. Window sums today's bars (the trade date the newest bar is on, on the market's clock: the New York date on a US stock; up to the last 500 bars) or the last Bars bars (60, up to 500). Measure picks contracts (six buckets on a US stock, two off it) or premium (three, or two). The bars stand under the chart on the live bar, calls in the up colour beside puts in the down colour in each bucket, titled by the measure and the window (Contracts by side, today, Coins by side, today on crypto, Premium by side, last 60 bars). A whole number prints bare and a coin's size to four decimals.
The column. lean is each bar's bullish contracts (calls at the ask or above plus puts at the bid or below) minus its bearish contracts (calls at the bid or below plus puts at the ask or above), a histogram in its own pane, the up colour at or above zero and the down colour below (lean_tone picks the colour); a bar with no contract on a side of the quote draws no column.
The readings. Data-only outputs: above_ask and below_bid (the bar's contracts outside the quote, calls and puts; NaN on crypto and CME, which carry no quote), and on the live bar call_ask_share and put_ask_share, the share of the window's call contracts and of its put contracts at the ask or above (bought, on crypto and CME), in percent.
What the settings do
- Measure: Counts contracts (coins on crypto), or premium (the money paid) in USD.
- Window: Sums today's trading day, or the number of recent bars you set below.
- Bars: How many recent bars the last bars window sums, from 1 to 500.
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. On crypto and CME, trades carry no quote, only whether the buyer or the seller started them. There the bars read Bought and Sold. A copy you run from the editor (where you write and run indicators) reads them too. On a US stock or ETF with listed options, every indicator in your browser can read them. They are real time if you have real-time US options data, and 15 minutes behind otherwise. On a CME futures chart, every indicator in your browser can read them too, once you accept the CME market data terms (Limitations). Alerts and Protected indicators (published ones that run on OpenMarket's servers) cannot read this options flow. On an option contract's own chart, it reads the symbol behind that contract, so the bars match the stock's chart. CME data is 10 minutes behind unless you have real-time CME data, and the legend shows which delay applies. It shows the last 7 days on 1-minute charts and the last 30 days on 5-minute charts. An older bar reads 0 everywhere and draws no column.
When data is missing
- A quiet side. A side where nothing traded in the bar reads 0, so it adds nothing to its group.
- A bar with no trades. A bar where nothing traded, or one older than the history the chart loads, reads 0 everywhere. It adds zeros to the window and draws no column. In the Console (the editor's prompt that shows a reading by its name),
lean_tone, the column's colour code, reads 1. The Console also reads 0 forabove_askandbelow_bidon a US stock, and blank on crypto and CME.
Customize it
- The session's verdict. Keep Window at today and Measure at premium: the dollars bought against the dollars sold since the open.
- A rolling view. Set Window to last bars and Bars to 12 on a 5-minute chart: the last hour's sides.
- Urgency alone. Read
above_askandbelow_bidin the Console: the contracts that crossed the spread to trade.
Run it
- In the editor's Explorer, press the Templates icon ("Browse starter templates") and pick Contract Sides under Order flow.
- Press Run on a Deribit BTC chart or on a US stock chart such as SPY: the bars stand under the chart with the column in its own pane; on a US stock the legend names the tier the read was served at.
- At the editor's Console prompt, type
call_ask_shareandput_ask_shareto read the window's shares bought at the ask.
Concepts used
- Data sources for
options_flow, its flow fields by side and itsmissingfill - Bars for
panel.barsover a frame with two series and a per-run title - Plotting for a histogram coloured by a ladder output
- Options kit for
optionsTradeDay