Spread and quote tracker
Level 1: the live spread and the size at the best bid and offer in the readout, a marker at the right edge when the spread widens, and a running record of the quote kept in a var. In replay, where there are no quotes, it reads the top of the recorded book instead.
//@xfscript 1
indicator("Spread Tracker")
let wide_ticks: float = input.float(2, "wideTicks", "Wide spread in ticks", 1, 50, 1)
let calm_color: color = input.color("#E5E7EB", "calmColor", "Normal")
let wide_color: color = input.color("#F59E0B", "wideColor", "Wide")
let tick: float = nz(chart.tick_size(), 0.25)
let quoted: bool = l1.supported() && l1.available()
let booked: bool = l2.supported() && l2.available()
var spread: float = nan
spread := nz(spread[1], nan)
var bid_size: float = nan
var ask_size: float = nan
bid_size := nz(bid_size[1], nan)
ask_size := nz(ask_size[1], nan)
if booked {
spread := l2.spread()
bid_size := l2.bid_size(0)
ask_size := l2.ask_size(0)
}
if quoted {
spread := l1.ask_price() - l1.bid_price()
bid_size := l1.bid_size()
ask_size := l1.ask_size()
}
let spread_ticks: float = spread / tick
let is_wide: bool = spread_ticks >= wide_ticks
var text_color: color = "#E5E7EB"
text_color := calm_color
if is_wide {
text_color := wide_color
}
plot.marker(last_bar && is_wide, close, wide_color, "x", "above", 10, 6)
if is_nan(spread) {
plot.label("NO QUOTE ON THIS CHART", "#6B7280")
} else {
plot.label(str.concat("SPREAD ", format.fixed(spread_ticks, 0), " TICKS BID ", format.compact(bid_size), " x ASK ", format.compact(ask_size)), text_color)
}