Matlab画k线图

KLine.m

function KLine(varargin)
%% fun help
% function Kplot(O,H,L,C)
%          Kplot(O,H,L,C,date)
%          Kplot(O,H,L,C,date,colorUp,colorDown,colorLine)
%
%          Kplot(OHLC)
%          Kplot(OHLC,date)
%          Kplot(OHLC,date,colorUp,colorDown,colorLine)
%
%  KLine(OHLC, 0, 'r', 'g', 'w');
%%
isMat = size(varargin{1}, 2); %返回第一个参数的值的列数
indexShift = 0; %参数索引位置
useDate = 0; %是否使用

%提取OHLC
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 = 'g'; 
    colorUp = 'r'; 
else
    colorUp = varargin{3+indexShift};
    colorDown = varargin{4+indexShift};
end

%设置Date值
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 = .3 * min([(date(2)-date(1)) (date(3)-date(2))]);


d = C - O; %收盘价-开盘价

hold on


%画k线, 收盘价小于开盘价,跌
n = find(d < 0);
for i = 1:length(n)
    line([date(n(i)) date(n(i))], [L(n(i)) H(n(i))], 'Color', colorDown) %x1, x2, y1, y2
    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

%画k线,收盘价大于开盘价,涨
n = find(d > 0);
for i = 1:length(n)
    line([date(n(i)) date(n(i))], [L(n(i)) H(n(i))], 'Color', colorUp) %x1, x2, y1, y2
    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

%画k线,收盘价等于开盘价
n = find(d == 0);
for i = 1:length(n)
    line([date(n(i)) date(n(i))], [L(n(i)) H(n(i))], 'Color', 'w') %x1, x2, y1, y2
    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, 'w')
end

hold off 


volume.m

function Volume(varargin)
%% fun help
% function Kplot(O,H,L,C)
%          Kplot(O,H,L,C,date)
%          Kplot(O,H,L,C,date,colorUp,colorDown,colorLine)
%
%          Kplot(OHLC)
%          Kplot(OHLC,date)
%          Kplot(OHLC,date,colorUp,colorDown,colorLine)
%
%  KLine(OHLC, 0, 'r', 'g', 'w');
%%
isMat = size(varargin{1}, 2); %返回第一个参数的值的列数
indexShift = 0; %参数索引位置
useDate = 0; %是否使用

%提取OHLC
if isMat == 6,
    O = varargin{1}(:,1);
    H = varargin{1}(:,2);
    L = varargin{1}(:,3);
    C = varargin{1}(:,4);
    V = varargin{1}(:,5);
else
    O = varargin{1};
    H = varargin{2};
    L = varargin{3};
    C = varargin{4};
    indexShift = 3;
end

%设置颜色的值
if nargin + isMat < 7
    colorDown = 'g'; 
    colorUp = 'r'; 
else
    colorUp = varargin{3+indexShift};
    colorDown = varargin{4+indexShift};
end

%设置Date值
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 = .3 * min([(date(2)-date(1)) (date(3)-date(2))]);


d = C - O; %收盘价-开盘价

hold on


%画k线, 收盘价小于开盘价,跌
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];
    y = [0      V(n(i))      V(n(i))      0];
    fill(x, y, colorDown)
end

%画k线,收盘价大于开盘价,涨
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];
    y=[0      V(n(i))      V(n(i))      0];
    fill(x, y, colorUp)
end

%画k线,收盘价等于开盘价
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];
    y=[0      V(n(i))      V(n(i))      0];
    fill(x, y, 'w')
end

hold off




KLineDemo.m

%加载数据,改为从网上直接获取或者从通达信客户端
load IF-20120104.mat;
F = F(1:100, :);


%画k线
subplot(3, 1, [1,2]);
OHLC = F(:, 3:6);
KLine(OHLC, 0, 'r', 'g', 'w');
%xlim([1,length( OHLC )]);


%画坐标轴文本
XTick = [];
XTickLabel = [];
    
XTick = [XTick; 1];
str = [num2str(F(1,1)), '-', num2str(F(1,2))];
XTickLabel{numel(XTickLabel)+1, 1} = str;


%10:00
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    
    
%11:30
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


%14:00
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));
if XTick(end) ~= ind
    XTick = [XTick; ind ];
    str = [num2str(F(ind, 1)),'-',num2str(F(ind, 2))];
    XTickLabel{numel(XTickLabel)+1, 1} = str;
end    
set(gca,'XTick', XTick);
set(gca,'XTickLabel', XTickLabel);
title('IF20120104 1分钟K线图', 'FontWeight','Bold', 'FontSize', 15);
grid on


%画成交量
subplot(3, 1, 3);
OHLCV = F(:, 3:8);
Volume(OHLCV, 0, 'r', 'g', 'w');
%title('成交量', 'FontWeight','Bold', 'FontSize', 15);
%xlim([1,length( OHLC )]);    
%画坐标轴文本
set(gca,'XTick', XTick);
set(gca,'XTickLabel', XTickLabel);
grid on
   





要达到国内客户端软件的样式还有很多地方需要修改,会继续完善。





  • 6
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值