小众知识

BS期权定价(C++)
2014-11-06 01:18:43   来源:   评论:0 点击:

C++版本BS期权定价模型
#include <cmath> // mathematical C library
using namespace std;
double N(const double& z); // declare cumulative distribution function.
 
//欧式看涨期权
double option_price_call_black_scholes(const double& S,
           const double& K,
           const double& r,
           const double& sigma,
           const double& time) // time to maturity
{
     double time_sqrt = sqrt(time);
     double d1 = (log(S/K)+r*time)/(sigma*time_sqrt)+0.5*sigma*time_sqrt;
     double d2 = d1-sigma*time_sqrt;
     return S*N(d1)-K*exp(-r*time)*N(d2);
};
 
//欧式看跌期权
double option_price_put_black_scholes(const double& S,
           const double& K,
           const double& r,
           const double& sigma,
           const double& time) // time to maturity
{
     double time_sqrt = sqrt(time);
     double d1 = (log(S/K)+r*time)/(sigma*time_sqrt)+0.5*sigma*time_sqrt;
     double d2 = d1-sigma*time_sqrt;
     return K*exp(-r*time)*N(-d2)-S*N(-d1);
};
 
//累积概率函数(标准正态分布)
double N(const double& z) {
    if (z >  6.0) { return 1.0; }; // this guards against overflow
    if (z < -6.0) { return 0.0; };
    double b1 =  0.31938153;
    double b2 = -0.356563782;
    double b3 =  1.781477937;
    double b4 = -1.821255978;
    double b5 =  1.330274429;
    double p  =  0.2316419;
    double c2 =  0.3989423;
    double a=fabs(z);
    double t = 1.0/(1.0+a*p);
    double b = c2*exp((-z)*(z/2.0));
    double n = ((((b5*t+b4)*t+b3)*t+b2)*t+b1)*t;
    n = 1.0-b*n;
    if ( z < 0.0 ) n = 1.0 - n;
    return n;
};

相关热词搜索:期权 定价 CPP

上一篇:美国期权交易中的做市商制度
下一篇:揭开期权价值的神秘面纱,期权的希腊字母详解

分享到: 收藏
评论排行
最新发布