/usr/include/ql/instruments/forward.hpp is in libquantlib0-dev 1.12-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2006 Allen Kuo
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it
under the terms of the QuantLib license. You should have received a
copy of the license along with this program; if not, please email
<quantlib-dev@lists.sf.net>. The license is also available online at
<http://quantlib.org/license.shtml>.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/
/*! \file forward.hpp
\brief Base forward class
*/
#ifndef quantlib_forward_hpp
#define quantlib_forward_hpp
#include <ql/instrument.hpp>
#include <ql/position.hpp>
#include <ql/time/calendar.hpp>
#include <ql/time/daycounter.hpp>
#include <ql/interestrate.hpp>
#include <ql/types.hpp>
#include <ql/handle.hpp>
#include <ql/payoff.hpp>
#include <ql/termstructures/yieldtermstructure.hpp>
namespace QuantLib {
//! Abstract base forward class
/*! Derived classes must implement the virtual functions
spotValue() (NPV or spot price) and spotIncome() associated
with the specific relevant underlying (e.g. bond, stock,
commodity, loan/deposit). These functions must be used to set the
protected member variables underlyingSpotValue_ and
underlyingIncome_ within performCalculations() in the derived
class before the base-class implementation is called.
spotIncome() refers generically to the present value of
coupons, dividends or storage costs.
discountCurve_ is the curve used to discount forward contract
cash flows back to the evaluation day, as well as to obtain
forward values for spot values/prices.
incomeDiscountCurve_, which for generality is not
automatically set to the discountCurve_, is the curve used to
discount future income/dividends/storage-costs etc back to the
evaluation date.
\todo Add preconditions and tests
\warning This class still needs to be rigorously tested
\ingroup instruments
*/
class Forward : public Instrument {
public:
//! \name Inspectors
//@{
virtual Date settlementDate() const;
const Calendar& calendar() const;
BusinessDayConvention businessDayConvention() const;
const DayCounter& dayCounter() const;
//! term structure relevant to the contract (e.g. repo curve)
Handle<YieldTermStructure> discountCurve() const;
//! term structure that discounts the underlying's income cash flows
Handle<YieldTermStructure> incomeDiscountCurve() const;
//! returns whether the instrument is still tradable.
bool isExpired() const;
//@}
//! returns spot value/price of an underlying financial instrument
virtual Real spotValue() const = 0;
//! NPV of income/dividends/storage-costs etc. of underlying instrument
virtual Real spotIncome(const Handle<YieldTermStructure>&
incomeDiscountCurve) const = 0;
//! \name Calculations
//@{
//! forward value/price of underlying, discounting income/dividends
/*! \note if this is a bond forward price, is must be a dirty
forward price.
*/
virtual Real forwardValue() const;
/*! Simple yield calculation based on underlying spot and
forward values, taking into account underlying income.
When \f$ t>0 \f$, call with:
underlyingSpotValue=spotValue(t),
forwardValue=strikePrice, to get current yield. For a
repo, if \f$ t=0 \f$, impliedYield should reproduce the
spot repo rate. For FRA's, this should reproduce the
relevant zero rate at the FRA's maturityDate_;
*/
InterestRate impliedYield(Real underlyingSpotValue,
Real forwardValue,
Date settlementDate,
Compounding compoundingConvention,
DayCounter dayCounter);
//@}
protected:
Forward(const DayCounter& dayCounter,
const Calendar& calendar,
BusinessDayConvention businessDayConvention,
Natural settlementDays,
const boost::shared_ptr<Payoff>& payoff,
const Date& valueDate,
const Date& maturityDate,
const Handle<YieldTermStructure>& discountCurve =
Handle<YieldTermStructure>());
void performCalculations() const;
/*! derived classes must set this, typically via spotIncome() */
mutable Real underlyingIncome_;
/*! derived classes must set this, typically via spotValue() */
mutable Real underlyingSpotValue_;
DayCounter dayCounter_;
Calendar calendar_;
BusinessDayConvention businessDayConvention_;
Natural settlementDays_;
boost::shared_ptr<Payoff> payoff_;
/*! valueDate = settlement date (date the fwd contract starts
accruing)
*/
Date valueDate_;
//! maturityDate of the forward contract or delivery date of underlying
Date maturityDate_;
Handle<YieldTermStructure> discountCurve_;
/*! must set this in derived classes, based on particular underlying */
Handle<YieldTermStructure> incomeDiscountCurve_;
};
//! Class for forward type payoffs
class ForwardTypePayoff : public Payoff {
public:
ForwardTypePayoff(Position::Type type, Real strike)
: type_(type),strike_(strike) {
QL_REQUIRE(strike >= 0.0,"negative strike given");
}
Position::Type forwardType() const { return type_; };
Real strike() const { return strike_; };
//! \name Payoff interface
//@{
std::string name() const { return "Forward";}
std::string description() const;
Real operator()(Real price) const;
//@}
protected:
Position::Type type_;
Real strike_;
};
// inline definitions
inline const Calendar& Forward::calendar() const {
return calendar_;
}
inline BusinessDayConvention Forward::businessDayConvention() const {
return businessDayConvention_;
}
inline const DayCounter& Forward::dayCounter() const {
return dayCounter_;
}
inline Handle<YieldTermStructure> Forward::discountCurve() const {
return discountCurve_;
}
inline Handle<YieldTermStructure> Forward::incomeDiscountCurve() const {
return incomeDiscountCurve_;
}
inline std::string ForwardTypePayoff::description() const {
std::ostringstream result;
result << name() << ", " << strike() << " strike";
return result.str();
}
inline Real ForwardTypePayoff::operator()(Real price) const {
switch (type_) {
case Position::Long:
return (price-strike_);
case Position::Short:
return (strike_-price);
default:
QL_FAIL("unknown/illegal position type");
}
}
}
#endif
|