使用C++期权定价

单期二叉树期权定价:

#include <cmath> // standard mathematical library.
#include <algorithm> // defining the max() operator
using namespace std;
 
double option_price_call_european_binomial_single_period(const double& S, // spot price
        const double& X, // exercise price
        const double& r, // interest rate(per period)
        const double& u, // up movement
        const double& d) // down movement
{
     double p_up = (exp(r)-d)/(u-d);
     double p_down = 1.0 - p_up;
     double c_u = max(0.0,u*S-X);
     double c_d = max(0.0,d*S-X);
     double call_price = exp(-r)*(p_up*c_u+p_down*c_d);
     return call_price;
};
 
多期二叉树期权定价:
#include <cmath> // standard mathematrical library
#include <algorithm> // defining the max() operator
#include <vector> // STL vector templates
using namespace std;
 
double option_price_call_europern_binomial_multi_period_given_ud(const double& S, // spot price
         const double& K, // exercise price
         const double& r, // interest rate(per period)
         const double& u, // up movement
         const double& d, // down movement
         const int& no_periods) // no steps in binomial tree
{
     double Rinv = exp(-r); // invers of interest rate
     double ud = u/d;
     double p_up = (exp(r)-d)/(u-d);
     double p_down = 1.0 - p_up;
     vector<double> prices(no_periods+1); // price of underlying security
     prices[0] = S*pow(d,no_periods);
     for(int i=1;i<=no_periods;++i) prices[i] = ud*prices[i-1];
     vector<double>call_values(no_periods+1); // value of corressponding call
     for(int i=0;i<=no_periods;++i) call_values[i] = max(0.0,prices[i]-K);
     for(int step=no_periods-1;step>=0;--step)
     {
         for(int i=0;i<=step;++i)
         {
             call_values[i] = (p_up*call_values[i+1]+p_down*call_values[i])*Rinv;
         }
     }
     return call_values[0];
};

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;
};


  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值