Skip to content

Exchange ohlcv

ExchangeOHLCV

fetch_historical_ohlcv

fetch_historical_ohlcv(contract: Contract, timeframe='1d', pages=3, reload=False) -> Bars

Fetches historical OHLCV (Open, High, Low, Close, Volume) data for a specified contract.

This asynchronous method retrieves the specified number of historical candle data pages for a given contract and timeframe. It can be used for various candle manipulation operations.

Attributes:

Name Type Description
contract Contract

The contract for which historical data is being fetched.

timeframe str

The time interval for the OHLCV data (default is "1d").

pages int

The number of pages of data to fetch (default is 3).

reload bool

Flag indicating whether to reload data if it exists (default is False).

Returns:

Name Type Description
Bars Bars

An instance of Bars containing the historical OHLCV data.

Examples:

>>> bars = await fetch_historical_ohlcv(contract, "1h", pages=5)
>>> print(bars)
Source code in src/tradingtoolbox/exchanges/exchange_ohlcv.py
async def fetch_historical_ohlcv(
    self, contract: Contract, timeframe="1d", pages=3, reload=False
) -> Bars:
    """
    Fetches historical OHLCV (Open, High, Low, Close, Volume) data for a specified contract.

    This asynchronous method retrieves the specified number of historical candle data pages
    for a given contract and timeframe. It can be used for various candle manipulation operations.

    Attributes:
        contract (Contract): The contract for which historical data is being fetched.
        timeframe (str): The time interval for the OHLCV data (default is "1d").
        pages (int): The number of pages of data to fetch (default is 3).
        reload (bool): Flag indicating whether to reload data if it exists (default is False).

    Returns:
        Bars: An instance of Bars containing the historical OHLCV data.

    Examples:
        >>> bars = await fetch_historical_ohlcv(contract, "1h", pages=5)
        >>> print(bars)
    """
    file_name = (
        f"./data/{self.exchange_name}_{contract.id}_{timeframe}_ohlcv.parquet"
    )
    cache = Cache(
        file_format=FileFormat.PARQUET,
        dump_format=FileFormat.NONE,
        cache_path=file_name,
        method=self._fetch_historical_ohlcv,
    )
    data = await cache.get_async(
        reload=reload, contract=contract, timeframe=timeframe, pages=pages
    )

    bars = Bars()
    for i in range(len(data)):
        candle = data.iloc[i]
        bar = OHLCV(
            contract.symbol,
            candle.timestamp_ms,
            candle.open,
            candle.high,
            candle.low,
            candle.close,
            candle.volume,
        )
        bars.add_ohlcv(bar)

    return bars