Skip to main content
Functions for analyzing order book data, including depth, liquidity, and spread metrics.

Overview

FunctionDescription
orderbook_depth()Get order book depth at specified levels
bid_ask_spread()Calculate current bid-ask spread
liquidity_score()Measure market liquidity

orderbook_depth()

Access order book depth data at specified price levels.

Signature

orderbook_depth(symbol: string, exchange: string, levels?: number = 20): TimeSeries

Parameters

ParameterTypeDescription
symbolstringTrading pair symbol
exchangestringExchange name
levelsnumberNumber of price levels (default: 20)

Returns

TimeSeries - Order book depth data with bid and ask volumes

Example

//@version=2

define(title="Order Book Depth", position="offchart", axis=true);

timeseries depthData = orderbook_depth(symbol=currentSymbol, exchange=currentExchange, levels=20);

plotBar(value=depthData, width=1, colors=["green", "red"], label=["Bid Depth", "Ask Depth"], desc=["Bid Side Volume", "Ask Side Volume"]);

bid_ask_spread()

Calculate the current bid-ask spread as a percentage.

Signature

bid_ask_spread(symbol: string, exchange: string): number

Parameters

ParameterTypeDescription
symbolstringTrading pair symbol
exchangestringExchange name

Returns

number - Spread as percentage of mid-price

Example

//@version=2

define(title="Bid-Ask Spread", position="offchart", axis=true);

var spreadValue = bid_ask_spread(symbol=currentSymbol, exchange=currentExchange);

// Highlight high spreads
var colorIndex = spreadValue > 0.1 ? 0 : 1;

plotLine(value=spreadValue, width=2, colors=["red", "green"], colorIndex=colorIndex, label=["Spread %"], desc=["Bid-Ask Spread Percentage"]);

liquidity_score()

Calculate a liquidity score based on order book depth and spread.

Signature

liquidity_score(symbol: string, exchange: string): number

Parameters

ParameterTypeDescription
symbolstringTrading pair symbol
exchangestringExchange name

Returns

number - Liquidity score (higher = more liquid)

Example

//@version=2

define(title="Liquidity Score", position="offchart", axis=true);

var liquidityValue = liquidity_score(symbol=currentSymbol, exchange=currentExchange);

plotLine(value=liquidityValue, width=2, colors=["blue"], label=["Liquidity"], desc=["Market Liquidity Score"]);

Complete Example: Order Book Analysis

//@version=2

define(title="Order Book Analysis", position="offchart", axis=true);

// Get order book data
timeseries depthData = orderbook_depth(symbol=currentSymbol, exchange=currentExchange, levels=50);
var spreadValue = bid_ask_spread(symbol=currentSymbol, exchange=currentExchange);
var liquidityValue = liquidity_score(symbol=currentSymbol, exchange=currentExchange);

// Calculate bid/ask imbalance
var bidVolume = depthData.bid;
var askVolume = depthData.ask;
var imbalance = (bidVolume - askVolume) / (bidVolume + askVolume);

// Color based on imbalance direction
var colorIndex = imbalance > 0 ? 0 : 1;

plotBar(value=imbalance, width=1, colors=["green", "red"], colorIndex=colorIndex, label=["Imbalance"], desc=["Bid/Ask Imbalance"]);

Tips

Order Book Interpretation

  • More bids than asks - Potential buying pressure (bullish)
  • More asks than bids - Potential selling pressure (bearish)
  • Large spread - Low liquidity, higher trading costs

Depth Analysis

Look for large orders (walls) that may act as support or resistance. These can indicate institutional interest.

Spread Monitoring

Monitor spread during different market conditions. Spreads typically widen during high volatility and narrow during quiet markets.

Data Availability

Order book data may not be available for all symbol/exchange combinations. Check data availability before building strategies.