Created
February 11, 2020 17:34
-
-
Save PatrickAlphaC/55741f59514c2fe2ec13e1f9b44d51c2 to your computer and use it in GitHub Desktop.
uses multithreading to get a list of tickers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from alpha_vantage.timeseries import TimeSeries | |
| from concurrent.futures import ThreadPoolExecutor | |
| import os | |
| KEY = os.path.expandvars("$ALPHAVANTAGE_API_KEY") | |
| ts = TimeSeries(key=KEY, output_format='pandas') | |
| def get_daily_adjusted_ignore_failure(ticker): | |
| try: | |
| return ts.get_daily_adjusted(symbol=ticker,outputsize='full') | |
| except Exception: | |
| print("An error occured with ticker {}\n".format(ticker)) | |
| # If you don't have a key that can do 3,500 tickers | |
| # feel free to use a subset | |
| # tickers_subset = tickers[:5] | |
| tickers_subset = tickers | |
| def get_data(tickers_subset): | |
| with ThreadPoolExecutor(max_workers=20) as executor: | |
| generator = executor.map(lambda ticker:get_daily_adjusted_ignore_failure(ticker), tickers_subset) | |
| return [data_pair for data_pair in list(generator) if data_pair] | |
| # This is a list of tuples of data, and metadata with all the tickers from the list | |
| dataset = get_data(tickers_subset) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment