【问题标题】:C++ and finance, trouble understanding syntax in these files [closed]C ++和金融,难以理解这些文件中的语法[关闭]
【发布时间】:2014-12-31 04:56:23
【问题描述】:

我刚从 Daniel Duffy 的《金融工程师 C++ 简介:面向对象的方法》一书中开始学习金融领域的 C++。经过一些工作,我完成了前两章,但关于课程的第三章让我有点困惑。我知道一个类由成员数据(在本例中为头文件)和成员函数(在本例中为代码文件)组成。本书继续讨论了构造函数、修饰符、选择器和析构函数。下面的头文件和代码文件中粘贴的内容可能是以显示这些功能的方式编码的。 我不想让这段代码更有效率,我想了解发生了什么。这个程序应该给出欧式期权的看跌/看涨价格及其各自的增量。我在概念上有点理解事物,但语法和结构才是真正让我困惑的地方。当我查找诸如 void 之类的定义时,我看不出它们如何与代码相匹配。

我会评论我认为正在发生的事情。我真的很感激一些帮助,因为此时我完全处于黑暗之中。最后带问号的 cmets 是我最需要帮助的地方。

还有一个资源可以帮助我对 C++ 中的事物和语法进行基本的外行解释。我不想掩盖本书或学习 C++ 中的任何内容。我想完全理解每一行代码。

如果需要,我可以上传或通过电子邮件发送 xcode 项目/文件。

提前感谢您,对于任何愚蠢的错误,我深表歉意。

//////////////类头文件/////////////////

#ifndef EuropeanOption_hpp //Regular header looking stuff. Not sure exactly what it means.
#define EuropeanOption_hpp


#include <string> //Include this if you are using strings.
using namespace std;       //Adding in the standard namespace library. Be careful with conflits between libraries in the future.

class EuropeanOption //Looks like we are defining a class here.
{
public:             //Public members of the EuropeanOption class can be accessed from anywhere.

    void init();    // What is this?
    void copy(const EuropeanOption& o2); //What's going on here?

    //Defining some constants here that we will do calculations later to get values for. These do not change hence the "const."
    double CallPrice() const;
    double PutPrice() const;
    double CallDelta() const;
    double PutDelta() const;
    double CallGamma() const;
    double PutGamma() const;
    double CallVega() const;
    double PutVega() const;

    //Stuff for the overly used and abused normal distribution that we will calculate later.
    double n(double x) const;
    double N(double x) const;

    //Defining some parameters. These can change, no const at the end.
    double r;
    double sig;
    double K;
    double T;
    double U;
    double b;

    string optType; // Option name (call, put)
    string unam;    // Name of underlying asset


public: //Public members of the EuropeanOption class can be accessed from anywhere.
    EuropeanOption();                           // Default call option
    EuropeanOption(const EuropeanOption& option2); //What?
    EuropeanOption (const string& optionType); //Who?
    virtual ~EuropeanOption();  //Where?

    EuropeanOption& operator = (const EuropeanOption& option2); //No idea what this is.

    // Functions that calculate option price and sensitivities
    double Price() const;
    double Delta() const;

    void toggle(); //What?


};

#endif

/////////////类代码文件///////////////

#ifndef EuropeanOption_cpp //Again, not really sure what this is but I know its important.
#define EuropeanOption_cpp


#include "EuropeanOption.hpp"
#include <math.h> //Include a math library.
#include <iostream> //Include IO stuff.

//////////// Gaussian functions /////////////////////////////////

double EuropeanOption::n(double x) const //With :: we made little n's member function part of the EuropeanOption class?
{ 

    double A = 1.0/sqrt(2.0 * 3.1415);
    return A * exp(-x*x*0.5);

}

double EuropeanOption::N(double x) const //Here we used little n to make big N's member function part of the European option class?
{ // The approximation to the cumulative normal distribution


    double a1 = 0.4361836;
    double a2 = -0.1201676;
    double a3 = 0.9372980;

    double k = 1.0/(1.0 + (0.33267 * x));

    if (x >= 0.0)
    {
        return 1.0 - n(x)* (a1*k + (a2*k*k) + (a3*k*k*k));
    }
    else
    {
        return 1.0 - N(-x);
    }

}


// Black and Scholes stock option model (1973)
double EuropeanOption::CallPrice() const //Defined callprices's function as part of the EuropeanOption class?
{

    double tmp = sig * sqrt(T);

    double d1 = ( log(U/K) + (b+ (sig*sig)*0.5 ) * T )/ tmp;
    double d2 = d1 - tmp;


    return (U * exp((b-r)*T) * N(d1)) - (K * exp(-r * T)* N(d2));

}

