matlab期权定价代码
时间: 2023-10-05 16:05:08 浏览: 342
以下是一个基于Black-Scholes模型的期权定价的Matlab代码:
```matlab
function [price,delta,gamma,vega,theta,rho] = bsprice(S,K,r,T,sigma,optionType)
%BSPRICE 计算欧式期权的价格和希腊值
%
% INPUTS:
% S: 资产价格
% K: 行权价格
% r: 无风险利率
% T: 到期时间
% sigma: 波动率
% optionType: 期权类型,'call'或'put'
%
% OUTPUTS:
% price: 期权价格
% delta: Delta值
% gamma: Gamma值
% vega: Vega值
% theta: Theta值
% rho: Rho值
if strcmpi(optionType,'call')
d1 = (log(S/K) + (r + sigma^2/2)*T) / (sigma * sqrt(T));
d2 = d1 - sigma * sqrt(T);
Nd1 = normcdf(d1);
Nd2 = normcdf(d2);
price = S * Nd1 - K * exp(-r*T) * Nd2;
delta = Nd1;
gamma = normpdf(d1) / (S * sigma * sqrt(T));
vega = S * normpdf(d1) * sqrt(T);
theta = - (S * normpdf(d1) * sigma / (2 * sqrt(T))) - r * K * exp(-r*T) * Nd2;
rho = K * T * exp(-r*T) * Nd2;
elseif strcmpi(optionType,'put')
d1 = (log(S/K) + (r + sigma^2/2)*T) / (sigma * sqrt(T));
d2 = d1 - sigma * sqrt(T);
Nd1 = normcdf(-d1);
Nd2 = normcdf(-d2);
price = K * exp(-r*T) * Nd2 - S * Nd1;
delta = -Nd1;
gamma = normpdf(d1) / (S * sigma * sqrt(T));
vega = S * normpdf(d1) * sqrt(T);
theta = - (S * normpdf(d1) * sigma / (2 * sqrt(T))) + r * K * exp(-r*T) * Nd2;
rho = -K * T * exp(-r*T) * Nd2;
else
error('输入的期权类型不正确,应该是''call''或''put''');
end
```
其中,`S`为标的资产价格,`K`为行权价格,`r`为无风险利率,`T`为到期时间,`sigma`为波动率,`optionType`为期权类型,可选值为`'call'`或`'put'`。函数返回期权价格`price`和希腊值`delta`、`gamma`、`vega`、`theta`、`rho`。
阅读全文