Esempio n. 1
0
    def test_historical_price__margin_adjustment__daily(self):
        # In case if we want only 1 historical bar and the last full bar was more than ~12 days ago, the adjustment of
        # the margin for the "number of days to go back" need to be performed
        self.current_time = str_to_date("2021-05-18 00:00:00.000000",
                                        DateFormat.FULL_ISO)
        actual_bars = self.data_provider.historical_price(
            self.ticker_1, PriceField.ohlcv(), 1, frequency=Frequency.DAILY)
        expected_bars = PricesDataFrame(data=[[25.0, 25.1, 25.2, None, 25.3]],
                                        index=[str_to_date('2021-05-05')],
                                        columns=PriceField.ohlcv())
        assert_dataframes_equal(actual_bars, expected_bars, check_names=False)

        self.current_time = str_to_date("2021-05-27 00:00:00.000000",
                                        DateFormat.FULL_ISO)
        actual_bars = self.data_provider.historical_price(
            self.ticker_1, PriceField.ohlcv(), 1, frequency=Frequency.DAILY)
        assert_dataframes_equal(actual_bars, expected_bars, check_names=False)

        with self.assertRaises(ValueError):
            self.current_time = str_to_date("2021-06-06 00:00:00.000000",
                                            DateFormat.FULL_ISO)
            self.data_provider.historical_price(self.ticker_1,
                                                PriceField.ohlcv(),
                                                1,
                                                frequency=Frequency.DAILY)
Esempio n. 2
0
    def get_current_bar(self, tickers: Union[Ticker, Sequence[Ticker]], frequency: Frequency = None) \
            -> Union[QFSeries, QFDataFrame]:
        """
        Gets the current bar(s) for given Ticker(s). If the bar is not available yet, None is returned.

        If the request for single Ticker was made, then the result is a QFSeries indexed with PriceFields (OHLCV).
        If the request for multiple Tickers was made, then the result has Tickers as an index and PriceFields
        as columns.

        In case of N minutes frequency, the current bar can be returned in the time between (inclusive) N minutes
        after MarketOpenEvent and the MarketCloseEvent).

        E.g. for 1 minute frequency, at 13:00 (if the market opens before 13:00), the 12:59 - 13:00 bar will be
        returned. In case of 15 minutes frequency, when the market opened less then 15 minutes ago, Nones will be
        returned. If current time ("now") contains non-zero seconds or microseconds, Nones will be returned.

        Parameters
        -----------
        tickers: Ticker, Sequence[Ticker]
            tickers of the securities which prices should be downloaded
        frequency: Frequency
            frequency of the data

        Returns
        -------
        QFSeries, QFDataFrame
            current bar

        """
        if not tickers:
            return QFSeries()

        frequency = frequency or self.fixed_data_provider_frequency or Frequency.MIN_1

        tickers, was_single_ticker_provided = convert_to_list(tickers, Ticker)

        current_datetime = self.timer.now()

        start_date = current_datetime - frequency.time_delta()
        prices_data_array = self.get_price(tickers=tickers,
                                           fields=PriceField.ohlcv(),
                                           start_date=start_date,
                                           end_date=current_datetime,
                                           frequency=frequency)
        try:
            last_available_bars = cast_data_array_to_proper_type(
                prices_data_array.loc[start_date])
        except KeyError:
            return QFDataFrame(index=tickers, columns=PriceField.ohlcv())

        if was_single_ticker_provided:
            last_available_bars = last_available_bars.iloc[0, :]

        return last_available_bars
