pybroker.data module

Contains DataSources used to fetch external data.

class Alpaca(api_key: str, api_secret: str)[源代码]

基类:DataSource

Retrieves stock data from Alpaca.

query(symbols: str | Iterable[str], start_date: str | datetime, end_date: str | datetime, timeframe: str | None = '1d', adjust: Any | None = None) DataFrame[源代码]

Queries data. Cached data is returned if caching is enabled by calling pybroker.cache.enable_data_source_cache().

参数:
  • symbols -- Symbols of the data to query.

  • start_date -- Start date of the data to query (inclusive).

  • end_date -- End date of the data to query (inclusive).

  • timeframe --

    Formatted string that specifies the timeframe resolution to query. The timeframe string supports the following units:

    • "s"/"sec": seconds

    • "m"/"min": minutes

    • "h"/"hour": hours

    • "d"/"day": days

    • "w"/"week": weeks

    An example timeframe string is 1h 30m.

  • adjust -- The type of adjustment to make.

返回:

pandas.DataFrame containing the queried data.

class AlpacaCrypto(api_key: str, api_secret: str)[源代码]

基类:DataSource

Retrieves crypto data from Alpaca.

参数:
  • api_key -- Alpaca API key.

  • api_secret -- Alpaca API secret.

COLUMNS: Final = ('symbol', 'date', 'open', 'high', 'low', 'close', 'volume', 'vwap', 'trade_count')
TRADE_COUNT: Final = 'trade_count'
query(symbols: str | Iterable[str], start_date: str | datetime, end_date: str | datetime, timeframe: str | None = '1d', _adjust: str | None = None) DataFrame[源代码]

Queries data. Cached data is returned if caching is enabled by calling pybroker.cache.enable_data_source_cache().

参数:
  • symbols -- Symbols of the data to query.

  • start_date -- Start date of the data to query (inclusive).

  • end_date -- End date of the data to query (inclusive).

  • timeframe --

    Formatted string that specifies the timeframe resolution to query. The timeframe string supports the following units:

    • "s"/"sec": seconds

    • "m"/"min": minutes

    • "h"/"hour": hours

    • "d"/"day": days

    • "w"/"week": weeks

    An example timeframe string is 1h 30m.

  • adjust -- The type of adjustment to make.

返回:

pandas.DataFrame containing the queried data.

class DataSource[源代码]

基类:ABC, DataSourceCacheMixin

Base class for querying data from an external source. Extend this class and override _fetch_data() to implement a custom DataSource that can be used with pybroker.strategy.Strategy.

abstract _fetch_data(symbols: frozenset[str], start_date: datetime, end_date: datetime, timeframe: str | None, adjust: Any | None) DataFrame[源代码]

Override this method to return data from a custom source. The returned pandas.DataFrame must contain the following columns: symbol, date, open, high, low, and close.

参数:
  • symbols -- Ticker symbols of the data to query.

  • start_date -- Start date of the data to query (inclusive).

  • end_date -- End date of the data to query (inclusive).

  • timeframe --

    Formatted string that specifies the timeframe resolution to query. The timeframe string supports the following units:

    • "s"/"sec": seconds

    • "m"/"min": minutes

    • "h"/"hour": hours

    • "d"/"day": days

    • "w"/"week": weeks

    An example timeframe string is 1h 30m.

  • adjust -- The type of adjustment to make.

返回:

pandas.DataFrame containing the queried data.

query(symbols: str | Iterable[str], start_date: str | datetime, end_date: str | datetime, timeframe: str | None = '', adjust: Any | None = None) DataFrame[源代码]

Queries data. Cached data is returned if caching is enabled by calling pybroker.cache.enable_data_source_cache().

参数:
  • symbols -- Symbols of the data to query.

  • start_date -- Start date of the data to query (inclusive).

  • end_date -- End date of the data to query (inclusive).

  • timeframe --

    Formatted string that specifies the timeframe resolution to query. The timeframe string supports the following units:

    • "s"/"sec": seconds

    • "m"/"min": minutes

    • "h"/"hour": hours

    • "d"/"day": days

    • "w"/"week": weeks

    An example timeframe string is 1h 30m.

  • adjust -- The type of adjustment to make.

返回:

pandas.DataFrame containing the queried data.

class DataSourceCacheMixin[源代码]

基类:object

Mixin that implements fetching and storing cached DataSource data.

get_cached(symbols: Iterable[str], timeframe: str, start_date: str | datetime | Timestamp | datetime64, end_date: str | datetime | Timestamp | datetime64, adjust: Any | None) tuple[DataFrame, Iterable[str]][源代码]

Retrieves cached data from disk when caching is enabled with pybroker.cache.enable_data_source_cache().

参数:
  • symbols -- Iterable of symbols for fetching cached data.

  • timeframe --

    Formatted string that specifies the timeframe resolution of the cached data. The timeframe string supports the following units:

    • "s"/"sec": seconds

    • "m"/"min": minutes

    • "h"/"hour": hours

    • "d"/"day": days

    • "w"/"week": weeks

    An example timeframe string is 1h 30m.

  • start_date -- Starting date of the cached data (inclusive).

  • end_date -- Ending date of the cached data (inclusive).

  • adjust -- The type of adjustment to make.

返回:

tuple[pandas.DataFrame, Iterable[str]] containing a pandas.DataFrame with the cached data, and an Iterable[str] of symbols for which no cached data was found.

set_cached(timeframe: str, start_date: str | datetime | Timestamp | datetime64, end_date: str | datetime | Timestamp | datetime64, adjust: Any | None, data: DataFrame)[源代码]

Stores data to disk cache when caching is enabled with pybroker.cache.enable_data_source_cache().

参数:
  • timeframe --

    Formatted string that specifies the timeframe resolution of the data to cache. The timeframe string supports the following units:

    • "s"/"sec": seconds

    • "m"/"min": minutes

    • "h"/"hour": hours

    • "d"/"day": days

    • "w"/"week": weeks

    An example timeframe string would be 1h 30m.

  • start_date -- Starting date of the data to cache (inclusive).

  • end_date -- Ending date of the data to cache (inclusive).

  • adjust -- The type of adjustment to make.

  • data -- pandas.DataFrame containing the data to cache.

class YFinance(auto_adjust: bool = False)[源代码]

基类:DataSource

Retrieves data from Yahoo Finance.

参数:

auto_adjust -- Whether to auto adjust close prices. If True, then adjusted close prices are stored in the close column. Defaults to False.

ADJ_CLOSE

Column name of adjusted close prices.

Type:

Final

ADJ_CLOSE: Final = 'adj_close'
query(symbols: str | Iterable[str], start_date: str | datetime, end_date: str | datetime, _timeframe: str | None = '', _adjust: Any | None = None) DataFrame[源代码]

Queries data from Yahoo Finance. The timeframe of the data is limited to per day only.

参数:
  • symbols -- Ticker symbols of the data to query.

  • start_date -- Start date of the data to query (inclusive).

  • end_date -- End date of the data to query (inclusive).

返回:

pandas.DataFrame containing the queried data.