double EuropeanOption::PutPrice() const //Defined putprices's function as part of the EuropeanOption class?
{

    double tmp = sig * sqrt(T);

    double d1 = ( log(U/K) + (b+ (sig*sig)*0.5 ) * T )/ tmp;
    double d2 = d1 - tmp;

    return (K * exp(-r * T)* N(-d2)) - (U * exp((b-r)*T) * N(-d1));

}

double EuropeanOption::CallDelta() const
{
    double tmp = sig * sqrt(T);

    double d1 = ( log(U/K) + (b+ (sig*sig)*0.5 ) * T )/ tmp;


    return exp((b-r)*T) * N(d1);
}

double EuropeanOption::PutDelta() const
{
    double tmp = sig * sqrt(T);

    double d1 = ( log(U/K) + (b+ (sig*sig)*0.5 ) * T )/ tmp;

    return exp((b-r)*T) * (N(d1) - 1.0);
}



/////////////////////////////////////////////////////////////////////////////////////

void EuropeanOption::init() //What is going on here?
{

    r = 0.08;
    sig= 0.30;
    K = 65.0;
    T = 0.25;
    U = 60.0;
    b = r;

    optType = "C";      //What?



}

void EuropeanOption::copy(const EuropeanOption& o2) //What's going on here?
{

    r   = o2.r;
    sig = o2.sig;   
    K   = o2.K;
    T   = o2.T;
    U   = o2.U;
    b   = o2.b;

    optType = o2.optType;



}

EuropeanOption::EuropeanOption() //Completely lost.
{

    init();
}

EuropeanOption::EuropeanOption(const EuropeanOption& o2) //??
{
    copy(o2);
}

EuropeanOption::EuropeanOption (const string& optionType) //??
{
    init();
    optType = optionType;

    if (optType == "c")
        optType = "C";

}



EuropeanOption::~EuropeanOption() //I've never seen the swiggly before.
{

}


EuropeanOption& EuropeanOption::operator = (const EuropeanOption& option2) //Still lost.
{

    if (this == &option2) return *this;

    copy (option2);

    return *this;
}


double EuropeanOption::Price() const //Looks like we are returning either a call or put price based on what was entered before??
{

    if (optType == "C")
    {
        return CallPrice();
    }
    else
        return PutPrice();

}

double EuropeanOption::Delta() const 
{
    if (optType == "C")
        return CallDelta();
    else
        return PutDelta();

}




void EuropeanOption::toggle()
{ // Change option type (C/P, P/C)??

    if (optType == "C")
        optType = "P";
    else
        optType = "C";
}

#endif

//////////主代码//////////////

#include "EuropeanOption.hpp"
#include <iostream>

//I did not comment on this section because I don't understand most of it and I think once I understand the class cpp & hpp files this will start to click. I do understand basic things here like int main(), endl, cout, cin. I also left the original author's comments in from the book.

int main()
{ 
    EuropeanOption callOption;
    cout << "Call option on a stock: " << callOption.Price() << endl;

    // Put option on a stock index
    EuropeanOption indexOption;
    indexOption.optType = "P";
    indexOption.U = 100.0;
    indexOption.K = 95.0;
    indexOption.T = 0.5;
    indexOption.r = 0.10;
    indexOption.sig = 0.20;

    double q = 0.05;        // Dividend yield
    indexOption.b = indexOption.r - q;

    cout << "Put option on an index: " << indexOption.Price() << endl;

    // Call and put options on a future
    EuropeanOption futureOption;
    futureOption.optType = "P";
    futureOption.U = 19.0;
    futureOption.K = 19.0;
    futureOption.T = 0.75;
    futureOption.r = 0.10;
    futureOption.sig = 0.28;

    futureOption.b = 0.0;

    cout << "Put option on a future: " << futureOption.Price() << endl;

    // Now change over to a call on the option
    futureOption.toggle();
    cout << "Call option on a future: " << futureOption.Price() << endl;


    // Call option on currency
    EuropeanOption currencyOption;
    currencyOption.optType = "C";
    currencyOption.U = 1.56;
    currencyOption.K = 1.60;
    currencyOption.T = 0.5;
    currencyOption.r = 0.06;
    currencyOption.sig = 0.12;

    double rf = 0.08;       // risk-free rate of foreign currency
    currencyOption.b = currencyOption.r - rf;

    cout << endl << "** Other pricing examples **" << endl << endl;

    cout << "Call option on a currency: " << currencyOption.Price() << endl;

    ////////   NOW CALCULATIONS OF SENSITIVITIES //////////////////////////////////

    // Call and put options on a future: Delta and Elasticity
    EuropeanOption futureOption2;
    futureOption2.optType = "P";
    futureOption2.U = 105.0;
    futureOption2.K = 100.0;
    futureOption2.T = 0.5;
    futureOption2.r = 0.10;
    futureOption2.sig = 0.36;

    futureOption2.b = 0.0;

    cout << "Delta on a put future: " << futureOption2.Delta() << endl;

    // Now change over to a call on the option
    futureOption2.toggle();
    cout << "Delta on a call future: " << futureOption2.Delta() << endl;


    // Stock Option: Gamma
    EuropeanOption stockOption;
    stockOption.optType = "C";
    stockOption.U = 55.0;
    stockOption.K = 60.0;
    stockOption.T = 0.75;
    stockOption.r = 0.10;
    stockOption.sig = 0.30;

    stockOption.b = stockOption.r;


    stockOption.toggle();

    // Calculating theta of a European stock index
    EuropeanOption indexOption2;
    indexOption2.optType = "P";
    indexOption2.U = 430.0;
    indexOption2.K = 405.0;
    indexOption2.T = 0.0833;    // One month expiration
    indexOption2.r = 0.07;
    indexOption2.sig = 0.20;

    double divYield = 0.05;     // Dividend yield, 5% per annum
    indexOption2.b = indexOption2.r - divYield;


    // Stock Option: Rho
    EuropeanOption stockOption2;
    stockOption2.optType = "C";
    stockOption2.U = 72.0;
    stockOption2.K = 75.0;
    stockOption2.T = 1.0;
    stockOption2.r = 0.09;
    stockOption2.sig = 0.19;

    stockOption2.b = stockOption2.r;


    // Calculating Cost of Carry of a European stock index
    EuropeanOption indexOption3;
    indexOption3.optType = "P";
    indexOption3.U = 500.0;
    indexOption3.K = 490.0;
    indexOption3.T = 0.222225;
    indexOption3.r = 0.08;
    indexOption3.sig = 0.15;

    double divYield3 = 0.05;        // Dividend yield, 5% per annum
    indexOption3.b = indexOption3.r - divYield3 ;


    return 0;
}

