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