Custom Share Price Report: Alpha Vantage API
by FuzzyPotato in Circuits > Computers
300 Views, 0 Favorites, 0 Comments
Custom Share Price Report: Alpha Vantage API
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:
- Computer with Internet access
- Integrated development environment (IDE) (I use PyCharm)
- Python libraries: Requests
- Alpha Vantage API key (Free registration required)
Install Requests Library
How to install the Requests Library:
- Open PyCharm and open your project or create a new one.
- Go to the project's Python Interpreter settings:
- On Windows/Linux: Go to "File" > "Settings", then navigate to "Project" > "Python Interpreter".
- 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.
- In the "Available Packages" window that appears, search for "requests" using the search box.
- Once you find the "requests" package, select it and click on the "Install Package" button.
- 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.
- Once the installation is complete, you can close the settings window.
Getting an API Key
To obtain an Alpha Vantage API key, follow these steps:
- Visit the Alpha Vantage website at https://www.alphavantage.co/.
- Click on the "Get your free API key" button, usually located in the top right corner of the website.
- Fill out the registration form with your details, including what best describes you, organisation (Make one up) and your email address.
- Click on the "Get API Key" button.
- Your API key will display on the screen
The Code
In this step, we will:
- Open a new Python file.
- Copy and paste the code into the Python file.
- Replace the X's in the API request URL with your actual Alpha Vantage API key.
- 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}")
Getting Share Prices
Here's an explanation of what the code will do:
- Importing the requests library, which allows us to send HTTP requests in Python.
- Creating a list of stock symbols (symbols) that we want to retrieve the latest stock price for.
- Starting a loop that iterates over each symbol in the symbols list.
- 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.
- Checking if the API request was successful by verifying that the HTTP status code of the response is 200.
- If the request was successful, we parse the JSON response using the response.json() method and store it in the data variable.
- Accessing the latest stock price from the JSON data by navigating through the dictionary structure (data["Global Quote"]["05. price"]).
- Printing the stock symbol and its corresponding latest stock price using string formatting.
- The loop continues to the next symbol in the list until all symbols have been processed.
Happy tinkering!