Alpha Vantage Python Library

3.0.0 · active · verified Thu Apr 16

The `alpha-vantage` library is a Python wrapper for the Alpha Vantage API, providing access to real-time and historical financial data, including stocks, cryptocurrencies, technical indicators, and economic data. The library is actively maintained, with the current version being 3.0.0, and receives regular updates.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `TimeSeries` object with an API key (preferably from an environment variable) and fetch daily adjusted stock data and a real-time global quote, displaying the output using pandas DataFrames. Ensure you have `pandas` installed for DataFrame output.

import os
from alpha_vantage.timeseries import TimeSeries
import pandas as pd

# Get your Alpha Vantage API key from environment variable or replace 'YOUR_API_KEY'
# Register for a free API key at https://www.alphavantage.co/support/#api-key
api_key = os.environ.get('ALPHAVANTAGE_API_KEY', 'YOUR_API_KEY')

if api_key == 'YOUR_API_KEY':
    print("Warning: Please replace 'YOUR_API_KEY' with your actual Alpha Vantage API key or set the ALPHAVANTAGE_API_KEY environment variable.")
    print("You can get a free API key at: https://www.alphavantage.co/support/#api-key")
    exit()

try:
    # Initialize TimeSeries with your API key and set output format to pandas DataFrame
    ts = TimeSeries(key=api_key, output_format='pandas')

    # Get daily adjusted time series for a symbol (e.g., IBM)
    print("Fetching daily adjusted data for IBM...")
    data, meta_data = ts.get_daily_adjusted(symbol='IBM', outputsize='compact')

    print("\n--- Daily Adjusted Data (last 5 rows) ---")
    print(data.tail())

    # Get real-time global quote for a symbol
    print("\nFetching global quote for AAPL...")
    global_quote, meta_global_quote = ts.get_global_quote(symbol='AAPL')

    print("\n--- Global Quote for AAPL ---")
    # The global_quote result is usually a dictionary, not a DataFrame for single quotes
    # Convert to DataFrame for consistent printing, if it's a dict (which it often is for single quote)
    if isinstance(global_quote, dict):
        print(pd.DataFrame([global_quote]))
    else:
        print(global_quote)

except Exception as e:
    print(f"An error occurred: {e}")
    print("Ensure your API key is correct and you are not exceeding rate limits (5 calls/minute for free tier).")

view raw JSON →