0

I'm new to Python and more specifically, Alpha Vantage. My issue here is that my code is collecting the data for the stock, but it is not graphing it. I ran this code on my cmd with 3.7 python and already updated all my packages. I have heard people have been having problems with matplotlib and 3.7 version of python, but I really want to figure out this API. This is my code below:

from alpha_vantage.timeseries 
import TimeSeries 
import matplotlib.pyplot as plt 
import sys

def stockchart(symbol):
    ts = TimeSeries(key='1ORS1XLM1YK1GK9Y', output_format='pandas')
    data, meta_data = ts.get_intraday(symbol=symbol, interval='1min', outputsize='full')
    print (data)
    data['close'].plot()
    plt.title('Stock chart')
    plt.show()

symbol=input("Enter symbol name:") stockchart(symbol)

I got this in response on my cmd after entering MSFT for the symbol name... which means I am pulling from the API but the data['close'] function is not working correctly with PANDAS

          1. open   2. high    3. low  4. close  5. volume
    date
    2018-09-05 09:30:00  111.1900  111.4000  111.1200  111.3500   673119.0

File "C:\Users\davis\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\core\internals.py", line 4115, in get
    loc = self.items.get_loc(item)
  File "C:\Users\davis\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\core\indexes\base.py", line 3080, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))
  File "pandas\_libs\index.pyx", line 140, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 1492, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 1500, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'close'
Sam Hill
  • 1
  • 2

1 Answers1

1

I had the same problem.

Pay attention in this line, that contain the names of columns:

 1. open   2. high    3. low  4. close  5. volume

So, change this line with name of column: For example:

data['close'].plot() change for data['4. close'].plot()

Anderson
  • 51
  • 1
  • 7