| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1073 人关注过本帖
标题:【小女准备今晚再改一遍程序】把我之前的程序贴上来backup!
取消只看楼主 加入收藏
xixifans
Rank: 1
等 级:新手上路
帖 子:118
专家分:2
注 册:2012-11-15
结帖率:100%
收藏
 问题点数:0 回复次数:10 
【小女准备今晚再改一遍程序】把我之前的程序贴上来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;

}










搜索更多相关主题的帖子: 基金 
2012-11-23 17:39
xixifans
Rank: 1
等 级:新手上路
帖 子:118
专家分:2
注 册:2012-11-15
收藏
得分:0 
回复 2楼 yuccn
已经编的编不动想要恢复backup了。。

看那没有形象的发型和憔悴的脸就知道这是一只和C++恋爱的宅女。yes,I am!
2012-11-23 19:42
xixifans
Rank: 1
等 级:新手上路
帖 子:118
专家分:2
注 册:2012-11-15
收藏
得分:0 
回复 5楼 w527705090
亲。。帮俺去看看新发的问题贴好不好

看那没有形象的发型和憔悴的脸就知道这是一只和C++恋爱的宅女。yes,I am!
2012-11-23 19:52
xixifans
Rank: 1
等 级:新手上路
帖 子:118
专家分:2
注 册:2012-11-15
收藏
得分:0 
回复 4楼 韦夏霆
哈哈。。这个我可以了!多敲敲程序。。。我深刻觉得自己敲能快速成长!你该改一个签名。。说C++是噩梦的孩纸不好啊。。要是美梦才可以

看那没有形象的发型和憔悴的脸就知道这是一只和C++恋爱的宅女。yes,I am!
2012-11-23 19:54
xixifans
Rank: 1
等 级:新手上路
帖 子:118
专家分:2
注 册:2012-11-15
收藏
得分:0 
回复 8楼 ltianc
这周一开始的。。

看那没有形象的发型和憔悴的脸就知道这是一只和C++恋爱的宅女。yes,I am!
2012-11-23 22:10
xixifans
Rank: 1
等 级:新手上路
帖 子:118
专家分:2
注 册:2012-11-15
收藏
得分:0 
回复 10楼 mfkblue
是本人~我原来的账号还有别的更本人的和C++书的合影呢。。可惜那照片不知道哪儿去了。。你的表情太不纯洁鸟!

看那没有形象的发型和憔悴的脸就知道这是一只和C++恋爱的宅女。yes,I am!
2012-11-24 00:17
xixifans
Rank: 1
等 级:新手上路
帖 子:118
专家分:2
注 册:2012-11-15
收藏
得分:0 
回复 12楼 TonyDeng
我怕被删账号。。555

看那没有形象的发型和憔悴的脸就知道这是一只和C++恋爱的宅女。yes,I am!
2012-11-24 00:57
xixifans
Rank: 1
等 级:新手上路
帖 子:118
专家分:2
注 册:2012-11-15
收藏
得分:0 
回复 14楼 TonyDeng
那。孩儿他妈也一起看?

看那没有形象的发型和憔悴的脸就知道这是一只和C++恋爱的宅女。yes,I am!
2012-11-25 17:36
xixifans
Rank: 1
等 级:新手上路
帖 子:118
专家分:2
注 册:2012-11-15
收藏
得分:0 
回复 16楼 信箱有效
知道了你也加不上。。我的验证是“我真名是什么”

看那没有形象的发型和憔悴的脸就知道这是一只和C++恋爱的宅女。yes,I am!
2012-11-25 17:37
xixifans
Rank: 1
等 级:新手上路
帖 子:118
专家分:2
注 册:2012-11-15
收藏
得分:0 
回复 21楼 信箱有效
你猜!

看那没有形象的发型和憔悴的脸就知道这是一只和C++恋爱的宅女。yes,I am!
2012-11-27 10:25
快速回复:【小女准备今晚再改一遍程序】把我之前的程序贴上来backup!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025815 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved