Skip to content

quantpylib.hft.lob

LOB

A class representing a Limit Order Book (LOB).

Attributes:

Name Type Description
timestamp int64_t

The current timestamp.

depth int

The depth of the order book.

safe_depth int

The safe depth of the order book.

bids ndarray

The bid levels.

asks ndarray

The ask levels.

timestamps_buffer RingBuffer

The buffer for timestamps.

bids_buffer RingBuffer

The buffer for bid levels.

asks_buffer RingBuffer

The buffer for ask levels.

mids_buffer RingBuffer

The buffer for mid prices.

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

Initialize the LOB.

Parameters:

Name Type Description Default
timestamp int64_t

The initial timestamp in ms. Defaults to -1.

-1
bids ndarray

The initial bid levels. Defaults to None.

None
asks ndarray

The initial ask levels. Defaults to None.

None
depth int

The depth of the order book. Defaults to 100.

100
buffer_size int

The size of the buffer. Defaults to 0.

0
apply_shadow_depth bool

Whether to maintain higher levels than depth for accuracy at speed cost. Defaults to False.

False
**kwargs

Additional keyword arguments.

{}

aggregator(interval=60 * 1000)

Aggregate the order book buffer into buckets.

Parameters:

Name Type Description Default
interval int

The interval for aggregation in milliseconds. Defaults to 60*1000.

60 * 1000

Returns:

Name Type Description
list

A list of aggregated buckets.

as_dict()

Get the order book as a dictionary.

Returns:

Name Type Description
dict

The order book as a dictionary.

as_tuple()

Get the order book as a tuple.

Returns:

Name Type Description
tuple

The order book as a tuple (timestamp, bids, asks).

buffer_as_list()

Get the buffer as a list.

Returns:

Name Type Description
list

The buffer as a list of dictionaries.

buffer_len()

Get the length of the buffer.

Returns:

Name Type Description
int

The length of the buffer.

clear_buffer()

Clear the buffers.

get_ask()

Get the best ask price.

Returns:

Name Type Description
float

The best ask price.

get_ask_sz()

Get the size of the best ask.

Returns:

Name Type Description
float

The size of the best ask.

get_asks()

Get the ask levels.

Returns:

Type Description

np.ndarray: The ask levels.

get_asks_buffer()

Get the ask levels buffer.

Returns:

Type Description

np.ndarray: The ask levels buffer.

get_asks_sample(n=-1, tau=-1, T=-1)

Get the sample of the most recent asks to take.

Parameters:

Name Type Description Default
n int

The number of samples to take.

-1
tau int

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

-1
T int

The terminal time. Defaults to last timestamp observed.

-1

Returns:

Type Description

np.ndarray: The sample of the ask levels.

get_bid()

Get the best bid price.

Returns:

Name Type Description
float

The best bid price.

get_bid_sz()

Get the size of the best bid.

Returns:

Name Type Description
float

The size of the best bid.

get_bids()

Get the bid levels.

Returns:

Type Description

np.ndarray: The bid levels.

get_bids_buffer()

Get the bid levels buffer.

Returns:

Type Description

np.ndarray: The bid levels buffer.

get_bids_sample(n=-1, tau=-1, T=-1)

Get the sample of the most recent bids to take.

Parameters:

Name Type Description Default
n int

The number of samples to take.

-1
tau int

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

-1
T int

The terminal time. Defaults to last timestamp observed.

-1

Returns:

Type Description

np.ndarray: The sample of the bid levels.

get_cumulative_size(dir, price)

Get the cumulative size and notional that can be instantaneously executed given a limit price.

Parameters:

Name Type Description Default
dir int

The direction (-1 for bids, 1 for asks).

required
price float

The price level.

required

Returns:

Name Type Description
tuple

The cumulative size and notional.

get_impact_price(dir, notional)

Get the impact price for a given direction and notional amount.

Parameters:

Name Type Description Default
dir int

The direction (-1 for bids, 1 for asks).

required
notional float

The notional amount.

required

Returns:

Name Type Description
float

The impact 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.

required
x bool

Whether to normalize by the mid price. Defaults to False.

False

Returns:

Name Type Description
float

The impact spread.

get_impact_worst(dir, sz)

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

Parameters:

Name Type Description Default
dir int

The direction (-1 for bids, 1 for asks).

required
sz float

The size of the market order.

required

Returns:

Name Type Description
float

The worst limit price.

get_mid()

Get the mid price.

Returns:

Name Type Description
float

The mid price.

get_mids_buffer()

Get the mid prices buffer.

Returns:

Type Description

np.ndarray: The mid prices buffer.

get_mids_sample(n=-1, tau=-1, T=-1)

Get the sample of the most recent mids to take.

Parameters:

Name Type Description Default
n int

The number of samples to take.

-1
tau int

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

-1
T int

The terminal time. Defaults to last timestamp observed.

-1

Returns:

Type Description

np.ndarray: The sample of the mid prices.

get_spread(x=False)

Get the spread of the top of order book.

Parameters:

Name Type Description Default
x bool

Whether to normalize by the mid price. Defaults to False.

False

Returns:

Name Type Description
float

The spread of the order book.

get_ts_buffer()

Get the timestamp buffer.

Returns:

Type Description

np.ndarray: The timestamp buffer.

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

Get the Volume Adjusted Mid Price (VAMP).

Parameters:

Name Type Description Default
notional float

The notional amount for both sides of the order book. Defaults to 0.

0
bid_notional float

The notional amount for bids. Defaults to notional if not provided.

0
ask_notional float

The notional amount for asks. Defaults to notional if not provided.

0

Returns:

Name Type Description
float

The VAMP.

get_vol(n=-1, tau=-1, T=-1, x=False, exp=False)

Get the volatility of the mid prices, excludes zero differences.

Parameters:

Name Type Description Default
n int

The number of samples to take.

-1
tau int

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

-1
T int

The terminal time. Defaults to last timestamp observed.

-1
x bool

Whether to normalize by the mid price. Defaults to False.

False
exp bool

Whether to use exponential weighting. Defaults to False.

False

Returns:

Name Type Description
float

The volatility of the mid prices.

sample_len(n=-1, tau=-1, T=-1)

Get the length of the most recent samples to take.

Parameters:

Name Type Description Default
n int

The number of samples to take.

-1
tau int

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

-1
T int

The terminal time. Defaults to last timestamp observed.

-1

Returns:

Name Type Description
int

The length of the sample.

update(timestamp, bids, asks, is_snapshot, is_sorted=True)

Update the order book with new bid and ask levels.

Parameters:

Name Type Description Default
timestamp int64_t

The new timestamp.

required
bids ndarray

The new bid levels.

required
asks ndarray

The new ask levels.

required
is_snapshot bool

Whether the update is a snapshot or delta update.

required
is_sorted bool

Whether the levels are already sorted (increasing/decreasing). Defaults to True.

True