Looping API Request in Python to retrieve data in bulk
Looping API Request in Python to retrieve data in bulk
Python stock price analysis 02: Extract stock prices with a list of ticker symbols
Introduction
My previous?article?talked about the basic idea of extracting stock price data with Python code. However, I only talked about extracting stock prices for 1 stock.
What if I want to analyze the relationship between all FAANG stocks? What if I want to extract stock prices for all the stocks in SP500?
In today’s article, let’s talk about extracting stock prices in bulk.
Revision — How to extract stock prices
Recall that this is the code to extract stock prices for the stock that we specify. In this case, I chose MSFT.
import requests
url = "https://alpha-vantage.p.rapidapi.com/query"
querystring = {"function":"TIME_SERIES_DAILY_ADJUSTED","symbol":"MSFT","outputsize":"full","datatype":"json"}
headers = {
"X-RapidAPI-Key": 'your-api-key',
"X-RapidAPI-Host": "alpha-vantage.p.rapidapi.com"
}
response = requests.request("GET", url, headers=headers, params=querystring)
print(response.text)
If you’re running this for the first time, refer to my?previous article?to set up the API and understand more about extracting stock prices.
Multiple stocks
We can’t just pass a list of ticker symbols into the query string. Passing this list [‘MSFT’, ‘AAPL’] into the symbol parameter will return only AAPL (the last ticker) price data.
querystring = {"function":"TIME_SERIES_DAILY_ADJUSTED","symbol":["MSFT","AAPL"],"outputsize":"full","datatype":"json"}
We need to write a list of ticker symbols and loop through the list to request stock price data for each of the symbols. Say, we want to retrieve the stock prices for FAANG-MT stocks. We will create a list that stores the ticker symbols for these stocks.
stocks = ['META','AAPL','AMZN','NFLX','GOOG','MSFT','TSLA']
Then we’ll loop through these ticker symbols and save those data into a JSON file. Before we get to the code, remember to create a JSON folder in the same directory as your python code.
领英推荐
Then we can write a loop to request data with the API and write to a JSON file.
import requests
import json
import os
json_path = f"{os.getcwd()}\\JSON"
url = "https://alpha-vantage.p.rapidapi.com/query"
headers = {
"X-RapidAPI-Key": "your-api-key",
"X-RapidAPI-Host": "alpha-vantage.p.rapidapi.com"
}
for symbol in stocks:
# Change ticker symbol in the query string in each loop
querystring = {"function":"TIME_SERIES_DAILY_ADJUSTED","symbol":symbol,"outputsize":"full","datatype":"json"}
print(f"Symbol = {symbol}")
# Get a new request in every loop
response = requests.request("GET", url, headers=headers, params=querystring)
print(f"Done request {symbol} data")
# Write the response into a JSON file in the JSON folder
with open(f"{json_path}/{symbol} stock prices.json", "w") as outfile:
json.dump(response.json(), outfile)
# Output message to indicate a successful loop
print(f"Wrote {symbol} to JSON file")
print("")
print('-'*30)
print("")
else:
# Output message to indicate the loop is complete
print(f"Wrote all symbols to JSON file")
Output
Then you can convert it into a pandas data frame using the method we discussed in the previous article.
Conclusion
That’s all for today’s article. In my next article, I will talk about how we can store these JSON files into one CSV file and the preparation needed to do that.
Thank you for reading today’s article. See you again in my next article.
Video Tutorial
About Me
Currently working as a Data Scientist. I provide consultancy, training, and professional services for data analytics problems to my clients worldwide. Would love to share my experience as a consultant so that everyone can learn something from it.
Medium: medium.com/@foocheechuan
Youtube: Chee-Chuan
Global Process Architect @ ABB | Business Process Management, Digital Transformation | Data, Cloud & Automation
2 年Good stuff. Also suggesting to introduce asyncio for better performance in bulk requests because people would usually request for many price data (and usually very frequent to keep updated)