CachedHTTPTransport#

API documentation for tradingstrategy.transport.cache.CachedHTTPTransport Python class in Trading Strategy framework.

class CachedHTTPTransport[source]#

Bases: object

A HTTP API transport that offers optional local caching of the results.

  • Download live and cached datasets from the candle server and cache locally on the filesystem

  • The download files are very large and expect to need several gigabytes of space for them

  • Has a default HTTP retry policy in the case network or server flakiness

__init__(download_func, endpoint=None, cache_period=datetime.timedelta(days=3), cache_path=None, api_key=None, timeout=15.0, add_exception_hook=True, retry_policy=None)[source]#
Parameters:
  • download_func (Callable) – Interactive download progress bar displayed during the download

  • endpoint (Optional[str]) – API server we are using - default is https://tradingstrategy.ai/api

  • cache_period – How many days we store the downloaded files

  • cache_path (Optional[str]) – Where we store the downloaded files

  • api_key (Optional[str]) – Trading Strategy API key to use download

  • timeout (float) – requests HTTP lib timeout

  • add_exception_hook – Automatically raise an error in the case of HTTP error. Prevents auto retries.

  • retry_policy (Optional[Retry]) – How to handle failed HTTP requests. If not given use the default somewhat graceful retry policy.

Methods

__init__(download_func[, endpoint, ...])

param download_func:

Interactive download progress bar displayed during the download

close()

Release any underlying sockets.

create_requests_client([retry_policy, ...])

Create HTTP 1.1 keep-alive connection to the server with optional authorization details.

fetch_candles_all_time(bucket)

Load candles and return a cached file where they are stored.

fetch_candles_by_pair_ids(pair_ids, time_bucket)

Load particular set of the candles and cache the result.

fetch_chain_status(chain_id)

Not cached.

fetch_clmm_liquidity_provision_candles_by_pair_ids(...)

Stream CLMM Parquet data from the server.

fetch_exchange_universe()

fetch_lending_candles_by_reserve_id(...[, ...])

Load particular set of the lending candles and cache the result.

fetch_lending_reserve_universe()

fetch_lending_reserves_all_time()

fetch_liquidity_all_time(bucket)

fetch_pair_universe()

fetch_top_pairs(chain_ids, exchange_slugs, ...)

Not cached.

fetch_trading_data_availability(pair_ids, ...)

Check the trading data availability at oracle's real time market feed endpoint.

fetch_tvl_by_pair_ids(pair_ids, time_bucket)

Load particular set of the TVL candles and cache the result.

get_abs_cache_path()

get_cached_file_path(fname)

get_cached_item(fname)

Get a cached file.

get_cached_item_with_status(fname)

Get a cached file.

get_json_response(api_path[, params])

message_of_the_day()

ping()

post_json_response(api_path[, params])

purge_cache([filename])

Delete all cached files on the filesystem.

register(first_name, last_name, email)

Makes a register request.

save_response(fpath, api_path[, params, ...])

Download a file to the cache and display a pretty progress bar while doing it.

__init__(download_func, endpoint=None, cache_period=datetime.timedelta(days=3), cache_path=None, api_key=None, timeout=15.0, add_exception_hook=True, retry_policy=None)[source]#
Parameters:
  • download_func (Callable) – Interactive download progress bar displayed during the download

  • endpoint (Optional[str]) – API server we are using - default is https://tradingstrategy.ai/api

  • cache_period – How many days we store the downloaded files

  • cache_path (Optional[str]) – Where we store the downloaded files

  • api_key (Optional[str]) – Trading Strategy API key to use download

  • timeout (float) – requests HTTP lib timeout

  • add_exception_hook – Automatically raise an error in the case of HTTP error. Prevents auto retries.

  • retry_policy (Optional[Retry]) – How to handle failed HTTP requests. If not given use the default somewhat graceful retry policy.

close()[source]#

Release any underlying sockets.

create_requests_client(retry_policy=None, api_key=None, add_exception_hook=True)[source]#

Create HTTP 1.1 keep-alive connection to the server with optional authorization details.

Parameters:
  • add_exception_hook – Automatically raise an error in the case of HTTP error

  • retry_policy (Optional[Retry]) –

  • api_key (Optional[str]) –

get_cached_item(fname)[source]#

Get a cached file.

  • Return None if the cache has expired

  • The cache timeout is coded in the file modified timestamp (mtime)

Parameters:

fname (Union[str, Path]) –

Return type:

Optional[Path]

get_cached_item_with_status(fname)[source]#

Get a cached file.

  • Return None if the cache has expired

  • The cache timeout is coded in the file modified timestamp (mtime)

Parameters:

fname (Union[str, Path]) –

Return type:

Tuple[pathlib.Path | None, CacheStatus]

purge_cache(filename=None)[source]#

Delete all cached files on the filesystem.

Parameters:

filename (Optional[Union[str, Path]]) – If given, remove only that specific file, otherwise clear all cached data.

save_response(fpath, api_path, params=None, human_readable_hint=None)[source]#

Download a file to the cache and display a pretty progress bar while doing it.

Parameters:
  • fpath – File system path where the download will be saved

  • api_path – Which Trading Strategy backtesting API we call to download the dataset.

  • params – HTTP request params, like the Authorization header

  • human_readable_hint (Optional[str]) – The status text displayed on the progress bar what’s being downloaded

fetch_chain_status(chain_id)[source]#

Not cached.

Parameters:

chain_id (int) –

Return type:

dict

fetch_top_pairs(chain_ids, exchange_slugs, addresses, method, min_volume_24h_usd=None, limit=None)[source]#

Not cached.

Parameters:
Return type:

dict

fetch_candles_all_time(bucket)[source]#

Load candles and return a cached file where they are stored.

  • If cached file exists return it directly

  • Wait if someone else is writing the file (in multiple parallel testers)

Parameters:

bucket (TimeBucket) –

Return type:

Path

fetch_lending_candles_by_reserve_id(reserve_id, time_bucket, candle_type=LendingCandleType.variable_borrow_apr, start_time=None, end_time=None)[source]#

Load particular set of the lending candles and cache the result.

For the candles format see tradingstrategy.lending.

Parameters:
  • reserve_id (int) – Lending reserve’s internal id we query data for. Get internal id from lending reserve universe dataset.

  • time_bucket (TimeBucket) – Candle time frame.

  • candle_type (LendingCandleType) – Lending candle type.

  • start_time (Optional[datetime]) – All candles after this. If not given start from genesis.

  • end_time (Optional[datetime]) – All candles before this

Returns:

Lending candles dataframe

Return type:

DataFrame

register(first_name, last_name, email)[source]#

Makes a register request.

The request does not load any useful payload, but it is assumed the email message gets verified and the user gets the API from the email.

Return type:

dict

fetch_candles_by_pair_ids(pair_ids, time_bucket, start_time=None, end_time=None, max_bytes=None, progress_bar_description=None)[source]#

Load particular set of the candles and cache the result.

If there is no cached result, load using JSONL.

More information in tradingstrategy.transport.jsonl.

For the candles format see tradingstrategy.candle.

Parameters:
  • pair_ids (Collection[int]) – Trading pairs internal ids we query data for. Get internal ids from pair dataset.

  • time_bucket (TimeBucket) – Candle time frame

  • start_time (Optional[datetime]) – All candles after this. If not given start from genesis.

  • end_time (Optional[datetime]) – All candles before this

  • max_bytes (Optional[int]) – Limit the streaming response size

  • progress_bar_description (Optional[str]) – Display on downlood progress bar

Returns:

Candles dataframe

Return type:

DataFrame

fetch_tvl_by_pair_ids(pair_ids, time_bucket, start_time=None, end_time=None, progress_bar_description=None)[source]#

Load particular set of the TVL candles and cache the result.

For the candles format see tradingstrategy.liquidity.

Parameters:
  • pair_ids (Collection[int]) –

    Trading pairs internal ids we query data for. Get internal ids from pair dataset.

    We should be able to handle unlimited pair count, as we do one request per pair.

  • time_bucket (TimeBucket) – Candle time frame

  • start_time (Optional[datetime]) – All candles after this. If not given start from genesis.

  • end_time (Optional[datetime]) – All candles before this

  • progress_bar_description (Optional[str]) – Display on downlood progress bar

Returns:

Liquidity dataframe.

See tradingstrategy.liquidity.

Return type:

DataFrame

fetch_clmm_liquidity_provision_candles_by_pair_ids(pair_ids, time_bucket, start_time=None, end_time=None, progress_bar_description=None)[source]#

Stream CLMM Parquet data from the server.

For the candles format see tradingstrategy.clmm.

Parameters:
  • pair_ids (Collection[int]) –

    Trading pairs internal ids we query data for. Get internal ids from pair dataset.

    We should be able to handle unlimited pair count, as we do one request per pair.

  • time_bucket (TimeBucket) – Candle time frame

  • start_time (Optional[datetime]) – All candles after this. If not given start from genesis.

  • end_time (Optional[datetime]) – All candles before this

  • progress_bar_description (Optional[str]) – Display on downlood progress bar

Returns:

CLMM dataframe.

See tradingstrategy.clmm.

Return type:

DataFrame

fetch_trading_data_availability(pair_ids, time_bucket)[source]#

Check the trading data availability at oracle’s real time market feed endpoint.

  • Trading Strategy oracle uses sparse data format where candles with zero trades are not generated. This is better suited for illiquid DEX markets with few trades.

  • Because of sparse data format, we do not know if there is a last candle available - candle may not be available yet or there might not be trades to generate a candle

  • This endpoint allows to check the trading data availability for multiple of trading pairs.

  • This endpoint is public

Parameters:
  • pair_ids (Collection[int]) – Trading pairs internal ids we query data for. Get internal ids from pair dataset.

  • time_bucket (TimeBucket) – Candle time frame

Returns:

Map of pairs -> their trading data availability

Return type:

Dict[int, TradingPairDataAvailability]