Skip to content

quantpylib.hft.trades

Trades

__init__(buffer_size=100)

A class representing a buffer for storing trade logs with attributes for timestamp, price, size, and direction.

Parameters:

Name Type Description Default
buffer_size int

The size of the buffer for storing trades. Defaults to 100.

100

append(trade=None, ts=None, price=None, size=None, dir=None)

Append a new trade to the buffer. A trade is specified by the arguments ts,price,size,dir or with the 4-tuple (ts,price,size,dir).

Parameters:

Name Type Description Default
ts int

The timestamp of the trade. Defaults to None.

None
price float

The price of the trade. Defaults to None.

None
size float

The size of the trade. Defaults to None.

None
dir int

The direction of the trade, 1 or -1. Defaults to None.

None
trade tuple

A tuple representing the trade (ts,price,size,dir).

None

buffer_len()

Get the length of the trade buffer.

Returns:

Name Type Description
int

The length of the trade buffer.

clear_buffer()

Clear the trade buffer.

get_buffer()

Get the current contents of the trade buffer.

Returns:

Type Description

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

get_flow_pnl(n=None, tau=None, T=None, min_n=10, default=0, norm_volume=True, norm_variance=False)

Get the flow pnl over the last sample trades, assuming zero-fee counterparty to all trades.

Parameters:

Name Type Description Default
n int

The number of trades to consider. Defaults to the whole stored buffer.

None
tau int

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

None
T int

The terminal time. Defaults to last timestamp observed.

None
min_n int

The minimum number of trades required to calculate the flow pnl. Defaults to 10.

10
default float

The default value to return if the buffer has less than min_n data points. Defaults to 0.0.

0
norm_volume bool

Normalize the pnl by the volume. Defaults to True.

True
norm_variance bool

Normalize the pnl by the square root of the number of trades. Defaults to False. Only applicable if norm_volume is True.

False

get_imbalance(n=None, tau=None, T=None, min_n=10, default=0, decay_function=None)

Get the trade imbalance over the last n trades.

Parameters:

Name Type Description Default
n int

The number of trades to consider. Defaults to the whole stored buffer.

None
default float

The default value to return if the buffer has less than n data points. Defaults to 0.0.

0
decay_function function

The decay function to apply to the samples. Defaults to simple weighting.

None

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

Get a sample of trades from the buffer.

Parameters:

Name Type Description Default
n int

The number of trades to sample.

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_ts_buffer()

Get the timestamps in the trade buffer.

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

Get the length of the samples.

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.