Skip to content

quantpylib.hft.lob

LOB

__init__(timestamp=-1, bids=None, asks=None, depth=100, buffer_size=100, apply_shadow_depth=False)

A class representing a Limit Order Book (LOB) for financial trading.

Parameters:

Name Type Description Default
timestamp float

The initial timestamp in milliseconds. Defaults to current time.

-1
bids ndarray

Initial bid array. Defaults to None.

None
asks ndarray

Initial ask array. Defaults to None.

None
depth int

The maximum depth of the order book. Defaults to 100.

100
buffer_size int

The size of the buffers for storing historical data. Defaults to 100.

100

as_dict()

Represent the current state of the order book as a dictionary.

Returns:

Name Type Description
dict

The current timestamp, bids, and asks.

as_tuple()

Represent the current state of the order book as a tuple.

Returns:

Name Type Description
tuple

The current timestamp, bids, and asks.

buffer_as_list()

Obtain the historical data buffers as a list of dictionaries.

buffer_len()

Get the length of the buffers.

Returns:

Name Type Description
int

The length of the buffers.

clear_buffer()

Clear all historical data buffers.

get_ask()

Get the best ask price.

get_ask_sz()

Get the best ask size.

get_asks()

Get the current ask array.

get_asks_buffer()

Get the buffer of three-dimensional (time,price,size) ask arrays.

Returns:

Type Description

np.ndarray: The array of ask arrays in the buffer.

get_asks_sample(n=None, tau=None, T=None)

Get the most recent sample of ask arrays.

Parameters:

Name Type Description Default
n int

The number of samples to take.

None
tau int

T - t, where t is the earliest sample time.

None
T int

The terminal time. Defaults to last timestamp observed.

None

get_bid()

Get the best bid price.

get_bid_sz()

Get the best bid size.

get_bids()

Get the current bid array.

get_bids_buffer()

Get the buffer of three-dimensional (time,price,size) bid arrays.

Returns:

Type Description

np.ndarray: The array of bid arrays in the buffer.

get_bids_sample(n=None, tau=None, T=None)

Get the most recent sample of bid arrays.

Parameters:

Name Type Description Default
n int

The number of samples to take.

None
tau int

T - t, where t is the earliest sample time.

None
T int

The terminal time. Defaults to last timestamp observed.

None

get_cumulative_size(dir, price)

Returns the size and volume that can be executed given a limit price instantaneously.

Parameters:

Name Type Description Default
dir int

The direction of the trade. 1 for buy, -1 for sell.

required
price float

The limit price.

required

get_impact_price(dir, notional)

Calculate the volume-weighted price for a given notional amount.

Parameters:

Name Type Description Default
dir int

The direction of the trade. 1 for buy, -1 for sell.

required
notional float

The notional amount.

required

Returns:

Name Type Description
float

The volume-weighted price.

get_impact_spread(notional, x=False)

Get the spread between impact prices from top of book.

Parameters:

Name Type Description Default
notional float

The notional amount for the calculation.

required
x bool

Whether to normalize by price.

False

get_impact_worst(dir, sz)

Returns the worst limit price for a given market order size.

Parameters:

Name Type Description Default
dir int

The direction of the trade. 1 for buy, -1 for sell.

required
sz float

The size of the market order.

required

get_mid()

Get the mid price of the current order book.

Returns:

Name Type Description
float

The mid price.

get_mids_buffer()

Get the buffer of one-dimensional mid prices.

Returns:

Type Description

np.ndarray: The array of mid prices in the buffer.

get_mids_sample(n=None, tau=None, T=None)

Get the most recent sample of mid prices.

Parameters:

Name Type Description Default
n int

The number of samples to take.

None
tau int

T - t, where t is the earliest sample time.

None
T int

The terminal time. Defaults to last timestamp observed.

None

get_spread(x=False)

Get the spread at top of book.

Parameters:

Name Type Description Default
x bool

Whether to normalize by price.

False

get_ts_buffer()

Get the buffer of one-dimensional timestamps.

Returns:

Type Description

np.ndarray: The array of timestamps in the buffer.

get_vamp(notional=None, bid_notional=None, ask_notional=None)

Calculate the volume-adjusted mid price.

Parameters:

Name Type Description Default
notional float

The notional amount for the calculation.

None
bid_notional float

The notional amount for the bid side. Defaults to notional.

None
ask_notional float

The notional amount for the ask side. Defaults to notional.

None

Returns:

Name Type Description
float

The volume-adjusted mid price.

get_vol(n=None, tau=None, T=None, x=False, exp=False)

Get mid-price volatility. Excludes zero differences.

Parameters:

Name Type Description Default
n int

The number of samples to use.

None
tau int

T - t, where t is the earliest sample time.

None
T int

The terminal time. Defaults to last timestamp observed.

None
x bool

Whether to normalize by price.

False
exp bool

Whether to use exponential weighting. Defaults to False.

False

sample_len(n=None, tau=None, T=None)

Get the length of the most recent samples to take.

Parameters:

Name Type Description Default
n int

The number of samples to take.

None
tau int

T - t, where t is the earliest sample time.

None
T int

The terminal time. Defaults to last timestamp observed.

None

Returns:

Name Type Description
int

The length of the samples.

update(timestamp, bids, asks, is_snapshot, sort_levels=False)

Update the order book with new bid and ask data.

Parameters:

Name Type Description Default
timestamp float

The current timestamp in milliseconds.

required
bids ndarray

The new bid array.

required
asks ndarray

The new ask array.

required
is_snapshot bool

Whether the update is a snapshot (true) or an incremental update (false).

required

a_in_b(a, b)

Check if elements of array a are in array b.

Parameters:

Name Type Description Default
a ndarray

The array of elements to check.

required
b ndarray

The array to check against.

required

Returns:

Type Description

np.ndarray: A boolean array indicating if elements of a are in b.