Skip to main content

A modern Python client for the Alpha Vantage Financial API with type-safe response parsing

Project description

Alpha Vantage API Client

A Python client for the Alpha Vantage API that provides easy access to financial market data including stocks, forex, and cryptocurrencies.

Features

  • Stock Time Series (Intraday, Daily, Weekly, Monthly)
  • Fundamental Data (Company Overview, Income Statement, Balance Sheet, Cash Flow)
  • Forex Data (Intraday, Daily, Weekly, Monthly)
  • Cryptocurrency Data (Intraday, Daily, Weekly, Monthly)
  • Type hints for better IDE support
  • Automatic error handling and rate limiting
  • Environment variable support for API key
  • Pydantic models for type-safe response parsing

Installation

pip install -r requirements.txt

Usage

First, you'll need to get an API key from Alpha Vantage.

You can either pass the API key directly to the client or set it as an environment variable:

from alphavantage import AlphaVantageClient

# Using environment variable ALPHA_VANTAGE_API_KEY
client = AlphaVantageClient()

# Or passing API key directly
client = AlphaVantageClient(api_key="your_api_key")

Examples

Getting Company Overview

# Get company overview with type-safe response
overview = client.get_company_overview(symbol="IBM")

# Access fields with IDE autocompletion
print(f"Company: {overview.name}")
print(f"Sector: {overview.sector}")
print(f"Market Cap: {overview.market_capitalization}")
print(f"PE Ratio: {overview.pe_ratio}")

Getting Financial Statements

# Get income statement
income_stmt = client.get_income_statement(symbol="IBM")

# Access annual and quarterly reports
for report in income_stmt.annual_reports:
    print(f"Fiscal Year: {report.fiscal_date_ending}")
    print(f"Total Revenue: {report.total_revenue}")
    print(f"Net Income: {report.net_income}")

# Get balance sheet
balance_sheet = client.get_balance_sheet(symbol="IBM")

# Access balance sheet items
for report in balance_sheet.quarterly_reports:
    print(f"Quarter: {report.fiscal_date_ending}")
    print(f"Total Assets: {report.total_assets}")
    print(f"Total Liabilities: {report.total_liabilities}")

# Get cash flow statement
cash_flow = client.get_cash_flow(symbol="IBM")

# Access cash flow items
for report in cash_flow.annual_reports:
    print(f"Fiscal Year: {report.fiscal_date_ending}")
    print(f"Operating Cashflow: {report.operating_cashflow}")
    print(f"Capital Expenditures: {report.capital_expenditures}")

Getting News & Sentiment

# Get news sentiment for specific tickers
news = client.get_news_sentiment(
    tickers="IBM,AAPL",
    topics="technology",
    limit=10
)

# Access news items with sentiment analysis
for item in news.feed:
    print(f"Title: {item.title}")
    print(f"Sentiment: {item.overall_sentiment_label}")
    print(f"Score: {item.overall_sentiment_score}")
    
    # Access ticker-specific sentiment
    for ticker in item.ticker_sentiment:
        print(f"Ticker: {ticker['ticker']}")
        print(f"Relevance: {ticker['relevance_score']}")

Getting Market Movers

# Get top gainers and losers
movers = client.get_top_gainers_losers()

# Access top gainers
print("Top Gainers:")
for gainer in movers.top_gainers:
    print(f"{gainer.ticker}: {gainer.change_percentage}%")

# Access top losers
print("\nTop Losers:")
for loser in movers.top_losers:
    print(f"{loser.ticker}: {loser.change_percentage}%")

Error Handling

The client includes built-in error handling for common API issues:

  • APIKeyError: Raised when there are issues with the API key
  • RateLimitError: Raised when API rate limits are exceeded
  • InvalidParameterError: Raised when invalid parameters are provided
  • APIError: Raised for other API-related errors

Example error handling:

from alphavantage import AlphaVantageClient, APIError, RateLimitError

try:
    client = AlphaVantageClient()
    overview = client.get_company_overview(symbol="INVALID")
except APIError as e:
    print(f"API Error: {e.message}")
except RateLimitError:
    print("Rate limit exceeded. Please wait before making more requests.")

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

alphavantage_finapi_client-0.1.0.tar.gz (13.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

alphavantage_finapi_client-0.1.0-py3-none-any.whl (12.1 kB view details)

Uploaded Python 3

File details

Details for the file alphavantage_finapi_client-0.1.0.tar.gz.

File metadata

File hashes

Hashes for alphavantage_finapi_client-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ad2172a20c69493ce19e87f36c6738024617f8c16c85d4efd94abac0367ac127
MD5 80269a722938f89fbd0ab1f8b11f22df
BLAKE2b-256 bd7d4300b6954d9a3d059b99c7ad0bfe30be3583af389b3f918763e7ba50c9c6

See more details on using hashes here.

File details

Details for the file alphavantage_finapi_client-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for alphavantage_finapi_client-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 25faaa9d62f5b61e928166ff0a67cc0ec5e062b2d22e40d1358ba3cfaecbf6d2
MD5 c2d295a699c6fd36da3d92e06bed0d94
BLAKE2b-256 9a672489958ea6734c6c5482b2705637c07f04ef349a8297ad48febcfe3dfd62

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page