Code covered by the BSD License  

Highlights from
Pricing Derivatives Securities using MATLAB

image thumbnail

Pricing Derivatives Securities using MATLAB

by

 

02 Apr 2007 (Updated )

Examples of pricing derivatives securities using MATLAB

optionvanilla(S,E,r,T,sigma,divYield,nSims,nSteps,type, showPlot)
function [oprice] = optionvanilla(S,E,r,T,sigma,divYield,nSims,nSteps,type, showPlot)
%OPTIONVANILLA Price European call or put using montecarlo simulation.
%          
%   oprice = optionvanilla(S0,Strike,rf,T,sigma,divYield,nSims,nSteps,type,showPlot)
%
%   Inputs: 
%     S0        - Current asset price.
%     Strike    - Exercise price.
%     rf        - Risk free rate.
%     T         - Maturity date in years.
%     sigma     - Volatility of the asset price.
%     divYield  - Dividend yield.
%     nSims     - Number of simulations.
%     nSteps    - Number of steps.
%     type      - Option type: 'call' or 'put'.
%     showPlot  - Determines whether the MonteCarlo paths are plotted{true | [false]}. 
%
%   Outputs:
%     oprice    - Price of the vanilla option
%
%   Copyright 1995-2010 The MathWorks, Inc.

%------------------------------------------------
%Checking inputs
%------------------------------------------------

%Force the state of the random number generator
randn('state' , 0);

%Determine the size of the timestep
Dt = T/nSteps;

%Generate the random numbers
mat = randn(nSteps , nSims);
mat = exp( (r-divYield-sigma^2/2)*Dt + sigma*sqrt(Dt).*mat );
mat = cumprod(mat , 1);
mat = mat.*S;

if(isempty(type))
    type = 'call';
end

if (nargin < 10)
    showPlot = false;
end

%------------------------------------------------
%Calculate the option price
%------------------------------------------------

if strcmp(type,'call')
    oFactor=1;  %call
else 
    oFactor=-1; %put
end

V = exp(-r*T) * max(oFactor*(mat(end,:)-E) , 0);
    
oprice = 1/nSims*sum(V);  

if(showPlot)
    plot(linspace(0, T, nSteps+1)', [S*ones(1,size(mat,2)); mat]);
    ylabel('Price of Underlying');
    xlabel('Time');
    title('Monte Carlo Paths for Option Pricing');
    grid on
    set(gcf, 'NumberTitle', 'off');
end


Contact us