Skip to content

Instantly share code, notes, and snippets.

@PatrickAlphaC
Last active October 7, 2019 18:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PatrickAlphaC/f6de4d775a5bc3b5ff03b2b4c1627cb9 to your computer and use it in GitHub Desktop.
Save PatrickAlphaC/f6de4d775a5bc3b5ff03b2b4c1627cb9 to your computer and use it in GitHub Desktop.
A simple start to threading in python with alpha vantage
from alpha_vantage.timeseries import TimeSeries
import threading
import os
# I use an environment vairable to get my key,
# you can also just hardcode your key in the code to test this
KEY = os.path.expandvars("$ALPHA_VANTAGE_HIGHER_KEY")
ts = TimeSeries(key=KEY, output_format='pandas')
# 10 tickers from the NASDAQ-100
tickers = ['ATVI','ADBE','AMD','ALXN','ALGN', 'GOOG', 'AMZN', 'AAL', 'ADI', 'AMAT']
# The simplest way to approach downloading data
def get_tickers_series():
for ticker in tickers:
ts.get_daily(symbol=ticker, outputsize='full')
def get_tickers_paralell():
threads = list()
for ticker in tickers:
ticker_thread = threading.Thread(target=ts.get_daily, kwargs={'symbol':ticker, 'outputsize': 'full'})
threads.append(ticker_thread)
ticker_thread.start()
for tick_thread in threads:
# JOIN IS IMPORTANT TO CALL
# This waits for the threads to finish
tick_thread.join()
# Now we measure performance
import timeit
print("Time it took to get 10 tickers in a row:")
print(timeit.timeit('get_tickers_series()', setup='from __main__ import get_tickers_series', number=1))
print("Time it took to get 10 tickers in paralell:")
print(timeit.timeit('get_tickers_paralell()', setup='from __main__ import get_tickers_paralell', number=1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment