コード例 #1
0
    def test_futures_chain_without_adjustment(self):
        timer = SettableTimer(self.end_date)
        self.future_ticker_1.initialize_data_provider(timer,
                                                      self.data_provider)

        futures_chain = FuturesChain(self.future_ticker_1, self.data_provider,
                                     FuturesAdjustmentMethod.NTH_NEAREST)

        # AB2021M is the current specific ticker till 2021-06-14 inclusive, afterwards the AB2021U
        start_date = str_to_date("2021-06-13")
        end_date = str_to_date("2021-06-17")
        fields = PriceField.ohlcv()
        prices = futures_chain.get_price(fields, start_date, end_date,
                                         Frequency.DAILY)

        prices_m_contract = self.data_provider.get_price(
            PortaraTicker("AB2021M", SecurityType.FUTURE, 1), fields,
            start_date, str_to_date("2021-06-14"), Frequency.DAILY)
        prices_u_contract = self.data_provider.get_price(
            PortaraTicker("AB2021U", SecurityType.FUTURE, 1), fields,
            str_to_date("2021-06-15"), end_date, Frequency.DAILY)

        assert_dataframes_equal(prices,
                                concat([prices_m_contract, prices_u_contract]),
                                check_names=False)
コード例 #2
0
    def test_belongs_to_family(self):
        self.assertTrue(
            self.future_ticker_1.belongs_to_family(
                PortaraTicker("AB2021M", SecurityType.FUTURE, 1)))
        self.assertTrue(
            self.future_ticker_1.belongs_to_family(
                PortaraTicker("AB1921K", SecurityType.FUTURE, 1)))
        self.assertTrue(
            self.future_ticker_1.belongs_to_family(
                PortaraTicker("AB2020Z", SecurityType.FUTURE, 1)))
        self.assertTrue(
            self.future_ticker_2.belongs_to_family(
                PortaraTicker("ABCD1234H", SecurityType.FUTURE, 17)))

        self.assertFalse(
            self.future_ticker_1.belongs_to_family(
                PortaraTicker("ABCD1234H", SecurityType.FUTURE, 17)))
        self.assertFalse(
            self.future_ticker_1.belongs_to_family(
                PortaraTicker("AB2020Z", SecurityType.FUTURE, 13)))
        self.assertFalse(
            self.future_ticker_2.belongs_to_family(
                PortaraTicker("AB2021M", SecurityType.FUTURE, 1)))
        self.assertFalse(
            self.future_ticker_2.belongs_to_family(
                PortaraTicker("AB1921K", SecurityType.FUTURE, 1)))
        self.assertFalse(
            self.future_ticker_2.belongs_to_family(
                PortaraTicker("AB2020Z", SecurityType.FUTURE, 1)))
コード例 #3
0
    def test_get_price_single_future_ticker_many_fields(self):
        data_provider = self.get_data_provider(self.future_ticker, self.fields)
        tickers_to_check = [PortaraTicker('AB2021M', SecurityType.FUTURE, 1),
                            PortaraTicker('AB2021U', SecurityType.FUTURE, 1)]

        prices = data_provider.get_price(tickers_to_check, self.fields, self.start_date, self.end_date, Frequency.MIN_1)

        self.assertEqual(type(prices), QFDataArray)
        self.assertEqual(prices.shape, (self.number_of_data_bars, len(tickers_to_check), len(self.fields)))
        self.assertEqual(Frequency.infer_freq(pd.to_datetime(prices.dates.values)), Frequency.MIN_1)
        self.assertCountEqual(prices.tickers.values, tickers_to_check)
コード例 #4
0
    def setUpClass(cls) -> None:
        cls.start_date = datetime(2021, 6, 11, 17, 13)
        cls.end_date = datetime(2021, 6, 14, 8, 46)

        cls.number_of_data_bars = 29
        cls.fields = PriceField.ohlcv()

        cls.ticker = PortaraTicker('AB', SecurityType.FUTURE, 1)
        cls.tickers = [PortaraTicker('AB', SecurityType.FUTURE, 1), PortaraTicker('ABCD', SecurityType.FUTURE, 7)]

        cls.future_ticker = PortaraFutureTicker("", "AB{}", 1, 1)
        cls.future_tickers = [PortaraFutureTicker("", "AB{}", 1, 1), PortaraFutureTicker("", "ABCD{}", 1, 7)]

        cls.futures_path = str(Path(__file__).parent / Path('input_data') / Path('Futures'))
コード例 #5
0
def daily_data_example(path_to_data_files: str):
    """
    In order to run the example you need to provide the path to the top directory, containing your data files. The
    example below will download Open, High, Low and Close Prices along with daily Volume for the E-mini S&P,
    EP2018M contract.

    Notes
    ---------
    The file containing data for the ticker should have the same name as the ticker that you are passing as the
    parameter. For example if you want to run this example for EP2018M, you should name your data file "EP2018M.csv"
    and download the prices using PortaraTicker("EP2018M").

    Parameters
    -----------
    path_to_data_files: str
        path to the top directory, which contains all your Portara data files
    """

    start_date = str_to_date('2018-02-26')
    end_date = str_to_date('2018-06-15')
    fields = PriceField.ohlcv()
    ticker = PortaraTicker('EP2018M', SecurityType.FUTURE, 50)
    daily_freq = Frequency.DAILY

    if path_to_data_files is None:
        raise ValueError("Please provide a correct path to the Portara data and assign it to the "
                         "path_to_data_files variable.")

    print('\nSingle ticker, daily frequency')
    dp = PortaraDataProvider(path_to_data_files, ticker, fields, start_date, end_date, daily_freq)
    prices = dp.get_price(ticker, fields, start_date, end_date, daily_freq)
    print(prices)
コード例 #6
0
    def _get_expiration_dates(self, dir_path: str, future_tickers: Sequence[FutureTicker]):
        tickers_dates_dict = {}

        for future_ticker in future_tickers:
            for path in list(Path(dir_path).glob('**/{}.txt'.format(future_ticker.family_id.replace("{}", "")))):
                try:
                    df = pd.read_csv(path, names=['Contract', 'Expiration Date'], parse_dates=['Expiration Date'],
                                     date_parser=lambda date: datetime.strptime(date, '%Y%m%d'), index_col="Contract")
                    df = df.rename(columns={'Expiration Date': ExpirationDateField.LastTradeableDate})
                    df.index = PortaraTicker.from_string(df.index, security_type=future_ticker.security_type,
                                                         point_value=future_ticker.point_value)

                    if all(future_ticker.belongs_to_family(x) for x in df.index):
                        tickers_dates_dict[future_ticker] = QFDataFrame(df)
                    else:
                        self.logger.info(f"Not all tickers belong to family {future_ticker}")

                except Exception:
                    self.logger.debug(f"File {path} does not contain valid expiration dates and therefore will be "
                                      f"excluded.")

        # Log all the future tickers, which could not have been mapped correctly
        tickers_without_matching_files = set(future_tickers).difference(tickers_dates_dict.keys())
        for ticker in tickers_without_matching_files:
            tickers_dates_dict[ticker] = QFDataFrame(columns=[ExpirationDateField.LastTradeableDate])
            self.logger.warning(f"No expiration dates were found for ticker {ticker}. Check if file "
                                f"{ticker.family_id.replace('{}', '')}.txt exists in the {dir_path} and if it contains"
                                f"valid expiration dates for the ticker.")

        return tickers_dates_dict
コード例 #7
0
    def test_designated_contracts(self):
        future_ticker = PortaraFutureTicker("",
                                            "AB{}",
                                            1,
                                            1,
                                            designated_contracts="MZ")

        timer = SettableTimer()
        future_ticker.initialize_data_provider(timer, self.data_provider)

        timer.set_current_time(str_to_date("2021-06-15"))
        specific_ticker = future_ticker.get_current_specific_ticker()
        self.assertEqual(specific_ticker,
                         PortaraTicker("AB2021Z", SecurityType.FUTURE, 1))
コード例 #8
0
    def test_get_current_specific_ticker(self):
        timer = SettableTimer()
        self.future_ticker_1.initialize_data_provider(timer,
                                                      self.data_provider)

        timer.set_current_time(str_to_date("2021-03-18"))
        specific_ticker = self.future_ticker_1.get_current_specific_ticker()
        self.assertEqual(specific_ticker,
                         PortaraTicker("AB2021M", SecurityType.FUTURE, 1))

        timer.set_current_time(str_to_date("2021-06-14"))
        specific_ticker = self.future_ticker_1.get_current_specific_ticker()
        self.assertEqual(specific_ticker,
                         PortaraTicker("AB2021M", SecurityType.FUTURE, 1))

        timer.set_current_time(str_to_date("2021-06-15"))
        specific_ticker = self.future_ticker_1.get_current_specific_ticker()
        self.assertEqual(specific_ticker,
                         PortaraTicker("AB2021U", SecurityType.FUTURE, 1))

        timer.set_current_time(datetime(2021, 12, 14, 23, 59))
        specific_ticker = self.future_ticker_1.get_current_specific_ticker()
        self.assertEqual(specific_ticker,
                         PortaraTicker("AB2021Z", SecurityType.FUTURE, 1))
コード例 #9
0
def continuous_contracts(path_to_data_files: str):
    """
    In order to run the example you need to provide the path to the top directory, containing your data files. The
    example below will download Open, High, Low and Close Prices along with Volume for the VIX Future CBOEFE and
    (Zero Adjust) and Johannesburg Wheat (Back Adjust) continuous contracts.

    Notes
    ---------
    The two files containing data for the ticker should have the same name as the tickers that you are passing as the
    parameter. For example - to run the example below, you will need to save prices of the data as VX.csv and WEAT.csv.

    Parameters
    -----------
    path_to_data_files: str
        path to the top directory, which contains all your Portara data files
    """

    start_date = str_to_date('2019-01-01')
    end_date = str_to_date('2019-01-10')
    fields = PriceField.ohlcv()
    tickers = [PortaraTicker("VX", SecurityType.FUTURE, 1000), PortaraTicker("WEAT", SecurityType.FUTURE, 100)]
    daily_freq = Frequency.DAILY

    if path_to_data_files is None:
        raise ValueError("Please provide a correct path to the Portara data and assign it to the "
                         "path_to_data_files variable.")

    print('\nMultiple continuous tickers, daily frequency, open prices')
    dp = PortaraDataProvider(path_to_data_files, tickers, fields, start_date, end_date, daily_freq)
    prices = dp.get_price(tickers, PriceField.Open, start_date, end_date, daily_freq)
    print(prices)

    print('\nMultiple continuous tickers, daily frequency, close prices')
    dp = PortaraDataProvider(path_to_data_files, tickers, fields, start_date, end_date, daily_freq)
    prices = dp.get_price(tickers, PriceField.Close, start_date, end_date, daily_freq)
    print(prices)
コード例 #10
0
def intraday_data_example(path_to_data_files: str):
    """
    In order to run the example you need to provide the path to the top directory, containing your data files. The
    example below will download Open, High, Low and Close Prices along with Volume for the Japanese
    Government Bonds, JGB2019H contract, for the 1-minute bars frequency.

    Notes
    ---------
    The file containing data for the ticker should have the same name as the ticker that you are passing as the
    parameter. For example if you want to run this example for EP2018M, you should name your data file "JGB2019H.csv"
    and download the prices using PortaraTicker("JGB2019H").

    Parameters
    -----------
    path_to_data_files: str
        path to the top directory, which contains all your Portara data files
    """

    start_date = str_to_date('2019-02-22 00:00:00.0', DateFormat.FULL_ISO)
    end_date = str_to_date('2019-02-22 00:09:00.0', DateFormat.FULL_ISO)
    fields = PriceField.ohlcv()
    ticker = PortaraTicker('JGB2019H', SecurityType.FUTURE, 1000000)
    intraday_frequency = Frequency.MIN_1

    if path_to_data_files is None:
        raise ValueError("Please provide a correct path to the Portara data and assign it to the "
                         "path_to_data_files variable.")

    print('\nSingle ticker, 1-minute frequency')
    dp = PortaraDataProvider(path_to_data_files, ticker, fields, start_date, end_date, intraday_frequency)
    prices = dp.get_price(ticker, fields, start_date, end_date, intraday_frequency)
    print(prices)

    print('\nSingle ticker, 5-minute frequency')
    prices = dp.get_price(ticker, fields, start_date, end_date, Frequency.MIN_5)
    print(prices)
コード例 #11
0
def future_ticker_example(path_to_data_files: str):
    """
    In order to run the example you need to provide the path to the top directory, containing your data files. The
    example below will:

    1. initialize the Silver FutureTicker
    2. return the list of tickers belonging to the futures chain
    3. return the current specific ticker
    4. check for some tickers, if they belong to the Silver futures family
    5. return Open, High, Low, Close and Volume pricing data for the current specific ticker

    Parameters
    -----------
    path_to_data_files: str
        path to the top directory, which contains all your Portara data files
    """

    start_date = str_to_date('2020-12-02')
    end_date = str_to_date('2021-02-01')
    fields = PriceField.ohlcv()
    # Use the front contract (N = 1)
    # Roll the tickers 1 day before the expiration (days_before_exp_date = 1)
    # Set the point value to 50 (value for each of the contracts can be checked in Portara)
    future_ticker = PortaraFutureTicker('Silver',
                                        'SIA{}',
                                        1,
                                        1,
                                        50,
                                        designated_contracts="HKNUZ")
    daily_freq = Frequency.DAILY

    if path_to_data_files is None:
        raise ValueError(
            "Please provide a correct path to the Portara data and assign it to the "
            "path_to_data_files variable.")

    dp = PortaraDataProvider(path_to_data_files, future_ticker, fields,
                             start_date, end_date, daily_freq)

    # Initialize the future ticker with the data provider and timer. Timer is used to identify the current front ticker.
    timer = SettableTimer()
    future_ticker.initialize_data_provider(timer, dp)

    print(
        '\nCurrent individual contract (front contract) as of 10th December 2020:'
    )
    timer.set_current_time(str_to_date('2020-12-10'))
    current_ticker = future_ticker.get_current_specific_ticker()
    print(f'> {current_ticker}')

    print(
        '\nCurrent individual contract (front contract) as of 10th January 2021:'
    )
    timer.set_current_time(str_to_date('2021-01-10'))
    current_ticker = future_ticker.get_current_specific_ticker()
    print(f'> {current_ticker}')

    print(
        '\nCheck if the following tickers belong to the Silver futures chain:')
    ticker = PortaraTicker('SIA2017H', SecurityType.FUTURE, 50)
    print(f'- {ticker}: {future_ticker.belongs_to_family(ticker)}')
    ticker = PortaraTicker('OH2017H', SecurityType.FUTURE, 50)
    print(f'- {ticker}: {future_ticker.belongs_to_family(ticker)}')
    ticker = PortaraTicker('SIA2017', SecurityType.FUTURE, 50)
    print(f'- {ticker}: {future_ticker.belongs_to_family(ticker)}')
    ticker = PortaraTicker('SIA1999Z', SecurityType.FUTURE, 50)
    print(f'- {ticker}: {future_ticker.belongs_to_family(ticker)}')

    print(
        '\nOpen, High, Low, Close and Volume pricing data for the current specific ticker (as of 10th January 2021)'
    )
    prices = dp.get_price(current_ticker, PriceField.ohlcv(), start_date,
                          end_date, daily_freq)
    print(prices)