Looping API Request in Python to retrieve data in bulk

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.

No alt text provided for this image
Image by Author

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

No alt text provided for this image
Image by Author

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.

LinkedIn: https://www.dhirubhai.net/in/foocheechuan/

Medium: medium.com/@foocheechuan

Youtube: Chee-Chuan

No alt text provided for this image
Photo by Annie Spratt on Unsplash
Anthony L.

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)

要查看或添加评论,请登录

Chee-Chuan Foo的更多文章

  • Machine Learning For Beginners

    Machine Learning For Beginners

    Introduction For beginners, machine learning might seem intimidating with all the calculus, statistics, and algorithms…

  • Setup an AWS database for FREE

    Setup an AWS database for FREE

    Setup free tier PostgreSQL database on AWS RDS Introduction For a student or beginner data scientist, you need to equip…

    4 条评论
  • Basic Data Preparation with Python (Stock Price Data)

    Basic Data Preparation with Python (Stock Price Data)

    Python stock price analysis 03: Learn typical data preparation process with stock prices data Python Stock Price Data…

  • Extract stock price data with Python

    Extract stock price data with Python

    Python stock price analysis 01: Get stock price data online Introduction Stock markets from all over the world generate…

  • Pivot Table in?Python

    Pivot Table in?Python

    Drag & Drop Data Analysis in Python Introduction Exploratory Data Analysis in Python was typically done by using the…

  • Tableau Quick Tip: Dynamic Chart Title

    Tableau Quick Tip: Dynamic Chart Title

    Create a drop-down to change the x & y axes. Make chart title change according to selection.

  • Tableau Desktop Quick Tip: The Problem with Pie Chart & Legend

    Tableau Desktop Quick Tip: The Problem with Pie Chart & Legend

    Data analysts always take pie charts and legends for granted, especially with many categories in a field. When it comes…

  • Simple Steps — Data Exploration

    Simple Steps — Data Exploration

  • Problem Solving: List Comprehension (Python)

    Problem Solving: List Comprehension (Python)

    Problem Solving: List Comprehension (Python) In my previous article, "List Comprehension (Python) Explained" I've…

  • List Comprehension (Python) Explained

    List Comprehension (Python) Explained

    List Comprehension (Python) Explained List is one of the simplest and most common data structure in Python. Today in…

社区洞察

其他会员也浏览了