Matlab画k线图

xiaoxiao2021-02-28  67

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; :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          :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 :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    

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

转载请注明原文地址: https://www.6miu.com/read-64923.html

最新回复(0)