加载中…

加载中...

正文 字体大小:

4.K线图以及常用技术指标的Matlab实现-基于Matlab的量化投资

(2012-08-04 21:27:03)
标签:

量化投资

matlab

k线图

常用技术指标

matlab实现

杂谈

分类: 基于MATLAB的量化投资

本次主要讲解用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

 

MatlabBar图中Bar颜色灵活设置的一点总结【by faruto

http://www.matlabsky.com/thread-24447-1-1.html

[原创]坐标轴刻度标签旋转升级版

http://www.matlabsky.com/thread-23524-1-1.html

 

========================

本次测试Demo使用数据,IF20120104的主力数据-1分钟线。数据形式:

4.K线图以及常用技术指标的Matlab实现-基于Matlab的量化投资

K线图实现:

4.K线图以及常用技术指标的Matlab实现-基于Matlab的量化投资
局部:
4.K线图以及常用技术指标的Matlab实现-基于Matlab的量化投资

该K线图实现与matlab自带的candle的K线图实现的区别是
matlab的candle生成的K线图阴线阳线的颜色是一样的,不能灵活设置。该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/5 scrsz(4)]);

 

subplot(3,1,[1 2]);

OHLC = F(:,3:6);

cndlV2(OHLC,0,'r','b','k');

xlim([1,length( OHLC )]);

 

    % % Tick Label Set

    XTick = [];

    XTickLabel = [];

   

    XTick = [XTick; 1];

    str = [num2str(F(1,1)),'-',num2str(F(1,2))];

    XTickLabel{numel(XTickLabel)+1, 1} = str;

 

    ind = find(F(:,2) == 1000, 1);

    if ~isempty(ind)

        XTick = [XTick; ind ];

        str = [num2str(F(ind, 1)),'-',num2str(F(ind, 2))];

        XTickLabel{numel(XTickLabel)+1, 1} = str;

    end   

   

    ind = find(F(:,2) == 1130, 1);

    if ~isempty(ind)

        XTick = [XTick; ind ];

        str = [num2str(F(ind, 1)),'-',num2str(F(ind, 2))];

        XTickLabel{numel(XTickLabel)+1, 1} = str;

    end

   

    ind = find(F(:,2) == 1400, 1);

    if ~isempty(ind)

        XTick = [XTick; ind ];

        str = [num2str(F(ind, 1)),'-',num2str(F(ind, 2))];

        XTickLabel{numel(XTickLabel)+1, 1} = str;

    end   

   

    ind = length(F(:,1));

    XTick = [XTick; ind ];

    str = [num2str(F(ind, 1)),'-',num2str(F(ind, 2))];

    XTickLabel{numel(XTickLabel)+1, 1} = str;

   

    set(gca,'XTick', XTick);

    set(gca,'XTickLabel', XTickLabel);

    TickLabelRotate(gca, 'x', 30, 'right');

   

title('K线图Matlab实现Demo', 'FontWeight','Bold', 'FontSize', 15);

 

subplot(313);

bar( F(:,7) );

xlim([1,length( OHLC )]);

title('成交量', 'FontWeight','Bold', 'FontSize', 15);

   

    set(gca,'XTick', XTick);

    set(gca,'XTickLabel', XTickLabel);

    TickLabelRotate(gca, 'x', 30, 'right');

[/code]

 

K线图函数代码(只需开高低收,即可画出K线图)

[code]

function cndlV2(varargin)

% See if we have [OHLC] or seperate vectors and retrieve our

% required variables (Feel free to make this code more pretty ;-)

isMat = size(varargin{1},2);

indexShift = 0;

useDate = 0;

 

if isMat == 4,

    O = varargin{1}(:,1);

    H = varargin{1}(:,2);

    L = varargin{1}(:,3);

    C = varargin{1}(:,4);

else

    O = varargin{1};

    H = varargin{2};

    L = varargin{3};

    C = varargin{4};

    indexShift = 3;

end

if nargin+isMat < 7,

    colorDown = 'k';

    colorUp = 'w';

    colorLine = 'k';

else

    colorUp = varargin{3+indexShift};

    colorDown = varargin{4+indexShift};

    colorLine = varargin{5+indexShift};

