Find the answer to your Linux question:
Results 1 to 2 of 2
It could run on my computer. But there is no result. I would appreciate if someone could help me with finding the problem. class BSoption { protected: double _S,_K,_TT,_r,_vola,_optiont; public: ...
  1. #1
    Just Joined!
    Join Date
    Dec 2010
    Posts
    1

    C++: Broadcast receives no answer

    It could run on my computer. But there is no result.
    I would appreciate if someone could help me with finding the problem.

    class BSoption {
    protected:
    double _S,_K,_TT,_r,_vola,_optiont;
    public:
    BSoption(double S=0, double K=0,double TT=0,double r=0,double vola=0,int optiont=0 );
    virtual double Price()const;
    void virtual display() const{};
    ~BSoption();
    };

    class FxOpt: public BSoption {
    double _qone;
    public:
    FxOpt(double S=0, double K=0,double TT=0,double r=0,double vola=0,double qone=0,int optiont=0);
    double Price() const;
    void display() const;
    ~FxOpt();
    };

    class DivOpt: public BSoption {
    double _qtwo;
    public:
    DivOpt(double S=0, double K=0,double TT=0,double r=0,double vola=0,double qtwo=0,int optiont=0);
    double Price() const;
    void display() const;
    ~DivOpt();
    };

    class FutureOpt : public BSoption {
    double _qthree;
    public:
    FutureOpt(double S=0,double K=0,double TT=0,double r=0,double vola=0,double qthree=0,int optiont=0);
    double Price() const;
    void display() const;
    ~FutureOpt();
    };


    void fn(BSoption& a);

    double CDFNor(double x);
    double PDFNor(double x);


    #include "stdafx.h"
    #include"BSopt.h"
    #include<algorithm>
    #include <iostream>
    #include <cmath>
    using namespace std;

    double BSPrice(double S, double K, double TT, double r, double vola,int optiont){
    double d1 =(log(S/K)+(r+1/2*pow(vola,2))*TT)/(vola*sqrt(TT));
    double d2=d1-vola*sqrt(TT);
    return S*CDFNor(d1)-K*exp(-r*TT)*CDFNor(d2)-S*optiont+K*exp(-r*TT)*optiont;
    };
    //BSoption::BSoption(double S, double K, double T, double r, double vola,double optiont): _S(S), _K(K), _T(T), _r(r), _vola(vola), _optiont(optiont)
    BSoption::BSoption(double S, double K, double TT , double r, double vola , int optiont ){
    this->_S = S;
    this->_K = K;
    this->_TT = TT;
    this->_r = r;
    this->_vola = vola;
    this->_optiont = optiont;

    };

    double BSoption::Price()const{
    return BSPrice(_S,_K,_TT,_r,_vola,_optiont);
    };

    BSoption::~BSoption(){};

    FxOpt::FxOpt(double S, double K,double TT,double r,double vola,double qone,int optiont):BSoption(S,K,TT,r,vola,optiont),_qone(qon e){};

    double FxOpt::Price()const{
    return BSPrice(_S*exp(-_qone*_TT), _K, _TT, _r, _vola,_optiont);
    };

    void FxOpt::display()const{
    cout<<"Price of FX option is ";
    BSoption::display();
    };

    FxOpt::~FxOpt(){};

    DivOpt:ivOpt(double S, double K,double TT,double r,double vola,double qtwo,int optiont):BSoption(S,K,TT,r,vola, optiont),_qtwo(qtwo){};

    double DivOpt::Price()const{
    return BSPrice(_S*exp(-_qtwo*_TT), _K, _TT, _r, _vola,_optiont);
    };

    void DivOpt::display()const{
    cout<< "Price of Div option is ";
    BSoption::display();
    };

    DivOpt::~DivOpt(){};

    FutureOpt::FutureOpt(double S, double K,double TT,double r,double vola,double qthree,int optiont):BSoption(S,K,TT,r,vola,optiont),_qthree(q three){
    };

    double FutureOpt::Price()const{
    return BSPrice(_S*exp(-_qthree*_TT), _K, _TT, _r, _vola,_optiont);
    };

    void FutureOpt::display()const{
    cout<< "Price of Future option is ";
    BSoption::display();
    };
    FutureOpt::~FutureOpt(){};


    void fn(BSoption& a){
    a.display();
    };


    double PDFNor(double x)
    {
    return ((1.0/sqrt(2.0*3.1415))* exp(-x*x*0.5));
    }

    double CDFNor(double x)
    {
    static double a[5] = {0.319381530,
    -0.356563782,
    1.781477937,
    -1.821255978,
    1.330274429};
    double result;
    if (x<-7.0)
    result = PDFNor(x)/sqrt(1.0+x*x);
    else
    {
    if (x>7.0)
    result = 1.0 - CDFNor(-x);
    else
    {
    double tmp = 1.0/(1.0+0.2316419*fabs(x));
    result=1-PDFNor(x)*
    (tmp*(a[0]+tmp*(a[1]+tmp*(a[2]+tmp*(a[3]+tmp*a[4])))));
    if (x<=0.0)
    result=1.0-result;
    }
    }
    return result;
    }

    #include "stdafx.h"
    #include <iostream>
    #include"stdlib.h"
    #include <cmath>
    #include"BSopt.h"
    #include<algorithm>
    using namespace std;
    int optiont,Type;
    //N: number of time steps, Option=1 for call; Excercise=1 for American
    double K, TT, S, vola, r, q;
    //K: strike, T: maturity, S: initial stock price,vola: volatility,
    //r:continuous componding risk free interest rate;q:continuous dividend yield;
    void main()

    {
    printf("Black-Scholes Formula for Pricing European put and call option on a stock (with Greeks).\n");
    printf("Please input Option (0 for call,1 for put): ");
    scanf_s("%d",&optiont);//input option
    while ((optiont!=1)&&(optiont!=0))
    {
    printf("----------------Error Input of Option type!-------------------------\n");
    printf("Please input Option (0 for call, 1 for put): ");
    scanf_s("%d",&optiont);
    }
    printf("optiont=%d\n", optiont);

    printf("Please input Type of Option(1 for FXoption, 2 for Divoption ,3 for Futureoption): ");
    scanf_s("%d",&Type);
    printf("Type = %d\n", Type);

    printf("Please input strike price K: ");
    scanf_s("%lf",&K);
    printf("K=%lf\n", K);

    printf("Please input Maturity TT: ");
    scanf_s("%lf",&TT);
    printf("T=%lf\n", TT);

    printf("Please input initial stock price S: ");
    scanf_s("%lf",&S);
    printf("S=%lf\n", S);

    printf("Please input volatility: ");
    scanf_s("%lf",&vola);
    printf("volatility=%lf\n", vola);

    //input r
    printf("Please input continuous componding risk free interest rate r: ");
    scanf_s("%lf",&r);
    while(r<0||r>1)
    {
    printf("----------------r must be between 0 and 1!-------------------------\n");
    printf("Please input continuous componding risk free interest rate r: ");
    scanf_s("%lf",&r);
    }
    printf("r=%lf\n", r);

    printf("Please input continuous dividend yield q: ");
    scanf_s("%lf",&q);
    while(q<0||q>1)
    {
    printf("----------------q must be between 0 and 1!-------------------------\n");
    printf("Please input continuous dividend yield q: ");
    scanf_s("%lf",&q);
    }
    printf("q=%lf\n", q);


    FxOpt fx(S,K,TT,r,vola,q,optiont);
    DivOpt dx(S,K,TT,r,vola,q,optiont);
    FutureOpt fux(S,K,TT,r,vola,q,optiont);

    if(Type==1)
    fn(fx);
    else if(Type==2)
    fn(dx);
    else if(Type==3)
    fn(fux);

    system("pause");
    return;
    }

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    First, put your code inside code blocks as in
    Code:
    // This is some C++ code
    class X {
    protected:
        int m_int;
        double m_double;
    public:
        X();
        X( const X& );
        virtual ~X();
        X& operator=( const X& rhs );
    };
    That way, the indentations are preserved, and it becomes a LOT easier to read an analyze.

    So, specifically what is your problem? You said something about "broadcasting" not working? What do you mean by that. FWIW, I have considerable experience in the development of software for the options trading industry. I worked at a company associated with the CBOE for a couple of years, writing risk analysis software.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...