Esempio n. 3
0
    def _get_current_bars(self, tickers: Sequence[Ticker]) -> QFDataFrame:
        """
        Gets the current bars for given Tickers. If the bars are not available yet, NaNs are returned.
        The result is a QFDataFrame with Tickers as an index and PriceFields as columns.

        In case of daily trading, the current bar is returned only at the Market Close Event time, as the get_price
        function will not return data for the current date until the market closes.

        In case of intraday trading (for N minutes frequency) the current bar can be returned in the time between
        (inclusive) N minutes after MarketOpenEvent and the MarketCloseEvent. Important: If current time ("now")
        contains non-zero seconds or microseconds, NaNs will be returned.

        """
        if not tickers:
            return QFDataFrame()

        assert self._frequency >= Frequency.DAILY, "Lower than daily frequency is not supported by the simulated " \
                                                   "executor"
        current_datetime = self._timer.now()

        market_close_time = current_datetime + MarketCloseEvent.trigger_time(
        ) == current_datetime

        if self._frequency == Frequency.DAILY:
            # In case of daily trading, the current bar can be returned only at the Market Close
            if not market_close_time:
                return QFDataFrame(index=tickers, columns=PriceField.ohlcv())
            else:
                current_datetime = date_to_datetime(current_datetime.date())
                start_date = current_datetime - self._frequency.time_delta()
                current_bar_start = current_datetime
        else:
            # In case of intraday trading the current full bar is always indexed by the left side of the time range
            start_date = current_datetime - self._frequency.time_delta()
            current_bar_start = start_date

        prices_data_array = self._data_handler.get_price(
            tickers=tickers,
            fields=PriceField.ohlcv(),
            start_date=start_date,
            end_date=current_datetime,
            frequency=self._frequency)
        try:
            current_bars = cast_data_array_to_proper_type(
                prices_data_array.loc[current_bar_start])
        except KeyError:
            current_bars = QFDataFrame(index=tickers,
                                       columns=PriceField.ohlcv())
        return current_bars
Esempio n. 4
0
    def get_data(self,
                 ticker_str: str,
                 end_date: datetime,
                 aggregate_volume: bool = False):
        """
        Downloads the OHCLV Prices data frame for the given ticker. In case of a FutureTicker, the function downloads
        the Futures Chain and applies backward adjustment to the prices.

        Parameters
        ----------
        ticker_str: str
            string representing the ticker for which the data should be downloaded
        end_date: datetime
            last date for the data to be fetched
        aggregate_volume: bool
            used only in case of future tickers - if set to True, the volume data would not be the volume for the
            given contract, but the volume aggregated across all contracts (for each day the volume will be simply
            the sum of all volumes of the existing contracts of the given future asset)
        """
        end_date = end_date + RelativeDelta(
            hour=0, minute=0, second=0, microsecond=0)
        start_date = end_date - RelativeDelta(days=self.num_of_bars_needed +
                                              50)
        ticker = self.ticker_name_to_ticker[ticker_str]
        if isinstance(ticker, FutureTicker):
            try:
                data_frame = self.futures_data[ticker].get_price(
                    PriceField.ohlcv(), start_date, end_date, Frequency.DAILY)
            except KeyError:
                # Ticker was not preloaded or the FutureChain has expired
                self.futures_data[ticker] = FuturesChain(
                    ticker, self.data_provider,
                    FuturesAdjustmentMethod.BACK_ADJUSTED)

                data_frame = self.futures_data[ticker].get_price(
                    PriceField.ohlcv(), start_date, end_date, Frequency.DAILY)
            if aggregate_volume:
                data_frame[
                    PriceField.Volume] = self._compute_aggregated_volume(
                        ticker, start_date, end_date)

        else:
            data_frame = self.data_provider.get_price(ticker,
                                                      PriceField.ohlcv(),
                                                      start_date, end_date,
                                                      Frequency.DAILY)

        data_frame = data_frame.dropna(how="all")
        return data_frame
Esempio n. 5
0
    def get_bar_for_today(
        self, tickers: Union[Ticker, Sequence[Ticker]]
    ) -> Union[pd.Series, pd.DataFrame]:
        """
        Gets the bar(s) for given Ticker(s) for today. If the bar is not available yet, None is returned.

        If the request for single Ticker was made, then the result is a pandas.Series indexed with PriceFields (OHLCV).
        If the request for multiple Tickers was made, then the result has Tickers as an index and PriceFields
        as columns.

        Normally, on working days the method will return non-empty bars in the time between (inclusive) MarketCloseEvent
        and midnight (exclusive).

        On non-working days or on working days in the time between midnight (inclusive) and MarketClose (exclusive),
        e.g. 12:00, the returned bars will contain Nones as values.
        """
        if not tickers:
            return pd.Series()

        tickers, was_single_ticker_provided = convert_to_list(tickers, Ticker)

        current_datetime = self.timer.now()
        current_date = self._zero_out_time_component(current_datetime)

        start_date = current_date - RelativeDelta(days=7)
        if self.time_helper.datetime_of_latest_market_event(
                MarketCloseEvent) < current_datetime:
            last_available_bars = pd.DataFrame(index=tickers,
                                               columns=PriceField.ohlcv())
        else:
            prices_data_array = self.get_price(
                tickers=tickers,
                fields=PriceField.ohlcv(),
                start_date=start_date,
                end_date=current_date)  # type: QFDataArray

            if current_date in prices_data_array.dates.to_index():
                # to_index used in the line above as a fix (xarray doesn't handle the "contains" check properly,
                # It thinks that datetime(some_date) != numpy.datetime64(some_date) because of different types)
                last_available_bars = prices_data_array.loc[
                    current_date, :, :].to_pandas()
            else:
                return pd.DataFrame(index=tickers, columns=PriceField.ohlcv())

        if was_single_ticker_provided:
            last_available_bars = last_available_bars.iloc[0, :]

        return last_available_bars
