【小女准备今晚再改一遍程序】把我之前的程序贴上来backup!
程序代码:
#ifndef FUND_H #define FUND_H /*此头文件用来包括所有基金的类定义 将fund分为封闭式基金closedendfund和openendfund,母基金motherfund 子基金umbrellafund,其中将fund声明为虚基类,尝试用virtual copy constructor*/ //base class:fund class fund { public: fund(){}; virtual double get_netvalue() const=0; virtual double get_stockprice() const=0; virtual fund* clone() const=0;//使用复制构造函数(克隆) private: }; //封闭式基金类定义 class closedendfund:public fund {public: closedendfund(double netvalue_,double stockprice_):netvalue(netvalue_),stockprice(stockprice_) {}; virtual fund* clone() const //function clone {return new closedendfund(*this); } virtual double get_netvalue() const {return netvalue;} virtual double get_stockprice() const {return stockprice;} private: double netvalue; double stockprice; }; //开放式基金类定义 class openendfund:public fund {public: openendfund(double netvalue_):netvalue(netvalue_) {}; virtual fund* clone() const //function clone {return new openendfund(*this); } virtual double get_netvalue() const {return netvalue;} virtual double get_stockprice() const {return netvalue;//不知道这样改了会不会bug.... } private: double netvalue; }; //母基金类定义 class motherfund: public fund//不知道为什么把这个继承改成public openendfund就不对了。。?这是为什么呢? { public: motherfund(double netvalue_) : netvalue(netvalue_) {}; virtual fund* clone() const//function clone {return new motherfund(*this); } virtual double get_netvalue() const {return netvalue;} virtual double get_stockprice() const {return netvalue;//不知道这样改了会不会bug.... } private: double netvalue; }; //子基金类定义 class umbrellafund : public fund//不知道为什么把这个继承改成public openendfund就不对了。。?这是为什么呢? { public: umbrellafund(double netvalue_,double stockprice_) : netvalue(netvalue_),stockprice(stockprice_) {}; virtual fund* clone() const //function clone {return new umbrellafund(*this); } virtual double get_netvalue() const {return netvalue;} virtual double get_stockprice() const {return stockprice;} private: double netvalue; double stockprice; }; #endif#include <iostream> #include "fund.h" #include "MCmodel.h" using namespace std; double LOFarbitrage(const motherfund& motherfund_,const umbrellafund& umbrellafundA_,const umbrellafund& umbrellafundB_,int shares_);/*考虑到在实际中的套利,基金在申购后需要在T+3才可以卖出, 故我们考虑在模型中使用蒙特卡洛方法来模拟三天后的基金价格, 并以此作为套利的依据*/ #ifndef SIMPLEMC_H #define SIMPLEMC_H #include "fund.h" #include <cstdlib> #include <cmath> double MonteCarlo(const fund& thefund, double Expiry, double Vol, double r, unsigned long NumberOfPaths); #endif/*考虑到在实际中的套利,基金在申购后需要在T+3才可以卖出, 故我们考虑在模型中使用蒙特卡洛方法来模拟三天后的基金价格, 并以此作为套利的依据*/ #ifndef SIMPLEMC_H #define SIMPLEMC_H #include "fund.h" #include <cstdlib> #include <cmath> double MonteCarlo(const fund& thefund, double Expiry, double Vol, double r, unsigned long NumberOfPaths); #endif//套利函数 //arbitrage function #include <iostream> #include "fund.h" #include "MCmodel1.h" using namespace std; double LOFarbitrage(const motherfund& motherfund_,const umbrellafund& umbrellafundA_,const umbrellafund& umbrellafundB_,int shares_) {double Pr=0.015; double Rr=0.005; double Br=0.0005; double condition1=(umbrellafundA_.get_stockprice()+umbrellafundB_.get_stockprice())*(1-Br)-2*motherfund_.get_netvalue()*(1+Pr); //[1] (Ma+Mb)*x*(1-br)> 2x*Nm*(1+Pr):T+3可以卖出 double condition2=(umbrellafundA_.get_stockprice()+umbrellafundB_.get_stockprice())*(1-Br)-2*motherfund_.get_netvalue()*(1+Rr); //[2] (Ma+Mb)*x*(1-br)< 2x*Nm*(1+Rr):T+1可以卖出 if (condition1>0) return condition1*shares_; else if (condition2<0) return (-1)*condition2*shares_; else return 0; }/*考虑到在实际中的套利,基金在申购后需要在T+2才可以卖出, 故我们考虑在模型中使用蒙特卡洛方法来模拟三天后的基金价格, 并以此作为套利的依据*/ //this CPP用来模拟stockprice #include "MCmodel1.h" #include <cmath> #include "fund.h" // the basic math functions should be in namespace std but aren't in VCPP6 #if !defined(_MSC_VER) using namespace std; #endif //此处定义生成随机数的方法 double GetOneGaussianBySummation() { double result=0; for (unsigned long j=0; j < 12; j++) result += rand()/static_cast<double>(RAND_MAX); result -= 6.0; return result; } double GetOneGaussianByBoxMuller() { double result; double x; double y; double sizeSquared; do { x = 2.0*rand()/static_cast<double>(RAND_MAX)-1; y = 2.0*rand()/static_cast<double>(RAND_MAX)-1; sizeSquared = x*x + y*y; } while ( sizeSquared >= 1.0); result = x*sqrt(-2*log(sizeSquared)/sizeSquared); return result; } //以下为蒙特卡洛方法的函数定义 double MonteCarlo(const fund& thefund, double Expiry, double Vol, double r, unsigned long NumberOfPaths) {//首先从fund的派生来获得其现价 double present_price=thefund.get_stockprice(); double variance = Vol*Vol*Expiry; double rootVariance = sqrt(variance); double itoCorrection = -0.5*variance; double movedSpot = present_price*exp(r*Expiry +itoCorrection); double thisfutureprice; double runningSum=0; for (unsigned long i=0; i < NumberOfPaths; i++) { double thisGaussian = GetOneGaussianByBoxMuller(); thisfutureprice = movedSpot*exp( rootVariance*thisGaussian); runningSum += thisfutureprice; } double mean = runningSum / NumberOfPaths; mean *= exp(-r*Expiry); double future_price= mean; return future_price; } #include "fund.h" #include "LOFarbitrage.h" #include <iostream> using namespace std; int main() {double mothernetvalue;//输入需要的五个值,具体需要的输入函数没写 double umbrellavalueA,umbrellapriceA; double umbrellavalueB,umbrellapriceB; int theshares;//用多少份来进行套利 cout<<"输入母基金净值"<<endl; cin>>mothernetvalue; cout<<"输入子基金A净值"<<endl; cin>>umbrellavalueA; cout<<"输入子基金A市价"<<endl; cin>>umbrellapriceA; cout<<"输入子基金B净值"<<endl; cin>>umbrellavalueB; cout<<"输入子基金B市价"<<endl; cin>>umbrellapriceB; cout<<"每次套利多少份"<<endl; cin>>theshares; motherfund the_motherfund(mothernetvalue); umbrellafund umbrellaA(umbrellavalueA,umbrellapriceA);//建立需要的五个变量 umbrellafund umbrellaB(umbrellavalueB,umbrellapriceB); double thepayoff=LOFarbitrage(the_motherfund,umbrellaA,umbrellaB,theshares);//代入这个函数计算有没有套利,套利出来是多少,为了简便没有的时候设为0 if (thepayoff==0) cout<<"此时无套利机会"<<endl; if (thepayoff!=0) cout<<"此时有套利机会,可以套利"<<thepayoff<<endl; }