Download code
From LiteratePrograms
Back to MATLAB_Financial_Toolbox
Download for Windows: zip
Download for UNIX: zip, tar.gz, tar.bz2
test_simplecandle.m
1 % Copyright (c) 2008 the authors listed at the following URL, and/or 2 % the authors of referenced articles or incorporated external code: 3 % http://en.literateprograms.org/MATLAB_Financial_Toolbox?action=history&offset=20071125111355 4 % 5 % Permission is hereby granted, free of charge, to any person obtaining 6 % a copy of this software and associated documentation files (the 7 % "Software"), to deal in the Software without restriction, including 8 % without limitation the rights to use, copy, modify, merge, publish, 9 % distribute, sublicense, and/or sell copies of the Software, and to 10 % permit persons to whom the Software is furnished to do so, subject to 11 % the following conditions: 12 % 13 % The above copyright notice and this permission notice shall be 14 % included in all copies or substantial portions of the Software. 15 % 16 % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 % EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 % MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 % IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 % CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 % TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 % SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 % 24 % Retrieved from: http://en.literateprograms.org/MATLAB_Financial_Toolbox?oldid=11719 25 26 27 28 sgm = 0.23 / sqrt(250); 29 mu = (1+ 0.04)^(1/250)-1; 30 S0 = 100; 31 nb = 1000; 32 sam = 10; 33 34 T = 1+(1:nb)'/250; 35 S = S0 * exp( (mu-sgm^2/2)*T + sgm * cumsum(randn(nb,1))); 36 37 Soc = [S(1:sam:end-1), S(sam-1:sam:end-1)]; 38 Shl = [max(reshape(S, sam, length(S)/sam)); min(reshape(S, sam, length(S)/sam))]; 39 Sohlc = [Soc(:,1), Shl', Soc(:,2)]; 40 t = T(ceil(sam/2):sam:end-1); 41 42 43 figure('Color',[0.9412 0.9412 0.9412 ]); 44 h1 = subplot(2,1,1); plot(T, S); 45 datetick('x',6,'keeplimits'); 46 47 volumes = ceil(exp( randn(length(Sohlc),1) + 7))+100; 48 49 50 data = struct('title', 'exp brownian motion', 'value', [Sohlc, volumes], ... 51 'names', {{'open', 'high', 'low', 'close', 'volume'}}, 'date', t); 52 53 54 55 h2 = subplot(2,1,2); 56 candle(Sohlc(:,2), Sohlc(:,3), Sohlc(:,4), Sohlc(:,1), [.5 0 0], t, 6) 57 linkaxes([h1, h2], 'x'); 58 59 60 61 simplecandleplot( data, 'prices', 'open;high;low;close', 'volumes', 'volume') 62 simplecandleplot( data, 'prices', 'open;high;low;close', 'volumes', 'volume', 'mode', 'highlow') 63
simplecandleplot.m
1 % Copyright (c) 2008 the authors listed at the following URL, and/or 2 % the authors of referenced articles or incorporated external code: 3 % http://en.literateprograms.org/MATLAB_Financial_Toolbox?action=history&offset=20071125111355 4 % 5 % Permission is hereby granted, free of charge, to any person obtaining 6 % a copy of this software and associated documentation files (the 7 % "Software"), to deal in the Software without restriction, including 8 % without limitation the rights to use, copy, modify, merge, publish, 9 % distribute, sublicense, and/or sell copies of the Software, and to 10 % permit persons to whom the Software is furnished to do so, subject to 11 % the following conditions: 12 % 13 % The above copyright notice and this permission notice shall be 14 % included in all copies or substantial portions of the Software. 15 % 16 % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 % EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 % MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 % IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 % CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 % TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 % SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 % 24 % Retrieved from: http://en.literateprograms.org/MATLAB_Financial_Toolbox?oldid=11719 25 26 function h = simplecandleplot(data, varargin) 27 % CANDLEPLOT - candleplot with volumes 28 % 29 % use 30 % h = simplecandleplot( data, 'prices', 'open;high;low;close', 'volumes', 'volume', options ... ) 31 % options: 32 % 'figure' - handle or empty (new figure) 33 % 'prop' - proportion of the figure dedicated to the volumes 34 % 'candles-color', 'volumes-color', 'volumes-linewidth' - eponyms 35 % 'date-format' - from datestr 36 % 'mode' - 'candle' (default) or 'highlow' 37 % 38 % example 39 % data = xdata('rload', 'bdata-test'); 40 % candleplot(data, 'prices', 'Open;High;Low;Last', 'volumes', 'Volume', 'dates-format', 13) 41 % 42 % See also pdates datestr xdata 43 44 opt = options({'prices', '', 'volumes', '', 'figure', [], 'prop', .2, ... 45 'candles-color', [0 .5 .5], 'volumes-color', [0 .5 .5], ... 46 'volumes-linewidth', 3, 'dates-format', 2, 'mode', 'candle'}, varargin); 47 48 f = opt.get('figure'); 49 if isempty(f) || ~ishandle(f) 50 f = figure('Color',[0.9412 0.9412 0.9412 ]); 51 else 52 figure(f); 53 end 54 55 56 prices_names = opt.get('prices'); 57 prices = xdata('get-col', data, prices_names); 58 h1 = subplot(2,1,1, 'unit', 'normalized'); 59 feval( opt.get('mode') , prices(:,2), prices(:,3), prices(:,4), prices(:,1), opt.get('candles-color'), data.date, 0); 60 61 62 volumes_name = opt.get('volumes'); 63 h2 = subplot(2,1,2, 'unit', 'normalized'); 64 [x,y] = barplot(data.date, xdata('get-col', data, volumes_name)); 65 plot(x,y, 'color', opt.get('volumes-color'), 'linewidth', opt.get('volumes-linewidth')); 66 67 dateaxis('X', opt.get('dates-format')); 68 69 70 p1 = get(h1, 'position'); 71 p2 = get(h2, 'position'); 72 c = opt.get('prop'); 73 dp2 = p1(2) - (p2(2)+p2(4)); 74 np24 = p2(4)*c + dp2; 75 np14 = p1(4) + p2(4) * (1-c); 76 np12 = p1(2) - p2(4) * (1-c); 77 set(h1, 'position', [p1(1) np12 p1(3) np14]); 78 set(h2, 'position', [p1(1) p2(2) p1(3) np24]); 79 linkaxes([h1;h2], 'x'); 80 ax = axis(h2); 81 axis(h2, [min(data.date) max(data.date) ax(3:4)]) 82 83 84 axes(h1); 85 title( data.title); ylabel('price'); 86 axes(h2); 87 ylabel('volume'); 88 89 90 h = [h1; h2]; 91
barplot.m
1 % Copyright (c) 2008 the authors listed at the following URL, and/or 2 % the authors of referenced articles or incorporated external code: 3 % http://en.literateprograms.org/MATLAB_Financial_Toolbox?action=history&offset=20071125111355 4 % 5 % Permission is hereby granted, free of charge, to any person obtaining 6 % a copy of this software and associated documentation files (the 7 % "Software"), to deal in the Software without restriction, including 8 % without limitation the rights to use, copy, modify, merge, publish, 9 % distribute, sublicense, and/or sell copies of the Software, and to 10 % permit persons to whom the Software is furnished to do so, subject to 11 % the following conditions: 12 % 13 % The above copyright notice and this permission notice shall be 14 % included in all copies or substantial portions of the Software. 15 % 16 % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 % EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 % MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 % IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 % CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 % TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 % SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 % 24 % Retrieved from: http://en.literateprograms.org/MATLAB_Financial_Toolbox?oldid=11719 25 26 function [xs, ys] = barplot(x,y,varargin) 27 % BARPLOT - computes new x and y (with nans) for bar plottings 28 % options: 29 % - level: 0* 30 % use: 31 % [xs, ys] = barplot(x,y,options); 32 % 33 % See also moustacheplot 34 opt = getopt('init',{'level',0},varargin); 35 level = getopt('get',opt,'level'); 36 37 xs = repmat(nan,3*length(x),1); 38 xs(1:3:length(xs)) = x; 39 xs(2:3:length(xs)) = x; 40 ys = repmat(nan,3*length(y),1); 41 ys(1:3:length(ys)) = y; 42 ys(2:3:length(ys)) = level; 43
xdata.m
1 % Copyright (c) 2008 the authors listed at the following URL, and/or 2 % the authors of referenced articles or incorporated external code: 3 % http://en.literateprograms.org/MATLAB_Financial_Toolbox?action=history&offset=20071125111355 4 % 5 % Permission is hereby granted, free of charge, to any person obtaining 6 % a copy of this software and associated documentation files (the 7 % "Software"), to deal in the Software without restriction, including 8 % without limitation the rights to use, copy, modify, merge, publish, 9 % distribute, sublicense, and/or sell copies of the Software, and to 10 % permit persons to whom the Software is furnished to do so, subject to 11 % the following conditions: 12 % 13 % The above copyright notice and this permission notice shall be 14 % included in all copies or substantial portions of the Software. 15 % 16 % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 % EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 % MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 % IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 % CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 % TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 % SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 % 24 % Retrieved from: http://en.literateprograms.org/MATLAB_Financial_Toolbox?oldid=11719 25 26 function out = xdata(mode, varargin) 27 % XDATA - simplest xdata function 28 % use: 29 % val = xdata('get-col', structured_data, names) 30 switch lower(mode) 31 case 'get-col' 32 data = varargin{1}; 33 names = varargin{2}; 34 if ~iscell(names) 35 names = regexp(names, '([^;]+)', 'tokens'); 36 names = cellfun(@(x)(x{1}),names,'UniformOutput', false); 37 end 38 out = repmat(nan,size(data.value,1),length(names)); 39 for i=1:length(names) 40 idx = strmatch(names{i}, data.names, 'exact'); 41 out(:,i) = data.value(:,idx); 42 end 43 44 otherwise 45 error('xdata:mode', 'unknown mode <%s>', mode); 46 end 47
