MATLAB里面自带了画K线的函数candle

  • 自带函数效果预览图
  • 原始candle的使用方法
    • 语法
    • 描述
    • 输入参数
  • 修改源代码
    • 原始代码
    • 修改部分
  • 最终显示预览
  • 修改后的完整代码

自带函数效果预览图


可以看出K线图有基本的框架,但是和我们平常的习惯不同,还需要修改其颜色,使得更直观些

原始candle的使用方法

语法

candle(Data)
candle(Data,Color)
h = candle(ax___)

描述

  1. 例子
    candle(Data)从证券的一系列开盘价、最高价、最低价和收盘价中绘制蜡烛形图。如果收盘价大于开盘价,则主体(开盘价和收盘价之间的区域)未填充;否则主体被填充。

  2. 例子
    candle(Data,Color)添加Color的可选参数.

  3. 例子
    h = candle(ax___)为ax添加可选参数. 默认为gca,可选任意坐标轴

输入参数

  1. Data=开盘价、最高价、最低价和收盘价数据
    开盘价、最高价、最低价和收盘价的数据,指定为矩阵、表格或时间表。对于矩阵输入,Data 是存储在相应列中的开盘价、最高价、最低价和收盘价的M矩阵。
    数据类型:double|table|timetable

  2. color= (可选) 三个元素颜色矢量
    图形窗口的背景颜色(默认) |颜色矢量[R G B] |字符串
    三个元素颜色矢量,指定为[R G B]颜色矢量或指定颜色名称的字符串。默认颜色因图形窗口的背景颜色而异。
    数据类型:double|string

修改源代码

原始代码

function varargout = candle(varargin)%--------------------------- Parsing/Validation --------------------------%
trynarginchk(1,Inf);[ax,args] = internal.finance.axesparser(varargin{:});if ~isempty(ax) && ~isscalar(ax)error(message('finance:internal:finance:axesparser:ScalarAxes'))endoutput = internal.finance.ftseriesInputParser(args, ...4,{'open','high','low','close'},{},{},{'Color'},{''},{@(x)1,@ischar},1);
catch MEthrowAsCaller(ME)
end[data,optional,dates,~] = output{:};
op = data(:,1);
hi = data(:,2);
lo = data(:,3);
cl = data(:,4);% Validation work will be left to child functions.
color = optional.Color;%------------------------------ Data Preparation -------------------------%% Need to pad all inputs with NaN's to leave spaces between day data
% Vertical High/Low lines data preparation.
numObs = length(hi(:));hiloVertical = [hi lo NaN(numObs, 1)]';
indexVertical = repmat(dates',3,1);% Boxes data preparation
if isdatetime(dates) && length(dates) > 1%If using datetimes, make the box width one half of the smallest%distance between datesinc = 1/4 * min(diff(dates));
elseinc = 0.25;
end
indexLeft = dates - inc;
indexRight = dates + inc;%------------------------------- Plot ------------------------------------%if isempty(ax)ax = gca;
end% Store NextPlot flag (and restore on cleanup):
next = get(ax,'NextPlot');
cleanupObj = onCleanup(@()set(ax,'NextPlot',next));backgroundColor = get(ax,'color');
if isempty(color)cls = get(ax, 'colororder');color = cls(1, :);
endh = gobjects(numObs+1,1); % Preallocate% Plot vertical lines
h(1) = plot(ax,indexVertical(:),hiloVertical(:),'Color',color,'AlignVertexCenters','on');set(ax,'NextPlot','add')% Plot filled boxes
colorSet = {backgroundColor,color};% Filled the boxes when opening price is greater than the closing price.
filledIndex = ones(numObs, 1);
filledIndex(op > cl) = 2;tryfor i = 1 : numObsh(i+1) = fill(ax, ...[indexLeft(i); indexLeft(i); indexRight(i); indexRight(i)], ...[op(i); cl(i); cl(i); op(i)],colorSet{filledIndex(i)},'Edgecolor',color, ...'AlignVertexCenters', 'on');end
catch MEthrowAsCaller(ME)
endswitch nextcase {'replace','replaceall'}grid(ax, 'on')case {'replacechildren','add'}  % Do not modify axes properties
endif nargout % Not equal to 0varargout = {h};
endend;

修改部分


1. 这里是输入参数的读取
2. 这里是单日数据的读取
由此可见,输入参数不管为多少个,只取前4个

1.这里是日期长度,后期画图plot需要的横坐标和纵坐标
2. 这里是画框需要的坐标点,后期画图fill需要的参数
3. 这里是坐标轴的设置,默认取当前axes


1. 这里是默认颜色的选取,可以不管
2. 这里是画直线的,也就是每天的最高价和最低价之间的细线,统一都是一种颜色,所以等会需要更
改,用两个plot分别画出不同颜色的细线
3. 这里是后面 fill 给框框填色的颜色选取,由前面的设置选取,需要更改
4. 这里是后面判断填色的依据

2处的修改

kk=indexVertical(:,find(op > cl));
kkk=hiloVertical(:,find(op > cl));
hh1=plot(ax,kk(:),kkk(:),'color',[0 150/255 0],'LineWidth',1,'AlignVertexCenters','on');
hold on;
kk=indexVertical(:,find(op <= cl));
kkk=hiloVertical(:,find(op <= cl));
hh2=plot(ax,kk(:),kkk(:),'r-','LineWidth',1,'AlignVertexCenters','on');
h = [hh1 hh2];

这里需要根据开盘价和收盘价设置响应的颜色,用两个 plot 得到相应的线,同时需要将句柄汇聚到h
3处的修改

colorSet = {[0 150/255 0],'red'};

这段代码的意思是跌了用深绿色,涨了用红色,深绿的RGB为[ 0 150 0],但是matlab的参数必须小于1,得除以255.

这里使用啦填充颜色的,我们前面已经修改了填充的颜色,还需要更改边框的颜色,也就是Edgecolor,代码如下:

   for i = 1 : numObsh(i+2) = fill(ax, ...[indexLeft(i); indexLeft(i); indexRight(i); indexRight(i)], ...[op(i); cl(i); cl(i); op(i)],colorSet{filledIndex(i)},'Edgecolor',colorSet{filledIndex(i)}, ...'AlignVertexCenters', 'on');end

最终显示预览


好了,基本都已经修改完成了,代码也带着大家详细解读了,你可以根据自己的需求再更改其他的细节

修改后的完整代码

   function varargout = candle(varargin)
%CANDLE Candlestick chart.
%
% Syntax:
%
%   candle(Data)
%   candle(Data,Color)
%   candle(ax,___)
%
% Description:
%
%   CANDLE plots a candlestick chart from a series of opening, high, low,
%   and closing prices of a security. If the closing price is greater than the
%   opening price, the body (the region between the open and close price)
%   is unfilled; otherwise the body is filled.
%
% Input Argument:
%
%   Data    - A matrix, table, or timetable. For matrix input, Data is an
%             M-by-4 matrix of opening, high, low, and closing prices.
%             Timetables and tables with M rows contain variables named
%             'Open', 'High', 'Low', and 'Close' (case insensitive).
%
% Optional Argument:
%
%   ax      - Valid axis object. The plot will be created in the axes specified
%             by ax instead of in the current axes (gca). The option ax can
%             precede any of the input argument combinations.
%
%   Color   - Three element color vector, [R G B], or a string specifying the
%             color name. The default color differs depending on the background
%             color of the figure window. See COLORSPEC for additional details.
%
% Output Argument:
%
%   h                    - Graphic handle of the figure.
%
%   See also HIGHLOW, KAGI, LINEBREAK, POINTFIG, PRICEANDVOL, RENKO, VOLAREA.%  Copyright 1995-2018 The MathWorks, Inc.%--------------------------- Parsing/Validation --------------------------%
trynarginchk(1,Inf);[ax,args] = internal.finance.axesparser(varargin{:});if ~isempty(ax) && ~isscalar(ax)error(message('finance:internal:finance:axesparser:ScalarAxes'))endoutput = internal.finance.ftseriesInputParser(args, ...4,{'open','high','low','close'},{},{},{'Color'},{''},{@(x)1,@ischar},1);
catch MEthrowAsCaller(ME)
end[data,optional,dates,~] = output{:};
op = data(:,1);
hi = data(:,2);
lo = data(:,3);
cl = data(:,4);% Validation work will be left to child functions.
color = optional.Color;%------------------------------ Data Preparation -------------------------%% Need to pad all inputs with NaN's to leave spaces between day data
% Vertical High/Low lines data preparation.
numObs = length(hi(:));hiloVertical = [hi lo NaN(numObs, 1)]';
indexVertical = repmat(dates',3,1);% Boxes data preparation
if isdatetime(dates) && length(dates) > 1%If using datetimes, make the box width one half of the smallest%distance between datesinc = 1/4 * min(diff(dates));
elseinc = 0.25;
end
indexLeft = dates - inc;
indexRight = dates + inc;%------------------------------- Plot ------------------------------------%if isempty(ax)ax = gca;
end% Store NextPlot flag (and restore on cleanup):
next = get(ax,'NextPlot');
cleanupObj = onCleanup(@()set(ax,'NextPlot',next));backgroundColor = get(ax,'color');
if isempty(color)cls = get(ax, 'colororder');color = cls(1, :);
endh = gobjects(numObs+1,1); % Preallocate% Plot vertical lines
kk=indexVertical(:,find(op > cl));
kkk=hiloVertical(:,find(op > cl));
hh1=plot(ax,kk(:),kkk(:),'color',[0 150/255 0],'LineWidth',1,'AlignVertexCenters','on');
hold on;
kk=indexVertical(:,find(op <= cl));
kkk=hiloVertical(:,find(op <= cl));
hh2=plot(ax,kk(:),kkk(:),'r-','LineWidth',1,'AlignVertexCenters','on');
h = [hh1 hh2];set(ax,'NextPlot','add')% Plot filled boxes
colorSet = {[0 150/255 0],'red'};% Filled the boxes when opening price is greater than the closing price.
filledIndex = ones(numObs, 1);
filledIndex(op <= cl) = 2;tryfor i = 1 : numObsh(i+2) = fill(ax, ...[indexLeft(i); indexLeft(i); indexRight(i); indexRight(i)], ...[op(i); cl(i); cl(i); op(i)],colorSet{filledIndex(i)},'Edgecolor',colorSet{filledIndex(i)}, ...'AlignVertexCenters', 'on');end
catch MEthrowAsCaller(ME)
endswitch nextcase {'replace','replaceall'}grid(ax, 'on')case {'replacechildren','add'}  % Do not modify axes properties
endif nargout % Not equal to 0varargout = {h};
endend

如何用matlab画股市K线,修改内置candle函数相关推荐

  1. matlab 中 t=0:t:(n-1)t;k=0:n-1,如何用matlab画出单位脉响应h(n)=sin(n

    公告: 为响应国家净网行动,部分内容已经删除,感谢读者理解. 话题:如何用matlab画出单位脉响应h(n)=sin(n回答:n=1:50;%可根据需求调整范围 h(n)=sin(n-10)/(n-1 ...

  2. 【金融工程实验】【matlab】使用candle函数画日均k线图

    使用candle函数画日均k线图 要求 数据 数据读入 数据处理 显示图形 本实验为课程设计需求 要求 下载一支股票2020年2月.3月的日线数据,并用MATLAB绘制日K线图,要求绘制5日均线和20 ...

  3. matlab鼠标三维坐标点,请问如何用matlab画三维点,已知x,y,z的坐标,在三维坐标系上显示...

    点击查看请问如何用matlab画三维点,已知x,y,z的坐标,在三维坐标系上显示具体信息 答:例如 : X=1,Y=2,Z=3; 代码就是: plot3(1,2,3,'*') grid on%加网格 ...

  4. 数学建模中如何用 matlab画漂亮的图(一)

    数学建模中如何用 matlab画漂亮的图(二维图形) 1 plot绘图命令*** 1.1 plot(x) 当x为实向量时,plot(x)绘制出的曲线,横坐标为该向量的下表,纵坐标为每一个下表位置所对应 ...

  5. matlab画平面心型线,如何用matlab画出心形线

    心形线,是一个圆上的固定一点在它绕着与其相切且半径相同的另外一个圆周滚动时所形成的轨迹线.下面就简单讲解一下如何用matlab画出心形线. 1.心形线的数学定义 2.编制的绘制心形线的matlab程序 ...

  6. k线形态python_如何用Python量化“相似K线”实现形态选股?

    (文章转载自公众号 MindGo量化平台, 作者 陈城) 导读:"历史会重演"是技术分析的三大假设之一,市场行为与投资者心理有着千丝万缕的联系.比如价格形态,它们通过一些特定的价格 ...

  7. python 相似形态 股票_如何用Python量化“相似K线”实现形态选股?

    导读:"历史会重演"是技术分析的三大假设之一,市场行为与投资者心理有着千丝万缕的联系.比如价格形态,它们通过一些特定的价格图表形状表现出来,而这些图形表示了人们对某市场看好或看淡的 ...

  8. python画出K线图及技术指标

    目录 安装mplfinance及Ta-lib 画图 安装mplfinance及Ta-lib mplfinance是基于matplotlib的金融数据可视化分析模块,前身是mpl_finance,比起m ...

  9. k线符号图解大全_股市k线图各种符号意义?k线符号图解大全!

    股市k线图各种颜色代表什么意思 白线是5日均线...黄线10日均线 ..紫线20日均线..绿线60日均线 可根据自己习惯调整参数 提供K线图的用法:K线根据计算单位的不同,一般分为:日K线.周K线.月 ...

最新文章

  1. 谷歌排名第一的编程语言,死磕它这两点,小白也能学的会!不信你看!
  2. 第十五届全国大学生智能车安徽赛区参赛须知和竞赛日程安排
  3. Shell-流程控制
  4. 基于jquery的php分页,基于jQuery封装的分页组件
  5. 编程开发使用的辅助软件大全
  6. PCM - partner channel management的数据库表
  7. Java 多线程异常捕获Runnable实现
  8. Kafka核心源码解析 - KafkaApis源码解析
  9. 运算放大器基本公式_还在被三阶/四阶/运算放大器滤波器PLL这些概念困扰?这篇文章帮你搞懂它...
  10. python21天打卡Day12--for循环,列表推导式-构建列表
  11. 软工作业PSP与单元测试训练 15100231
  12. G1手机上的VOIP之旅 - SIP Server + SipDroid
  13. mycat基础实验之主从配置读写分离和分表
  14. sqlserver2008r2通过发布和订阅的方式进行数据库同步
  15. 成绩查询系统源代码-Leo老师
  16. 征信报告 加密文档_Secret Folder for Mac(文件加密隐藏软件)
  17. 【供应链架构day7】美团供应链的架构之道:O2O关键战场在供给端
  18. 如何制作自己的网站?
  19. 压力面试问题——当你和好友,同一天同一家公司面试同一个岗位,你更希望谁入选?3种回答方式解析|智测优聘总结
  20. Linux服务器带宽占用高导致无法登录的处理经验分享

热门文章

  1. 网络空间测绘——MQTT服务篇
  2. 爬虫之request模块
  3. 高绩效人力资源团队与众不同的 5 件事
  4. Complexity 汇总
  5. js基础day01小结
  6. [css3]圆盘旋转动画
  7. 计算机网络学习一(计算机网络概述)
  8. STM32F103ZET6应用之-HX711重力传感器与hc_sr04超声波模块联合应用
  9. 系统集成项目管理工程师学习经验集【60,59】
  10. CEC2017:鱼鹰优化算法(Osprey optimization algorithm,OOA)求解cec2017(提供MATLAB代码)