一,我的C/C++/C#资源和学习篇
二,基于C++的标准期权定价(标准看涨欧式/看跌欧式/标准看跌美式)[by fantuanxiaot]
程序基于广泛的面向对象编程:
首先定义需要的函数,诸如正态分布cdf:正态分布cdf函数的数值参考方法文献如下:
本帖隐藏的内容
程序:
- // 误差函数的近似公式
- double erf(double a)
- {
- double a1=0.0705230784,
- a2=0.0422820123,
- a3=0.0092705272,
- a4=0.0001520143,
- a5=0.0002765672,
- a6=0.0000430638;
- a=1+a1*a+a2*a*a+a3*a*a*a+a4*a*a*a*a+a5*a*a*a*a*a+a6*a*a*a*a*a*a;
- a=pow(a,16);
- a=1-1/a;
- return(a);
- }
- // 正态分布的累积分布函数
- double NormCDF(double x)
- {
- double result;
- if(x>=0)
- {
- result=0.5+0.5*erf(x/sqrt(2));
- } else if(x<0)
- {
- result=0.5-0.5*erf(fabs(x/sqrt(2)));
- }
- return(result);
- }
- // 检测正态分布的累积密度函数
- // int main()
- // {
- // double x1=0;
- // double x2=2;
- // double x3=3;
- // double x4=-2;
- // double x5=-3;
- // cout<<NormCDF(x1)<<endl;
- // cout<<NormCDF(x2)<<endl;
- // cout<<NormCDF(x3)<<endl;
- // cout<<NormCDF(x4)<<endl;
- // cout<<NormCDF(x5)<<endl;
- // return 0;
- // }
- // 以上的检测质量还行
- // 以上的检测质量还行
- ////////////////////////////////////////////////////////////////////////
- class OptionPricing
- {
- protected:
- double S;
- double K;
- double Sigma;
- double Rate;
- double Time;
- // 结果的输出
- public:
- // 构造函数
- OptionPricing(double s,double k,double si,double r,double t):
- S(s),K(k),Sigma(si),Rate(r),Time(t){}
- virtual void OptionPricingResultShow()
- {
- cout<<endl;
- }
- };
这是标准的欧式看涨期权的定价类
- // 标准的欧式期权定价
- class EurCallOptionPricing:public OptionPricing
- {
- public:
- EurCallOptionPricing(double s,double k,double si,double r,double t):
- OptionPricing(s,k,si,r,t){}
- virtual void OptionPricingResultShow()
- {
- double EuropeCallOptionPricing,Omiga;
- // 定价
- Omiga=Rate*Time+Sigma*Sigma*Time/2-log(K/S);
- Omiga=Omiga/(Sigma*sqrt(Time));
- EuropeCallOptionPricing=S*NormCDF(Omiga)-K*exp(-Rate*Time)*NormCDF(Omiga-Sigma*sqrt(Time));
- cout<<"您设定的参数:"<<endl;
- cout<<"基期价格:"<<S<<endl;
- cout<<"目标价格:"<<K<<endl;
- cout<<"到期时间:"<<S<<endl;
- cout<<"波动参数:"<<Sigma<<endl;
- cout<<"无风险收益率:"<<Rate<<endl;
- cout<<"标准的欧式看涨期权的价格为:"<<EuropeCallOptionPricing<<endl;
- }
- };
这是标准的欧式看跌期权的定价类:
- class EurPutOptionPricing:public OptionPricing
- {
- public:
- EurPutOptionPricing(double s,double k,double si,double r,double t):
- OptionPricing(s,k,si,r,t){}
- virtual void OptionPricingResultShow()
- {
- double EuropeCallOptionPricing,EuropePutOptionPricing,Omiga;
- // 定价
- Omiga=Rate*Time+Sigma*Sigma*Time/2-log(K/S);
- Omiga=Omiga/(Sigma*sqrt(Time));
- EuropeCallOptionPricing=S*NormCDF(Omiga)-K*exp(-Rate*Time)*NormCDF(Omiga-Sigma*sqrt(Time));
- EuropePutOptionPricing=EuropeCallOptionPricing+K*exp(-Rate*Time)-S;
- cout<<"您设定的参数:"<<endl;
- cout<<"基期价格:"<<S<<endl;
- cout<<"目标价格:"<<K<<endl;
- cout<<"到期时间:"<<S<<endl;
- cout<<"波动参数:"<<Sigma<<endl;
- cout<<"无风险收益率:"<<Rate<<endl;
- cout<<"标准的欧式看跌期权的价格为:"<<EuropePutOptionPricing<<endl;
- }
- };
- class USAPutOptionPricing:public OptionPricing
- {
- protected:
- // 定义划分的区间数目
- int N;
- public:
- USAPutOptionPricing(double s,double k,double si,double r,double t,int n):
- OptionPricing(s,k,si,r,t),N(n){}
- virtual void OptionPricingResultShow()
- {
- // 定义要划分的二叉树矩阵
- double Matrix[100][100];
- int i,j;
- double Probability,u,d,deltaT,Price1,Price2;
- deltaT=Time/N;
- u=exp(Sigma*sqrt(deltaT));
- d=exp(-Sigma*sqrt(deltaT));
- Probability=(1+Rate*deltaT-d)/(u-d);
- for(i=0;i<=N;i++)
- {
- Matrix[i][N]=max(0,K-S*pow(u,N-i)*pow(d,i));
- }
- for(i=N-1;i>=0;i--)
- {
- for(j=0;j<=i;j++)
- {
- Price1=exp(-Rate*deltaT)*((1-Probability)*(Matrix[j+1][i+1])
- +Probability*(Matrix[j][i+1]));
- Price2=K-S*pow(u,i-j)*pow(d,j);
- Matrix[j][i]=max(Price1,Price2);
- }
- }
- cout<<"您设定的参数:"<<endl;
- cout<<"基期价格:"<<S<<endl;
- cout<<"目标价格:"<<K<<endl;
- cout<<"到期时间:"<<S<<endl;
- cout<<"波动参数:"<<Sigma<<endl;
- cout<<"无风险收益率:"<<Rate<<endl;
- cout<<"标准的美式看跌期权的价格为:"<<Matrix[0][0]<<endl;
- }
- };
最终的主函数:
- // 主函数
- int main()
- {
- // 定义对象
- EurCallOptionPricing eurcall(10,12,0.03,0.03/360,100);
- USAPutOptionPricing usaput(10,12,0.03,0.03/360,100,10);
- EurPutOptionPricing eurput(10,12,0.03,0.03/360,100);
- // 输出结果
- eurcall.OptionPricingResultShow();
- cout<<endl;
- eurput.OptionPricingResultShow();
- cout<<endl;
- usaput.OptionPricingResultShow();
- return 0;
- }
所有的程序如下:
本帖隐藏的内容
运行环境:visual Studio2012
运行结果: