def test_get_futures_chain_1_ticker(self):
        bbg_fut_chain_tickers = self.data_provider.get_futures_chain_tickers(
            self.TICKER_2, ExpirationDateField.all_dates())
        preset_fut_chain_tickers = self.data_handler.data_provider.get_futures_chain_tickers(
            self.TICKER_2, ExpirationDateField.all_dates())

        self.assertCountEqual(bbg_fut_chain_tickers[self.TICKER_2],
                              preset_fut_chain_tickers[self.TICKER_2])
    def test_get_futures_chain_tickers(self):
        fut_chain_dict = data_provider.get_futures_chain_tickers(
            self.tickers, ExpirationDateField.all_dates())
        bbg_fut_chain_dict = bbg_provider.get_futures_chain_tickers(
            self.tickers, ExpirationDateField.all_dates())

        for ticker in self.tickers:
            self.assertCountEqual(fut_chain_dict[ticker],
                                  bbg_fut_chain_dict[ticker])
    def test_get_futures_chain_multiple_tickers(self):
        tickers = [self.TICKER_2]
        bbg_fut_chain_tickers = self.data_provider.get_futures_chain_tickers(
            tickers, ExpirationDateField.all_dates())
        preset_fut_chain_tickers = self.data_handler.data_provider.get_futures_chain_tickers(
            tickers, ExpirationDateField.all_dates())

        for ticker in tickers:
            self.assertCountEqual(bbg_fut_chain_tickers[ticker],
                                  preset_fut_chain_tickers[ticker])
    def _get_futures_chain_tickers(self):
        """
        Function used to download the expiration dates of the futures contracts, in order to return afterwards current
        futures tickers. It uses the list of month codes of designated contracts and filter out these, that should not
        be considered by the future ticker.
        """
        futures_chain_tickers_df = self._data_provider.get_futures_chain_tickers(self,
                                                                                 ExpirationDateField.all_dates())[self]
        # Get the minimum date
        futures_chain_tickers = futures_chain_tickers_df.min(axis=1)
        futures_chain_tickers = QFSeries(data=futures_chain_tickers.index, index=futures_chain_tickers.values)
        futures_chain_tickers.index = pd.to_datetime(futures_chain_tickers.index)

        # Filter out the non-designated contracts
        seed = self.family_id.split("{}")[0]
        designated_contracts_seeds = tuple(seed + month_code for month_code in self.designated_contracts)
        futures_chain_tickers = futures_chain_tickers[futures_chain_tickers.apply(
            lambda t: t.ticker.startswith(designated_contracts_seeds)
        )]
        return futures_chain_tickers
    def __init__(self, data_provider: DataProvider,
                 tickers: Union[Ticker, Sequence[Ticker]],
                 fields: Union[PriceField,
                               Sequence[PriceField]], start_date: datetime,
                 end_date: datetime, frequency: Frequency):
        # Convert fields into list in order to return a QFDataArray as the result of get_price function
        fields, _ = convert_to_list(fields, PriceField)

        # Convert the tickers to list and remove duplicates
        tickers, _ = convert_to_list(tickers, Ticker)
        tickers = list(dict.fromkeys(tickers))

        future_tickers = [
            ticker for ticker in tickers if isinstance(ticker, FutureTicker)
        ]
        non_future_tickers = [
            ticker for ticker in tickers
            if not isinstance(ticker, FutureTicker)
        ]

        exp_dates = None
        all_tickers = non_future_tickers

        if future_tickers:
            exp_dates = data_provider.get_futures_chain_tickers(
                future_tickers, ExpirationDateField.all_dates())

            # Filter out all theses specific future contracts, which expired before start_date
            for ft in future_tickers:
                all_tickers.extend(
                    chain_tickers_within_range(ft, exp_dates[ft], start_date,
                                               end_date))

        data_array = data_provider.get_price(all_tickers, fields, start_date,
                                             end_date, frequency)

        super().__init__(data=data_array,
                         exp_dates=exp_dates,
                         start_date=start_date,
                         end_date=end_date,
                         frequency=frequency)
Example #6
0
    def _compute_aggregated_volume(self, ticker: FutureTicker, start_date: datetime, end_date: datetime) \
            -> Optional[QFSeries]:
        # Compute the TOTAL VOLUME (aggregated across contracts)
        futures_chain_tickers_df = self.data_provider.get_futures_chain_tickers(
            ticker, ExpirationDateField.all_dates())[ticker]
        # Get the minimum date
        futures_chain_tickers = futures_chain_tickers_df.min(axis=1)
        futures_chain_tickers = QFSeries(data=futures_chain_tickers.index,
                                         index=futures_chain_tickers.values)
        futures_chain_tickers.index = to_datetime(futures_chain_tickers.index)
        futures_chain_tickers = futures_chain_tickers.sort_index()

        futures_chain_tickers = futures_chain_tickers.loc[start_date:end_date +
                                                          RelativeDelta(
                                                              months=6)]
        all_specific_tickers = futures_chain_tickers.values.tolist()
        volume_df = self.data_provider.get_price(
            all_specific_tickers, PriceField.Volume, start_date, end_date,
            Frequency.DAILY).dropna(axis=1, how="all").fillna(0)

        return volume_df.sum(axis=1) if not volume_df.empty else None
    def _get_futures_chain_tickers(self):
        """
        Function used to download the expiration dates of the futures contracts, in order to return afterwards current
        futures tickers. It uses the list of month codes of designated contracts and filter out these, that should not
        be considered by the future ticker.
        """
        futures_chain_tickers_df = self._data_provider.get_futures_chain_tickers(
            self, ExpirationDateField.all_dates())[self]
        # Get the minimum date
        futures_chain_tickers_series = futures_chain_tickers_df.min(axis=1)

        # Filter out the non-designated contracts
        month_codes = "|".join(self.designated_contracts)
        contracts_pattern = self.family_id.format(f"({month_codes})\\d{{1,2}}")
        designated_contracts = futures_chain_tickers_series.index[
            futures_chain_tickers_series.index.map(lambda t: bool(
                re.search(f"^{contracts_pattern}$", t.as_string())))]
        futures_chain_tickers_series = futures_chain_tickers_series.loc[
            designated_contracts]

        futures_chain_tickers = QFSeries(futures_chain_tickers_series.index,
                                         futures_chain_tickers_series.values)
        futures_chain_tickers.index = to_datetime(futures_chain_tickers.index)
        return futures_chain_tickers
    def test_get_expiration_dates(self):
        tickers_dict = self.data_provider.get_futures_chain_tickers(
            self.ticker_1, ExpirationDateField.all_dates())
        exp_dates = tickers_dict[self.ticker_1]

        self.assertEqual(type(exp_dates), QFDataFrame)
Example #9
0
 def get_data_func(
         data_prov: DataProvider, tickers_for_single_data_provider
 ) -> Dict[FutureTicker, QFSeries]:
     return data_prov.get_futures_chain_tickers(
         tickers_for_single_data_provider,
         ExpirationDateField.all_dates())