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> ×) 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; }