【问题讨论】:

  • 在为金融工程师深入研究 C++ 之前,您可能会一目了然地花时间研究 C++ 本身。如果您更了解核心语言及其工作原理(怪癖和所有内容),那么您在这些文件中标记的大多数内容可能对您来说会更清楚。一些潜在的书源can be found here。一个优秀的语言和标准库参考站点can be found here
  • 我还建议您获取代码,编译它,然后在调试器下一次一步运行它。这样,您可以看到程序的流程、观察变量、每一行的作用等等。
  • 如果你想很好地理解C++,你必须长期学习和使用它;我们无法在几页文字中传达这种专业知识。至于您的具体问题,我什至无法在简要阅读您的帖子中找到它们(这是我愿意在没有看到任何光明的情况下投资的全部内容)。尝试使用simplest code that can illustrate it 将其简化为一个简单的问题。而且不仅仅是为了我们,或者帮助吸引有用的答案——总的来说,这是一个非常有用的习惯。

标签: c++ options finance


【解决方案1】:

我最初不打算回答,因为这里有很多关于开始 c++ 的内容,但我认为给您一个高级概述并让您了解所涉及的学习曲线可能会有所帮助。我认为你应该放慢速度,看看像 Accelerated C++ http://www.amazon.com/Accelerated-C-Practical-Programming-Example/dp/020170353X 这样的书,它将涵盖所有这些内容,并让你在几周内了解所有这些内容。有一段时间没做过c++编程了,不过还是试着简单解释一下上面你没看懂的一些基本点:

头文件用于帮助加快编译时间并组织代码。如果所有内容都在一个文件中(如果您愿意,您可以将上述所有内容写入一个文件中),那么对该文件的任何更改都必须重新编译。通过拆分,您可以获得编译速度的优势(在大型项目中)并将接口与实现分开。您可以将头文件视为声明 cpp 文件将要实现的内容。例如,可以在头文件中声明init()方法,在.cpp文件中实现。

其他几点:

  1. #ifndef / #define 在此示例中是标头保护。它告诉编译器处理这个文件,如果它还没有这样做的话。这些是预处理器指令,可以以多种不同方式使用,您现在不必担心。

  2. const 出现在函数声明之后意味着函数不能更改任何类变量。您必须通过谷歌搜索“const 正确性”来阅读更多相关信息,因为 const 在 C++ 中以多种不同方式使用。

  3. ~ 是一个析构函数。它用于销毁内存中的对象。互联网上有大量关于构造函数和析构函数的文档。

  4. 主文件是程序的入口点。它使用您在其他文件中编写的类。如果你学会了如何编写一些简单的程序,这一切都会有意义。

我认为这已经足够了。

【讨论】:

  • 是的,我最初买的那本书基本上在几秒钟内就从 0 变成了 100,没有给我适当的语言介绍。我将对此采取新的方法。我将看看 Accelerated C++,然后尝试将它们的代码应用到数学金融中。感谢您抽出时间整理并给我看这本书。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-03
  • 2018-09-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多