《quantitative finance with cpp》阅读笔记17---给ContinuousTimeOption写接口并对KnockOutCallOption期权进行定价
作者:yunjinqi   类别:    日期:2023-12-28 14:44:04    阅读:456 次   消耗积分:0 分    

Design an interface called ContinuousTimeOption. It should have a

function payoff which takes a vector of stock prices and a vector of times

at which the price occurred. This should return the payoff that would have

occurred if the stock price had varied linearly between the time points. 

(Obviously we can’t actually supply the stock price at an infinite number of

points). The ContinuousTimeOption should also have a function called getMaturity().

Write an implementation of ContinuousTimeOption called KnockOutCallOption. 

Add a function to MonteCarloPricer which can approximate the

price of any ContinuousTimeOption given a finite number of time steps.



为了解决这个问题,实现了两个类并且修改了MonteCarloPricer,结果和答案并不完全一致,可能并不一定会正确。代码参考地址:对比视图(17fa4808b46b3a603f38799d1f1d836df14b7532...7742462cae22cb40f572c41d8ba1183e228ead55) · 云金杞/learn_cpp_series - Gitee.com


ContinuousTimeOption.h

#pragma once
#include "stdafx.h"
#include "testing.h"
/**
 *   This states that all path independent options
 *   have a payoff determined by the final stock price
 */
class ContinuousTimeOption {
public:
    /*  A virtual destructor */
    virtual ~ContinuousTimeOption() {}
    /*  Returns the payoff at maturity */
    virtual double payoff(
        std::vector<double> &prices, std::vector<double> &times) const = 0;
    /*  Returns the maturity of the option */
    virtual double getMaturity() const
        = 0;
};


KnockOutCallOption.h


#pragma once
#include "ContinuousTimeOption.h"
class KnockOutCallOption: public ContinuousTimeOption {
public:
    KnockOutCallOption();
    /*  Returns the payoff at maturity */
    double payoff(
        std::vector<double>& prices) const;
    /*  Returns the maturity of the option */
    double getMaturity() const;
    void setStrike(double s);
    void setMaturity(double m);
    void setKnockPrice(double p);
private:
    double strike;
    double maturity;
    double knockPrice;
};


KnockOutCallOption.cpp

#include "KnockOutCallOption.h"

KnockOutCallOption::KnockOutCallOption() {
	strike = 0.0;
	maturity = 0.0;
	knockPrice = 0.0;
}


double KnockOutCallOption::getMaturity() const{
	return maturity;
}

void KnockOutCallOption::setStrike(double s) { strike = s; };
void KnockOutCallOption::setMaturity(double m) { maturity = m; };
void KnockOutCallOption::setKnockPrice(double p) { knockPrice = p; };

double KnockOutCallOption::payoff(std::vector<double>& prices) const{
	double lastPrice = prices[prices.size() - 1];
	if (lastPrice > strike) {
		for (auto p : prices) {
			if (p >= knockPrice) {
				return 0.0;
			}
		}
		return lastPrice - strike;
	}
	else {
		return 0.0;
	}
	double result =  - strike;
	
}


版权所有,转载本站文章请注明出处:云子量化, http://www.woniunote.com/article/382
上一篇:《quantitative finance with cpp》阅读笔记16---使用数值积分的方法计算路径依赖期权价格
下一篇:《quantitative finance with cpp》阅读笔记18---使用指针作为参数进行求和