Download code

From LiteratePrograms

Jump to: navigation, search

Back to Swiss_army_knife_MATLAB_programs_for_quantitative_finance

Download for Windows: zip

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

brownexp.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/Swiss_army_knife_MATLAB_programs_for_quantitative_finance?action=history&offset=20061105071300
 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/Swiss_army_knife_MATLAB_programs_for_quantitative_finance?oldid=7929
25 
26 function data = brownexp(volatility, nb_points, varargin)
27 % BROWNEXP - simulation of a brownian exponential diffusion

28 %  use:

29 % > data = brownexp(volatility, nb_points, options)

30 % options:

31 % - drift         :   0

32 % - start-price   : 100

33 % - time-step     :  1/250

34 % - nb-simulations:   1

35 % - time-horizon  :  []

36 % if time-horizon not empty, it replaces nb_points

37 %

38 %  example:

39 % data = brownexp(.20, 250, 'nb-simulations', 5)

40 % plotstruct( data)

41 
42 opt = options({'drift', 0, 'start-price', 100, 'time-step', 1/250, ...
43                'nb-simulations', 1, 'time-horizon', []}, varargin);
44 
45 if nargin < 2
46     nb_points = 100;
47 end
48 time_step = opt.get('time-step');
49 if ~isempty(opt.get('time-horizon'))
50     time_horizon = opt.get('time-horizon');
51 else
52     time_horizon = time_step*(nb_points-1);
53 end
54 
55 dates  = (0:time_step:time_horizon)';
56 values = opt.get('start-price')*exp( (opt.get('drift') - volatility^2/2) * ...
57          dates+volatility *sqrt(time_step)*cumsum(randn(length(dates),opt.get('nb-simulations'))));
58 
59 data   = struct( 'title', 'simulations', 'value', values, 'date', dates, ...
60                  'names', {tokenize(sprintf('sim-%d;',1:size(values,2)),';')});

61 


firstoo.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/Swiss_army_knife_MATLAB_programs_for_quantitative_finance?action=history&offset=20061105071300
 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/Swiss_army_knife_MATLAB_programs_for_quantitative_finance?oldid=7929
25 
26 function z = firstoo( n)
27 % FIRSTOO - my first real MATLAB object

28 % z=firstoo(2)

29 % z.add_n(3)

30 % z.get_n()

31 % z.set_n(7)

32 % z.get_n()

33 this.n = n;
34 z = struct('get_n',@get_n,'add_n',@add_n, 'set_n', @set_n);
35     function a = get_n()
36         a=this.n;
37     end
38     function u = set_n( m)
39         this.n = m;
40     end
41     function u = add_n(u)
42         u = u +this.n;
43     end
44 end
45 


plotstruct.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/Swiss_army_knife_MATLAB_programs_for_quantitative_finance?action=history&offset=20061105071300
 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/Swiss_army_knife_MATLAB_programs_for_quantitative_finance?oldid=7929
25 
26 function h = plotstruct(data, varargin)
27 % PLOTSTRUCT - plot a structured dataset

28 % returns an handle on mydateticks

29 % use:

30 %  h = plotstruct(data, options)

31 % options:

32 % - dates    : false

33 % - figure   : []

34 % - linewidth: 2

35 opts = options({'dates', false, 'figure', [], 'linewidth', 2}, varargin);
36 h = [];
37 g = opts.get('figure');
38 if isempty(g)
39    g = figure;
40 else
41    g = figure(g);
42 end
43 plot(data.date, data.value, 'linewidth', opts.get('linewidth'));
44 legend(data.names);
45 title(data.title);
46 if opts.get('dates')
47    h=mydateticks; 
48 end
49 


build_hook.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/Swiss_army_knife_MATLAB_programs_for_quantitative_finance?action=history&offset=20061105071300
 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/Swiss_army_knife_MATLAB_programs_for_quantitative_finance?oldid=7929
25 
26 function z = build_hook(varargin)
27 % BUILD_HOOK - build an hook

28 % a hook is a functionnal object with a field an 3 methods:

29 % - names  , which is the names of the columns to work on

30 % - func   , which is an handle on the function to apply to columns

31 % - datefun, which is an handle on the function to apply to dates

32 % - colfun , which is an handle on the function to apply on column names

