mpl_finance 无法将 -100000 转换为日期
mpl_finance cannot convert -100000 to a date
我正在尝试使用 mpl_finance 制作一个简单的烛台 ohlc 图表。在他们的网站上,它说 candlestick_ohlc 方法的引号参数中的第一个元素是日期。它说它们必须以浮动日期格式进行格式化。然而,当我使用 date2num 时,它给我一个错误,说 "Cannot convert -100000 to a date. This often happens if non-datetime values are passed to an axis that expects datetime objects." 当我使用没有 date2num 方法的原始列表时,它给我一个错误,指向一行代码 - xy=(t-OFFFSET, lower ) 和一个
消息 -“- 的操作数类型不受支持:'datetime.date' 和 'float'。它似乎希望我使用浮点数而不是 datetime.date,但这与之前的错误相矛盾。这是我的代码。任何帮助将不胜感激。提前致谢。
import requests
import json
import pprint
import matplotlib.pyplot as plt
from mpl_finance import candlestick_ohlc
import matplotlib.dates as mdates
from datetime import datetime, date, time
url = "https://www.alphavantage.co/query"
function = "TIME_SERIES_DAILY"
symbol = "MSFT"
api_key = "K2H0JNUZBWYKW02L"
data = { "function": function,
"symbol": symbol,
"apikey": api_key }
page = requests.get(url, params = data)
thedata = page.json()
days = []
dailyopen = []
dailyclose = []
dailyhigh = []
dailylow = []
dailyvol = []
delimitedyear = []
delimitedday = []
delimitedmonth = []
thedatakeys = list(thedata['Time Series (Daily)'].keys())
thedatakeys.sort()
for day in thedatakeys:
days.append(day)
dailyopen.append(float(thedata['Time Series (Daily)'][day]
['1. open']))
dailyhigh.append(float(thedata['Time Series (Daily)'][day]
['2. high']))
dailylow.append(float(thedata['Time Series (Daily)'][day]
['3. low']))
dailyclose.append(float(thedata['Time Series (Daily)']
[day]['4. close']))
dailyvol.append(float(thedata['Time Series (Daily)'][day]
['5. volume']))
counter = 0
for day in days:
delimitedyear.append(days[counter][0:4])
delimitedmonth.append(days[counter][5:7])
delimitedday.append(days[counter][8:10])
counter = counter + 1
d = []
for newcounter in range(len(delimitedyear)):
d.append(date(int(delimitedyear[newcounter]),
int(delimitedmonth[newcounter]),
int(delimitedday[newcounter])))
formatteddates = mdates.date2num(d)
ohlc = [formatteddates, dailyopen, dailyhigh, dailylow,
dailyclose]
print(formatteddates)
fl, ax = plt.subplots(figsize = (10,5))
candlestick_ohlc(ax, ohlc, width=.6, colorup='green',
colordown='red')
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m'))
ax.grid(False)
plt.show()
candlestick_ohlc
的输入格式需要是 (datetime, open, high, ...)
元组的列表,或相应的 numpy 数组。
在这里,您提供的是 [[datetime1, datetime2, ...], [open1, open2, ...], ...]
的列表。
要转换为所需的格式,您可以例如使用
ohlc = list(zip(formatteddates, dailyopen, dailyhigh, dailylow, dailyclose))
我正在尝试使用 mpl_finance 制作一个简单的烛台 ohlc 图表。在他们的网站上,它说 candlestick_ohlc 方法的引号参数中的第一个元素是日期。它说它们必须以浮动日期格式进行格式化。然而,当我使用 date2num 时,它给我一个错误,说 "Cannot convert -100000 to a date. This often happens if non-datetime values are passed to an axis that expects datetime objects." 当我使用没有 date2num 方法的原始列表时,它给我一个错误,指向一行代码 - xy=(t-OFFFSET, lower ) 和一个 消息 -“- 的操作数类型不受支持:'datetime.date' 和 'float'。它似乎希望我使用浮点数而不是 datetime.date,但这与之前的错误相矛盾。这是我的代码。任何帮助将不胜感激。提前致谢。
import requests
import json
import pprint
import matplotlib.pyplot as plt
from mpl_finance import candlestick_ohlc
import matplotlib.dates as mdates
from datetime import datetime, date, time
url = "https://www.alphavantage.co/query"
function = "TIME_SERIES_DAILY"
symbol = "MSFT"
api_key = "K2H0JNUZBWYKW02L"
data = { "function": function,
"symbol": symbol,
"apikey": api_key }
page = requests.get(url, params = data)
thedata = page.json()
days = []
dailyopen = []
dailyclose = []
dailyhigh = []
dailylow = []
dailyvol = []
delimitedyear = []
delimitedday = []
delimitedmonth = []
thedatakeys = list(thedata['Time Series (Daily)'].keys())
thedatakeys.sort()
for day in thedatakeys:
days.append(day)
dailyopen.append(float(thedata['Time Series (Daily)'][day]
['1. open']))
dailyhigh.append(float(thedata['Time Series (Daily)'][day]
['2. high']))
dailylow.append(float(thedata['Time Series (Daily)'][day]
['3. low']))
dailyclose.append(float(thedata['Time Series (Daily)']
[day]['4. close']))
dailyvol.append(float(thedata['Time Series (Daily)'][day]
['5. volume']))
counter = 0
for day in days:
delimitedyear.append(days[counter][0:4])
delimitedmonth.append(days[counter][5:7])
delimitedday.append(days[counter][8:10])
counter = counter + 1
d = []
for newcounter in range(len(delimitedyear)):
d.append(date(int(delimitedyear[newcounter]),
int(delimitedmonth[newcounter]),
int(delimitedday[newcounter])))
formatteddates = mdates.date2num(d)
ohlc = [formatteddates, dailyopen, dailyhigh, dailylow,
dailyclose]
print(formatteddates)
fl, ax = plt.subplots(figsize = (10,5))
candlestick_ohlc(ax, ohlc, width=.6, colorup='green',
colordown='red')
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m'))
ax.grid(False)
plt.show()
candlestick_ohlc
的输入格式需要是 (datetime, open, high, ...)
元组的列表,或相应的 numpy 数组。
在这里,您提供的是 [[datetime1, datetime2, ...], [open1, open2, ...], ...]
的列表。
要转换为所需的格式,您可以例如使用
ohlc = list(zip(formatteddates, dailyopen, dailyhigh, dailylow, dailyclose))