本次主要讲解用Matlab来实现K线图以及常用的技术指标。
<基于Matlab的量化投资>系列帖子目录
http://www.matlabsky.com/thread-28536-1-1.html
1.前言-基于Matlab的量化投资
http://www.matlabsky.com/thread-28489-1-1.html
2.从一个例子说起-基于Matlab的量化投资
http://www.matlabsky.com/thread-28490-1-1.html
3.关于BackTesting中一些细节的思考-基于Matlab的量化投资
http://www.matlabsky.com/thread-28573-1-1.html
Matlab的Bar图中Bar颜色灵活设置的一点总结【by faruto】
http://www.matlabsky.com/thread-24447-1-1.html
[原创]坐标轴刻度标签旋转升级版
http://www.matlabsky.com/thread-23524-1-1.html
========================
本次测试Demo使用数据,IF在20120104的主力数据-1分钟线。数据形式:
K线图实现:
实现测试脚本代码:
[code]
%% K线图Matlab实现Demo
% by LiYang/faruto
%Email:farutoliyang@gmail.com
% 2012/8/4
scrsz =get(0,'ScreenSize');
figure('Position',[1 1 scrsz(3)*4/5scrsz(4)]);
subplot(3,1,[1 2]);
OHLC = F(:,3:6);
cndlV2(OHLC,0,'r','b','k');
xlim([1,length( OHLC )]);
title('K线图Matlab实现Demo', 'FontWeight','Bold','FontSize', 15);
subplot(313);
bar( F(:,7) );
xlim([1,length( OHLC )]);
title('成交量', 'FontWeight','Bold', 'FontSize',15);
[/code]
K线图函数代码(只需开高低收,即可画出K线图)
[code]
functioncndlV2(varargin)
% See if we have [OHLC] orseperate vectors and retrieve our
% required variables (Feelfree to make this code more pretty ;-)
isMat =size(varargin{1},2);
indexShift = 0;
useDate = 0;
if isMat == 4,
else
end
if nargin+isMat< 7,
else
end
if nargin+isMat< 6,
else
end
% w = Width of body, changemultiplier to draw body thicker or thinner
% the 'min' ensures noerrors on weekends ('time gap Fri. Mon.' >wanted
% spacing)
w=.3*min([(date(2)-date(1))(date(3)-date(2))]);
%%%%%%%%%%%Find up and downdays%%%%%%%%%%%%%%%%%%%
d=C-O;
l=length(d);
hold on
%%%%%%%%draw line from Lowto High%%%%%%%%%%%%%%%%%
for i=1:l
end
%%%%%%%%%%draw white (oruser defined) body (down day)%%%%%%%%%%%%%%%%%
n=find(d<0);
for i=1:length(n)
end
%%%%%%%%%%draw black (oruser defined) body(up day)%%%%%%%%%%%%%%%%%%%
n=find(d>=0);
for i=1:length(n)
end
if (nargin+isMat> 5) &&useDate,
%
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
hold off
[/code]
常见技术指标
1)MA(移动平均)
2)MACD(指数平滑异同移动平均线)
MACD 的计算:
(1)计算短期(S日)指数移动平均线和长期(L日)指数移动平均线EMA1、EMA2。
(2)计算离差值DIF=EMA1-EMA2。
(3)计算DIF的N日指数移动平均线,即DEA。
(4)计算MACD=2*(DIF-DEA)。
3)DMA(平均线差指标)
DMA的计算:
(1)计算短期(S日)移动均线和长期(L日)移动均线MA1、MA2。
(2)计算平均线差DMA=MA1-MA2。
(3)计算DMA的M日移动平均线,即AMA。
4)TRIX(三重指数平滑移动平均指标)
TRIX的计算:
(1)计算N日的指数移动平均线EMA。
(2)对上述EMA再进行两次N 日指数移动平均后得到TR。
(3)计算TRIX=(TR-昨日TR)/昨日TR*100。
(4)计算TRIX的M日简单移动平均MATRIX。
实现Demo
实现测试脚本代码:
[code]
%% 常见技术指标Matlab实现
% byLiYang/faruto
%Email:farutoliyang@gmail.com
% 2012/8/4
%% MA
S = 5;
L = 20;
[SMA, LMA] = movavg(Data, S,L);
SMA(1:S-1) = NaN;
LMA(1:L-1) = NaN;
scrsz =get(0,'ScreenSize');
figure('Position',[1 1scrsz(3)*4/5 scrsz(4)]);
subplot(221);
OHLC = F(:,3:6);
cndlV2(OHLC,0,'r','b','k');
xlim([1,length( OHLC)]);
hold on;
H1 =plot(SMA,'g','LineWidth',1.5);
H2 =plot(LMA,'r','LineWidth',1.5);
title('常见技术指标SMA(简单移动平均线)Matlab实现Demo', 'FontWeight','Bold', 'FontSize',15);
M ={'MA5';'MA20'};
legend([H1,H2],M);
%% MACD
S = 12;
L = 26;
EMA1 = EMA(Data,S);
EMA2 = EMA(Data,L);
DIFF = EMA1-EMA2;
M = 10;
DEA = EMA(DIFF,M);
MACD =2*(DIFF-DEA);
subplot(222);
MACD_p = MACD;
MACD_n = MACD;
MACD_p(MACD_p<0) = 0;
MACD_n(MACD_n>0) = 0;
bar(MACD_p,'r','EdgeColor','r');
hold on;
bar(MACD_n,'b','EdgeColor','b');
plot(DIFF,'k','LineWidth',1.5);
plot(DEA,'g','LineWidth',1.5);
xlim([1,length( OHLC)]);
title('常见技术指标MACD(指数平滑异同移动平均线)Matlab实现Demo', 'FontWeight','Bold', 'FontSize',15);
%% DMA
S = 5;
L = 20;
[MA1, MA2] = movavg(Data, S,L);
MA1(1:S-1) = NaN;
MA2(1:L-1) = NaN;
DMA = MA1-MA2;
M = 5;
AMA = movavg(DMA, M,M);
AMA(1:M-1) = NaN;
subplot(223);
hold on;
plot(DMA,'k','LineWidth',1.5);
plot(AMA,'r','LineWidth',1.5);
title('常见技术指标DMA(平均线差指标)Matlab实现Demo', 'FontWeight','Bold', 'FontSize',15);
legend('DMA','AMA');
xlim([1,length( OHLC)]);
%% TRIX
N = 2;
ema = EMA(Data,N);
M = 20;
TR = EMA( EMA(ema,N) ,N);
TRIX = (TR(2:end)-TR(1:end-1) )./TR(1:end-1)*100;
TRIX = [NaN;TRIX];
MATRIX = movavg(TRIX, M,M);
subplot(224);
hold on;
plot(TRIX,'k','LineWidth',1.5);
plot(MATRIX,'r','LineWidth',1.5);
title('常见技术指标TRIX(三重指数平滑移动平均指标)Matlab实现Demo', 'FontWeight','Bold', 'FontSize',15);
legend('TRIX','MATRIX');
xlim([1,length( OHLC)]);
[/code]
联系客服