BloombergDataProvider

class qf_lib.data_providers.bloomberg.bloomberg_data_provider.BloombergDataProvider(settings: Settings, timer: Optional[Timer] = None)[source]

Bases: AbstractPriceDataProvider, TickersUniverseProvider, FuturesDataProvider, ExchangeRateProvider

Data Provider which provides financial data from Bloomberg.

Methods:

connect()

Connects to Bloomberg data service and holds a connection.

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_current_values(tickers, fields[, overrides])

Gets the current values of fields for given tickers.

get_history(tickers, fields, start_date[, ...])

Gets historical data from Bloomberg from the (start_date - end_date) time range.

get_last_available_exchange_rate(...[, ...])

Get last available exchange rate from the base currency to the quote currency in the provided frequency.

get_tabular_data(ticker, field[, overrides])

Provides current tabular data from Bloomberg.

get_tickers_universe(universe_ticker[, ...])

Returns a list of all members of an index.

get_tickers_universe_with_weights(...[, ...])

Returns the tickers belonging to a specified universe, along with their corresponding weights, at a given date.

get_unique_tickers(universe_ticker)

Retrieves a list of unique tickers that belong to the specified universe, regardless of the date.

price_field_to_str_map()

Method has to be implemented in each data provider in order to be able to use get_price.

supported_ticker_types()

Returns classes of tickers which are supported by this DataProvider.

connect()[source]

Connects to Bloomberg data service and holds a connection. Connecting might take about 10-15 seconds

expiration_date_field_str_map(ticker: BloombergTicker = 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_current_values(tickers: Union[BloombergTicker, Sequence[BloombergTicker]], fields: Union[str, Sequence[str]], overrides: Optional[Dict[str, str]] = None) Union[None, float, str, QFSeries, QFDataFrame][source]

Gets the current values of fields for given tickers.

Parameters:
  • tickers (BloombergTicker, Sequence[BloombergTicker]) – tickers for securities which should be retrieved

  • fields (str, Sequence[str]) – fields of securities which should be retrieved

Returns:

  • QFDataFrame/QFSeries – Either QFDataFrame with 2 dimensions: ticker, field or QFSeries with 1 dimensions: ticker of field (depending if many tickers or fields was provided) is returned.

  • overrides (Optional[Dict[str, str]]) – A dictionary where each key is a field name (as a string) that corresponds to a default field in the Bloomberg request, and the value is the new value (as a string) to override the default value for that field. The dictionary allows for multiple fields to be overridden at once, with each key representing a specific field to be modified, and the associated value specifying the replacement value for that field. If not provided, the default values for all fields will be used.

Raises:

BloombergError – When couldn’t get the data from Bloomberg Service

get_history(tickers: Union[BloombergTicker, Sequence[BloombergTicker]], fields: Union[str, Sequence[str]], start_date: datetime, end_date: datetime = None, frequency: Frequency = None, currency: str = None, overrides: Optional[Dict] = None, look_ahead_bias: bool = False, **kwargs) Union[QFSeries, QFDataFrame, QFDataArray][source]

Gets historical data from Bloomberg from the (start_date - end_date) time range. In case of frequency, which is higher than daily frequency (intraday data), the data is indexed by the start_date. E.g. Time range: 8:00 - 8:01, frequency: 1 minute - indexed with the 8:00 timestamp

Parameters:
  • tickers (Ticker, Sequence[Ticker]) – Tickers’ of securities which should be retrieved.

  • fields (None, str, Sequence[str]) – Fields of securities which should be retrieved. If None, all available fields will be returned (only supported by few DataProviders).

  • 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. It defaults to DAILY.

  • currency (str) –

  • overrides (Optional[Dict[str, str]]) – A dictionary where each key is a field name (as a string) that corresponds to a default field in the Bloomberg request, and the value is the new value (as a string) to override the default value for that field. The dictionary allows for multiple fields to be overridden at once, with each key representing a specific field to be modified, and the associated value specifying the replacement value for that field. If not provided, the default values for all fields will be used.

  • 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.

Returns:

If possible the result will be squeezed, so that instead of returning QFDataArray, data of lower dimensionality will be returned. The results will be either an 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) or QFSeries (with 1 dimensions: date). If no data is available in the database or a non existing ticker was provided an empty structure (QFSeries, QFDataFrame or QFDataArray) will be returned returned.

