《quantitative finance with cpp》阅读笔记24---编写一个亚式期权(AsianOption)并画出来UML图
作者:yunjinqi   类别:    日期:2024-01-02 15:03:33    阅读:265 次   消耗积分:0 分    

Write an AsianOption which represents an Asian option. Show where this new class fits into the UML diagram of options.


对于Asian Option的描述,可以参考下面的网站

An Asian option (or average value option) is a special type of option contract. For Asian options, the payoff is determined by the average underlying price over some pre-set period of time. This is different from the case of the usual European option and American option, where the payoff of the option contract depends on the price of the underlying instrument at exercise; Asian options are thus one of the basic forms of exotic options.

There are two types of Asian options: Average Price Option (fixed strike), where the strike price is predetermined and the averaging price of the underlying asset is used for payoff calculation; and Average Strike Option (floating strike), where the averaging price of the underlying asset over the duration becomes the strike price.

本文采用第一种亚式期权的描述来编写该类型。参考代码如下:


很明显,这个是路径依赖的期权,期权价格跟过去的价格路径有关,所以需要首先写一个路径依赖期权的接口,这个接口和原先路径不依赖期权接口几乎一样:

#pragma once

#include "stdafx.h"
#include "ContinuousTimeOptionBase.h"
#include "testing.h"

/**
 *   This states that all path independent options
 *   have a payoff determined by the final stock price
 */
class PathDependentOption :
    public ContinuousTimeOptionBase {
public:
    /*  A virtual destructor */
    virtual ~PathDependentOption(){};
    /*  Returns the payoff at maturity */
    virtual double payoff(double endStockPrice) const
        = 0;
    /*  Compute the payoff from a price path */
    double payoff(
        const std::vector<double>& stockPrices) const {
        return payoff(stockPrices.back());
    }
    /*  Is the option path dependent? */
    bool isPathDependent() const {
        return true;
    };
};


然后写AsianOption类,用于实现亚式期权的定价

AsianOption.h

#pragma once
#include "stdafx.h"
#include "BlackScholesModel.h"
#include "PathDependentOption.h"
#include "MonteCarloPricer.h"
#include "matlib.h"
#include "testing.h"

class AsianOption : public PathDependentOption {
public:
    /*  Calculate the payoff of the option given
        a history of prices */
    double payoff(
        const std::vector<double>& stockPrices
    ) const;

    double price(const BlackScholesModel& bsm)
        const;
};

void testAsianOption();


AsianOption.cpp

#include "AsianOption.h"

double AsianOption::payoff(const std::vector<double>& stockPrices
) const {
	double avgPrice = mean(stockPrices);
	double strike = getStrike();
	if (avgPrice > strike) {
		return avgPrice - strike;
	}
	else {
		return 0;
	}
}

double AsianOption::price(const BlackScholesModel& bsm) const {
	MonteCarloPricer pricer;
	return pricer.price(*this, bsm);

}

void testAsianOption() {
	std::cout << "AsianOption is not tested by the author" << std::endl;
}


尝试画出来了期权的UML图:

image.png

版权所有,转载本站文章请注明出处:云子量化, http://www.woniunote.com/article/389
上一篇:《quantitative finance with cpp》阅读笔记23---写二元看涨期权、二元看跌期权类,并改写看跌期权以便继承PathIndependentOption类
下一篇:《quantitative finance with cpp》阅读笔记25---为Shape相关的类画出UML图