-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathKplotNew.m
More file actions
97 lines (93 loc) · 2.8 KB
/
KplotNew.m
File metadata and controls
97 lines (93 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
function KplotNew(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)
% required inputs: column vectors of O(pen), H(igh), L(ow)
% and C(lose) prices of commodity.
% Alternative: OHLC matrix of size [rowsx4]
%
% Note: identical inputs as required for barChartPlot except for colors
% 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;
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';
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};
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)
if length(date)>=3
w=.3*min([(date(2)-date(1)) (date(3)-date(2))]);
else
w = .3;
end
%%%%%%%%%%%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);
% ŃôĎß
if O(i)<=C(i)
colorLine = colorUp;
line([date(i) date(i)],[L(i) O(i)],'Color',colorLine);
line([date(i) date(i)],[C(i) H(i)],'Color',colorLine);
end
% ŇőĎß
if O(i)>C(i)
colorLine = colorDown;
line([date(i) date(i)],[L(i) C(i)],'Color',colorLine);
line([date(i) date(i)],[O(i) H(i)],'Color',colorLine);
end
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,'EdgeColor', 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,'EdgeColor', colorUp);
end
xlim([0 length(O)+1]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
hold off