pybroker.data module
Contains DataSource
s used to fetch external data.
- class Alpaca(api_key: str, api_secret: str)[source]
Bases:
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 [source]
Queries data. Cached data is returned if caching is enabled by calling
pybroker.cache.enable_data_source_cache()
.- Parameters:
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.
- Returns:
pandas.DataFrame
containing the queried data.
- class AlpacaCrypto(api_key: str, api_secret: str)[source]
Bases:
DataSource
Retrieves crypto data from Alpaca.
- Parameters:
api_key – Alpaca API key.
api_secret – Alpaca API secret.
- COLUMNS: Final = ('symbol', 'date', 'open', 'high', 'low', 'close', 'volume', 'vwap', 'trade_count')
- query(symbols: str | Iterable[str], start_date: str | datetime, end_date: str | datetime, timeframe: str | None = '1d', _: str | None = None) DataFrame [source]
Queries data. Cached data is returned if caching is enabled by calling
pybroker.cache.enable_data_source_cache()
.- Parameters:
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.
- Returns:
pandas.DataFrame
containing the queried data.
- class DataSource[source]
Bases:
ABC
,DataSourceCacheMixin
Base class for querying data from an external source. Extend this class and override
_fetch_data()
to implement a customDataSource
that can be used withpybroker.strategy.Strategy
.- abstract _fetch_data(symbols: frozenset[str], start_date: datetime, end_date: datetime, timeframe: str | None, adjust: Any | None) DataFrame [source]
Override this method to return data from a custom source. The returned
pandas.DataFrame
must contain the following columns:symbol
,date
,open
,high
,low
, andclose
.- Parameters:
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.
- Returns:
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 [source]
Queries data. Cached data is returned if caching is enabled by calling
pybroker.cache.enable_data_source_cache()
.- Parameters:
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.
- Returns:
pandas.DataFrame
containing the queried data.
- class DataSourceCacheMixin[source]
Bases:
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]] [source]
Retrieves cached data from disk when caching is enabled with
pybroker.cache.enable_data_source_cache()
.- Parameters:
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.
- Returns:
tuple[pandas.DataFrame, Iterable[str]]
containing apandas.DataFrame
with the cached data, and anIterable[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)[source]
Stores data to disk cache when caching is enabled with
pybroker.cache.enable_data_source_cache()
.- Parameters:
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[source]
Bases:
DataSource
Retrieves data from Yahoo Finance.
- ADJ_CLOSE
Column name of adjusted close prices.
- Type:
Final
- query(symbols: str | Iterable[str], start_date: str | datetime, end_date: str | datetime, _timeframe: str | None = '', _adjust: Any | None = None) DataFrame [source]
Queries data from Yahoo Finance. The timeframe of the data is limited to per day only.
- Parameters:
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).
- Returns:
pandas.DataFrame
containing the queried data.