Search code examples
pandasmatplotlibalpha-vantage

Plot pandas-data from Alpha Vantage


I just installed the alpha_vantage module in addition to pandas, and have gotten the api-key etc. I now want to plot som data regarding a stock. Written in the module readme (see here) this is how you do it:

from alpha_vantage.timeseries import TimeSeries

import matplotlib.pyplot as plt

ts = TimeSeries(key='YOUR_API_KEY', output_format='pandas')
data, meta_data = ts.get_intraday(symbol='MSFT',interval='1min', outputsize='full')
data['4. close'].plot()
plt.title('Intraday Times Series for the MSFT stock (1 min)')
plt.show()

But when i write just the same in my project whit my own api-key, i get this error:

  File "C:\Users\augbi\PycharmProjects\DjangoProsjekt\main\views.py", line 159, in <module>
  data['4. close'].plot()
  TypeError: tuple indices must be integers or slices, not str

I printed the data, so you can see the format here ( the top line is the result of data.index:

index: <built-in method index of tuple object at 0x0F7CBC28>
(                     1. open  2. high   3. low  4. close  5. volume
date
2021-01-04 19:55:00  3179.94  3183.45  3179.94   3183.45     1317.0
2021-01-04 19:50:00  3179.00  3180.00  3178.26   3178.26      851.0
2021-01-04 18:52:00  3178.11  3178.11  3178.11   3178.11      648.0
2021-01-04 18:15:00  3177.00  3177.00  3177.00   3177.00      505.0
2021-01-04 18:09:00  3177.00  3177.00  3177.00   3177.00      224.0
...                      ...      ...      ...       ...        ...
2020-12-22 07:40:00  3212.78  3212.78  3212.78   3212.78      703.0
2020-12-22 07:34:00  3210.00  3210.00  3210.00   3210.00      755.0
2020-12-22 07:27:00  3208.19  3208.19  3208.19   3208.19      510.0
2020-12-22 07:14:00  3204.00  3204.00  3204.00   3204.00      216.0
2020-12-22 07:08:00  3204.00  3204.00  3204.00   3204.00      167.0

My own code is here:

ts2 = TimeSeries(key='ALPA_KEY', output_format='pandas')
data = ts2.get_intraday(symbol='AMZN',interval='1min', outputsize='full')
print("index:", data.index)
print(data)
data['4. close'].plot()
plt.title('Intraday Times Series for the AMZN stock (1 min)')
plt.show()

My wish is to plot the "4. Closing" column, and if possible the corresponding times. This data represents stock-prise for amazon for 1 day.

Thank you very much in advance!


Solution

  • I had an Alpah-Vantage APIKEY, so I used the following code to check. It didn't come back with an error as you asked. When I checked the output format, it was not the normal pandas format, but an extended format.

    data
    (                     1. open  2. high   3. low  4. close  5. volume
     date                                                               
     2021-01-04 19:55:00  3179.94  3183.45  3179.94   3183.45     1317.0
     2021-01-04 19:50:00  3179.00  3180.00  3178.26   3178.26      851.0
     2021-01-04 18:52:00  3178.11  3178.11  3178.11   3178.11      648.0
     2021-01-04 18:15:00  3177.00  3177.00  3177.00   3177.00      505.0
     2021-01-04 18:09:00  3177.00  3177.00  3177.00   3177.00      224.0
     ...                      ...      ...      ...       ...        ...
     2020-12-22 07:40:00  3212.78  3212.78  3212.78   3212.78      703.0
     2020-12-22 07:34:00  3210.00  3210.00  3210.00   3210.00      755.0
     2020-12-22 07:27:00  3208.19  3208.19  3208.19   3208.19      510.0
     2020-12-22 07:14:00  3204.00  3204.00  3204.00   3204.00      216.0
     2020-12-22 07:08:00  3204.00  3204.00  3204.00   3204.00      167.0
     
     [3440 rows x 5 columns],
     {'1. Information': 'Intraday (1min) open, high, low, close prices and volume',
      '2. Symbol': 'AMZN',
      '3. Last Refreshed': '2021-01-04 19:55:00',
      '4. Interval': '1min',
      '5. Output Size': 'Full size',
      '6. Time Zone': 'US/Eastern'})
    

    From this format, a normal data frame can be obtained with data[0], so a graph can be created. The following is the code to get the graph.

    data[0]
        1. open     2. high     3. low  4. close    5. volume
    date                    
    2021-01-04 19:55:00     3179.94     3183.45     3179.94     3183.45     1317.0
    2021-01-04 19:50:00     3179.00     3180.00     3178.26     3178.26     851.0
    2021-01-04 18:52:00     3178.11     3178.11     3178.11     3178.11     648.0
    2021-01-04 18:15:00     3177.00     3177.00     3177.00     3177.00     505.0
    2021-01-04 18:09:00     3177.00     3177.00     3177.00     3177.00     224.0
    
    ts2 = TimeSeries(key=api_key, output_format='pandas')
    data = ts2.get_intraday(symbol='AMZN',interval='1min', outputsize='full')
    print("index:", data.index)
    print(data)
    data[0]['4. close'].plot()
    plt.title('Intraday Times Series for the AMZN stock (1 min)')
    plt.show()
    
    

    enter image description here