// Large taker orders as levels, the way the chart's own Large orders draws them: each order over a
// size, joined from its fills, is a line where it started (a buy's lowest fill, a sell's highest)
// beside a faint band of the prices it ate. The line runs right until a later bar trades back to
// it; until then it carries a tag with the order's size, and after it is a dashed hairline. Orders
// price came back to within their own bar, or on the very next one, never stood, so they are left out.
indicator('Large taker orders', { overlay: true, maxLines: 40, maxBoxes: 40, maxLabels: 48 });
const minUsd = input.float('Minimum order ($)', 2_000_000, { min: 0 });
const chart = bars();
const orders = takerOrders({ minNotional: minUsd });
const tape = trades();
// This bar's orders until it closes: time, level, the prices eaten, USD signed by side (buys above 0), and whether a later trade reached the level.
const PENDING = 32;
const pTime = state.list<number>(PENDING, 'live');
const pLevel = state.list<number>(PENDING, 'live');
const pLo = state.list<number>(PENDING, 'live');
const pHi = state.list<number>(PENDING, 'live');
const pUsd = state.list<number>(PENDING, 'live');
const pHit = state.list<boolean>(PENDING, 'live');
// The levels: the bar each started on, its price, the band, USD, and the bar price came back to it (-1 while it stands).
const KEPT = 300;
const start = state.list<number>(KEPT);
const level = state.list<number>(KEPT);
const lo = state.list<number>(KEPT);
const hi = state.list<number>(KEPT);
const usd = state.list<number>(KEPT);
const end = state.list<number>(KEPT);
orders.onOrder((o) => {
if (pTime.length === PENDING) return;
const low = Math.min(o.firstPrice, o.lastPrice);
const high = Math.max(o.firstPrice, o.lastPrice);
pTime.push(o.time);
pLevel.push(o.side === 'buy' ? low : high);
pLo.push(low);
pHi.push(high);
pUsd.push(o.side === 'buy' ? o.notional : -o.notional);
pHit.push(false);
});
// A later trade at or through a level, the same bar: at or below a buy's, at or above a sell's.
tape.onTrade((t) => {
for (let i = 0; i < pTime.length; i += 1) {
if (pHit.get(i) || !(t.time > pTime.get(i))) continue;
if (pUsd.get(i) > 0 ? t.price <= pLevel.get(i) : t.price >= pLevel.get(i)) pHit.set(i, true);
}
});
const reached = (i: number, low: number, high: number) => (usd.get(i) > 0 ? low <= level.get(i) : high >= level.get(i));
chart.onClose(() => {
// Price came back to a standing level: it ends here, or is dropped when that is the very next bar.
for (let i = start.length - 1; i >= 0; i -= 1) {
if (end.get(i) >= 0 || !reached(i, chart.low, chart.high)) continue;
if (chart.index === start.get(i) + 1) {
for (const list of [start, level, lo, hi, usd, end]) list.set(i, list.get(list.length - 1));
for (const list of [start, level, lo, hi, usd, end]) list.pop();
} else end.set(i, chart.index);
}
for (let i = 0; i < pTime.length; i += 1) {
if (pHit.get(i)) continue;
if (start.length === KEPT) for (const list of [start, level, lo, hi, usd, end]) list.shift();
start.push(chart.index);
level.push(pLevel.get(i));
lo.push(pLo.get(i));
hi.push(pHi.get(i));
usd.push(pUsd.get(i));
end.push(-1);
}
for (const list of [pTime, pLevel, pLo, pHi, pUsd, pHit]) list.clear();
});
// Weight by how many times the minimum an order is, as the chart's Large orders does.
const weight = (amount: number) => (amount >= minUsd * 10 ? { width: 3 as const, alpha: 1 } : amount >= minUsd * 3 ? { width: 2 as const, alpha: 0.95 } : { width: 1 as const, alpha: 0.85 });
const SHOWN = 20;
const TAGS = 8;
chart.onBar(() => {
if (!chart.isLast) return;
// The standing levels, biggest first, and the latest that ended. The forming bar can end one too.
const standing: number[] = [];
const ended: number[] = [];
for (let i = 0; i < start.length; i += 1) {
const open = end.get(i) < 0 && !(start.get(i) < chart.index && reached(i, chart.low, chart.high));
(open ? standing : ended).push(i);
}
standing.sort((a, b) => Math.abs(usd.get(b)) - Math.abs(usd.get(a)));
ended.sort((a, b) => (end.get(b) < 0 ? chart.index : end.get(b)) - (end.get(a) < 0 ? chart.index : end.get(a)));
const draws = [...ended.slice(0, SHOWN).map((i) => ({ i, open: false })), ...standing.slice(0, SHOWN).reverse().map((i) => ({ i, open: true }))];
for (const { i, open } of draws) {
const amount = Math.abs(usd.get(i));
const ink = usd.get(i) > 0 ? color.up : color.down;
const w = weight(amount);
const to = open ? chart.index : end.get(i) < 0 ? chart.index : end.get(i);
const strength = open ? w.alpha : w.alpha * 0.5;
if (hi.get(i) > lo.get(i)) {
draw.box({ from: { bar: start.get(i), price: hi.get(i) }, to: { bar: to, price: lo.get(i) }, border: null, fill: color.withAlpha(ink, open ? 0.1 : 0.06) });
}
draw.line({ from: { bar: start.get(i), price: level.get(i) }, to: { bar: to, price: level.get(i) }, color: color.withAlpha(ink, strength), width: open ? w.width : 1, style: open ? 'solid' : 'dashed' });
draw.label({ at: { bar: start.get(i), price: level.get(i) }, text: '', style: 'circle', size: open && w.width > 1 ? 'small' : 'tiny', color: color.withAlpha(ink, open ? 1 : 0.6) });
}
// Tags on the biggest standing levels, at the newest bar, where their lines end.
for (const i of standing.slice(0, TAGS)) {
const ink = usd.get(i) > 0 ? color.up : color.down;
draw.label({ at: { bar: chart.index, price: level.get(i) }, text: fmt.usd(Math.abs(usd.get(i))), style: 'left', color: color.panel, textColor: ink, size: 'small' });
}
});