ResampledLiquidityUniverse#

API documentation for tradingstrategy.liquidity.ResampledLiquidityUniverse Python class in Trading Strategy framework.

class ResampledLiquidityUniverse[source]#

Bases: object

Backtesting performance optimised version of liquidity universe.

  • The class is designed to be used with strategies that trade a large number of pairs and picks pairs by their liquidity criteria, which varies over time

  • Because liquidity information does not need to be as accurate as pricing information, and a rough estimate of liquidity is enough for most strategies, we can preconstruct an optimised version of liquidity universe

  • This version of liquidity universe is designed for the maximum speed of get_liquidity_fast() with (pair id, timestamp) parameter

  • Some of the speed is achieved by precomputing upsampling liquidity data to a longer frequency (day, week)

  • Some of the speed is achieved by using extra indexes and memory

  • Some of the speed is achieved by forward filling any data gaps, so we do not need to worry about the sparse data

The speedup difference between :py:meth`LiquidityUniverse.get_liquidity_with_tolerance` and py:meth:get_liquidity_fast is more than 10x. The practice speed increase for multipair liquidity aware strategy backtest can be 2x - 3x.

Note

The resampling itself will take some time, so optimally the resampled data should be stored on-disk cache between different backtest runs

__init__(df, column='close', resample_period='1D', resample_method='min')[source]#

Calculate optimised liquidity universe.

  • Only extract the column we are interested in

  • Resample to a smaller data

  • Resample by the

Note

For a large number of pairs, resampling will take a long time. You should remove any unnecessary pairs in df before creating a resampled version.

Parameters:
  • df (DataFrame) –

    Liquidity dataframe.

    You should prepare this by dropping any unwanted pairs.

  • column

    Which column value we use for the resampled series.

    Defaults to (daily) close liquidity value.

  • resample_period

    Pandas frequency alias string for the new timeframe duration.

    E.g. 1D for daily.

    Must be a fixed frequency e.g. 30D instead of M.

    See https://stackoverflow.com/a/35339226/315168

  • resample_method – How to we resample the liquidity

Methods

__init__(df[, column, resample_period, ...])

Calculate optimised liquidity universe.

get_liquidity_fast(pair_id, when)

Get the available liquidity for a trading pair at a specific time point.

get_samples_by_pair(pair_id)

Access and cache the resampled liquidity data per pair.

__init__(df, column='close', resample_period='1D', resample_method='min')[source]#

Calculate optimised liquidity universe.

  • Only extract the column we are interested in

  • Resample to a smaller data

  • Resample by the

Note

For a large number of pairs, resampling will take a long time. You should remove any unnecessary pairs in df before creating a resampled version.

Parameters:
  • df (DataFrame) –

    Liquidity dataframe.

    You should prepare this by dropping any unwanted pairs.

  • column

    Which column value we use for the resampled series.

    Defaults to (daily) close liquidity value.

  • resample_period

    Pandas frequency alias string for the new timeframe duration.

    E.g. 1D for daily.

    Must be a fixed frequency e.g. 30D instead of M.

    See https://stackoverflow.com/a/35339226/315168

  • resample_method – How to we resample the liquidity

get_samples_by_pair(pair_id)[source]#

Access and cache the resampled liquidity data per pair.

Parameters:

pair_id (int) –

Return type:

DataFrame

get_liquidity_fast(pair_id, when)[source]#

Get the available liquidity for a trading pair at a specific time point.

The liquidity is read from an upsampled buffer.

The lookup is fast because any timestamp is rounded down to the nearest frequency full timestamp and looked up there.

Parameters:
  • pair_id (int) – Trading pair id

  • when (Timestamp) – Timestamp to query

Returns:

Return available liquidty or 0 if no liquidity was seen before the timestamp.

Return type:

float