| CheapHLOCPlot(t,Hi,Lo,Op,Cl,Vo,M1,M2)
|
function CheapHLOCPlot(t,Hi,Lo,Op,Cl,Vo,M1,M2)
% CHEAPHLOCPLOT A free High-Low-Open-Close
% (and volume and moving average) plot to
% answer a CSSM thread ("Subject: on using
% matlab to plot stock charts").
%
% Parm # Description Size Optional
% ------ ----------- ---- --------
% 1 dates 1xN No
% 2 High prices 1xN No
% 3 Low prices 1xN No
% 4 Open prices 1xN No
% 5 Close prices 1xN No
% 6 Volume 1xN Yes
% 7 Moving Average lag 1 1xN Yes
% 8 Moving Average lag 2 1xN Yes
%
% If no parameters are provided, random data
% will be used
%
% For educational purposes only
%
% IT'S NOT FANCY BUT IT WORKS
% Michael Robbins, CFA
% Robbins@Bloomberg.net
% michael.robbins@us.cibc.com
if nargin > 5
DOVO = 1;
else
DOVO = 0;
end;
if nargin > 7
DOMA = 1;
else
DOMA = 0;
end;
if nargin == 0
DOVO = 1;
DOMA = 1;
MA1=30;
MA2=60;
% CREATE VERY NAIVE RANDOM DATA
% I'm sure there's a vectorized solution,
% but I'm lazy
t = today-99:today;
Lo = 99;
Hi = 101;
Op = 100;
Cl = 101;
Range = Hi - Lo;
for i=2:100
Mid = Range./2+Lo(i-1);
Lo(i) = Mid-5.*rand(1)-2.5;
Hi(i) = Mid+5.*rand(1)+2.5;
Range = Hi(i) - Lo(i);
Op(i) = Range.*rand(1)+Lo(i);
Cl(i) = Range.*rand(1)+Lo(i);
end;
Vo = 1000.*rand(1,100);
end;
% PLOT HI-LO
figure;
if DOVO subplot(3,3,1:6); end;
h=line([t(:) t(:)].',[Lo(:) Hi(:)].');
set(h,'color','k');
datetick;
% PLOT OPEN
h=line([t(:)-0.5 t(:)].',[Op(:) Op(:)].');
set(h,'color','k');
% PLOT CLOSE
h=line([t(:) t(:)+0.5].',[Cl(:) Cl(:)].');
set(h,'color','k');
% MOVING AVERAGES
% I'm sure there's a vectorized solution,
% but I'm lazy. I think you can use FILTER
% instead of the loops.
if DOMA
title(sprintf( ...
'Price with Moving Average [%d,%d]', ...
MA1,MA2));
for j=1:2
if j==1
MA=MA1;
c = 'b';
else
MA=MA2;
c = 'r';
end;
MAval = [];
for i=length(t):-1:MA
MAval = [MAval mean(Cl(i:-1:i-MA+1))];
end;
hold on;
plot(t(end:-1:MA),MAval,c);
end;
else
title('Price');
end;
% VOL
if DOVO
subplot(3,3,7:9);
bar(t,Vo);
datetick;
title('Volume');
end;
|
|