Download code

Jump to: navigation, search

Back to MATLAB_Financial_Toolbox

Download for Windows: zip

Download for UNIX: zip, tar.gz, tar.bz2

xdata.m

 1 % The authors of this work have released all rights to it and placed it
 2 % in the public domain under the Creative Commons CC0 1.0 waiver
 3 % (http://creativecommons.org/publicdomain/zero/1.0/).
 4 % 
 5 % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 % EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 % MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 % IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 % CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 % TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 % SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 % 
13 % Retrieved from: http://en.literateprograms.org/MATLAB_Financial_Toolbox?oldid=11719
14 
15 function out = xdata(mode, varargin)
16 % XDATA - simplest xdata function
17 % use:
18 %  val = xdata('get-col', structured_data, names)
19 switch lower(mode)
20     case 'get-col'
21         data  = varargin{1};
22         names = varargin{2};
23         if ~iscell(names)
24             names =  regexp(names, '([^;]+)', 'tokens');
25             names = cellfun(@(x)(x{1}),names,'UniformOutput', false);
26         end
27         out = repmat(nan,size(data.value,1),length(names));
28         for i=1:length(names)
29             idx   = strmatch(names{i}, data.names, 'exact');
30             out(:,i) = data.value(:,idx);
31         end
32         
33     otherwise
34         error('xdata:mode', 'unknown mode <%s>', mode);
35 end


hijacker
hijacker
hijacker
hijacker

test_simplecandle.m

 1 % The authors of this work have released all rights to it and placed it
 2 % in the public domain under the Creative Commons CC0 1.0 waiver
 3 % (http://creativecommons.org/publicdomain/zero/1.0/).
 4 % 
 5 % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 % EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 % MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 % IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 % CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 % TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 % SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 % 
13 % Retrieved from: http://en.literateprograms.org/MATLAB_Financial_Toolbox?oldid=11719
14 
15 
16 
17 sgm = 0.23 / sqrt(250);
18 mu  = (1+ 0.04)^(1/250)-1;
19 S0  = 100;
20 nb  = 1000;
21 sam = 10;
22 
23 T   = 1+(1:nb)'/250;
24 S   = S0 * exp( (mu-sgm^2/2)*T + sgm * cumsum(randn(nb,1)));
25 
26 Soc   = [S(1:sam:end-1), S(sam-1:sam:end-1)];
27 Shl   = [max(reshape(S, sam, length(S)/sam)); min(reshape(S, sam, length(S)/sam))];
28 Sohlc = [Soc(:,1), Shl', Soc(:,2)];
29 t     = T(ceil(sam/2):sam:end-1);
30 
31 figure('Color',[0.9412 0.9412 0.9412 ]);
32 h1 = subplot(2,1,1); plot(T, S); 
33 datetick('x',6,'keeplimits');
34 
35 volumes = ceil(exp( randn(length(Sohlc),1) + 7))+100;
36 
37 data    = struct('title', 'exp brownian motion', 'value', [Sohlc, volumes], ...
38                  'names', {{'open', 'high', 'low', 'close', 'volume'}}, 'date', t);
39 
40 
41 h2 = subplot(2,1,2);
42 candle(Sohlc(:,2), Sohlc(:,3), Sohlc(:,4), Sohlc(:,1), [.5 0 0], t, 6)
43 linkaxes([h1, h2], 'x');
44 
45 
46 
47 simplecandleplot( data, 'prices', 'open;high;low;close', 'volumes', 'volume')
48 simplecandleplot( data, 'prices', 'open;high;low;close', 'volumes', 'volume', 'mode', 'highlow')


hijacker
hijacker
hijacker
hijacker

simplecandleplot.m

 1 % The authors of this work have released all rights to it and placed it
 2 % in the public domain under the Creative Commons CC0 1.0 waiver
 3 % (http://creativecommons.org/publicdomain/zero/1.0/).
 4 % 
 5 % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 % EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 % MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 % IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 % CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 % TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 % SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 % 
13 % Retrieved from: http://en.literateprograms.org/MATLAB_Financial_Toolbox?oldid=11719
14 
15 function h = simplecandleplot(data, varargin)
16 % CANDLEPLOT - candleplot with volumes
17 %
18 % use
19 %  h = simplecandleplot( data, 'prices', 'open;high;low;close', 'volumes', 'volume', options ... )
20 % options:
21 %  'figure' - handle or empty (new figure)
22 %  'prop'   - proportion of the figure dedicated to the volumes
23 %  'candles-color', 'volumes-color', 'volumes-linewidth' - eponyms
24 %  'date-format' - from datestr
25 %  'mode'   - 'candle' (default) or 'highlow'
26 %
27 % example
28 %  data = xdata('rload', 'bdata-test');
29 %  candleplot(data, 'prices', 'Open;High;Low;Last', 'volumes', 'Volume', 'dates-format', 13)
30 %
31 % See also pdates datestr xdata 
32 
33 opt = options({'prices', '', 'volumes', '', 'figure', [], 'prop', .2, ...
34                'candles-color', [0 .5 .5], 'volumes-color', [0 .5 .5], ...
35                'volumes-linewidth', 3, 'dates-format', 2, 'mode', 'candle'}, varargin);
36 
37 f = opt.get('figure');
38 if isempty(f) || ~ishandle(f)
39     f = figure('Color',[0.9412 0.9412 0.9412 ]);
40 else
41     figure(f);
42 end
43 
44 prices_names = opt.get('prices');
45 prices = xdata('get-col', data, prices_names);
46 h1     = subplot(2,1,1, 'unit', 'normalized');
47 feval( opt.get('mode') ,  prices(:,2), prices(:,3), prices(:,4), prices(:,1), opt.get('candles-color'), data.date, 0);
48 
49 volumes_name = opt.get('volumes');
50 h2     = subplot(2,1,2, 'unit', 'normalized');
51 [x,y]  = barplot(data.date, xdata('get-col', data, volumes_name));
52 plot(x,y, 'color', opt.get('volumes-color'), 'linewidth', opt.get('volumes-linewidth'));
53 
54 dateaxis('X', opt.get('dates-format'));
55 
56 p1 = get(h1, 'position');
57 p2 = get(h2, 'position');
58 c  = opt.get('prop');
59 dp2  = p1(2) - (p2(2)+p2(4));
60 np24 = p2(4)*c + dp2;
61 np14 = p1(4) + p2(4) * (1-c);
62 np12 = p1(2) - p2(4) * (1-c);
63 set(h1, 'position', [p1(1) np12  p1(3) np14]);
64 set(h2, 'position', [p1(1) p2(2) p1(3) np24]);
65 linkaxes([h1;h2], 'x');
66 ax = axis(h2);
67 axis(h2, [min(data.date) max(data.date) ax(3:4)])
68 
69 axes(h1); 
70 title( data.title); ylabel('price');
71 axes(h2); 
72 ylabel('volume');
73 
74 h = [h1; h2];


hijacker
hijacker
hijacker
hijacker

barplot.m

 1 % The authors of this work have released all rights to it and placed it
 2 % in the public domain under the Creative Commons CC0 1.0 waiver
 3 % (http://creativecommons.org/publicdomain/zero/1.0/).
 4 % 
 5 % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 % EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 % MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 % IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 % CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 % TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 % SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 % 
13 % Retrieved from: http://en.literateprograms.org/MATLAB_Financial_Toolbox?oldid=11719
14 
15 function [xs, ys] = barplot(x,y,varargin)
16 % BARPLOT - computes new x and y (with nans) for bar plottings
17 % options:
18 % - level: 0*
19 % use:
20 % [xs, ys] = barplot(x,y,options);
21 %
22 % See also moustacheplot 
23 opt = getopt('init',{'level',0},varargin);
24 level = getopt('get',opt,'level');
25 
26 xs = repmat(nan,3*length(x),1);
27 xs(1:3:length(xs)) = x;
28 xs(2:3:length(xs)) = x;
29 ys = repmat(nan,3*length(y),1);
30 ys(1:3:length(ys)) = y;
31 ys(2:3:length(ys)) = level;


hijacker
hijacker
hijacker
hijacker