Invalid API call when using alpha vantage python wrapper with list argument

58 Views Asked by At

I'm trying to pull multiple stock ticker data from the alpha vantage python wrapper in an iterative fashion as opposed to calling each individually.

It works if I pass a list of explicit values but when I introduce a postgresql populated list to parse through I get the Invalid API call. I'm at a loss.

The code works like this:

stock_symbols = ['X','Y','Z','A','B','C','etc] 

async def get_data(stock_symbol):
    
    # create alpha vantage timeseries object
    ts = TimeSeries(key = config.ALPHAV_KEY, output_format = 'pandas')
    data, _ = await ts.get_intraday(stock_symbol, interval = '5min', outputsize = 'full')
    await ts.close()
    return data

loop = asyncio.get_event_loop()
tasks = [get_data(stock_symbol) for stock_symbol in stock_symbols]
group1 = asyncio.gather(*tasks)
results = loop.run_until_complete(group1)
loop.close()
print(results)

But passing the values like this throws the error:

 # connect to Database
connection = psycopg2.connect(host = config.DB_HOST, database = config.DB_NAME, user=config.DB_USER, password = config.DB_PASS)

cursor = connection.cursor(cursor_factory=psycopg2.extras.DictCursor)

cursor.execute("SELECT * FROM data WHERE id IN (SELECT product FROM old_products)")

rows = cursor.fetchall()

stock_symbols = []  
for row in rows:
    stock_symbols.append(row['symbol'])


async def get_data(stock_symbol):
    
    # create alpha vantage timeseries object
    ts = TimeSeries(key = config.ALPHAV_KEY, output_format = 'pandas')
    data, _ = await ts.get_intraday(stock_symbol, interval = '5min', outputsize = 'full')
    await ts.close()
    return data

loop = asyncio.get_event_loop()
tasks = [get_data(stock_symbol) for stock_symbol in stock_symbols]
group1 = asyncio.gather(*tasks)
results = loop.run_until_complete(group1)
loop.close()
print(results)

So it's working as suggested at https://pypi.org/project/alpha-vantage/ but why won't it work passing the values in the same way ultimately as it's still passing a list through the function and into the get_intraday timeseries object call. Am I missing something obvious?

The traceback is:

Traceback (most recent call last):
  File "/Users/test/Documents/Programming/VSCode/Ark_Funds_Tracking/pop_prices_EOD.py", line 34, in <module>
    results = loop.run_until_complete(group1)
  File "/Users/test/opt/anaconda3/envs/Py310/lib/python3.10/asyncio/base_events.py", line 649, in run_until_complete
    return future.result()
  File "/Users/test/Documents/Programming/VSCode/Ark_Funds_Tracking/pop_prices_EOD.py", line 27, in get_data
    data, _ = await ts.get_intraday(stock_symbol, interval = '5min', outputsize = 'full')
  File "/Users/test/opt/anaconda3/envs/Py310/lib/python3.10/site-packages/alpha_vantage/async_support/alphavantage.py", line 164, in _format_wrapper
    call_response, data_key, meta_data_key = await func(
  File "/Users/test/opt/anaconda3/envs/Py310/lib/python3.10/site-packages/alpha_vantage/async_support/alphavantage.py", line 106, in _call_wrapper
    return await self._handle_api_call(url), data_key, meta_data_key
  File "/Users/test/opt/anaconda3/envs/Py310/lib/python3.10/site-packages/alpha_vantage/async_support/alphavantage.py", line 254, in _handle_api_call
    raise ValueError(json_response["Error Message"])

ValueError: Invalid API call. Please retry or visit the documentation (https://www.alphavantage.co/documentation/) for TIME_SERIES_INTRADAY.

0

There are 0 best solutions below