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


你可能感兴趣的:(使用C++期权定价)