33 
34 opt = options({'names', '', 'func', @(v,d)(v), 'datefun', @(d,v)(d), 'colfun', @(x)(x)}, varargin);
35 if isempty(opt.get('names'))
36     error('build_hook:names', 'I need names of columns to work on');
37 end
38 
39 this.opt = opt;
40 z = @apply;
41 
42 function data = apply(data, varargin)
43     names = this.opt.get('names');
44     idx   = strmatch(names,data.names,'exact');
45     data.value = data.value(:,idx);
46     data.names = data.names(idx);
47 
48     data.value = feval(this.opt.get('func'),data.value,data.date,varargin{:});
49     data.date  = feval(this.opt.get('datefun'), data.date,data.value);
50     data.names = feval(this.opt.get('colfun'), data.names);
51 end
52 
53 end
54 


options.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/Swiss_army_knife_MATLAB_programs_for_quantitative_finance?action=history&offset=20061105071300
  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/Swiss_army_knife_MATLAB_programs_for_quantitative_finance?oldid=7929
 25 
 26 function z = options(default, overwrite)
 27 % OPTIONS - sandbox for optionnal parameters management

 28 %

 29 % use:

 30 % opt = options({'alpha',1,'beta',2},{'alpha',8,'gamma',3})

 31 % opt.get('alpha')

 32 % opt.set('alpha',3)

 33 % opt.get('alpha')

 34 % opt.get()

 35 
 36 %%** Optional parameters management

 37 
 38 %<* Initialization of the structure

 39 values = default;
 40 if nargin>1
 41     for i=1:2:length(overwrite)-1
 42         idx = strmatch(overwrite{i},values(1:2:end-1),'exact');
 43         if isempty(idx)
 44             %< New parameter

 45             % Added to the list of parameters

 46             values{end+1} = overwrite{i};
 47             values{end+1} = overwrite{i+1};
 48             %>

 49         else
 50             %< Parameter exists

 51             % the new value replace the old one

 52             values{idx(1)*2} = overwrite{i+1};
 53             %>

 54         end
 55     end
 56 end
 57 
 58 %< Internal variable

 59 this = struct('value',{values});
 60 %>

 61 %< Publication of external methods

 62 z    = struct('get',@get_v,'set',@set_v);
 63 %>

 64 %>*

 65 
 66 %%** Internals

 67 
 68 %<* Get a value

 69 function [r, h] = get_v( name, def_value)
 70     h = 1:length(this.value);
 71     if nargin==0
 72         %< No argument

 73         % Return the internal memory

 74         r = this.value;
 75         %>

 76     else
 77         idx = strmatch(name, this.value(1:2:end-1),'exact');
 78         if isempty(idx) & nargin>1
 79             %< Default value

 80             % Unknown argument but default value is provided.

 81             r = def_value;
 82             h = 0;
 83             %>

 84         elseif isempty(idx) & nargin < 2
 85             %< Unknown argument

 86             error('options:get:unknown','parameter with name <%s> unknown',name);

 87             %>

 88         else
 89             %< Argument found

 90             % Its value (and the associate index) is returned

 91             r = this.value{idx(1)*2-1+1};
 92             h = idx(1)*2-1+1;
 93             %>

 94         end
 95     end
 96 end
 97 
 98 %>*

 99 %<* Set a value

100 function set_v(name, value)
101     [v,h] = get_v(name, []);
102     if h>0
103         this.value{h} = value;
104     else
105         this.value{end+1} = name;
106         this.value{end+1} = value;
107     end
108 end
109 
110 %>*

111 end
112 


mydateticks.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/Swiss_army_knife_MATLAB_programs_for_quantitative_finance?action=history&offset=20061105071300
 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/Swiss_army_knife_MATLAB_programs_for_quantitative_finance?oldid=7929
25 
26 function z = mydateticks( varargin)
27 % MYDATETICKS - simple dateticks management

28 
29 %%** MyDateTicks

30 opt = options({'axe-handle',gca, 'nb-steps',[], 'text-handle', [], ...
31                'datestr-format',24, 'rotation', 60, 'fontsize',8, ...
32                'fontname','Arial'},varargin);
33 %<* Initializations

34 this.options = opt;
35 redraw;
36 %>*

37 z    = struct('redraw', @redraw);
38 
39 %%** Redraw

40 function redraw
41     %<* Get info

42     % From internal state

43     ah = this.options.get('axe-handle');
44     th = this.options.get('text-handle');
45     %>*

46     %<* Cleaning

47     % Select good axe, clean xtickslabels

48     axes(ah);
49     ax  = axis;
50     dtx = get(ah,'XTick');
51     set(ah,'XTickLabel',[]);
52     if ~isempty(th)
53         delete(th);
54     end
55     %>*

56     %<* Nb of ticks

57     % It's a function of the number of pixel on screen

