Skip to content

quantpylib.hft.lob

LOB

__init__(timestamp=time.time() * 1000, bids=None, asks=None, depth=100, buffer_size=100)

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.

time() * 1000
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_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_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_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_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_spread()

Get the spread of the current top of book.

Returns:

Name Type Description
float

The spread.

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)

Calculate the volume-adjusted mid price.

Parameters:

Name Type Description Default
notional float

The notional amount for the calculation.

required

Returns:

Name Type Description
float

The volume-adjusted mid price.

get_vol(n=None, absolute=True, exp=False)

Calculate the volatility of the mid prices over the last n updates. Excludes zero differences.

Parameters:

Name Type Description Default
n int

The number of updates to consider. Defaults to the length of the mids buffer.

None
absolute bool

Whether to return absolute volatility. Defaults to True.

True
exp bool

Whether to use exponential weighting. Defaults to False.

False

Returns:

Name Type Description
float

The calculated volatility.

load_buffer(timestamps_buffer, bids_buffer, asks_buffer, mids_buffer) staticmethod

Load a Limit Order Book (LOB) from existing buffers.

Parameters:

Name Type Description Default
timestamps_buffer RingBuffer

Buffer containing timestamps.

required
bids_buffer RingBuffer

Buffer containing bid arrays.

required
asks_buffer RingBuffer

Buffer containing ask arrays.

required
mids_buffer RingBuffer

Buffer containing mid prices.

required

Returns:

Name Type Description
LOB

The loaded Limit Order Book.

load_numpy_arr(timestamps_array, bids_array, asks_array, mids_array) staticmethod

Load a Limit Order Book (LOB) from existing numpy arrays. Buffer attributes are instantiated exactly equal to the length of arrays passed in.

Parameters:

Name Type Description Default
timestamps_array ndarray

Array containing timestamps.

required
bids_array ndarray

Array containing bid arrays.

required
asks_array ndarray

Array containing ask arrays.

required
mids_array ndarray

Array containing mid prices.

required

Returns:

Name Type Description
LOB

The loaded Limit Order Book.

update(timestamp, bids, asks, is_snapshot)

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.