PresetDataProvider
- class qf_lib.data_providers.preset_data_provider.PresetDataProvider(data: QFDataArray, start_date: datetime, end_date: datetime, frequency: Frequency, exp_dates: Dict[FutureTicker, QFDataFrame] = None, timer: Optional[Timer] = None)[source]
Bases:
AbstractPriceDataProvider,FuturesDataProviderWrapper on QFDataArray which makes it a DataProvider.
- Parameters:
data – data to be wrapped, indexed by date, (specific) tickers and fields
start_date – beginning of the cached period (not necessarily the first date in the data)
end_date – end of the cached period (not necessarily the last date in the data)
frequency – frequency of the data
exp_dates – dictionary mapping FutureTickers to QFDataFrame of contracts expiration dates, belonging to the certain future ticker family
Methods:
expiration_date_field_str_map([ticker])Method has to be implemented in each data provider in order to be able to use get_futures_chain_tickers.
get_futures_chain_tickers(tickers, ...)Returns tickers of futures contracts, which belong to the same futures contract chain as the provided ticker (tickers), along with their expiration dates in form of a QFSeries or QFDataFrame.
get_history(tickers, fields, start_date[, ...])Gets historical attributes (fields) of different securities (tickers).
get_price(tickers, fields, start_date[, ...])Gets adjusted historical Prices (Open, High, Low, Close) and Volume
historical_price(tickers, fields, nr_of_bars)Returns the latest available data samples, which simply correspond to the last available <nr_of_bars> number of bars.
Method has to be implemented in each data provider in order to be able to use get_price.
Returns classes of tickers which are supported by this DataProvider.
- expiration_date_field_str_map(ticker: Ticker = None) Dict[ExpirationDateField, str][source]
Method has to be implemented in each data provider in order to be able to use get_futures_chain_tickers. Returns dictionary containing mapping between ExpirationDateField and corresponding string that has to be used by get_futures_chain_tickers method.
- Parameters:
ticker (None, Ticker) – ticker is optional and might be uses by particular data providers to create appropriate dictionary
- Returns:
mapping between ExpirationDateField and corresponding strings
- Return type:
Dict[ExpirationDateField, str]
- get_futures_chain_tickers(tickers: Union[FutureTicker, Sequence[FutureTicker]], expiration_date_fields: Union[ExpirationDateField, Sequence[ExpirationDateField]]) Dict[FutureTicker, Union[QFSeries, QFDataFrame]][source]
Returns tickers of futures contracts, which belong to the same futures contract chain as the provided ticker (tickers), along with their expiration dates in form of a QFSeries or QFDataFrame.
- Parameters:
tickers (FutureTicker, Sequence[FutureTicker]) – tickers for which should the future chain tickers be retrieved
expiration_date_fields (ExpirationDateField, Sequence[ExpirationDateField]) – field that should be downloaded as the expiration date field, by default last tradeable date
- Returns:
Returns a dictionary, which maps Tickers to QFDataFrame, consisting of the expiration dates of Future Contracts: Dict[FutureTicker, QFDataFrame]]. The QFDataFrames contain the specific Tickers, which belong to the corresponding futures family, same as the FutureTicker, and are indexed by the expiration dates of the specific future contracts.
- Return type:
Dict[FutureTicker, QFDataFrame]
- get_history(tickers: Union[Ticker, Sequence[Ticker]], fields: Union[Any, Sequence[Any]], start_date: datetime, end_date: datetime = None, frequency: Frequency = None, look_ahead_bias: bool = False, **kwargs) Union[QFSeries, QFDataFrame, QFDataArray][source]
Gets historical attributes (fields) of different securities (tickers).
- Parameters:
tickers (Ticker, Sequence[Ticker]) – tickers for securities which should be retrieved
fields (None, str, Sequence[str]) – fields of securities which should be retrieved.
start_date (datetime) – date representing the beginning of historical period from which data should be retrieved
end_date (datetime) – date representing the end of historical period from which data should be retrieved; if no end_date was provided, by default the current date will be used
frequency (Frequency) – frequency of the data
look_ahead_bias (bool) – if set to False, the look-ahead bias will be taken care of to make sure no future data is returned
kwargs – kwargs should not be used on the level of AbstractDataProvider. They are here to provide a common interface for all data providers since some of the specific data providers accept additional arguments
- Returns:
If possible the result will be squeezed, so that instead of returning a QFDataArray, data of lower dimensionality will be returned. The results will be either a QFDataArray (with 3 dimensions: date, ticker, field), a QFDataFrame (with 2 dimensions: date, ticker or field; it is also possible to get 2 dimensions ticker and field if single date was provided), a QFSeries (with 1 dimensions: date) or a float / str (in case if a single ticker, field and date were provided). If no data is available in the database or a non existing ticker was provided an empty structure (nan, QFSeries, QFDataFrame or QFDataArray) will be returned.
- Return type:
QFSeries, QFDataFrame, QFDataArray, float, str
- get_price(tickers: Union[Ticker, Sequence[Ticker]], fields: Union[PriceField, Sequence[PriceField]], start_date: datetime, end_date: datetime = None, frequency: Frequency = None, look_ahead_bias: bool = False, **kwargs) Union[None, PricesSeries, PricesDataFrame, QFDataArray][source]
Gets adjusted historical Prices (Open, High, Low, Close) and Volume
- Parameters:
tickers (Ticker, Sequence[Ticker]) – tickers for securities which should be retrieved
fields (PriceField, Sequence[PriceField]) – fields of securities which should be retrieved
start_date (datetime) – date representing the beginning of historical period from which data should be retrieved
end_date (datetime) – date representing the end of historical period from which data should be retrieved; if no end_date was provided, by default the current date will be used
frequency (Frequency) – frequency of the data
look_ahead_bias (False) – if set to False, no future data will be ever returned
- Returns:
If possible the result will be squeezed so that instead of returning QFDataArray (3-D structure), data of lower dimensionality will be returned. The results will be either an QFDataArray (with 3 dimensions: dates, tickers, fields), PricesDataFrame (with 2 dimensions: dates, tickers or fields. It is also possible to get 2 dimensions ticker and field if single date was provided), or PricesSeries with 1 dimension: dates. All the containers will be indexed with PriceField whenever possible (for example: instead of ‘Close’ column in the PricesDataFrame there will be PriceField.Close)
- Return type:
None, PricesSeries, PricesDataFrame, QFDataArray
- historical_price(tickers: Union[Ticker, Sequence[Ticker]], fields: Union[PriceField, Sequence[PriceField]], nr_of_bars: int, end_date: Optional[datetime] = None, frequency: Frequency = None, **kwargs) Union[PricesSeries, PricesDataFrame, QFDataArray][source]
Returns the latest available data samples, which simply correspond to the last available <nr_of_bars> number of bars.
In case of intraday data and N minutes frequency, the most recent data may not represent exactly N minutes (if the whole bar was not available at this time). The time ranges are always aligned to the market open time. Non-zero seconds and microseconds are in the above case omitted (the output at 11:05:10 will be exactly the same as at 11:05).
- Parameters:
tickers (Ticker, Sequence[Ticker]) – ticker or sequence of tickers of the securities
fields (PriceField, Sequence[PriceField]) – PriceField or sequence of PriceFields of the securities
nr_of_bars (int) – number of data samples (bars) to be returned. Note: while requesting more than one ticker, some tickers may have fewer than n_of_bars data points
end_date (Optional[datetime]) – last date which should be considered in the query, the nr_of_bars that should be returned will always point to the time before end_date. The parameter is optional and if not provided, the end_date will point to the current user time.
frequency – frequency of the data
- Return type:
- price_field_to_str_map() Dict[PriceField, str][source]
Method has to be implemented in each data provider in order to be able to use get_price. Returns dictionary containing mapping between PriceField and corresponding string that has to be used by get_history method to get appropriate type of price series.
- Returns:
mapping between PriceFields and corresponding strings
- Return type:
Dict[PriceField, str]