I have a excel file download on internet. file looks like this:
> 2017-08-01 00:00 1269.50000 1269.70000 1269.50000 1269.670000
> 2017-08-01 00:01 1269.690000 1269.70000 1269.650000 1269.670000
> 2017-08-01 00:02 1269.680000 1269.690000 1269.650000 1269.670000
> 2017-08-01 00:03 1269.650000 1269.70000 1269.650000 1269.680000
> 2017-08-01 00:04 1269.660000 1269.70000 1269.560000 1269.630000
> 2017-08-01 00:05 1269.650000 1269.680000 1269.620000 1269.660000
> 2017-08-01 00:06 1269.670000 1269.690000 1269.640000 1269.660000
> 2017-08-01 00:07 1269.680000 1269.680000 1269.540000 1269.580000
> 2017-08-01 00:08 1269.570000 1269.60000 1269.540000 1269.550000
> 2017-08-01 00:09 1269.570000 1269.590000 1269.540000 1269.570000
first I load the file into numpy.array filestyle: it look like this:
array([[Timestamp('2017-07-02 22:01:00'), 1241.65, 1241.85, 1241.63,
1241.85],
[Timestamp('2017-07-02 22:02:00'), 1241.95, 1241.95, 1241.85,
1241.95],
[Timestamp('2017-07-02 22:03:00'), 1242.05, 1242.15, 1242.05,
1242.15],
...,
[Timestamp('2017-07-31 23:57:00'), 1267.49, 1267.67, 1267.45,
1267.63],
[Timestamp('2017-07-31 23:58:00'), 1267.65, 1267.67, 1267.4, 1267.56],
[Timestamp('2017-07-31 23:59:00'), 1267.55, 1267.55, 1267.36,
1267.43]], dtype=object)
because matplotlib.candelstick_ohlc command only support datenum format, I transform the [0]column into this:
array([[736512.9173611111, 1241.65, 1241.85, 1241.63, 1241.85],
[736512.9180555556, 1241.95, 1241.95, 1241.85, 1241.95],
[736512.91875, 1242.05, 1242.15, 1242.05, 1242.15],
...,
[736541.9979166667, 1267.49, 1267.67, 1267.45, 1267.63],
[736541.9986111111, 1267.65, 1267.67, 1267.4, 1267.56],
[736541.9993055556, 1267.55, 1267.55, 1267.36, 1267.43]], dtype=object)
then I use matplotlib.candlestick_ohlc to plot candlestick picture but result is not good,looks like this command did not parse the date rightly(not just can not show on the X-axis but also can not map the data in the right position of XY-graph, it causes the whole image to be a mass).image like this: image1 can not find even a single symbol
then I split o,h,l,c information from file and use matplotlib.candlestick2_ohlc ,it can work but does not contain time factor because of the command not having one,image like this: image2 overview image. it can expand to detail which can show one candlestick
how to figure out the problem,i think it has two way:(1)use matplotlib.candlestick_ohlc to parse right datetime, so it can map the information in the right position of XY-graph;(2)use matplotlib.candlestick2_ohlc and add time factor into it. my code like this:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import datetime as dt
import matplotlib.finance as fin
import matplotlib as mpl
df_data=pd.read_excel('HSI.xlsx')
candle_1=df_data.values
for record in candle_1:
record[0]=mpl.dates.date2num(np.datetime64(record[0]).astype(dt.datetime))
t=df_data.iloc[:,0]
o=df_data.iloc[:,1]
h=df_data.iloc[:,2]
l=df_data.iloc[:,3]
c=df_data.iloc[:,4]
fig,ax1=plt.subplots()
##市场数据图表实现
fin.candlestick_ohlc(ax1,candle_1,colorup='r', colordown='g', alpha=0.75,width=0.4)
plt.style.use('ggplot')
plt.show()
and another just change a little:
fin.candlestick2_ohlc(ax1,o,h,l,c,colorup='r', colordown='g', alpha=0.75,width=0.4)
the right candlestick image when i use candlestick2_ohlc command to picture,but this command has no time factor: