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

我行我乐
公众号:逻辑客栈
我的博客:
https://blog.yuccn. net
2012-11-23 19:01
xixifans
Rank: 1
等 级:新手上路
帖 子:118
专家分:2
注 册:2012-11-15
收藏
得分:0 
回复 2楼 yuccn
已经编的编不动想要恢复backup了。。

看那没有形象的发型和憔悴的脸就知道这是一只和C++恋爱的宅女。yes,I am!
2012-11-23 19:42
韦夏霆
Rank: 1
来 自:南京
等 级:新手上路
帖 子:3
专家分:0
注 册:2012-11-23
收藏
得分:0 
太牛叉了,那么长的程序还叫新手呀!小弟连这种程序都没弄懂呢
#include<iostream>
using namespace std;
double sn=100;
int main(){
    void height(double& hn,int i);
    double hn=100;
    int i=1;
    height(hn,i);
    hn=hn/2;
    cout<<"第十次反弹的高度为:"<<hn<<endl;
    return 0;
}
void height(double& hn,int i){
    hn=hn/2;
    sn=sn+2*hn;
    i++;
    if(i==10){
        cout<<"第十次落地时经历的路程为:"<<sn<<endl;
        return;
    }
    if(i<10){
        height(hn,i);
    }
}

C++是我的噩梦
2012-11-23 19:45
w527705090
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:11
帖 子:441
专家分:1882
注 册:2011-6-28
收藏
得分:0 
来占个座~~

有心者,千方百计;无心者,千难万难。
2012-11-23 19:47
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
ltianc
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:56
专家分:128
注 册:2012-10-16
收藏
得分:0 
我的个神呐,这个程序你用了几个星期?

世界等我去改变。
2012-11-23 22:04
xixifans
Rank: 1
等 级:新手上路
帖 子:118
专家分:2
注 册:2012-11-15
收藏
得分:0 
回复 8楼 ltianc
这周一开始的。。

看那没有形象的发型和憔悴的脸就知道这是一只和C++恋爱的宅女。yes,I am!
2012-11-23 22:10
mfkblue
Rank: 5Rank: 5
等 级:职业侠客
帖 子:472
专家分:343
注 册:2008-12-21
收藏
得分:0 
传说中腐女啊,不知道头像是不是本人的,长的还真水灵,美女要不要来发展一段纯洁的爱情关系啊?
2012-11-23 23:23
快速回复:【小女准备今晚再改一遍程序】把我之前的程序贴上来backup!
数据加载中...
 
   



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

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