Inheritance 调用QuantLib::Instrument NPV()方法时无法取消引用空句柄
设置 首先,Inheritance 调用QuantLib::Instrument NPV()方法时无法取消引用空句柄,inheritance,this,handle,quantlib,Inheritance,This,Handle,Quantlib,设置 首先,使用名称空间QuantLib来缩短代码片段 继承自ZeroCouponBondWrapper继承自ZeroCouponBond:其目的只是为构造函数参数设置一些默认值,总体而言,是直接从收益率曲线嵌入定价引擎的设置: class ZeroCouponBondWrapper : public ZeroCouponBond { public: ZeroCouponBondWrapper(Real faceAmount, const Date& ma
使用名称空间QuantLib
来缩短代码片段
继承自ZeroCouponBondWrapper
继承自ZeroCouponBond
:其目的只是为构造函数参数设置一些默认值,总体而言,是直接从收益率曲线嵌入定价引擎的设置:
class ZeroCouponBondWrapper :
public ZeroCouponBond
{
public:
ZeroCouponBondWrapper(Real faceAmount,
const Date& maturityDate,
const boost::shared_ptr< YieldTermStructure >& yieldTermStructure,
Real redemption);
virtual ~ZeroCouponBondWrapper();
};
ZeroCouponBondWrapper::ZeroCouponBondWrapper(Real faceAmount,
const Date& maturityDate,
const boost::shared_ptr< YieldTermStructure >& yieldTermStructure,
Real redemption)
: ZeroCouponBond(2, TARGET(), faceAmount, maturityDate, Following, redemption, Date())
{
boost::shared_ptr< PricingEngine > discountingBondEngine(new DiscountingBondEngine(Handle< YieldTermStructure >(yieldTermStructure)));
this->setPricingEngine(discountingBondEngine);
}
它的构造函数实现处理zerocuponbondwrapper
构造函数:
EquityProtectionCertificate::EquityProtectionCertificate(Real x0,
const boost::shared_ptr< YieldTermStructure >& dividendTS,
const boost::shared_ptr< YieldTermStructure >& riskFreeTS,
const boost::shared_ptr< BlackVolTermStructure >& blackVolTS,
const Date& maturityDate,
Real faceAmount,
Real redemption,
const boost::shared_ptr< YieldTermStructure >& yieldTermStructure,
Real startingReferenceValue,
Real protection,
Real participation,
Real cap)
: ZeroCouponBondWrapper(faceAmount, maturityDate, yieldTermStructure, redemption),
N_(faceAmount), S0_(x0)
{
boost::shared_ptr< EuropeanOption > theFloor(new EuropeanOptionWrapper(Option::Call,
startingReferenceValue,
maturityDate,
x0,
riskFreeTS,
dividendTS,
blackVolTS));
europeanOptions_.push_back(theFloor);
boost::shared_ptr< EuropeanOption > theCap(new EuropeanOptionWrapper(Option::Call,
startingReferenceValue * cap,
maturityDate,
x0,
riskFreeTS,
dividendTS,
blackVolTS));
europeanOptions_.push_back(theCap);
}
(N
和S0
只是其他私有double
变量)
问题
在构造函数中调用
this->NPV()
时,它返回正确的值:这意味着正确调用了setPricinGenEngine()
。但是,当您在CalculateNPV()
内部调用this->NPV()
时,它不会返回任何值:相反,它返回一个异常,该异常表示空句柄不能被取消引用。你能解释一下我使用this->NPV()
和/或继承概念的错误吗?你也可以发布EquityProtectionCertificate
的构造函数吗?当然可以,Luigi。。。好的,我不想说它在我的机器上工作,但它在我的机器上工作;至少是零优惠券部分,因为我缺少EuropeanOptionWrapper
类的代码。你确定引发异常的是this->NPV()
,而不是选项上对NPV
的调用吗?嗯。。。我说过。让我再深入一点,我不想写这么多的手稿,只是因为问题不一致而取消它。也就是说,既然你已经在证书中存储了这两个选项,如果您也存储了绑定而不是从它继承,那么可能会更简单…您是否也可以发布EquityProtectionCertificate
的构造函数?当然,Luigi。。。好的,我不想说它在我的机器上工作,但它在我的机器上工作;至少是零优惠券部分,因为我缺少EuropeanOptionWrapper
类的代码。你确定引发异常的是this->NPV()
,而不是选项上对NPV
的调用吗?嗯。。。我说过。让我再深入一点,我不想写这么多的手稿,只是因为问题不一致而取消它。也就是说,既然你已经在证书中存储了这两个选项,如果你也存储了债券,而不是继承债券,可能会更简单。。。
EquityProtectionCertificate::EquityProtectionCertificate(Real x0,
const boost::shared_ptr< YieldTermStructure >& dividendTS,
const boost::shared_ptr< YieldTermStructure >& riskFreeTS,
const boost::shared_ptr< BlackVolTermStructure >& blackVolTS,
const Date& maturityDate,
Real faceAmount,
Real redemption,
const boost::shared_ptr< YieldTermStructure >& yieldTermStructure,
Real startingReferenceValue,
Real protection,
Real participation,
Real cap)
: ZeroCouponBondWrapper(faceAmount, maturityDate, yieldTermStructure, redemption),
N_(faceAmount), S0_(x0)
{
boost::shared_ptr< EuropeanOption > theFloor(new EuropeanOptionWrapper(Option::Call,
startingReferenceValue,
maturityDate,
x0,
riskFreeTS,
dividendTS,
blackVolTS));
europeanOptions_.push_back(theFloor);
boost::shared_ptr< EuropeanOption > theCap(new EuropeanOptionWrapper(Option::Call,
startingReferenceValue * cap,
maturityDate,
x0,
riskFreeTS,
dividendTS,
blackVolTS));
europeanOptions_.push_back(theCap);
}
double EquityProtectionCertificate::CalculateNPV()
{
double npv = 0.0;
npv += this->NPV();
npv += (europeanOptions_[0]->NPV() * N_ / S0_);
npv -= (europeanOptions_[1]->NPV() * N_ / S0_);
return npv;
}