Esempio n. 6
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)
Esempio n. 7
0
    def setUp(self):
        tickers = [BloombergTicker("AAPL US Equity")]
        all_fields = PriceField.ohlcv()

        self._mocked_prices_arr = self._make_mock_data_array(
            tickers, all_fields)
        self._price_provider_mock = PresetDataProvider(self._mocked_prices_arr,
                                                       self.data_start_date,
                                                       self.data_end_date,
                                                       self.frequency)

        risk_estimation_factor = 0.05
        data_handler = Mock()
        data_handler.get_last_available_price.return_value = None
        alpha_model = self.DummyAlphaModel(risk_estimation_factor,
                                           data_handler)

        ts = self._test_trading_session_init()

        # Mock the backtest result in order to be able to compare transactions
        self.transactions = []
        ts.monitor.record_transaction.side_effect = lambda transaction: self.transactions.append(
            transaction)
        self.portfolio = ts.portfolio

        AlphaModelStrategy(ts, {alpha_model: tickers}, use_stop_losses=True)
        ts.start_trading()
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)
Esempio n. 9
0
    def _create_price_provider_mock(self, tickers):
        mock_data_array = QFDataArray.create(
            dates=date_range(start='2009-12-28', end='2009-12-30', freq='D'),
            tickers=tickers,
            fields=PriceField.ohlcv(),
            data=[
                # 2009-12-28
                [
                    # Open High  Low   Close Volume
                    [25.0, 25.1, 25.2, 26.0, 25.3],  # MSFT
                    [27.0, 27.1, 27.2, 28.0, 27.3]  # AAPL
                ],
                # 2009-12-29
                [
                    # Open High  Low   Close Volume
                    [None, None, None, None, None],  # MSFT
                    [29.0, 29.1, 29.2, 30.0, 29.3]  # AAPL
                ],
                # 2009-12-30
                [
                    # Open High  Low   Close Volume
                    [31.0, 31.1, 31.2, 32.0, 31.3],  # MSFT
                    [None, None, None, None, None]  # AAPL
                ]
            ])

        price_data_provider_mock = Mock(spec=DataProvider,
                                        frequency=Frequency.DAILY)
        price_data_provider_mock.get_price.side_effect = lambda t, fields, start_time, end_time, frequency: \
            mock_data_array.loc[start_time:end_time, t, fields].to_pandas()

        return price_data_provider_mock
    def setUp(self):
        all_fields = PriceField.ohlcv()

        self._mocked_prices_arr = self._make_mock_data_array(self.tickers, all_fields)
        self._price_provider_mock = PresetDataProvider(self._mocked_prices_arr,
                                                       self.data_start_date, self.data_end_date, self.frequency)
        self.timer = SettableTimer()
        self.alpha_model_type = DummyAlphaModel
Esempio n. 11
0
 def use_data_preloading(self,
                         tickers: Union[Ticker, Sequence[Ticker]],
                         time_delta: RelativeDelta = None):
     if time_delta is None:
         time_delta = RelativeDelta(years=1)
     data_history_start = self.start_date - time_delta
     self.data_handler.use_data_bundle(tickers, PriceField.ohlcv(),
                                       data_history_start, self.end_date)
Esempio n. 12
0
    def _get_data_for_backtest(self) -> QFDataArray:
        """
        Creates a QFDataArray containing OHLCV values for all tickers passes to Fast Alpha Models Tester.
        """
        print("\nLoading all price values of tickers:")
        self._timer.set_current_time(self._end_date)
        tickers_dict = {}
        for ticker in tqdm(self._tickers, file=sys.stdout):
            if isinstance(ticker, FutureTicker):
                fc = FuturesChain(ticker, self._data_handler)
                tickers_dict[ticker] = fc.get_price(PriceField.ohlcv(), self._start_date, self._end_date,
                                                    Frequency.DAILY)
            else:
                tickers_dict[ticker] = self._data_handler.get_price(ticker, PriceField.ohlcv(), self._start_date,
                                                                    self._end_date)

        prices_data_array = tickers_dict_to_data_array(tickers_dict, self._tickers, PriceField.ohlcv())
        return prices_data_array
Esempio n. 13
0
        def get_price(tickers, fields, start_date, end_date, _):
            prices_bar = [5.0, 10.0, 1.0, 4.0, 50]  # Open, High, Low, Close, Volume

            dates_index = pd.date_range(start_date, end_date, freq='B')
            tickers, got_single_ticker = convert_to_list(tickers, Ticker)
            fields, got_single_field = convert_to_list(fields, PriceField)
            got_single_date = len(dates_index) == 1

            prices_df = pd.DataFrame(
                index=pd.Index(dates_index, name=TICKERS),
                columns=pd.Index(PriceField.ohlcv(), name=FIELDS),
                data=[prices_bar] * len(dates_index)
            )
            data_array = tickers_dict_to_data_array({
                ticker: prices_df for ticker in self.tickers
            }, self.tickers, PriceField.ohlcv())

            return normalize_data_array(data_array.loc[start_date:end_date, tickers, fields], tickers, fields,
                                        got_single_date, got_single_ticker, got_single_field)
def _get_test_case_set_up():
    timer = SettableTimer()

    tickers = [QuandlTicker("MSFT", "WIKI"), QuandlTicker("AAPL", "WIKI")]
    price_fields = PriceField.ohlcv()
    price_data_provider_mock = _create_price_provider_mock(
        tickers, price_fields=price_fields)

    data_handler = DataHandler(price_data_provider_mock, timer)

    return timer, tickers, price_fields, data_handler
Esempio n. 15
0
    def test_historical_price__before_data_starts__daily(self):
        self.current_time = str_to_date("2021-04-30 00:00:00.000000",
                                        DateFormat.FULL_ISO)

        with self.assertRaises(ValueError):
            self.data_provider.historical_price(self.ticker_2,
                                                PriceField.Open,
                                                2,
                                                frequency=Frequency.DAILY)
            self.data_provider.historical_price([self.ticker_1, self.ticker_2],
                                                PriceField.ohlcv(),
                                                1,
                                                frequency=Frequency.DAILY)
    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'))
Esempio n. 17
0
    def test_historical_price__single_ticker__multiple_fields__daily(self):
        self.current_time = str_to_date("2021-05-06 00:00:00.000000",
                                        DateFormat.FULL_ISO)

        # Test when the current day does not have the open price
        actual_bars = self.data_provider.historical_price(
            self.ticker_2, PriceField.ohlcv(), 2, frequency=Frequency.DAILY)
        expected_bars = PricesDataFrame(
            data=[[29.0, 29.1, 29.2, 30.0, 29.3],
                  [27.0, 27.1, 27.2, None, 27.3]],
            index=[str_to_date('2021-05-02'),
                   str_to_date('2021-05-05')],
            columns=PriceField.ohlcv())
        assert_dataframes_equal(expected_bars, actual_bars, check_names=False)

        self.current_time = str_to_date("2021-05-06 00:00:00.000000",
                                        DateFormat.FULL_ISO)

        actual_bars = self.data_provider.historical_price(
            self.ticker_2, PriceField.ohlcv(), 3, frequency=Frequency.DAILY)
        expected_bars = PricesDataFrame(data=[[27.0, 27.1, 27.2, 28.0, 27.3],
                                              [29.0, 29.1, 29.2, 30.0, 29.3],
                                              [27.0, 27.1, 27.2, None, 27.3]],
                                        index=[
                                            str_to_date('2021-05-01'),
                                            str_to_date('2021-05-02'),
                                            str_to_date('2021-05-05')
                                        ],
                                        columns=PriceField.ohlcv())
        assert_dataframes_equal(expected_bars, actual_bars, check_names=False)

        # More than 3 bars are not available
        with self.assertRaises(ValueError):
            self.data_provider.historical_price(self.ticker_2,
                                                PriceField.ohlcv(),
                                                4,
                                                frequency=Frequency.DAILY)
