<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">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
</pre>
<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1561534406451 ql-align-center" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; text-align: left; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>
Python学习交流群:1004391443
网友评论