簡體   English   中英

如何使用 python alpha_vantage API 返回擴展的盤中數據?

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

我一直在使用alpha vantage python API一段時間,但我只需要提取每日和盤中時間序列數據。 我正在嘗試提取擴展的盤中數據,但沒有任何運氣讓它發揮作用。 嘗試運行以下代碼:

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)

給我以下錯誤:

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)

有趣的是,如果您查看TimeSeries class ,它指出擴展盤中作為“一個 csv_reader 對象中的時間序列”返回,而對我有用的其他所有內容都作為“兩個 json 對象”返回。 我 99% 確定這與問題有關,但我不完全確定,因為我認為調用日內擴展 function 至少會返回 SOMETHING(盡管它采用不同的格式),但只是給了我一個錯誤。

另一個有趣的小提示是 function 拒絕將“adjusted = True”(或 False)作為輸入,盡管它在文檔中......可能不相關,但它可能有助於診斷。

似乎 TIME_SERIES_INTRADAY_EXTENDED 只能返回 CSV 格式,但 alpha_vantage 包裝器應用 JSON 方法,這會導致錯誤。

我的解決方法:

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)

這是一種簡單的方法。

ticker = 'IBM'
date= 'year1month2'
apiKey = 'MY API KEY'

df = pd.read_csv('https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY_EXTENDED&symbol='+ticker+'&interval=15min&slice='+date+'&apikey='+apiKey+'&datatype=csv&outputsize=full') 

#Show output
print(df)
import pandas as pd

symbol = 'AAPL'
interval = '15min'
slice = 'year1month1'
api_key = ''
adjusted = '&adjusted=true&'

csv_url = 'https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY_EXTENDED&symbol='+symbol+'&interval='+interval+'&slice='+slice+adjusted+'&apikey='+api_key

data = pd.read_csv(csv_url)
print(data.head)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM