def test_graph4(self): """ This test is failing with Python 3.4 if many pictures are drawn. """ fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") fix_tkinter_issues_virtualenv() from matplotlib import pyplot as plt cache = os.path.abspath(os.path.split(__file__)[0]) cache = os.path.join(cache, "temp_cache4") stock = StockPrices(TestStockGraph4.tick, folder=cache, url=TestStockGraph4.source) ret = stock.returns()["2012-04-01":"2014-04-15"] fig, ax = plt.subplots(figsize=(16, 8)) ax = stock.plot(ax=ax) ax = ret.plot(ax=ax, axis=2) img = os.path.abspath( os.path.join(os.path.split(__file__)[0], "temp_image4.png")) if os.path.exists(img): os.remove(img) fig.savefig(img) plt.close('all') self.assertExists(img)
def test_covariance(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") cache = get_temp_folder(__file__, "temp_cache_cov") stocks = [StockPrices(t, folder=cache, url=TestStockHttp.source) for t in TestStockHttp.ticks] dates = StockPrices.available_dates(stocks) ok = dates[dates["missing"] == 0] stocks = [v.keep_dates(ok) for v in stocks] cov = StockPrices.covariance(stocks) self.assertEqual(len(cov), 3) cor = StockPrices.covariance(stocks, cov=False) self.assertEqual(len(cor), 3) t = TestStockHttp.ticks[1] self.assertTrue( abs(cor.loc[t, t] - 1) < 1e-5) self.assertTrue(abs(cor.iloc[2, 2] - 1) < 1e-5) ret, mat = StockPrices.covariance(stocks, cov=False, ret=True) self.assertEqual(len(ret), 3)
def test_save_stock_yahoo_new(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") temp = get_temp_folder(__file__, "temp_cache_file_yahoo") cache = temp stock = StockPrices("AAPL", folder=cache, url="yahoo_new", end=datetime.datetime(2014, 1, 15)) file = os.path.join(cache, "save.txt") if os.path.exists(file): os.remove(file) stock.to_csv(file) self.assertTrue(os.path.exists(file)) stock2 = StockPrices(file, sep="\t") self.assertEqual(stock.dataframe.shape, stock2.dataframe.shape) df = stock2.dataframe file = os.path.join(cache, "out_excel.xlsx") if os.path.exists(file): os.remove(file) df.to_excel(file) self.assertTrue(os.path.exists(file))
def test_save_stock_yahoo_new(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") temp = get_temp_folder(__file__, "temp_cache_file_yahoo") cache = temp stock = StockPrices( "AAPL", folder=cache, url="yahoo_new", end=datetime.datetime( 2014, 1, 15)) file = os.path.join(cache, "save.txt") if os.path.exists(file): os.remove(file) stock.to_csv(file) self.assertTrue(os.path.exists(file)) stock2 = StockPrices(file, sep="\t") self.assertEqual(stock.dataframe.shape, stock2.dataframe.shape) df = stock2.dataframe file = os.path.join(cache, "out_excel.xlsx") if os.path.exists(file): os.remove(file) df.to_excel(file) self.assertTrue(os.path.exists(file))
def test_save_stock_quandl(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") temp = get_temp_folder(__file__, "temp_cache_file_quandl") cache = temp try: stock = StockPrices("EURONEXT/BNP", url="quandl", folder=cache, end=datetime.datetime(2017, 1, 15)) except LimitExceededError: warnings.warn( "[test_save_stock_quandl] reached quandl free quota. Stop test.") return file = os.path.join(cache, "save.txt") if os.path.exists(file): os.remove(file) stock.to_csv(file) self.assertTrue(os.path.exists(file)) stock2 = StockPrices(file, sep="\t") self.assertEqual(stock.dataframe.shape, stock2.dataframe.shape) df = stock2.dataframe file = os.path.join(cache, "out_excel.xlsx") if os.path.exists(file): os.remove(file) df.to_excel(file) self.assertTrue(os.path.exists(file))
def test_graph_ohlc(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") fix_tkinter_issues_virtualenv() from matplotlib import pyplot as plt cache = os.path.abspath(os.path.split(__file__)[0]) cache = os.path.join(cache, "temp_cache_ohlc") stock = StockPrices(TestStockGraphOHLC.tick, folder=cache, url=TestStockGraphOHLC.source) fig, ax = plt.subplots(figsize=(16, 8)) ax = stock.plot(ax=ax, field="ohlc") img = os.path.abspath( os.path.join( os.path.split(__file__)[0], "temp_image_ohlc.png")) if os.path.exists(img): os.remove(img) fig.savefig(img) plt.close('all') self.assertExists(img)
def test_save_stock_google(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") temp = get_temp_folder(__file__, "temp_cache_file_google") cache = temp try: stock = StockPrices("NASDAQ:MSFT", folder=cache, end=datetime.datetime(2014, 1, 15), url="google") except StockPricesHTTPException as e: warnings.warn(str(e)) return file = os.path.join(cache, "save.txt") if os.path.exists(file): os.remove(file) stock.to_csv(file) self.assertTrue(os.path.exists(file)) stock2 = StockPrices(file, sep="\t") self.assertEqual(stock.dataframe.shape, stock2.dataframe.shape) df = stock2.dataframe file = os.path.join(cache, "out_excel.xlsx") if os.path.exists(file): os.remove(file) df.to_excel(file) self.assertTrue(os.path.exists(file))
def test_save_stock_quandl(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") temp = get_temp_folder(__file__, "temp_cache_file_quandl") cache = temp try: stock = StockPrices("EURONEXT/BNP", url="quandl", folder=cache, end=datetime.datetime(2017, 1, 15)) except LimitExceededError: # pylint: disable=W0703 warnings.warn( "[test_save_stock_quandl] reached quandl free quota. Stop test." ) return file = os.path.join(cache, "save.txt") if os.path.exists(file): os.remove(file) stock.to_csv(file) self.assertTrue(os.path.exists(file)) stock2 = StockPrices(file, sep="\t") self.assertEqual(stock.dataframe.shape, stock2.dataframe.shape) df = stock2.dataframe file = os.path.join(cache, "out_excel.xlsx") if os.path.exists(file): os.remove(file) df.to_excel(file) self.assertTrue(os.path.exists(file))
def test_save_stock_google(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") temp = get_temp_folder(__file__, "temp_cache_file_google") cache = temp try: stock = StockPrices("NASDAQ:MSFT", folder=cache, end=datetime.datetime(2014, 1, 15), url="google") except StockPricesHTTPException as e: warnings.warn(str(e)) return file = os.path.join(cache, "save.txt") if os.path.exists(file): os.remove(file) stock.to_csv(file) self.assertTrue(os.path.exists(file)) stock2 = StockPrices(file, sep="\t") self.assertEqual(stock.dataframe.shape, stock2.dataframe.shape) df = stock2.dataframe file = os.path.join(cache, "out_excel.xlsx") if os.path.exists(file): os.remove(file) df.to_excel(file) self.assertTrue(os.path.exists(file))
def test_graph4(self): """ This test is failing with Python 3.4 if many pictures are drawn. """ fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") fix_tkinter_issues_virtualenv() from matplotlib import pyplot as plt cache = os.path.abspath(os.path.split(__file__)[0]) cache = os.path.join(cache, "temp_cache4") stock = StockPrices(TestStockGraph4.tick, folder=cache, url=TestStockGraph4.source) ret = stock.returns()["2012-04-01":"2014-04-15"] fig, ax = plt.subplots(figsize=(16, 8)) ax = stock.plot(ax=ax) ax = ret.plot(ax=ax, axis=2) img = os.path.abspath( os.path.join( os.path.split(__file__)[0], "temp_image4.png")) if os.path.exists(img): os.remove(img) fig.savefig(img) plt.close('all') self.assertExists(img)
def test_graph(self): """ This test is failing with Python 3.4 if many pictures are drawn. """ fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") fix_tkinter_issues_virtualenv() from matplotlib import pyplot as plt cache = os.path.abspath(os.path.split(__file__)[0]) cache = os.path.join(cache, "temp_cache2") stocks = [ StockPrices(t, folder=cache, url=TestStockGraph.source) for t in TestStockGraph.tick ] if True: fLOG("A", sys.executable) fig, ax = plt.subplots() ax = StockPrices.draw(stocks, figsize=(16, 8), field=["Open", "Close"], ax=ax) img = os.path.abspath( os.path.join(os.path.split(__file__)[0], "temp_image.png")) if os.path.exists(img): os.remove(img) fig.savefig(img) plt.close('all') self.assertExists(img) if True: fLOG("B") fig, ax = plt.subplots() ax = StockPrices.draw(stocks, begin="2010-01-01", ax=ax) img = os.path.abspath( os.path.join(os.path.split(__file__)[0], "temp_image2.png")) if os.path.exists(img): os.remove(img) fig.savefig(img) plt.close('all') self.assertExists(img) if True: fLOG("C") fig, ax = plt.subplots() ax = StockPrices.draw(stocks[:1], begin="2010-01-01", ax=ax) img = os.path.abspath( os.path.join(os.path.split(__file__)[0], "temp_image3.png")) if os.path.exists(img): os.remove(img) fig.savefig(img) plt.close('all') self.assertExists(img) fLOG("thisend")
def test_available_dates(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") cache = get_temp_folder(__file__, "temp_cache_dates") stocks = [StockPrices(t, folder=cache, url=TestStockHttp.source) for t in TestStockHttp.ticks] av = StockPrices.available_dates(stocks) self.assertTrue(len(av) > 0) missing = stocks[-1].missing(av) self.assertTrue(missing is None or len(missing) > 0)
def test_save_stock_datetime(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") temp = get_temp_folder(__file__, "temp_save_stock_datetime") cache = temp stock = StockPrices(TestStockFileDatetime.tick, use_dtime=True, folder=cache, end=datetime.datetime(2014, 1, 15), url=TestStockFileDatetime.source) file = os.path.join(cache, "save.txt") if os.path.exists(file): os.remove(file) stock.to_csv(file) self.assertTrue(os.path.exists(file)) stock2 = StockPrices(file, sep="\t", use_dtime=True) self.assertEqual(stock.dataframe.shape, stock2.dataframe.shape) df = stock2.dataframe file = os.path.join(cache, "out_excel.xlsx") if os.path.exists(file): os.remove(file) df.to_excel(file) self.assertTrue(os.path.exists(file)) ret = stock2.returns() df.loc["2013-04-01":"2013-04-30", "Close"] = 0 ret.dataframe.loc["2013-04-01":"2013-04-30", "Close"] = 0
def test_index(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") cache = get_temp_folder(__file__, "temp_cache_index") stock = StockPrices(TestStockHttp.ticks[0], folder=cache, end=datetime.datetime(2014, 1, 15), url=TestStockHttp.source) some = stock["2001-01-01":"2002-02-02"] self.assertTrue(isinstance(some, StockPrices)) self.assertTrue(len(some) < 1000)
def test_graph3(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") fix_tkinter_issues_virtualenv() from matplotlib import pyplot as plt cache = os.path.abspath(os.path.split(__file__)[0]) cache = os.path.join(cache, "temp_cache3") stock = StockPrices(TestStockGraph3.tick[0], folder=cache, url=TestStockGraph3.source) stock2 = StockPrices(TestStockGraph3.tick[1], folder=cache, url=TestStockGraph3.source) fig, ax = plt.subplots(figsize=(16, 8)) ax = stock.plot(ax=ax) ax = stock2.plot(ax=ax, axis=2) img = os.path.abspath( os.path.join(os.path.split(__file__)[0], "temp_image3.png")) if os.path.exists(img): os.remove(img) fig.savefig(img) plt.close('all') self.assertExists(img)
def test_save_stock_google(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") cache = os.path.abspath(os.path.split(__file__)[0]) cache = os.path.join(cache, "temp_cache_file_google") name = os.path.join(cache, TestLONGStockFile.tick.replace( ":", "_") + ".2000-01-03.2014-01-15.txt") if os.path.exists(name): os.remove(name) try: stock = StockPrices(TestLONGStockFile.tick, url=TestLONGStockFile.source, folder=cache, end=datetime.datetime(2014, 1, 15)) except ImportError as e: # There is an issue with pandas_datareader on travis. # Not up to date with the latest pandas. if is_travis_or_appveyor(): warnings.warn( "Probably an issue with pandas_datareader.\n" + str(e)) return else: raise e file = os.path.join(cache, "save.txt") if os.path.exists(file): os.remove(file) stock.to_csv(file) self.assertExists(file) stock2 = StockPrices(file, sep="\t") self.assertEqual(stock.dataframe.shape, stock2.dataframe.shape) df = stock2.dataframe file = os.path.join(cache, "out_excel.xlsx") if os.path.exists(file): os.remove(file) df.to_excel(file) self.assertExists(file)
def test_download_stock(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") cache = get_temp_folder(__file__, "temp_cache_download_http") stock = StockPrices(TestStockHttp.ticks[0], folder=cache, end=datetime.datetime(2014, 1, 15), url=TestStockHttp.source) u = TestStockHttp.ticks[0].replace(":", "_") name = os.path.join(cache, u + ".2000-01-03.2014-01-15.txt") self.assertTrue(os.path.exists(name)) df = stock.dataframe self.assertTrue(len(df) > 0)
def test_download_stock_google(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") cache = get_temp_folder(__file__, "temp_url_google") try: stock = StockPrices("NASDAQ:MSFT", folder=cache, begin=datetime.datetime(2014, 1, 15)) except StockPricesHTTPException as e: warnings.warn(str(e)) return df = stock.dataframe dmin = df.Date.min() self.assertIn("2014", str(dmin)) self.assertTrue(stock.url_.startswith( "https://finance.google.com/finance/historical?q=NASDAQ:MSFT&startdate=Jan+15%2C+2014"))
def test_no_wifi(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") data = os.path.abspath(os.path.split(__file__)[0]) data = os.path.join(data, "data") file = os.path.join(data, "BNP.PA.2000-01-03.2014-02-24.txt") fLOG(os.path.exists(file)) fLOG(file) try: StockPrices(file) except StockPricesException as e: if "pandas cannot parse the file" not in str(e): raise Exception("unexpected error (2)") from e if "Error tokenizing data" in str(e): raise Exception("unexpected error (3)") from e
def test_download_stock_yahoo(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") cache = get_temp_folder(__file__, "temp_url_yahoo") exc = [] for n in range(0, 4): stock = StockPrices("^FCHI", folder=cache, url=TestStockUrlYahoo.source, begin=datetime.datetime(2014, 1, 15)) df = stock.dataframe dmin, dmax = df.Date.min(), df.Date.max() try: self.assertIn("2014", str(dmin)) self.assertNotEqual(dmin, dmax) if "^FCHI.2014-01-15" not in stock.url_: raise AssertionError(stock.url_) return except AssertionError as e: exc.append(e) if len(exc) > 0: e = exc[0] raise AssertionError('nb tries={0}'.format(len(exc))) from e
def test_graph(self): """ This test is failing with Python 3.4 if many pictures are drawn. """ fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") fix_tkinter_issues_virtualenv() from matplotlib import pyplot as plt cache = os.path.abspath(os.path.split(__file__)[0]) cache = os.path.join(cache, "temp_cache2") stocks = [StockPrices(t, folder=cache, url=TestStockGraph.source) for t in TestStockGraph.tick] if True: fLOG("A", sys.executable) fig, ax = plt.subplots() ax = StockPrices.draw( stocks, figsize=( 16, 8), field=[ "Open", "Close"], ax=ax) img = os.path.abspath( os.path.join( os.path.split(__file__)[0], "temp_image.png")) if os.path.exists(img): os.remove(img) fig.savefig(img) plt.close('all') self.assertExists(img) if True: fLOG("B") fig, ax = plt.subplots() ax = StockPrices.draw(stocks, begin="2010-01-01", ax=ax) img = os.path.abspath( os.path.join( os.path.split(__file__)[0], "temp_image2.png")) if os.path.exists(img): os .remove(img) fig.savefig(img) plt.close('all') self.assertExists(img) if True: fLOG("C") fig, ax = plt.subplots() ax = StockPrices.draw(stocks[:1], begin="2010-01-01", ax=ax) img = os.path.abspath( os.path.join( os.path.split(__file__)[0], "temp_image3.png")) if os.path.exists(img): os .remove(img) fig.savefig(img) plt.close('all') self.assertExists(img) fLOG("thisend")