Return type:

QFSeries, QFDataFrame, QFDataArray

get_last_available_exchange_rate(base_currency: str, quote_currency: str, frequency: Frequency = Frequency.DAILY)[source]

Get last available exchange rate from the base currency to the quote currency in the provided frequency.

Parameters:
  • base_currency (str) – ISO code of the base currency (ex. ‘USD’ for US Dollar)

  • quote_currency (str) – ISO code of the quote currency (ex. ‘EUR’ for Euro)

  • frequency (Frequency) – frequency of the returned data

Returns:

last available exchange rate

Return type:

float

get_tabular_data(ticker: BloombergTicker, field: str, overrides: Optional[Dict[str, str]] = None) List[source]

Provides current tabular data from Bloomberg. It is a wrapper around get_current_values.

Parameters:
  • ticker (BloombergTicker) – ticker for security that should be retrieved

  • field (str) – field of security that should be retrieved

  • overrides (Optional[Dict[str, str]]) – A dictionary where each key is a field name (as a string) that corresponds to a default field in the Bloomberg request, and the value is the new value (as a string) to override the default value for that field. The dictionary allows for multiple fields to be overridden at once, with each key representing a specific field to be modified, and the associated value specifying the replacement value for that field. If not provided, the default values for all fields will be used.

Returns:

tabular data for the given ticker and field

Return type:

List

get_tickers_universe(universe_ticker: BloombergTicker, date: Optional[datetime] = None, display_figi: bool = False) List[BloombergTicker][source]

Returns a list of all members of an index. It will not return any data for indices with more than 20,000 members.

Parameters:
  • universe_ticker (Ticker) – The ticker symbol representing the index or universe for which the tickers are being queried.

  • date (datetime) – The date for which the tickers’ universe data is requested.

  • display_figi (bool) – The following flag can be used to have this field return Financial Instrument Global Identifiers (FIGI). By default set to False, which results in returning tickers identifiers instead of FIGI.

Returns:

A list of tickers (Ticker objects) that were included in the index on the specified date.

Return type:

List[Ticker]

get_tickers_universe_with_weights(universe_ticker: BloombergTicker, date: Optional[datetime] = None, display_figi: bool = False) QFSeries[source]

Returns the tickers belonging to a specified universe, along with their corresponding weights, at a given date. The result is a QFSeries indexed by ticker objects, with the values representing the respective weights of each ticker in the universe.

Important: It will not return any data for indices with more than 20,000 members.

Parameters:
  • universe_ticker (Ticker) – The ticker symbol representing the index or universe for which the tickers are being queried.

  • date (datetime) – The date for which the tickers’ universe data is requested. If not provided, it defaults to current date.

  • display_figi (bool) – The following flag can be used to have this field return Financial Instrument Global Identifiers (FIGI). By default set to False, which results in returning tickers identifiers instead of FIGI.

Returns:

A QFSeries indexed by Ticker objects, where the values are the weights of the respective tickers in the universe at a given date.

Return type:

QFSeries

get_unique_tickers(universe_ticker: Ticker) List[Ticker][source]

Retrieves a list of unique tickers that belong to the specified universe, regardless of the date.

Parameters:

universe_ticker (Ticker) – The ticker symbol representing the index or universe for which the tickers are being queried.

Returns:

A list of unique tickers (Ticker objects) that are part of the specified universe (regardless of the date).

Return type:

List[Ticker]

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]

supported_ticker_types()[source]

Returns classes of tickers which are supported by this DataProvider.