【Matlab量化策略基础】——基于Matlab金融工具箱进行行情数据绘图
发布时间:2017/11/08 08:08:08 7238 1
今天小编给大家带来新的一期Matlab量化策略基础系列(二) 大家在MATLAB界面上点击绘图按钮,就能够找到** `FINANCE TOOLBOX` **提供的绘制图标了。下面我们逐一进行学习。 ![](https://bpstoragenormal.blob.core.chinacloudapi.cn/digquant/prod/./data/attachment/forum/201711/02/160400p7o55za556u71ozp.png) 首先我们得先取得行情数据,这是通过**`Auto-Trader`**的**` traderGetKData`**函数来完成的。 更多函数介绍,请点击[【行情数据函数】](http://www.atrader.com.cn/misc.php?mod=faq "【行情数据函数】")查看 ### 行情数据函数获取 **` traderGetKData`** ```matlab [time, open, high, low, close, volume, turnover, openinterest] = traderGetKData(Market, Code, KFrequency, KFreNum, BeginDate, EndDate, FilledUp, FQ) ``` + 释义 根据起止时间点提取K线数据;可通过此函数获取该时段内重要的价量数据 + 输入参数 + `Market` + 释义:市场类型 + 类型:str + `SZSE` + 释义:深圳股票 + 类型:str + `SSE` + 释义:上海股票 + 类型:str + ` SHFE` + 释义:上海期货 + 类型:str + `DCE` + 释义:大连商品 + 类型:str + `CZCE` + 释义:郑州商品 + 类型:str + ` CFFEX` + 释义:中金所 + 类型:str + `Code` + 释义:交易品种代码 + 类型:str,如:`000002` + `KFrequency` + 释义:K线的时间级别 + 类型:str,如: `min`, `day` + `KFreNum` + 释义:K线的频率 + 类型:int + BeginDate:整型,如20140608 + 释义:开始日期 + 类型:int,如:`20140608` + `EndDate` + 释义:结束日期 + 类型:int,如:`20140609`,为0时取到当天 + `FilledUp` + 释义:补齐类型 + 类型:false or true, false表示不补齐,true为补齐 + `FQ` + 释义:复权类型 + 类型:str,`NA`不复权,`FWard`向前复权,`BWard`向后复权 + 输出参数 + `time` + 释义:时间 + `open` + 释义:开盘价 + `high` + 释义:最高价 + `low` + 释义:最低价 + `close` + 释义:收盘价 + `volume` + 释义:成交量 + `turnover` + 释义:成交金额 + `openinterest` + 释义:持仓量 #### 我们取沪深300指数日行情数据,时间范围是:20160501——20171001 ```matlab clc,clear [time, open, high, low, close, volume, turnover, openinterest] = traderGetKData('sse', '000300', 'day', 1, 20160101, 20170101, false, 'FWard'); data000300 = [time,open,high,close,low,volume]; openprice = data000300(:,2)'; % 开盘价 highprice = data000300(:,3)'; % 最高价 closeprice = data000300(:,4)'; % 最低价 lowprice = data000300(:,5)'; % 收盘价 ``` ### candle(阴阳蜡烛图)函数 在Matlab窗口命令输入**`help candle`**,我们可以看到其函数具体的应用: ![](https://bpstoragenormal.blob.core.chinacloudapi.cn/digquant/prod/./data/attachment/forum/201711/02/161701bbmm7ymmym7up517.png) 我们可以看到**`candle`**函数有两种形式: ```matlab candle(HighPrices,LowPrices,ClosePrices,OpenPrices) candle(HighPrices,LowPrices,ClosePrices,OpenPrices,Color,Dates,Dateform) ``` + 输入参数: + `HighPrices` + 最高价 + `LowPrices` + 最低价 + `ClosePrices` + 收盘价 + `OpenPrices` + 开盘价 + `Color` + (可选)蜡烛图颜色,默认为蓝色 + `Dates` + (可选)日期 + `Dateform` + (可选)时间格式 ### ##### 具体程序如下: ```matlab clc,clear [time, open, high, low, close, volume, turnover, openinterest] = traderGetKData('sse', '000300', 'day', 1, 20170701, 20171001, false, 'FWard'); data000300 = [time,open,high,close,low,volume]; openprice = data000300(:,2); % 开盘价 highprice = data000300(:,3); % 最高价 closeprice = data000300(:,5); % 收盘价 lowprice = data000300(:,4); % 最低价 candle(highprice,lowprice,closeprice,openprice); ``` ![](https://bpstoragenormal.blob.core.chinacloudapi.cn/digquant/prod/./data/attachment/forum/201711/02/173645succtezjprfptgmf.jpg) ##### 修改x轴显示的日期: ```matlab clc,clear [time, open, high, low, close, volume, turnover, openinterest] = traderGetKData('sse', '000300', 'day', 1, 20170701, 20171001, false, 'FWard'); data000300 = [time,open,high,close,low,volume]; openprice = data000300(:,2); % 开盘价 highprice = data000300(:,3); % 最高价 closeprice = data000300(:,5); % 收盘价 lowprice = data000300(:,4); % 最低价 candle(highprice,lowprice,closeprice,openprice,[],time,26); title('HS300指数20170701——20171001行情数据') ``` ![](https://bpstoragenormal.blob.core.chinacloudapi.cn/digquant/prod/./data/attachment/forum/201711/02/174042ij33939gt33go51g.jpg) ##### 为什么是26的解释: 原文链接:[【Matlab量化策略基础】——常用日期函数介绍及使用方法](https://www.digquant.com.cn/research/community/298 "【Matlab量化策略基础】——常用日期函数介绍及使用方法") ```matlab candle(highprice,lowprice,closeprice,openprice,[],time,26); ``` ![](https://bpstoragenormal.blob.core.chinacloudapi.cn/digquant/prod/./data/attachment/forum/201711/02/174300es6snp896s69s6us.png) 交易数据量也可以用**`bar()`**函数绘图。 ![](https://bpstoragenormal.blob.core.chinacloudapi.cn/digquant/prod/./data/attachment/forum/201711/02/175906f92b88oer2rrov59.jpg) ###bolling(布林带)函数 布林线(Bollinger Band)是由三条线组成,在中间的通常为 20 天平均线,而在上下的两条线则分别为 Up 线和 Down 线,算法是首先计出过去 20 日收巿价的标准差 SD(Standard Deviation) ,通常再乘 2 得出 2 倍标准差, Up 线为 20 天平均线加 2 倍标准差, Down 线则为 20 天平均线减 2 倍标准差。 中间线 = 20 日均线 Up 线 = 20 日均线 + 2SD(20 日收巿价) Down 线 =20 日均线 - 2SD(20 日收巿价) 在Matlab里面。计算布林带的函数是**`bollinger`**,其画图函数是 **`bolling`** 在Matlab窗口命令输入**`help bolling`**和**`help bollinger`**,我们可以看到其函数具体的应用: ![](https://bpstoragenormal.blob.core.chinacloudapi.cn/digquant/prod/./data/attachment/forum/201711/02/185604oegmszm1fhr6dy7r.png) ![](https://bpstoragenormal.blob.core.chinacloudapi.cn/digquant/prod/./data/attachment/forum/201711/02/195710icld65ztlopojl3j.png) 我们可以看到**`bolling`**函数有两种形式: ```matlab bolling(Asset,Samples,Alpha,Width) [Movavgv,UpperBand,LowerBand] = bolling(Asset,Samples,Alpha,Width) ``` + 输入参数: + `Asset` + 资产 + `Samples` + 样本数量 + `Alpha` + 移动平均变量的指数变量(权重),默认值为0 + `Width` + 相对应的标准差,默认值为2 ### ##### 具体程序如下: ```matlab clc,clear [time, open, high, low, close, volume, turnover, openinterest] = traderGetKData('sse', '000300', 'day', 1, 20170201, 20171001, false, 'FWard'); data000300 = [time,open,high,close,low,volume]; openprice = data000300(:,2); % 开盘价 highprice = data000300(:,3); % 最高价 closeprice = data000300(:,5); % 收盘价 lowprice = data000300(:,4); % 最低价![](https://bpstoragenormal.blob.core.chinacloudapi.cn/digquant/prod/./data/attachment/forum/201711/03/113349v2dh88828yrhnbr2.jpg) bolling(closeprice,20,2) axis tight ``` ![](https://bpstoragenormal.blob.core.chinacloudapi.cn/digquant/prod/./data/attachment/forum/201711/02/201758n444e8zkgh8fetek.jpg) **`bolling`**函数: ```matlab [mid,uppr,lowr] = bollinger(data,wsize,wts,nstd) [midfts,upprfts,lowrfts] = bollinger(tsobj,wsize,wts,nstd) ``` + 输入参数: + `data` + 时间序列数据 +` wsize` + 窗口大小,默认为20 + `wts` + 权重因子,默认为0 + `nstd` + 上下界的标准差倍数,默认为2 ### ```matlab clc,clear [time, open, high, low, close, volume, turnover, openinterest] = traderGetKData('sse', '000300', 'day', 1, 20160101, 20171001, false, 'FWard'); data000300 = [time,open,high,close,low,volume]; openprice = data000300(:,2); % 开盘价 highprice = data000300(:,3); % 最高价 closeprice = data000300(:,5); % 收盘价 lowprice = data000300(:,4); % 最低价 %计算移动平均值 wsize=20; wts=0; nstd=2; [mid, uppr, lowr] = bollinger(closeprice, wsize, wts, nstd); plot(time,closeprice,'k'); hold on plot(time(wsize:end),mid(wsize:end),'r'); plot(time(wsize:end),uppr(wsize:end),'b'); plot(time(wsize:end),lowr(wsize:end),'b'); dateaxis('x',12) legend('closeprice','mid','uppr','lowr') xlabel('date') ylabel('price') title('bollinger') ``` ![](https://bpstoragenormal.blob.core.chinacloudapi.cn/digquant/prod/./data/attachment/forum/201711/02/204006vlcszlzchccsycsv.jpg) ###highlow(高低价)函数 在Matlab窗口命令输入**`help highlow`**,我们可以看到其函数具体的应用: ![](https://bpstoragenormal.blob.core.chinacloudapi.cn/digquant/prod/./data/attachment/forum/201711/02/205722z5fjvfztzekcf21v.png) 我们可以看到**` highlow`**函数有两种形式: ```matlab highlow(High,Low,Close,Open,Color) highlow(High,Low,Close,Open,Color,Dates,Dateform) Handles = highlow(High,Low,Close,Open,Color,Dates,Dateform) ``` + 输入参数: + `High` + 最高价 + `Low` + 最低价 + `Close` + 收盘价 + `Open` + 开盘价 + `Color` + (可选)颜色设置 + `Dates` + (可选)日期 + `,Dateform` + (可选)时间格式 ### ##### 具体程序如下: ```matlab clc,clear [time, open, high, low, close, volume, turnover, openinterest] = traderGetKData('sse', '000300', 'day', 1, 20170801, 20171001, false, 'FWard'); data000300 = [time,open,high,close,low,volume]; openprice = data000300(:,2); % 开盘价 highprice = data000300(:,3); % 最高价 closeprice = data000300(:,5); % 收盘价 lowprice = data000300(:,4); % 最低价 highlow(highprice,lowprice,closeprice,openprice,'b',time,26); title('HS300指数20170701——20171001行情数据') ``` ![](https://bpstoragenormal.blob.core.chinacloudapi.cn/digquant/prod/./data/attachment/forum/201711/02/211216t17b1i73kskmsvzz.jpg) ### kagi(折线图)函数 在Matlab窗口命令输入**`help kagi`**,我们可以看到其函数具体的应用: ![](https://bpstoragenormal.blob.core.chinacloudapi.cn/digquant/prod/./data/attachment/forum/201711/02/211608xswwq22lghnv2552.png) 我们可以看到**`kagi`**函数有两种形式: ```matlab kagi(X) ``` ##### 具体程序如下: ```matlab clc,clear [time, open, high, low, close, volume, turnover, openinterest] = traderGetKData('sse', '000300', 'day', 1, 20170101, 20171001, false, 'FWard'); data000300 = [time,open,high,close,low,volume]; openprice = data000300(:,2); % 开盘价 highprice = data000300(:,3); % 最高价 closeprice = data000300(:,5); % 收盘价 lowprice = data000300(:,4); % 最低价 data = [time+6500, closeprice]; kagi(data); dateaxis('x',3) grid on axis tight title('HS300指数20170101——20171001行情数据') ``` ![](https://bpstoragenormal.blob.core.chinacloudapi.cn/digquant/prod/./data/attachment/forum/201711/02/212346q2u9zxrca3c22022.jpg) ### renko(砖形图)函数 在Matlab窗口命令输入**`help renko`**,我们可以看到其函数具体的应用: ![](https://bpstoragenormal.blob.core.chinacloudapi.cn/digquant/prod/./data/attachment/forum/201711/02/212612j03hzbhztmbhtoth.png) 我们可以看到**`renko`**函数有两种形式: ```matlab renko(X) renko(X,threshold) ``` ##### 具体程序如下: ```matlab clc,clear [time, open, high, low, close, volume, turnover, openinterest] = traderGetKData('sse', '000300', 'day', 1, 20170101, 20171001, false, 'FWard'); data000300 = [time,open,high,close,low,volume]; openprice = data000300(:,2); % 开盘价 highprice = data000300(:,3); % 最高价 closeprice = data000300(:,5); % 收盘价 lowprice = data000300(:,4); % 最低价 data = [time, closeprice]; kagi(data); dateaxis('x',3) grid on axis tight title('HS300指数20170101——20171001行情数据') ``` ![](https://bpstoragenormal.blob.core.chinacloudapi.cn/digquant/prod/./data/attachment/forum/201711/02/212957fya3z3b4smab0myb.jpg) ### volarea(成交量面积图)函数 在Matlab窗口命令输入**`help volarea`**,我们可以看到其函数具体的应用: ![](https://bpstoragenormal.blob.core.chinacloudapi.cn/digquant/prod/./data/attachment/forum/201711/03/112242xlpt9z5qaiphphig.png) 我们可以看到**`volarea`**函数有两种形式: ```matlab volarea(X) ``` ##### 具体程序如下: ```matlab clc,clear [time, open, high, low, close, volume, turnover, openinterest] = traderGetKData('sse', '000300', 'day', 1, 20170101, 20171001, false, 'FWard'); data000300 = [time,open,high,close,low,volume]; openprice = data000300(:,2); % 开盘价 highprice = data000300(:,3); % 最高价 closeprice = data000300(:,5); % 收盘价 lowprice = data000300(:,4); % 最低价 data = [time, closeprice,volume]; volarea(data) %grid on axis tight title('HS300指数20170101——20171001行情数据') ``` ![](https://bpstoragenormal.blob.core.chinacloudapi.cn/digquant/prod/./data/attachment/forum/201711/03/112439gea7br2d01q08bbv.jpg) ### pointfig(涨跌点图)函数 在Matlab窗口命令输入**`help pointfig`**,我们可以看到其函数具体的应用: ![](https://bpstoragenormal.blob.core.chinacloudapi.cn/digquant/prod/./data/attachment/forum/201711/03/112815yimi8bihmqdr54i5.png) 我们可以看到**` pointfig`**函数有两种形式: ```matlab pointfig(Asset) ``` ##### 具体程序如下: ```matlab clc,clear [time, open, high, low, close, volume, turnover, openinterest] = traderGetKData('sse', '000300', 'day', 1, 20170101, 20171001, false, 'FWard'); data000300 = [time,open,high,close,low,volume]; openprice = data000300(:,2); % 开盘价 highprice = data000300(:,3); % 最高价 closeprice = data000300(:,5); % 收盘价 lowprice = data000300(:,4); % 最低价 pointfig(closeprice) grid on axis tight title('HS300指数20170101——20171001行情数据') ``` ![](https://bpstoragenormal.blob.core.chinacloudapi.cn/digquant/prod/./data/attachment/forum/201711/03/113434szpv8qdyeevld973.jpg) ### priceandvol(涨跌点图)函数 在Matlab窗口命令输入**`help priceandvol`**,我们可以看到其函数具体的应用: ![](https://bpstoragenormal.blob.core.chinacloudapi.cn/digquant/prod/./data/attachment/forum/201711/03/113617tn4xuwua1lvxxt1l.png) 我们可以看到**` priceandvol`**函数有两种形式: ```matlab priceandvol(X) % X为数组:依次是[日期,开盘价,最高价,最低价,收盘价,成交量] ``` ##### 具体程序如下: ```matlab clc,clear [time, open, high, low, close, volume, turnover, openinterest] = traderGetKData('sse', '000300', 'day', 1, 20170101, 20171001, false, 'FWard'); data000300 = [time,open,high,close,low,volume]; openprice = data000300(:,2); % 开盘价 highprice = data000300(:,3); % 最高价 closeprice = data000300(:,5); % 收盘价 lowprice = data000300(:,4); % 最低价 data = [time,openprice,highprice,lowprice,closeprice,volume]; priceandvol(data) grid on title('HS300指数20170101——20171001行情数据') ``` ![](https://bpstoragenormal.blob.core.chinacloudapi.cn/digquant/prod/./data/attachment/forum/201711/03/114054ej0zqba3uib3boza.jpg) ### movavg(移动平均图)函数 在Matlab窗口命令输入**`help movavg`**,我们可以看到其函数具体的应用: ![](https://bpstoragenormal.blob.core.chinacloudapi.cn/digquant/prod/./data/attachment/forum/201711/03/114425y155y1pedd257zt1.png) 我们可以看到**` movavg`**函数有两种形式: ```matlab movavg(Asset,Lead,Lag,Alpha) [Short,Long] = movavg(Asset,Lead,Lag,Alpha) %返回数据,不画图 ``` + 输入参数: + `Asset` + 资产序列价格 + `Lead` + 短样本平滑操作 + `Lag` + 长样本平滑操作 + `Alpha` + 平均的方法 + Alpha = 0(默认),算数移动平均值 + Alpha = 0.5,平方根加权移动平均值 + Alpha = 1,线性加权移动平均值 + Alpha = 4,均方加权移动平均值 + Alpha ='e',指数移动平均值 ### ##### 具体程序如下: ```matlab clc,clear [time, open, high, low, close, volume, turnover, openinterest] = traderGetKData('sse', '000300', 'day', 1, 20170101, 20171001, false, 'FWard'); data000300 = [time,open,high,close,low,volume]; openprice = data000300(:,2); % 开盘价 highprice = data000300(:,3); % 最高价 closeprice = data000300(:,5); % 收盘价 lowprice = data000300(:,4); % 最低价 movavg(closeprice,5,20,0) grid on axis tight title('HS300指数20170101——20171001行情数据') ``` ![](https://bpstoragenormal.blob.core.chinacloudapi.cn/digquant/prod/./data/attachment/forum/201711/03/141139tuezr7ptv97u7c2l.jpg)
3个赞 收藏
发表评论
0条评论
contact@digquant.com.cn

联系

邮箱

0755-8695-2080

联系

电话

关注

微信

关注

QQ

回到

顶部