好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Python量化交易-绘制蜡烛图

mpl_finance模块已经从matlibplot里独立出来,实现了蜡烛线绘制功能,其包含的函数有:

1、 candlestick2_ochl(ax,opens,closes,highs,lows,width=4,colorup='k',colordown='r',alpha=0.75)

2、 candlestick2_ohlc(ax,opens,closes,highs,lows,width=4,colorup='k',colordown='r',alpha=0.75)

3、 candlestick_ochl(ax,quotes,width=0.2,colorup='k',colordown='r',alpha=1.0)

4、 candlestick_ohlc(ax,quotes,width=0.2,colorup='k',colordown='r',alpha=1.0)

5、 plot_day_summary2_ochl(ax,opens,closes,highs,lows,ticksize=4,colorup='k',colordown='r')

6、 plot_day_summary2_ohlc(ax,opens,highs,lows,closes,ticksize=4,colorup='k',colordown='r')

7、 plot_day_summary_oclh(ax,quotes,ticksize=3,colorup='k',colordown='r')

8、 plot_day_summary_ohlc(ax,quotes,ticksize=3,colorup='k',colordown='r')

9、 volume_overlay(ax,opens,closes,volummes,colorup='k',colordown='r',width=4,alpha=1.0)

10、 volume_overlay2(ax,closes,volumes,colorup='k',colordown='r',width=4,alpha=1.0)

11、 volume_overlay3(ax,quotes,colorup='k',colordown='r',width=4,alpha=1.0)

注意点:

mpl_finance模块使用时间需要是浮点类型数据,需要使用matplotlib中dates模块的date2num函数进行转换。

转换方式如下:

timeRecord = dts.date2num(datetime.datetime(yearTime,mothTime,dayTime,hourTime,minitueTime,secTime,msSecTime))

相关代码如下:

# 导入相关库

matplotlib.dates mdates
matplotlib.pyplot plt
pandas pd
matplotlib.dates MONDAY, DateFormatter, DayLocator, WeekdayLocator
tushare ts
mpl_finance candlestick_ohlc
mondays = WeekdayLocator(MONDAY)        alldays = DayLocator()              weekFormatter = DateFormatter()  dayFormatter = DateFormatter()      quotes = ts.get_k_data(, =, =, =, =)
(quotes.index)
quotes.index = pd.to_datetime(quotes[])
(quotes.index)

fig, ax = plt.subplots()
fig.subplots_adjust(=)

ax.xaxis.set_minor_locator(alldays)
candlestick_ohlc(ax, (mdates.date2num(quotes.index.to_pydatetime()),quotes[], quotes[],quotes[], quotes[]),=,=, =)

ax.xaxis_date()
ax.autoscale_view()

plt.setp(plt.gca().get_xticklabels(), =, =)
plt.show()

查看更多关于Python量化交易-绘制蜡烛图的详细内容...

  阅读:33次