Esempio n. 18
0
    def test_historical_price__multiple_tickers__multiple_fields__daily(self):
        self.current_time = str_to_date("2021-05-06 00:00:00.000000",
                                        DateFormat.FULL_ISO)

        # Test when the current day does not have the open price
        actual_bars = self.data_provider.historical_price(
            [self.ticker_1, self.ticker_2],
            PriceField.ohlcv(),
            2,
            frequency=Frequency.DAILY)
        self.assertEqual(type(actual_bars), QFDataArray)
        assert_series_equal(actual_bars.dates.to_pandas(),
                            date_range('2021-05-04', '2021-05-05',
                                       freq="D").to_series(),
                            check_names=False)
    def setUp(self):
        all_fields = PriceField.ohlcv()

        self._mocked_prices_arr = self._make_mock_data_array(
            self.tickers, all_fields)
        self._price_provider_mock = PresetDataProvider(self._mocked_prices_arr,
                                                       self.data_start_date,
                                                       self.end_date)

        risk_estimation_factor = 0.05
        self.alpha_model = DummyAlphaModel(risk_estimation_factor)

        self.ts = self._test_trading_session_init()
        model_tickers_dict = {self.alpha_model: self.tickers}
        AlphaModelStrategy(self.ts, model_tickers_dict, use_stop_losses=True)
        self.ts.start_trading()
 def setUp(self):
     self.timer, self.tickers, self.data_handler = _get_test_case_set_up()
     self.tickers_index = pd.Index(self.tickers, name='tickers')
     self.fields_index = pd.Index(PriceField.ohlcv(), name='fields')
     MarketOpenEvent.set_trigger_time({
         "hour": 13,
         "minute": 30,
         "second": 0,
         "microsecond": 0
     })
     MarketCloseEvent.set_trigger_time({
         "hour": 20,
         "minute": 0,
         "second": 0,
         "microsecond": 0
     })
    def setUp(self):
        try:
            self.data_provider = get_data_provider()
        except Exception as e:
            raise self.skipTest(e)

        self.TICKER_1.initialize_data_provider(self.timer, self.data_provider)
        self.TICKER_2.initialize_data_provider(self.timer, self.data_provider)
        data_provider = PrefetchingDataProvider(self.data_provider,
                                                self.TICKER_2,
                                                PriceField.ohlcv(),
                                                self.start_date, self.end_date,
                                                self.frequency)

        self.timer.set_current_time(self.end_date)
        self.data_handler = DailyDataHandler(data_provider, self.timer)
Esempio n. 22
0
    def use_data_preloading(self,
                            tickers: Union[Ticker, Sequence[Ticker]],
                            time_delta: RelativeDelta = None):
        if time_delta is None:
            time_delta = RelativeDelta(years=1)
        data_start = self.start_date - time_delta

        # The tickers and price fields are sorted in order to always return the same hash of the data bundle for
        # the same set of tickers and fields
        tickers, _ = convert_to_list(tickers, Ticker)
        self.data_handler.use_data_bundle(sorted(tickers),
                                          sorted(PriceField.ohlcv()),
                                          data_start, self.end_date,
                                          self.frequency)
        self._hash_of_data_bundle = compute_container_hash(
            self.data_handler.data_provider.data_bundle)
        self.logger.info("Preloaded data hash value {}".format(
            self._hash_of_data_bundle))
Esempio n. 23
0
    def _add_page(self, ticker: Ticker):
        self._add_header()

        self.document.add_element(ParagraphElement("\n"))
        self.document.add_element(HeadingElement(2, ticker.as_string()))
        self.document.add_element(ParagraphElement("\n"))

        price_df = self.price_provider.get_price(ticker, PriceField.ohlcv(), self.start_date, self.end_date)

        self._insert_table_with_overall_measures(price_df, ticker)
        self.document.add_element(ParagraphElement("\n"))

        self._add_price_chart(price_df)
        self.document.add_element(ParagraphElement("\n"))

        self._add_trend_strength_chart(price_df)
        self.document.add_element(ParagraphElement("\n"))

        self._add_up_and_down_trend_strength(price_df)
        self.document.add_element(NewPageElement())  # add page break
Esempio n. 24
0
def _acquire_data(input_file: str):
    data_frame = read_csv(input_file, parse_dates=['dates'])

    # The data_frame contains following headers: dates, open, high, low, close, volume, ticker.
    dates = data_frame['dates'].drop_duplicates()
    tickers = [DummyTicker(t) for t in data_frame['tickers'].unique()]

    # The columns in the csv file are in the exactly same order as the corresponding fields in the list below
    fields = PriceField.ohlcv()

    # Create a data array
    data_array = data_frame.set_index(['dates', 'tickers']).stack().to_xarray()

    # Construct a QFDataArray based on the data_array and sort it by the 'dates' coordinate
    data = QFDataArray.create(dates=dates,
                              tickers=tickers,
                              fields=fields,
                              data=data_array).sortby('dates')

    start_date = dates.iloc[0]
    end_date = dates.iloc[-1]

    return data, start_date, end_date
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)
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)
Esempio n. 27
0
 def _get_data_for_backtest(self):
     self._timer.set_current_time(self._end_date)
     prices_data_array = self._data_handler.get_price(
         self._tickers, PriceField.ohlcv(), self._start_date,
         self._end_date)
     return prices_data_array
