Alpha Vantage & Streamlit - How to Create a Simple Stock Tracking Applications in Minutes

Michał Żelazkiewicz

4 October 2023, 4 min read

thumbnail post

What's inside

  1. What are Alpha Vantage and Streamlit?
  2. How to start?
  3. Summary

In this article, we’d like to show you how to create a simple stock tracking application within minutes with Alpha Vantage and Stramlit.

What are Alpha Vantage and Streamlit?

Alpha Vantage

Alpha Vantage is a service that provides stock market data conveniently in JSON API. It offers free API Keys upon request, so we can use it without additional costs.

With Alpha Vantage, you can browse real-time and historical market data for Stock, Forex, Commodity, and Crypto.

pierwszy

Streamlit

Streamlit is an open-source Python library that allows you to create custom web apps used mostly for data science and lately machine learning. It does not require you to know all the latest design trends, JS libs, and tricks and spend many hours writing the boilerplate code.

With Streamlit, you can create and run your first data-driven application within a few minutes.

drugi

How to start?

If you want a quick dive-in explore our public repository directly and see the magic unfold: Sunscraper’s Alpha Vantage & Streamlit Demo.

Otherwise, follow the steps below.

Setup

To start you need to have Python 3 available and install the Streamlit package. It is strongly recommended to use a virtual environment for package installation.

Type in your terminal:

python3.10 -m venv venv
source venv/bin/activate

pip install streamlit

Get Alpha Vantage API Key

The second step is to obtain the API key required to use service functionalities freely. To accomplish this task, visit the Alpha Vantage (https://www.alphavantage.co/) and request a free API Key.

Once the required information is provided, the key will be displayed to you, and you will be able to enter it into the app. Otherwise, you will be limited to the data accessible for the demo key, which might still be enough for this short example.

Write the Stock Tracker App

Now is the time for some coding. Create a new file named app.py.

# Import required libraries

import streamlit as st
import pandas as pd
import requests

API_KEY = 'demo' # Replace it with real value

# Ask user for stock symbol
symbol = st.text_input('Enter stock symbol:', 'IBM').upper()

# API Endpoint to retrieve Daily Time Series
url = f"https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol={symbol}&apikey={API_KEY}"

# Request the data, parse JSON response and store it in Python variable
r = requests.get(url, timeout=5)
data = r.json()

# Extract basic information from collected data
information = data['Meta Data']['1. Information']
symbol = data['Meta Data']['2. Symbol']
last_refreshed = data['Meta Data']['3. Last Refreshed']

# Display the collected data to user using Streamline functions
st.write('# Alpha Vantage stock price data')
st.write('## ' + information)
st.write('### ' + symbol)
st.write('### Last update: ' + last_refreshed)

st.write('## Time Series (Daily)')

# Use Pandas' Data Frame to prepare data to be displayed in charts
df = pd.DataFrame.from_dict(data['Time Series (Daily)'], orient='index')

df = df.reset_index()
df.columns = ['date', 'open', 'high', 'low', 'close', 'volume']

df['open'] = df['open'].astype(float)
df['high'] = df['high'].astype(float)
df['low'] = df['low'].astype(float)
df['close'] = df['close'].astype(float)
df['volume'] = df['volume'].astype(int)

df['date'] = pd.to_datetime(df['date'])
df = df.sort_values(by='date')

# Display Streamline charts
st.line_chart(df.set_index('date')[['open', 'high', 'low', 'close']])
st.bar_chart(df.set_index('date')['volume'])

Run the App

Running the Streamlit application is simple. Just type the following command and watch the results:

streamlit run app.py

It will open a web page in your browser that allows you to see your application.

trzeci

Summary

This short demo shows how fast and straightforward it can be to write a fully working and usable data-driven app with Streamlit. It lowers the learning curve required for displaying the results of your data scientist work to the world and simplifies the development and deployment process.

There is a lot more in both Alpha Vantage and Streamlit. Both have very well-developed APIs and possibilities as well as rich documentation, which can be helpful in hard times. However, the main conclusion is that Streamlit allows you to focus on your core work instead of writing one more schematic web application and simultaneously achieve more than satisfying results.

Diving into these technologies and maximizing their potential requires expertise. If you’re keen on leveraging these tools for your project or have further inquiries, the seasoned professionals at Sunscrapers are just a message away.

Let’s pioneer the future of data-driven applications together. Contact us today.

Share

Recent posts

See all blog posts

Are you ready for your next project?

Whether you need a full product, consulting, tech investment or an extended team, our experts will help you find the best solutions.

Hi there, we use cookies to provide you with an amazing experience on our site. If you continue without changing the settings, we’ll assume that you’re happy to receive all cookies on Sunscrapers website. You can change your cookie settings at any time.