mpl_finance无法将-100000转换为日期 [英] mpl_finance cannot convert -100000 to a date

查看:93
本文介绍了mpl_finance无法将-100000转换为日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用mpl_finance创建一个简单的烛台ohlc图表。在他们的网站上,它表示Candlestick_ohlc方法的quotes参数中的第一个元素是日期。它表示必须将其格式化为浮动日期格式。但是,当我使用date2num时,它给我一个错误,提示无法将-100000转换为日期。如果将非datetime值传递给需要datetime对象的轴,则通常会发生这种情况。当我使用没有date2num方法的原始列表时,它给我一个错误,它指向一行代码-xy =(t-OFFFSET,lower)和
消息-不支持的操作数类型- :'datetime.date'和'float'。似乎要我使用float而不是datetime.date但这与先前的错误相矛盾。这是我的代码。任何帮助将不胜感激。在此先感谢

I am trying to make a simple candlestick ohlc chart with mpl_finance. On their website, it says that the first element in the quotes argument of the candlestick_ohlc method is the dates. It says that they must be formatted in the float date format. When i use date2num however, it gives me an error that says"Cannot convert -100000 to a date. This often happens if non-datetime values are passed to an axis that expects datetime objects." When i use my original list without the date2num method, it gives me an error that points to a line of code- xy=(t-OFFFSET, lower) and a message -"unsupported operand type(s) for -: 'datetime.date' and 'float'. it seems as if it wants me to use a float instead of a datetime.date but that contradicts the previous error. Here is my code. Any help will be greatly appreciated. Thanks in advance.

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 的输入格式必须为(日期时间,打开时间,高位...)元组或相应的numpy数组。

这里提供的是<$ c $的列表c> [[datetime1,datetime2,...],[open1,open2,...],...] 代替。

The format of the input to candlestick_ohlc needs to be a list of (datetime, open, high, ...) tuples, or the respective numpy array.
Here you are providing a list of [[datetime1, datetime2, ...], [open1, open2, ...], ...] instead.

要转换为所需格式,您可以例如使用

To convert to the required format, you may e.g. use

ohlc = list(zip(formatteddates, dailyopen, dailyhigh, dailylow, dailyclose))

这篇关于mpl_finance无法将-100000转换为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