resample_candles#

API documentation for tradingstrategy.utils.groupeduniverse.resample_candles Python function.

resample_candles(df, resample_freq, shift=None)[source]#

Downsample or upsample OHLCV candles or liquidity samples.

E.g. upsample 1h candles to 1d candles.

See also: py:func:resample_price_series.

Example:

# Transform daily candles to monthly candles
from tradingstrategy.utils.groupeduniverse import resample_candles

single_pair_candles = raw_candles.loc[raw_candles["pair_id"] == pair.pair_id]
single_pair_candles = single_pair_candles.set_index("timestamp", drop=False)
monthly_candles = resample_candles(single_pair_candles, TimeBucket.d30)
monthly_candles = resample_candles(single_pair_candles, TimeBucket.d30)
assert len(monthly_candles) <= len(single_pair_candles) / 4
Parameters:
  • df (DataFrame) –

    DataFrame of price, liquidity or lending rate candles.

    Must contain candles only for a single trading pair.

    Supported columns: open, high, low, close. Optional: pair_id, volume.

    Any other columns in DataFrame are destroyed in the resampling process.

  • resample_freq (Timedelta) –

    Resample frequency.

    E.g.`pd.Timedelta(days=1)` create daily candles from hourly candles.

  • shift (Optional[int]) –

    Before resampling, shift candles to left or right.

    The shift is measured in number of candles, not time. Make sure the DataFrame is forward filled first, see forward_fill().

    Set to 1 to shift candles one step right, -1 to shift candles one step left.

    There might not be enough rows to shift. E.g. shift=-1 or shift=1 and len(df) == 1. In this case, an empty data frame is returned.

Returns:

Resampled candles in a new DataFrame.

Contains an added timestamp column that is also the index.

If the input DataFrame is zero-length, then return it as is.

Return type:

DataFrame