GroupedLiquidityUniverse#
tradingstrategy.liquidity.GroupedLiquidityUniverse Python class in Trading Strategy framework.
- 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)[source]#
- Parameters:
time_bucket – What bar size candles we are operating at. Default to daily. TODO: Currently not used. Will be removed in the future versions.
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.
fix_wick_threshold –
Apply abnormal high/low wick fix filter.
Percent value of maximum allowed high/low wick relative to close. By default fix values where low is 90% lower than close and high is 90% higher than close.
See
tradingstrategy.utils.groupeduniverse.fix_bad_wicks()
for more information.df (DataFrame) –
Methods
__init__
(df[, time_bucket, ...])- param time_bucket:
clear_cache
()Clear candles cached by pair.
Create a liquidity universe without any data.
forward_fill
([columns, drop_other_columns])Forward-fill sparse OHLCV candle 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_last_entries_by_pair_and_timestamp
(...)Get samples for a single pair before a timestamp.
get_liquidity_samples_by_pair
(pair_id)Get samples for a single pair.
get_liquidity_with_tolerance
(pair_id, when, ...)Get the available liquidity for a trading pair at a specific time point/
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, ...])Get all candles/liquidity samples for the single alone pair in the universe by a certain timestamp.
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.
- __init__(df, time_bucket=TimeBucket.d1, timestamp_column='timestamp', index_automatically=True)[source]#
- Parameters:
time_bucket – What bar size candles we are operating at. Default to daily. TODO: Currently not used. Will be removed in the future versions.
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.
fix_wick_threshold –
Apply abnormal high/low wick fix filter.
Percent value of maximum allowed high/low wick relative to close. By default fix values where low is 90% lower than close and high is 90% higher than close.
See
tradingstrategy.utils.groupeduniverse.fix_bad_wicks()
for more information.df (DataFrame) –
- get_liquidity_samples_by_pair(pair_id)[source]#
Get samples for a single pair.
If the pair does not exist return None.
- get_liquidity_with_tolerance(pair_id, when, tolerance, kind='close')[source]#
Get the available liquidity for a trading pair at a specific time point/
The liquidity is defined as one-sided as in XY liquidity model.
Example:
liquidity_amount, last_trade_delay = liquidity_universe.get_liquidity_with_tolerance( sushi_usdt.pair_id, pd.Timestamp("2021-12-31"), tolerance=pd.Timedelta("1y"), ) assert liquidity_amount == pytest.approx(2292.4517) assert last_trade_delay == pd.Timedelta('4 days 00:00:00')
- Parameters:
pair_id (int) – Trading pair id
when (Timestamp) – Timestamp to query
kind – One of OHLC data points: “open”, “close”, “low”, “high”
tolerance (Timedelta) – If there is no liquidity sample available at the exact timepoint, look to the past to the get the nearest sample. For example if candle time interval is 5 minutes and look_back_timeframes is 10, then accept a candle that is maximum of 50 minutes before the timepoint.
- Returns:
Return (price, delay) tuple.
We always return a price. In the error cases an exception is raised. The delay is the timedelta between the wanted timestamp and the actual timestamp of the candle.
Candles are always timestamped by their opening.
- Raises:
LiquidityDataUnavailable –
There were no samples available with the given condition.
This can happen when
There has not been a single trade yet
There hasn’t been any trades since tolerance time window
- Return type:
- 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.
Warning
This is an early alpha method and has been deprecated. Please use
get_liquidity_with_tolerance()
instead.- Parameters:
- Returns:
We always return
- Raises:
LiquidityDataUnavailable – There was no liquidity sample available
- Return type: