Exemplo n.º 1
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)
    def test_get_price_aggregation_single_ticker(self):
        MarketOpenEvent.set_trigger_time({"hour": 17, "minute": 13, "second": 0, "microsecond": 0})

        dp = PortaraDataProvider(self.futures_path, self.ticker, PriceField.Close, self.start_date, self.end_date,
                                 Frequency.MIN_1)
        prices = dp.get_price(self.ticker, PriceField.Close, self.start_date, self.end_date, Frequency.MIN_1)
        prices5 = dp.get_price(self.ticker, PriceField.Close, self.start_date, self.end_date, Frequency.MIN_5)
        prices15 = dp.get_price(self.ticker, PriceField.Close, self.start_date, self.end_date, Frequency.MIN_15)

        self.assertTrue(len(prices5))
        self.assertEqual(type(prices5), PricesSeries)
        self.assertEqual(Frequency.infer_freq(prices5.index), Frequency.MIN_5)

        assert_series_equal(prices5, prices.resample('5T', origin=datetime(2021, 6, 17, 17, 13)).last().dropna(),
                            check_names=False)

        self.assertTrue(len(prices15))
        self.assertEqual(type(prices15), PricesSeries)
        self.assertEqual(Frequency.infer_freq(prices15.index), Frequency.MIN_15)
        assert_series_equal(prices15, prices.resample('15T', origin=datetime(2021, 6, 17, 17, 13)).last().dropna(),
                            check_names=False)
Exemplo n.º 3
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)
Exemplo n.º 4
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)
Exemplo n.º 5
0
 def get_data_provider(self, tickers, fields) -> PortaraDataProvider:
     return PortaraDataProvider(self.futures_path, tickers, fields,
                                self.start_date, self.end_date,
                                Frequency.DAILY)
Exemplo n.º 6
0
 def setUp(self) -> None:
     self.data_provider = PortaraDataProvider(
         self.futures_path, [self.future_ticker_1, self.future_ticker_2],
         PriceField.ohlcv(), self.start_date, self.end_date,
         Frequency.DAILY)
Exemplo n.º 7
0
class TestPortaraFutureTicker(TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        cls.start_date = str_to_date('2021-05-18')
        cls.end_date = str_to_date('2021-06-28')

        cls.future_ticker_1 = PortaraFutureTicker("", "AB{}", 1, 1)
        cls.future_ticker_2 = PortaraFutureTicker("", "ABCD{}", 1, 11, 17)

        cls.futures_path = str(
            Path(__file__).parent / Path('input_data') / Path('Futures'))

    def setUp(self) -> None:
        self.data_provider = PortaraDataProvider(
            self.futures_path, [self.future_ticker_1, self.future_ticker_2],
            PriceField.ohlcv(), self.start_date, self.end_date,
            Frequency.DAILY)

    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))

    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)))

    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))

    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)
Exemplo n.º 8
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)