how to deal with the error and mpf.plot(pdata) didn't work.
thx
import requests
import json
import pandas as pd
import mplfinance as mpf
import matplotlib.pyplot as plt
url = "https://api.huobi.pro/market/history/kline?period=5min&size=3&symbol=btcusdt"
r = requests.get(url)
data = json.loads(r.content)['data']
print("data:", data)
pdata = pd.DataFrame(data)
pdata.index.name = 'id'
pdata = pdata[['id', 'open', 'close', 'high', 'low', 'vol', 'amount', 'count']]
pdata['id'] = pd.to_datetime(pdata['id'], unit='s')
mpf.plot(pdata)
Hi franklee00,
It appears the issues lies in the fact that the data that API call is returning isn't in the format that mplfinance expects. The data you specified is a list of dictionaries which each have id, open, close, high, low, volume, etc values. mplfinance expects a dictionary which keys such as 'Data', 'Open', 'Close', 'High', 'Low', 'Volume' each with a value in the form of a list of values.
I went ahead and reformatted your data for you here:
import requests
import json
import pandas as pd
import datetime
import mplfinance as mpf
import matplotlib.pyplot as plt
url = "https://api.huobi.pro/market/history/kline?period=5min&size=3&symbol=btcusdt"
r = requests.get(url)
data = json.loads(r.content)['data']
reformatted_data = dict()
reformatted_data['Date'] = []
reformatted_data['Open'] = []
reformatted_data['High'] = []
reformatted_data['Low'] = []
reformatted_data['Close'] = []
reformatted_data['Volume'] = []
for dict in data:
reformatted_data['Date'].append(datetime.datetime.fromtimestamp(dict['id']))
reformatted_data['Open'].append(dict['open'])
reformatted_data['High'].append(dict['high'])
reformatted_data['Low'].append(dict['low'])
reformatted_data['Close'].append(dict['close'])
reformatted_data['Volume'].append(dict['vol'])
print("reformatted data:", reformatted_data)
pdata = pd.DataFrame.from_dict(reformatted_data)
pdata.set_index('Date', inplace=True)
mpf.plot(pdata)
Another note is that you need to convert your time stamps into date time objects within your data. This can done with the datetime.datetime.fromtimestamp(TIMESTAMP) function available when you import datetime.
Hopefully, this helps!
-- Caleb
Hi franklee00,
It appears the issues lies in the fact that the data that API call is returning isn't in the format that mplfinance expects. The data you specified is a list of dictionaries which each have id, open, close, high, low, volume, etc values. mplfinance expects a dictionary which keys such as 'Data', 'Open', 'Close', 'High', 'Low', 'Volume' each with a value in the form of a list of values.
I went ahead and reformatted your data for you here:
import requests import json import pandas as pd import datetime import mplfinance as mpf import matplotlib.pyplot as plt url = "https://api.huobi.pro/market/history/kline?period=5min&size=3&symbol=btcusdt" r = requests.get(url) data = json.loads(r.content)['data'] reformatted_data = dict() reformatted_data['Date'] = [] reformatted_data['Open'] = [] reformatted_data['High'] = [] reformatted_data['Low'] = [] reformatted_data['Close'] = [] reformatted_data['Volume'] = [] for dict in data: reformatted_data['Date'].append(datetime.datetime.fromtimestamp(dict['id'])) reformatted_data['Open'].append(dict['open']) reformatted_data['High'].append(dict['high']) reformatted_data['Low'].append(dict['low']) reformatted_data['Close'].append(dict['close']) reformatted_data['Volume'].append(dict['vol']) print("reformatted data:", reformatted_data) pdata = pd.DataFrame.from_dict(reformatted_data) pdata.set_index('Date', inplace=True) mpf.plot(pdata)Another note is that you need to convert your time stamps into date time objects within your data. This can done with the
datetime.datetime.fromtimestamp(TIMESTAMP)function available when you import datetime.Hopefully, this helps!
-- Caleb
thank u very much.
the new API is so convenient!
What if I m taking data from Api like NSEPY
Then how to solve it
What if I m taking data from Api like NSEPY
Then how to solve it
@Kartket You would need to convert the NSEPY data to be the same format that mplfinance is expecting. The specifics on how you would do this will be dependent on the format of the NSEPY data, but the solution will be very similar to the code I provided last year.
Most helpful comment
Hi franklee00,
It appears the issues lies in the fact that the data that API call is returning isn't in the format that mplfinance expects. The data you specified is a list of dictionaries which each have id, open, close, high, low, volume, etc values. mplfinance expects a dictionary which keys such as 'Data', 'Open', 'Close', 'High', 'Low', 'Volume' each with a value in the form of a list of values.
I went ahead and reformatted your data for you here:
Another note is that you need to convert your time stamps into date time objects within your data. This can done with the
datetime.datetime.fromtimestamp(TIMESTAMP)function available when you import datetime.Hopefully, this helps!
-- Caleb