Matlab画k线图

来源:互联网 发布:wifi网络监控软件 编辑:程序博客网 时间:2024/04/27 19:39

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; %是否使用%提取OHLCif 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))';    endend%设置宽度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')endhold 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; %是否使用%提取OHLCif 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))';    endend%设置宽度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')endhold 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:00ind = 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:30ind = 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:00ind = 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   





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





0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 麦芒5开不了机怎么办 麦芒6针丢了怎么办 麦芒6扬声器坏了怎么办 华为手机2s太卡怎么办 华为麦芒6网速慢怎么办 华为麦芒5太卡怎么办 小米note3拍照反应慢怎么办 华为刷机后还要账号密码怎么办 刷机后忘记华为账号和密码怎么办 荣耀7x耗电快怎么办 小米2s死机后怎么办? 电信合约卡不想用了怎么办 vivo合约机掉了怎么办 华为合约机丢了怎么办 两年合约机掉了怎么办 电信合约机丢了怎么办 s8合约机坏了怎么办 合约机的卡掉了怎么办 移动合约机屏幕碎了怎么办 5s用不了电信卡怎么办 vivo手机4g信号差怎么办 电信dns辅服务器未响应怎么办 笔记本wifi下载速度慢怎么办 苹果wifi下载速度慢怎么办 小米手机wifi下载速度慢怎么办 苹果8plus上网慢怎么办 小米5c死机了怎么办 苹果x自拍反方向怎么办 硅胶手机壳出油怎么办 指环扣松了怎么办图解 塑料放久了发粘怎么办 橡胶时间久了粘怎么办 胶的手机套变黄怎么办 手机壳硅胶变黄怎么办 硅胶手机壳大了怎么办 硅胶手机壳变大了怎么办 硅胶手机壳有点大怎么办 硅胶手机壳粘手怎么办 透明手机壳变黄怎么办? 耳机胶套经常掉怎么办 硅胶手机壳粘毛怎么办