python画蜡烛图_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))

相关代码如下:

# 导入相关库

# import datetime

import matplotlib.dates as mdates

import matplotlib.pyplot as plt

import pandas as pd

from matplotlib.dates import MONDAY, DateFormatter, DayLocator, WeekdayLocator

import tushare as ts

from mpl_finance import candlestick_ohlc

# import numpy as np

mondays = WeekdayLocator(MONDAY) # major ticks on the mondays

alldays = DayLocator() # minor ticks on the days

weekFormatter = DateFormatter('%b %d') # e.g., Jan 12

dayFormatter = DateFormatter('%d') # e.g., 12

# a = ts.get_hist_data('600848',start='2018-01-05',end='2018-02-01')

quotes = ts.get_k_data('600519', ktype='D', autype='qfq', start='2019-01-01', end='')

print(quotes.index)

quotes.index = pd.to_datetime(quotes['date'])

print(quotes.index)

# quotes = pd.DataFrame(quotes)

# select desired range of dates

# quotes = quotes[(quotes.index >= date1) & (quotes.index <= date2)]

fig, ax = plt.subplots()

fig.subplots_adjust(bottom=0.2)

# ax.xaxis.set_major_locator(mondays)

ax.xaxis.set_minor_locator(alldays)

# ax.xaxis.set_major_formatter(weekFormatter)

# ax.xaxis.set_minor_formatter(dayFormatter)

# plot_day_summary(ax, quotes, ticksize=3)

candlestick_ohlc(ax, zip(mdates.date2num(quotes.index.to_pydatetime()),quotes['open'], quotes['high'],quotes['low'], quotes['close']),width=0.6,colorup='r', colordown='g')

ax.xaxis_date()

ax.autoscale_view()

plt.setp(plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right')

plt.show()

# 24 2019-02-12

你可能感兴趣的:(python画蜡烛图)