Market History Data

class tradeforce.market.history.MarketHistory(root, path_current=None)

Central unit for fetching and storing market history data.

Keeps a local cache of the candle history data in memory (pd.DataFrame) and on disk (feather file). Provides methods for data retrieval, updates, and caching. Interacts with various backends via interfaces: Currently MongoDB and PostgreSQL. Bitfinex is the only supported exchange at the moment.

Parameters:
  • root (Tradeforce) – The main Tradeforce instance. Provides access to config, logger or any other module.

  • path_current (str) – The current working directory. Defaults to the directory of execution.

get_market_history(assets=None, start=None, end=None, from_list=None, latest_candle=False, idx_type='loc', metrics=None, pct_change=False, pct_as_factor=True, pct_first_row=None, fill_na=False, uniform_cols=False)

Retrieve (a filtered subset of) the market history data as a DataFrame.

Parameters:
  • assets (Optional[list[str]]) – A list of asset symbols to include in the result. Defaults to all available assets.

  • start (Optional[int]) – The start index or timestamp for the data.

  • end (Optional[int]) – The end index or timestamp for the data.

  • from_list (Optional[ndarray]) – A NumPy array of indexes or timestamps to select from.

  • latest_candle (bool) – If True, return only the latest candle.

  • idx_type (str) – Indexing type to use. Either “loc” (default) or “iloc”.

  • metrics (Optional[list]) – A list of metrics to include in the result. Defaults to [“o”, “h”, “l”, “c”, “v”].

  • pct_change (bool) – If True, return percentage change values.

  • pct_as_factor (bool) – If True, return percentage change as a factor (1.0 + pct_change).

  • pct_first_row (UnionType[int, float, None]) – The first row value to use when calculating percentage change.

  • fill_na (bool) – If True, fill NaN values in the DataFrame.

  • uniform_cols (bool) – If True, return a DataFrame with uniform column naming.

Return type:

DataFrame

Returns:

A DataFrame containing the filtered market history data.

async load_history()

Load market history data using a specified method or try all available methods.

Attempts to load market history data from one of the following sources: local cache, backend, or API. The source can be forced by the configuration settings. If the source is set to “none”, it will try all available methods in the mentioned order. After successfully loading the history, it runs the post-processing.

Returns:

The loaded market history data as a DataFrame.

Return type:

pd.DataFrame

Raises:

SystemExit – If the data could not be loaded using the specified method or any method when trying all available methods.