Introduction: Custom Share Price Report: Alpha Vantage API

About: Hello! My name is FuzzyPotato, I have a passion for electronics, Python programming, and DIY projects. I have always been fascinated by the way things work, and I have a love for using technology to solve prob…

In this instructable, I'll guide you through the process of creating a Custom Share Price Report using the Alpha Vantage API. By leveraging this powerful tool, you'll be able to access real-time and historical stock market data to generate personalized reports tailored to your specific interests. Whether you're an investor, financial analyst, or simply curious about the stock market, this project will empower you to track and analyze share prices effectively. Let's get started on building your own dynamic share price report with the Alpha Vantage API.

Supplies

What you'll need:

  1. Computer with Internet access
  2. Integrated development environment (IDE) (I use PyCharm)
  3. Python libraries: Requests
  4. Alpha Vantage API key (Free registration required)

Step 1: Install Requests Library

How to install the Requests Library:

  1. Open PyCharm and open your project or create a new one.
  2. Go to the project's Python Interpreter settings:
  3. On Windows/Linux: Go to "File" > "Settings", then navigate to "Project" > "Python Interpreter".
  4. In the Python Interpreter settings, you will see a list of installed packages for your project. Click on the "+" button to add a new package.
  5. In the "Available Packages" window that appears, search for "requests" using the search box.
  6. Once you find the "requests" package, select it and click on the "Install Package" button.
  7. PyCharm will download and install the requests library for your project. You can monitor the installation progress in the "Event Log" tab at the bottom of the PyCharm window.
  8. Once the installation is complete, you can close the settings window.

Step 2: Getting an API Key

To obtain an Alpha Vantage API key, follow these steps:

  1. Visit the Alpha Vantage website at https://www.alphavantage.co/.
  2. Click on the "Get your free API key" button, usually located in the top right corner of the website.
  3. Fill out the registration form with your details, including what best describes you, organisation (Make one up) and your email address.
  4. Click on the "Get API Key" button.
  5. Your API key will display on the screen

Step 3: The Code

In this step, we will:

  1. Open a new Python file.
  2. Copy and paste the code into the Python file.
  3. Replace the X's in the API request URL with your actual Alpha Vantage API key.
  4. Run the script to make API requests for each symbol and display the latest stock prices in the console.


Feel free to customize the symbols list to retrieve the latest prices for your preferred stocks.


import requests

symbols = ["TSLA", "AAPL", "GOOG", "MSFT"]

for symbol in symbols:
# Make the API request to Alpha Vantage for each symbol
response = requests.get(f"https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol={symbol}&apikey=xxxxxxxxxxxxxxxx")

# Check if the request was successful
if response.status_code == 200:
# Parse the JSON response to get the latest stock price
data = response.json()
price = data["Global Quote"]["05. price"]
print(f"{symbol} stock price: {price}")

Step 4: Getting Share Prices

Here's an explanation of what the code will do:

  1. Importing the requests library, which allows us to send HTTP requests in Python.
  2. Creating a list of stock symbols (symbols) that we want to retrieve the latest stock price for.
  3. Starting a loop that iterates over each symbol in the symbols list.
  4. Inside the loop, we make an API request to the Alpha Vantage API for each symbol using the requests.get() function. The API request URL includes the symbol and our API key.
  5. Checking if the API request was successful by verifying that the HTTP status code of the response is 200.
  6. If the request was successful, we parse the JSON response using the response.json() method and store it in the data variable.
  7. Accessing the latest stock price from the JSON data by navigating through the dictionary structure (data["Global Quote"]["05. price"]).
  8. Printing the stock symbol and its corresponding latest stock price using string formatting.
  9. The loop continues to the next symbol in the list until all symbols have been processed.


Happy tinkering!

Step 5: