PairCandleMetadata#

API documentation for tradingstrategy.transport.pair_candle_cache.PairCandleMetadata Python class in Trading Strategy framework.

class PairCandleMetadata[source]#

Bases: object

Utility class for serialising / deserialising pair candle metadata

The metadata file stores a map of pair_ids, with start_time and end_time per pair_id. These values represent the start and end dates of candles that have been fetched.

The metadata dates may be different than the earliest or latest candle entries for the corresponding pair - e.g. if the pair’s trading activity began at a date later than the earliest requested start date.

__init__(pairs=<factory>, _file_path='', _last_modified_at=None)#
Parameters:
Return type:

None

Methods

__init__([pairs, _file_path, _last_modified_at])

delta_fetch_start_time([lookback_hours])

Calculate the start time for delta fetches, accounting for data freshness.

from_dict(kvs, *[, infer_missing])

from_json(s, *[, parse_float, parse_int, ...])

latest_end_time()

Latest end_time value across all pairs (or None)

load(fname)

Load metadata from file, or create empty if doesn't exist

partition_for_fetch(pair_ids, start_time, ...)

Partition pair IDs into full fetch vs.

save()

Save metadata to the file it was loaded from

schema(*[, infer_missing, only, exclude, ...])

to_dict([encode_json])

to_json(*[, skipkeys, ensure_ascii, ...])

update(pair_ids, start_time, end_time)

Update pair entries with new start_time and end_time

Attributes

pairs

last_modified_at

Get the modification time when this metadata was loaded, or None if newly created

classmethod load(fname)[source]#

Load metadata from file, or create empty if doesn’t exist

Parameters:

fname (str) – File location where metadata will be loaded from (if present) and saved to

Return type:

PairCandleMetadata

latest_end_time()[source]#

Latest end_time value across all pairs (or None)

Return type:

datetime.datetime | None

property last_modified_at: datetime.datetime | None#

Get the modification time when this metadata was loaded, or None if newly created

save()[source]#

Save metadata to the file it was loaded from

Return type:

None

update(pair_ids, start_time, end_time)[source]#

Update pair entries with new start_time and end_time

If the existing entry has an earlier start or later end, the furthest extent values are retained.

Parameters:
  • pair_ids (Collection[int]) – Trading pairs internal ids that are included in the pair candle cache.

  • start_time (datetime) – The new start_time that candles were fetched with.

  • end_time (datetime) – The new end_time that candles were fetched with.

Return type:

None

partition_for_fetch(pair_ids, start_time, end_time)[source]#

Partition pair IDs into full fetch vs. delta fetch requirements.

The partition is determined based on the existing cache metadata and the requested start and end times.

Parameters:
  • pair_ids (Collection[int]) – Trading pairs internal ids to be partitioned (may or may not be in cache).

  • start_time (datetime) – The new start_time that candles will be fetched with.

  • end_time (datetime) – The new end_time that candles will be fetched with.

Returns:

PairFetchPartition - named tuple of full_fetch_ids, delta_fetch_ids

Return type:

PairFetchPartition

delta_fetch_start_time(lookback_hours=48)[source]#

Calculate the start time for delta fetches, accounting for data freshness.

Returns the earlier of: - last_modified_at minus lookback window (to refetch potentially incomplete data) - latest_end_time (to avoid gaps)

Returns None if metadata has never been saved.

Parameters:

lookback_hours (int) –

Return type:

datetime.datetime | None

__init__(pairs=<factory>, _file_path='', _last_modified_at=None)#
Parameters:
Return type:

None