end

if nargin+isMat < 6,

    date = (1:length(O))';

else

    if varargin{2+indexShift} ~= 0

        date = varargin{2+indexShift};

        useDate = 1;

    else

        date = (1:length(O))';

    end

end

 

% w = Width of body, change multiplier to draw body thicker or thinner

% the 'min' ensures no errors on weekends ('time gap Fri. Mon.' > wanted

% spacing)

w=.3*min([(date(2)-date(1)) (date(3)-date(2))]);

%%%%%%%%%%%Find up and down days%%%%%%%%%%%%%%%%%%%

d=C-O;

l=length(d);

hold on

%%%%%%%%draw line from Low to High%%%%%%%%%%%%%%%%%

for i=1:l

   line([date(i) date(i)],[L(i) H(i)],'Color',colorLine)

end

%%%%%%%%%%draw white (or user defined) body (down day)%%%%%%%%%%%%%%%%%

n=find(d<0);

for i=1:length(n)

    x=[date(n(i))-w date(n(i))-w date(n(i))+w date(n(i))+w date(n(i))-w];

    y=[O(n(i)) C(n(i)) C(n(i)) O(n(i)) O(n(i))];

    fill(x,y,colorDown)

end

%%%%%%%%%%draw black (or user defined) body(up day)%%%%%%%%%%%%%%%%%%%

n=find(d>=0);

for i=1:length(n)

    x=[date(n(i))-w date(n(i))-w date(n(i))+w date(n(i))+w date(n(i))-w];

    y=[O(n(i)) C(n(i)) C(n(i)) O(n(i)) O(n(i))];

    fill(x,y,colorUp)

end

 

if (nargin+isMat > 5) && useDate,

%     tlabel('x');

    dynamicDateTicks

end

 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

hold off

[/code]

 

常见技术指标

1MA(移动平均)

2MACD指数平滑异同移动平均线

MACD 的计算:

1)计算短期(S日)指数移动平均线和长期(L日)指数移动平均线EMA1EMA2

2)计算离差值DIF=EMA1-EMA2

3)计算 DIFN日指数移动平均线,即DEA

4)计算MACD=2*(DIF-DEA)

3DMA平均线差指标

DMA的计算:

1)计算短期(S日)移动均线和长期(L日)移动均线MA1MA2

2)计算平均线差DMA=MA1-MA2

3)计算DMAM日移动平均线,即AMA

4TRIX(三重指数平滑移动平均指标)

TRIX的计算:

1)计算N日的指数移动平均线EMA

2)对上述EMA再进行两次N 日指数移动平均后得到TR

3)计算TRIX=(TR-昨日TR)/昨日TR*100

4)计算TRIXM日简单移动平均MATRIX

 

实现Demo

4.K线图以及常用技术指标的Matlab实现-基于Matlab的量化投资

实现测试脚本代码:

[code]

%% 常见技术指标Matlab实现

% by LiYang/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 1 scrsz(3)*4/5 scrsz(4)]);

 

subplot(221);

OHLC = F(:,3:6);

cndlV2(OHLC,0,'r','b','k');

xlim([1,length( OHLC )]);

    set(gca,'XTick', XTick);

    set(gca,'XTickLabel', XTickLabel);

    TickLabelRotate(gca, 'x', 30, 'right');

 

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 )]);

    set(gca,'XTick', XTick);

    set(gca,'XTickLabel', XTickLabel);

    TickLabelRotate(gca, 'x', 30, 'right');

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 )]);

    set(gca,'XTick', XTick);

    set(gca,'XTickLabel', XTickLabel);

    TickLabelRotate(gca, 'x', 30, 'right');

%% 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 )]);

    set(gca,'XTick', XTick);

    set(gca,'XTickLabel', XTickLabel);

    TickLabelRotate(gca, 'x', 30, 'right');

[/code]


0

阅读 评论 收藏 转载 喜欢 打印举报
  • 评论加载中,请稍候...
发评论

    发评论

    以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

      

    新浪BLOG意见反馈留言板 电话:4006900000 提示音后按1键(按当地市话标准计费) 欢迎批评指正

    新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 会员注册 | 产品答疑

    新浪公司 版权所有