I'm trying to create a function to calculate option price under Black-Scholes model. But it keeps saying that there is a mistake on d1; I double check it, but I can't find a mistake in there. Perhaps there is other thing I'm missing?

3 views (last 30 days)
function [c,p]=european_formula(id, K, T, S, sigma, q, r)
i=id;
X=K;
t=T;
S0=S;
v=sigma;
divd=q;
R=r;
d1=(log(So/X)+(R-d+0.5*v^2)*t)/(v*sqrt(t));
d2=d1-v*sqrt(t);
if i==1
c=So*exp(-divd*t)*cdf(d1)-x*exp(-r*t)*cdf(d2);
else
p=-S0*exp(-divd*t)*cdf(-d1)+x*exp(-r*t)*cdf(-d2);
end

Answers (1)

Darshan Ramakant Bhat
Darshan Ramakant Bhat on 6 Mar 2017
In the line
d1=(log(So/X)+(R-d+0.5*v^2)*t)/(v*sqrt(t));
What is 'So' ?, it seems to be undefined. I think it should be 'S0'. Similarly in the line
c=So*exp(-divd*t)*cdf(d1)-x*exp(-r*t)*cdf(d2);
'So' is appearing again. I think this is the error you are getting.
Regards
Darshan Bhat

Categories

Find more on Financial Toolbox 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!