58     nb_steps = this.options.get('nb-steps');
59     if isempty(nb_steps)
60         posf     = get(gcf,'position');
61         posa     = get(gca,'position');
62         w        = posf(3)*posa(3);
63         nb_steps = round(w/20);
64     end
65     dv  = ax(1):(ax(2)-ax(1))/nb_steps:ax(2);
66     if abs(dv(end)-ax(2))<eps
67         dv = [dv, ax(2)];
68     end
69     %>*

70     th = text(dv,repmat(ax(3),length(dv),1), ...
71               datestr(dv,this.options.get('datestr-format')), ...
72               'Rotation',this.options.get('rotation'),...
73               'HorizontalAlignment','Right', ...
74               'fontsize',this.options.get('fontsize'), ...
75               'fontname',this.options.get('fontname'));
76     this.options.set('text-handle',th);
77 end
78 
79 end
80 


tokenize.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/Swiss_army_knife_MATLAB_programs_for_quantitative_finance?action=history&offset=20061105071300
 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/Swiss_army_knife_MATLAB_programs_for_quantitative_finance?oldid=7929
25 
26 function t = tokenize(str,sep)
27 % TOKENIZE - string into cellarray using a separator

28 if nargin<2
29    sep = ';';
30 end
31 t = cellfun(@(x)(x{1}),regexp(str,sprintf('([^%s]+)%s',sep,sep),'tokens'),'Uniformoutput',false);

32 


myuserdata.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/Swiss_army_knife_MATLAB_programs_for_quantitative_finance?action=history&offset=20061105071300
 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/Swiss_army_knife_MATLAB_programs_for_quantitative_finance?oldid=7929
25 
26 function z = myuserdata(handle, mode, name, value, def_value)
27 % MYUSERDATA - a function to access the UserData field

28 % use:

29 %  myuserdata(gca,'set','my-data', rand(10,2))

30 %  v = myuserdata(gca,'get','my-data')

31 
32 ud = get(handle,'UserData');
33 switch lower(mode)
34     case 'set'
35         if isempty(ud)
36             ud = options({name, value});
37         else
38             ud.set(name,value);
39         end
40         set(handle,'UserData',ud);
41     case 'get'
42         if isempty(ud)
43             if nargin > 3
44                 z = value;
45             else
46                 error('myuserdata:get:unknown','option <%s> not present in the UserData field',name);

47             end
48         else
49             if nargin > 3
50                 z = ud.get(name, value);
51             else
52                 z = ud.get(name);
53             end
54         end
55     otherwise
56         error('myuserdata:mode','mode <%s> unknown', mode);

57 end
58 


example4data_structure.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/Swiss_army_knife_MATLAB_programs_for_quantitative_finance?action=history&offset=20061105071300
 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/Swiss_army_knife_MATLAB_programs_for_quantitative_finance?oldid=7929
25 
26 data = struct('title','Randomized quotes','date',(today:today+199)', ...
27               'value',100*cumprod(1+[randn(200,1)* 0.10, randn(200,1)* 0.21]/16), ...
28               'names', {{'Ticker1', 'Ticker2'}})
29 
30 figure;
31 plot(data.date,data.value,'linewidth',2);
32 title(data.title); 
33 axis([min(data.date) max(data.date) min(data.value(:)) max(data.value(:))]);
34 z = mydateticks('datestr-format',25);
35 legend(data.names);
36 
37 dformat = 'dd/mm/yyyy';
38 dates = {'10/06/2006' '14/09/2006'};
39 if iscell(dates)
40     nate = datenum(dates{1},dformat);
41     if length(dates)>1
42         nate = [nate, datenum(dates{2},dformat)];
43     end
44     dates = nate;
45 end
46 if length(dates)<2
47     dates = [dates, dates];
48 end
49 idx = find((data.date>=dates(1)) & (data.date<=dates(2)));
50 data.value = data.value(idx,:);
51 data.date  = data.date(idx);
52 
53 
54 figure;
55 plot(data.date,data.value,'linewidth',2);
56 title(data.title); 
57 axis([min(data.date) max(data.date) min(data.value(:)) max(data.value(:))]);
58 z = mydateticks('datestr-format',25);
59 legend(data.names);
60 
61 
62 figure;
63 plot(data.date,data.value,'linewidth',2);
64 title(data.title); 
65 axis([min(data.date) max(data.date) min(data.value(:)) max(data.value(:))]);
66 z = mydateticks('datestr-format',25);
67 legend(data.names);
68 
69 


Views
Personal tools