From c3394c8cdc81fbb895d35fe85066c256531c2eb4 Mon Sep 17 00:00:00 2001 From: n1rna Date: Fri, 18 Oct 2019 19:06:35 +0330 Subject: [PATCH 1/4] Add support for multi tickers in list_tickers function --- tests/test_tiingo.py | 15 +++++++++++++++ tiingo/api.py | 13 ++++++++----- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/tests/test_tiingo.py b/tests/test_tiingo.py index c92997a..c8daa39 100644 --- a/tests/test_tiingo.py +++ b/tests/test_tiingo.py @@ -107,6 +107,21 @@ class TestTickerPrices(TestCase): frequency="30Min") self.assertGreater(len(prices), 1) + @vcr.use_cassette('tests/fixtures/list_stock_tickers.yaml') + def test_list_all_stock_tickers(self): + tickers = self._client.list_tickers() + assert len(tickers) > 1 + assert any(ticker['assetType' == 'Stock' for ticker in tickers) + assert any(ticker['assetType' == 'Mutual Fund' for ticker in tickers) + assert any(ticker['assetType' == 'ETF' for ticker in tickers) + + def test_list_multi_stock_tickers(self): + tickers = self._client.list_tickers(['Stock', 'ETF']) + assert len(tickers) > 1 + assert any(ticker['assetType' == 'Stock' for ticker in tickers) + assert any(ticker['assetType' == 'ETF' for ticker in tickers) + assert all(ticker['assetType' != 'Mutual Fund' for ticker in tickers) + @vcr.use_cassette('tests/fixtures/list_stock_tickers.yaml') def test_list_stock_tickers(self): tickers = self._client.list_stock_tickers() diff --git a/tiingo/api.py b/tiingo/api.py index f02023a..8829603 100644 --- a/tiingo/api.py +++ b/tiingo/api.py @@ -94,7 +94,7 @@ class TiingoClient(RestClient): # TICKER PRICE ENDPOINTS # https://api.tiingo.com/docs/tiingo/daily - def list_tickers(self, assetType): + def list_tickers(self, assetTypes=[]): """Return a list of dicts of metadata tickers for all supported tickers of the specified asset type, as well as metadata about each ticker. This includes supported date range, the exchange the ticker is traded @@ -108,17 +108,20 @@ class TiingoClient(RestClient): raw_csv = get_buffer_from_zipfile(zipdata, 'supported_tickers.csv') reader = csv.DictReader(raw_csv) + if not len(assetTypes): + return [row for row in reader] + return [row for row in reader - if row.get('assetType') == assetType] + if row.get('assetType') in assetTypes] def list_stock_tickers(self): - return self.list_tickers('Stock') + return self.list_tickers(['Stock']) def list_etf_tickers(self): - return self.list_tickers('ETF') + return self.list_tickers(['ETF']) def list_fund_tickers(self): - return self.list_tickers('Mutual Fund') + return self.list_tickers(['Mutual Fund']) def get_ticker_metadata(self, ticker, fmt='json'): """Return metadata for 1 ticker From 9bfa49f585a102391088436de8d36583ac5c2219 Mon Sep 17 00:00:00 2001 From: n1rna Date: Sat, 19 Oct 2019 21:58:42 +0330 Subject: [PATCH 2/4] Fix missed line in test_tiingo --- tests/test_tiingo.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_tiingo.py b/tests/test_tiingo.py index c8daa39..9245185 100644 --- a/tests/test_tiingo.py +++ b/tests/test_tiingo.py @@ -115,6 +115,7 @@ class TestTickerPrices(TestCase): assert any(ticker['assetType' == 'Mutual Fund' for ticker in tickers) assert any(ticker['assetType' == 'ETF' for ticker in tickers) + @vcr.use_cassette('tests/fixtures/list_stock_tickers.yaml') def test_list_multi_stock_tickers(self): tickers = self._client.list_tickers(['Stock', 'ETF']) assert len(tickers) > 1 From 60d0b0af029ed2a492fc9d13625f6f122f04e90a Mon Sep 17 00:00:00 2001 From: n1rna Date: Sat, 19 Oct 2019 21:59:12 +0330 Subject: [PATCH 3/4] Use python set instead of list in list_tickers for better performance --- tiingo/api.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tiingo/api.py b/tiingo/api.py index 8829603..32b8be9 100644 --- a/tiingo/api.py +++ b/tiingo/api.py @@ -111,8 +111,9 @@ class TiingoClient(RestClient): if not len(assetTypes): return [row for row in reader] + t = set(assetTypes) return [row for row in reader - if row.get('assetType') in assetTypes] + if row.get('assetType') in t] def list_stock_tickers(self): return self.list_tickers(['Stock']) From d7e58c622ff3f75a5bcc39979db39b624cbe8389 Mon Sep 17 00:00:00 2001 From: n1rna Date: Sun, 20 Oct 2019 11:00:44 +0330 Subject: [PATCH 4/4] Change name of variable to --- tiingo/api.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tiingo/api.py b/tiingo/api.py index 32b8be9..85cf58b 100644 --- a/tiingo/api.py +++ b/tiingo/api.py @@ -111,9 +111,9 @@ class TiingoClient(RestClient): if not len(assetTypes): return [row for row in reader] - t = set(assetTypes) + assetTypesSet = set(assetTypes) return [row for row in reader - if row.get('assetType') in t] + if row.get('assetType') in assetTypesSet] def list_stock_tickers(self): return self.list_tickers(['Stock'])