pythonjsonalpha-vantagecsvreader

How do you use the python alpha_vantage API to return extended intraday data?


I have been working with the alpha vantage python API for a while now, but I have only needed to pull daily and intraday timeseries data. I am trying to pull extended intraday data, but am not having any luck getting it to work. Trying to run the following code:

from alpha_vantage.timeseries import TimeSeries

apiKey = 'MY API KEY'

ts = TimeSeries(key = apiKey, output_format = 'pandas')

totalData, _ = ts.get_intraday_extended(symbol = 'NIO', interval = '15min', slice = 'year1month1')

print(totalData)

gives me the following error:

Traceback (most recent call last):
  File "/home/pi/Desktop/test.py", line 9, in <module>
    totalData, _ = ts.get_intraday_extended(symbol = 'NIO', interval = '15min', slice = 'year1month1')
  File "/home/pi/.local/lib/python3.7/site-packages/alpha_vantage/alphavantage.py", line 219, in _format_wrapper
    self, *args, **kwargs)
  File "/home/pi/.local/lib/python3.7/site-packages/alpha_vantage/alphavantage.py", line 160, in _call_wrapper
    return self._handle_api_call(url), data_key, meta_data_key
  File "/home/pi/.local/lib/python3.7/site-packages/alpha_vantage/alphavantage.py", line 354, in _handle_api_call
    json_response = response.json()
  File "/usr/lib/python3/dist-packages/requests/models.py", line 889, in json
    self.content.decode(encoding), **kwargs
  File "/usr/lib/python3/dist-packages/simplejson/__init__.py", line 518, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3/dist-packages/simplejson/decoder.py", line 370, in decode
    obj, end = self.raw_decode(s)
  File "/usr/lib/python3/dist-packages/simplejson/decoder.py", line 400, in raw_decode
    return self.scan_once(s, idx=_w(s, idx).end())
simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

What is interesting is that if you look at the TimeSeries class, it states that extended intraday is returned as a "time series in one csv_reader object" whereas everything else, which works for me, is returned as "two json objects". I am 99% sure this has something to do with the issue, but I'm not entirely sure because I would think that calling intraday extended function would at least return SOMETHING (despite it being in a different format), but instead just gives me an error.

Another interesting little note is that the function refuses to take "adjusted = True" (or False) as an input despite it being in the documentation... likely unrelated, but maybe it might help diagnose.


Solution

  • Seems like TIME_SERIES_INTRADAY_EXTENDED can return only CSV format, but the alpha_vantage wrapper applies JSON methods, which results in the error.

    My workaround:

    from alpha_vantage.timeseries import TimeSeries
    import pandas as pd
    
    apiKey = 'MY API KEY'
    
    ts = TimeSeries(key = apiKey, output_format = 'csv')
    
    #download the csv
    totalData = ts.get_intraday_extended(symbol = 'NIO', interval = '15min', slice = 'year1month1')
    
    #csv --> dataframe
    df = pd.DataFrame(list(totalData[0]))
    
    #setup of column and index
    header_row=0
    df.columns = df.iloc[header_row]
    df = df.drop(header_row)
    df.set_index('time', inplace=True)
    
    #show output
    print(df)