def test_yahoo(self): """ Yahoo data test """ try: yahoo = YahooDataProvider( tickers=["AEO", "ABBY"], start=datetime.datetime(2018, 1, 1), end=datetime.datetime(2018, 12, 31) ) yahoo.run() similarity = np.array([ [1.00000000e+00, 8.44268222e-05], [8.44268222e-05, 1.00000000e+00] ]) covariance = np.array( [[7.035e+00, -1.653e-04], [-1.653e-04, 1.199e-06]]) with self.subTest('test YahooDataProvider get_covariance_matrix'): np.testing.assert_array_almost_equal(yahoo.get_covariance_matrix(), covariance, decimal=3) with self.subTest('test YahooDataProvider get_similarity_matrix'): np.testing.assert_array_almost_equal(yahoo.get_similarity_matrix(), similarity, decimal=3) except NameError as ex: self.skipTest(str(ex)) except QiskitFinanceError as ex: self.skipTest("Test of YahooDataProvider skipped {}".format(str(ex)))
def test_yahoo_wrong_use(self): """ Yahoo! wrong use test """ try: yahoo = YahooDataProvider(tickers=["AEO", "ABBY"], start=datetime.datetime(2018, 1, 1), end=datetime.datetime(2018, 12, 31)) # Now, the .run() method is expected, which does the actual data loading with self.subTest('test YahooDataProvider get_covariance_matrix'): self.assertRaises(QiskitFinanceError, yahoo.get_covariance_matrix) with self.subTest('test YahooDataProvider get_similarity_matrix'): self.assertRaises(QiskitFinanceError, yahoo.get_similarity_matrix) except MissingOptionalLibraryError as ex: self.skipTest(str(ex))
def generate_values(stock_tickers, start_date, end_date): """ Given a list of stocks, generates the Yahoo Finance prices DataFrame prices_df. Every price series is a stored in a column. The prices df is resampled on month for the time being, and has the latest monthly datapoint as value. :param stock_tickers: list, list of stock names. :start_date: datetime.date or datetime.datetime :end_date: datetime.date or datetime.datetime :return: prices, returns mean, returns covariance """ data = YahooDataProvider(tickers=stock_tickers, start=start_date, end=end_date) data.run() prices = np.array([s.iloc[-1] for s in data._data]) return prices, data.get_period_return_mean_vector( ), data.get_period_return_covariance_matrix()
def test_yahoo(self, tickers, covariance, similarity): """ Yahoo data test """ try: yahoo = YahooDataProvider( tickers=tickers, start=datetime.datetime(2018, 1, 1), end=datetime.datetime(2018, 12, 31), ) yahoo.run() with self.subTest("test YahooDataProvider get_covariance_matrix"): np.testing.assert_array_almost_equal( yahoo.get_covariance_matrix(), np.array(covariance), decimal=0) with self.subTest("test YahooDataProvider get_similarity_matrix"): np.testing.assert_array_almost_equal( yahoo.get_similarity_matrix(), np.array(similarity), decimal=1) except MissingOptionalLibraryError as ex: self.skipTest(str(ex)) except QiskitFinanceError as ex: self.skipTest("Test of YahooDataProvider skipped {}".format( str(ex)))