Esempio n. 28
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)
Esempio n. 29
0
    def _mock_get_price(self, tickers, fields, start_date, end_date,
                        frequency):
        tickers, got_single_ticker = convert_to_list(tickers, Ticker)
        fields, got_single_field = convert_to_list(fields, PriceField)

        mock_daily_data = QFDataArray.create(
            dates=date_range(start='2021-05-01', end='2021-05-06', freq='D'),
            tickers=[self.ticker_1, self.ticker_2],
            fields=PriceField.ohlcv(),
            data=[
                # 2021-05-01
                [
                    # Open High  Low   Close Volume
                    [25.0, 25.1, 25.2, 26.0, 25.3],  # TICKER 1
                    [27.0, 27.1, 27.2, 28.0, 27.3]  # TICKER 2
                ],
                # 2021-05-02
                [
                    # Open High  Low   Close Volume
                    [None, None, None, None, None],  # TICKER 1
                    [29.0, 29.1, 29.2, 30.0, 29.3]  # TICKER 2
                ],
                # 2021-05-03
                [
                    # Open High  Low   Close Volume
                    [31.0, 31.1, 31.2, 32.0, 31.3],  # TICKER 1
                    [None, None, None, None, None]  # TICKER 2
                ],
                # 2021-05-04
                [
                    # Open High  Low   Close Volume
                    [31.0, None, None, None, None],  # TICKER 1
                    [None, None, None, None, None]  # TICKER 2
                ],
                # 2021-05-05
                [
                    # Open High  Low   Close Volume
                    [25.0, 25.1, 25.2, None, 25.3],  # TICKER 1
                    [27.0, 27.1, 27.2, None, 27.3]  # TICKER 2
                ],
                # 2021-05-06
                [
                    # Open High  Low   Close Volume
                    [None, None, None, None, None],  # TICKER 1
                    [None, None, None, None, None]  # TICKER 2
                ],
            ])

        mock_intraday_data = QFDataArray.create(
            dates=date_range(start='2021-05-01', end='2021-05-06', freq='D'),
            tickers=[self.ticker_1, self.ticker_2],
            fields=PriceField.ohlcv(),
            data=[
                # 2021-05-01
                [
                    # Open High  Low   Close Volume
                    [25.0, 25.1, 25.2, 26.0, 25.3],  # TICKER 1
                    [27.0, 27.1, 27.2, 28.0, 27.3]  # TICKER 2
                ],
                # 2021-05-02
                [
                    # Open High  Low   Close Volume
                    [None, None, None, None, None],  # TICKER 1
                    [29.0, 29.1, 29.2, 30.0, 29.3]  # TICKER 2
                ],
                # 2021-05-03
                [
                    # Open High  Low   Close Volume
                    [31.0, 31.1, 31.2, 32.0, 31.3],  # TICKER 1
                    [None, None, None, None, None]  # TICKER 2
                ],
                # 2021-05-04
                [
                    # Open High  Low   Close Volume
                    [31.0, None, None, None, None],  # TICKER 1
                    [None, None, None, None, None]  # TICKER 2
                ],
                # 2021-05-05
                [
                    # Open High  Low   Close Volume
                    [25.0, 25.1, 25.2, None, 25.3],  # TICKER 1
                    [27.0, 27.1, 27.2, None, 27.3]  # TICKER 2
                ],
                # 2021-05-06
                [
                    # Open High  Low   Close Volume
                    [None, None, None, None, None],  # TICKER 1
                    [None, None, None, None, None]  # TICKER 2
                ],
            ])

        data = mock_daily_data.loc[start_date:end_date, tickers, fields] if frequency == Frequency.DAILY else \
            mock_intraday_data.loc[start_date:end_date, tickers, fields]
        return normalize_data_array(data, tickers, fields, False,
                                    got_single_ticker, got_single_field, True)
 def test_get_price_with_uncached_fields(self):
     with self.assertRaises(ValueError):
         _ = self.prefetching_data_provider.get_price(
             self.cached_tickers, PriceField.ohlcv(), self.start_date,
             self.end_date, self.frequency)