Question: Below is MATLAB code to price AMERICAN options using binomial method, can it be modified so it allows for pricing both AMERICAN and EURPEAN options?
Below is MATLAB code to price AMERICAN options using binomial method, can it be modified so it allows for pricing both AMERICAN and EURPEAN options?
function price = binomial_pricer(S0, K, r, sigma, T, type, NumSteps, varargin) % Values American Options, both calls and puts, accommodating different % types of dividends %INPUTS % S0 : Initial Stock Price % K : Strike Price % r : Risk-free Rate % sigma : Stock volatility % T : Term to Maturity % type : Option type = Call 'C' or Put 'P' % NumSteps : Number of steps in binomial tree % varargin : variable number of other input arguments. % No dividend: enter no extra input argument. % Continuous dividend yield % y : - 1 extra input which is the yield. % Discrete dividends % D : the dividend vector % xdate : time to ex-dividend date vector (in years) %OUTPUT % price : option value %% compute tree parameters dt = T/NumSteps; %length of each time step, in years u = exp(sigma*sqrt(dt)); d = 1/u; q = (exp(r *dt) - d)/(u - d); %risk neutral prob of up state df = exp(-r*dt); %discount factor %% Forward propagation to construct the underlying price tree % First, preallocation St = zeros(NumSteps + 1, NumSteps + 1); % matrix to hold underlying prices ft = zeros(NumSteps + 1, NumSteps + 1); % matrix to hold option prices % Construct price tree % Go through different scenarios of dividend based on the number of % variable input arguments n = length(varargin); switch n case 0 % No dividend % Populate the tree with prices St(1,1) = S0; for j = 2:NumSteps + 1 for i = 1:j-1 St(i,j) = St(i, j-1)*u; end St(i+1,j) = St(i, j-1)*d; end case 1 % dividend yield y = varargin{1}; % % Recalculate q q = (exp((r - y)*dt) - d)/(u - d); % Populate the tree with prices St(1,1) = S0; for j = 2:NumSteps + 1 for i = 1:j-1 St(i,j) = St(i, j-1)*u; end St(i+1,j) = St(i, j-1)*d; end case 2 % Scenario 3: discrete dividend % load input variables for this scenario D = varargin{1}; %dividend amount xdate = varargin{2}; %dividend timing % Remove NPV of dividends from initial price pv_D = npv(D, xdate, r); St(1,1) = S0-pv_D; % Construct price tree that excludes the dividend for j = 2:NumSteps + 1 for i = 1:j-1 St(i,j) = St(i, j-1)*u; end St(i+1,j) = St(i, j-1)*d; end % Then add back dividend for nodes that occur before ex div date tau=0; %Track time through tree steps % Iterate through each node of the tree for j = 1:NumSteps % check if current time step is before the dividend div_to_addback = 0; for k=1:length(D) if tau < xdate(k) div_to_addback = div_to_addback + exp(-r*(xdate(k)-tau))*D(k); end end St(:,j)=St(:,j)+ div_to_addback; tau=tau+dt; %Increase by 1 timestep end end %% Compute option prices at terminal nodes and perform backward induction to complete the option price tree if type == 'C' ft(:, end) = max(St(:, end) - K, 0); % Backward induction for j = NumSteps :-1:1 for i = 1:j CV = df * (q*ft(i, j+1) + (1-q)*ft(i+1, j+1)); IV = St(i,j) - K; ft(i,j) = max(CV, IV); end end elseif type == 'P' ft(:, end) = max( K - St(:, end), 0); % Backward induction for j = NumSteps :-1:1 for i = 1:j CV = df * (q*ft(i, j+1) + (1-q)*ft(i+1, j+1)); IV = K - St(i,j); ft(i,j) = max(CV, IV); end end else error('binomial_pricer - Option type must be C or P'); end price = ft(1,1); Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
