【问题标题】:Graphing an Alpha Vantage API with Python使用 Python 绘制 Alpha Vantage API
【发布时间】:2019-02-16 14:35:15
【问题描述】:

我是 Python 新手,更具体地说,是 Alpha Vantage。我的问题是我的代码正在收集股票的数据,但它没有绘制图表。我用 3.7 python 在我的 cmd 上运行了这段代码,并且已经更新了我所有的包。我听说人们在使用 matplotlib 和 3.7 版本的 python 时遇到问题,但我真的很想弄清楚这个 API。这是我下面的代码:

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)

在输入 MSFT 作为符号名称后,我在我的 cmd 上得到了这个响应...这意味着我正在从 API 中提取,但 data['close'] 函数无法与 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'

【问题讨论】:

    标签: python-3.x matplotlib alpha-vantage


    【解决方案1】:

    我遇到了同样的问题。

    注意这一行,其中包含列的名称:

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

    因此,将这一行更改为列名:例如:

    data['close'].plot() 更改为 data['4. close'].plot()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-12
      • 1970-01-01
      • 1970-01-01
      • 2020-09-21
      • 1970-01-01
      • 1970-01-01
      • 2020-07-03
      • 1970-01-01
      相关资源
      最近更新 更多