Search code examples
pythonpython-3.xalpha-vantage

Alpha Vantage passing multiple queries - Global Quote


I am trying to pass values from an Alpha Vantage query into a pandas df so that I can extract certain values.

When I run a simple, 1 symbol query, I can easily pass the information into a df. After that, I can transpose the df and extract the data using the columns.

An example is:

import requests
import alpha_vantage
import pandas as pd

API_URL = "https://www.alphavantage.co/query"
data = {
    "function": "GLOBAL_QUOTE",
    "symbol": "MSFT",
    "apikey": "XXX",
    }

response = requests.get(API_URL, params=data)
print(response.json())

df = pd.DataFrame(response.json())
df = df.T
print()
print()
print(df)

This returns:

enter image description here

However, I am looking to pass multiple symbols, I keep getting an error saying:

{'Error Message': 'Invalid API call. Please retry or visit the documentation (https://www.alphavantage.co/documentation/) for GLOBAL_QUOTE.'}

The code being used is:

import requests
import alpha_vantage
import pandas as pd

df = pd.DataFrame()
API_URL = "https://www.alphavantage.co/query"

symbols= ["IBM", "MSFT", "APPL"]
for symbol in symbols:
    data = {
        "function": "GLOBAL_QUOTE",
        "symbol": symbols,
        "apikey": "XXX",
        }

    response = requests.get(API_URL, params=data)

    print(response.json())

I believe that corresponds to the format that is expected from the website:

https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=MSFT&apikey=demo

My question is: How can I pass multiple requests to the Alpha Vantage API using a list of symbols?


Solution

  • Change "symbols" to "symbol"

    "symbol": symbols, -> "symbol": symbol, Right now you're passing the entire list as data.

    import alpha_vantage
    import pandas as pd
    
    df = pd.DataFrame()
    API_URL = "https://www.alphavantage.co/query"
    
    symbols= ["IBM", "MSFT", "APPL"]
    for symbol in symbols:
        data = {
            "function": "GLOBAL_QUOTE",
            "symbol": symbol,
            "apikey": "XXX",
            }
    
        response = requests.get(API_URL, params=data)
    
        print(response.json())