简体   繁体   中英

mpl_finance cannot convert -100000 to a 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()

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 eg use

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM