GroupedLiquidityUniverse#

tradingstrategy.liquidity.GroupedLiquidityUniverse class.

class GroupedLiquidityUniverse[source]#

Bases: PairGroupedUniverse

A universe where each trading pair has its own liquidity data feed.

This is helper class to create foundation for multi pair strategies.

For the data logistics purposes, all candles are lumped together in single columnar data blobs. However, it rarely makes sense to execute operations over different trading pairs. :py:class`GroupedLiquidityUniverse` creates trading pair id -> liquidity sample data grouping out from raw liquidity sample.

__init__(df, time_bucket=TimeBucket.d1, timestamp_column='timestamp', index_automatically=True)#
Parameters
  • time_bucket – What bar size candles we are operating at. Default to daily. TODO: Deprecate - not used?

  • timestamp_column – What column use to build a time index. Used for QStrader / Backtrader compatibility.

  • index_automatically – Convert the index to use time series. You might avoid this with QSTrader kind of data.

  • df (DataFrame) –

Methods

__init__(df[, time_bucket, ...])

param time_bucket

create_empty()

Create a liquidity universe without any data.

get_all_pairs()

Go through all liquidity samples, one DataFrame per trading pair.

get_all_samples_by_range(start, end)

Get list of candles/samples for all pairs at a certain range.

get_all_samples_by_timestamp(ts)

Get list of candles/samples for all pairs at a certain timepoint.

get_closest_liquidity(pair_id, when[, kind, ...])

Get the available liuqidity for a trading pair at a specific timepoint or some candles before the timepoint.

get_columns()

Get column names from the underlying pandas.GroupBy object

get_liquidity_samples_by_pair(pair_id)

Get samples for a single pair.

get_pair_count()

Return the number of pairs in this dataset

get_pair_ids()

Get all pairs present in the dataset

get_prior_timestamp(ts)

Get the first timestamp in the index that is before the given timestamp.

get_sample_count()

Return the dataset size - how many samples total for all pairs

get_samples_by_pair(pair_id)

Get samples for a single pair.

get_single_pair_data([timestamp, sample_count])

Get all candles/liquidity samples for the single alone pair in the universe.

get_timestamp_range([use_timezone])

Return the time range of data we have for.

iterate_samples_by_pair_range(start, end)

Get list of candles/samples for all pairs at a certain range.

get_liquidity_samples_by_pair(pair_id)[source]#

Get samples for a single pair.

If the pair does not exist return None.

Parameters

pair_id (PrimaryKey) –

Return type

Optional[DataFrame]

get_closest_liquidity(pair_id, when, kind='open', look_back_time_frames=5)[source]#

Get the available liuqidity for a trading pair at a specific timepoint or some candles before the timepoint.

The liquidity is defined as one-sided as in XY liquidity model.

Parameters
  • pair_id (PrimaryKey) – Traing pair id

  • when (Timestamp) – Timestamp to query

  • kind – One of liquidity samples: “open”, “close”, “low”, “high”

  • look_back_timeframes – If there is no liquidity sample available at the exact timepoint, look to the past to the get the nearest sample

Returns

We always return

Raises

LiquidityDataUnavailable – There was no liquidity sample available

Return type

USDollarAmount

static create_empty()[source]#

Create a liquidity universe without any data.

Return type

GroupedLiquidityUniverse