Is there a simple way to condense the following codes?
3 views (last 30 days)
Show older comments
function greek=OptionGreeks(opt,arg1,arg2,arg3,arg4,arg5)
seed=rng;
if nargin==3
greek1=opt.opt1.OptionGreeks(arg1, arg2);
rng(seed);
greek2=opt.opt2.OptionGreeks(arg1, arg2);
elseif nargin==4
greek1=opt.opt1.OptionGreeks(arg1, arg2, arg3);
rng(seed);
greek2=opt.opt2.OptionGreeks(arg1, arg2, arg3);
elseif nargin==5
greek1=opt.opt1.OptionGreeks(arg1, arg2, arg3, arg4);
rng(seed);
greek2=opt.opt2.OptionGreeks(arg1, arg2, arg3, arg4);
else
greek1=opt.opt1.OptionGreeks(arg1, arg2, arg3, arg4, arg5);
rng(seed);
greek2=opt.opt2.OptionGreeks(arg1, arg2, arg3, arg4, arg5);
end
greek=greek1-greek2;
end
Above are my codes. opt is a SpreadOption class that has properties opt1 and opt2, which are Option class. Function SpreadOption.OptionGreeks() calls function Option.OptionGreeks() that accepts different number of input arguments.
I'm wondering if there's a way to condense the codes.
Also, I'm wondering what the easiest way is to customize my input argument, like opt.OptionPrice(S=3610, method='binomial tree', brunches=100000)? (just like python and R do)?It seems not convenient since MATLAB cannot overload/override a certain function.
0 Comments
Accepted Answer
OCDER
on 2 Aug 2018
function greek = OptionGreeks(opt, varargin)
if length(varargin) > 5; varargin = varargin(1:5); end
seed = rng;
greek1=opt.opt1.OptionGreeks(varargin{:});
rng(seed);
greek2=opt.opt2.OptionGreeks(varargin{:});
greek=greek1-greek2;
4 Comments
Walter Roberson
on 2 Aug 2018
MATLAB does not support variable=value options.
See https://www.mathworks.com/help/matlab/matlab_prog/input-parser-validation-functions.html for the standard input parsing routines.
More Answers (0)
See Also
Categories
Find more on Data Type Identification in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!