alpha_vantage Python Library

repository·develop·Indexed 26 days ago

https://github.com/romeltorres/alpha_vantage

A Python module providing an interface to the Alpha Vantage API for retrieving real-time and historical financial data. It supports stocks, cryptocurrencies, forex, and technical indicators, with output options for JSON and pandas DataFrames. The library includes specialized submodules for time series, technical indicators, and sector performance, as well as asyncio support for concurrent API calls.

Tokens
1.4K
Snippets
7
Records
12
Agent score
89%

What's inside alpha_vantage

  1. Overview of alpha_vantage submodules

    develop

    The alpha_vantage package is organized into several specialized submodules for accessing different types of financial data from the Alpha Vantage API:

    • alpha_vantage.alphavantage: Core Alpha Vantage functionality.
    • alpha_vantage.cryptocurrencies: Cryptocurrency market data.
    • alpha_vantage.foreignexchange: Foreign exchange (Forex) data.
    • alpha_vantage.sectorperformance: Sector performance data.
    • alpha_vantage.techindicators: Technical indicators (e.g., SMA, EMA, RSI).
    • alpha_vantage.timeseries: Time series data (e.g., daily, intraday, weekly stock prices).
  2. Use Asyncio for concurrent API calls

    develop

    For Python 3.5+, you can use the asynchronous support located in alpha_vantage.async_support. This is useful for performing multiple API calls concurrently to improve performance. Remember to call .close() on the object when finished.

    import asyncio
    from alpha_vantage.async_support.timeseries import TimeSeries
    
    symbols = ['AAPL', 'GOOG', 'TSLA', 'MSFT']
    
    async def get_data(symbol):
        ts = TimeSeries(key='YOUR_KEY_HERE')
        data, _ = await ts.get_quote_endpoint(symbol)
        await ts.close()
        return data
    
    loop = asyncio.get_event_loop()
    tasks = [get_data(symbol) for symbol in symbols]
    group1 = asyncio.gather(*tasks)
    results = loop.run_until_complete(group1)
    loop.close()
    print(results)
  3. Install alpha_vantage

    develop

    You can install the package via pip. For full functionality including pandas support, install pandas alongside the library.

    To install with pandas support:

    pip install alpha_vantage pandas

    To install from source:

    git clone https://github.com/RomelTorres/alpha_vantage.git
    pip install -e alpha_vantage
    pip install alpha_vantage pandas
  4. Configure TimeSeries output format and indexing

    develop

    The TimeSeries class allows you to configure the output format and the pandas DataFrame indexing type during initialization.

    • output_format: Set to 'pandas' to receive results as pandas DataFrames. Default is JSON dictionaries. Note that ForeignExchange and TechIndicators do not support 'csv' format.
    • indexing_type: When using pandas, set to 'date' (default) for date string indexing or 'integer' for integer indexing.
  5. Get Cryptocurrency data

    develop

    Use the CryptoCurrencies class from alpha_vantage.cryptocurrencies to retrieve digital currency data.

    from alpha_vantage.cryptocurrencies import CryptoCurrencies
    
    cc = CryptoCurrencies(key='YOUR_API_KEY', output_format='pandas')
    data, meta_data = cc.get_digital_currency_daily(symbol='BTC', market='CNY')
  6. Get intraday stock data

    develop

    Use get_intraday to retrieve intraday time series data. You can specify a month parameter (e.g., '2014-01') to query historical data for a specific month.

    from alpha_vantage.timeseries import TimeSeries
    
    ts = TimeSeries(key='YOUR_API_KEY')
    # Get current intraday data
    data, meta_data = ts.get_intraday('GOOGL')
    
    # Get historical intraday data for a specific month
    data, meta_data = ts.get_intraday('GOOGL', month='2014-01', interval='30min')
  7. Use TechIndicators for technical analysis

    develop

    Import TechIndicators from alpha_vantage.techindicators to access various technical indicators like Simple Moving Average (SMA) or Bollinger Bands (BBands).

    from alpha_vantage.techindicators import TechIndicators
    
    ti = TechIndicators(key='YOUR_API_KEY')
    
    # Get SMA values for a specific month
    data, meta_data = ti.get_sma('GOOGL', month='2014-01', interval='30min')
    
    # Get Bollinger Bands
    data, meta_data = ti.get_bbands(symbol='MSFT', interval='60min', time_period=60)