| 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 | #include <ql/event.hpp> |
| 21 | #include <ql/instruments/forward.hpp> |
| 22 | #include <ql/termstructures/yieldtermstructure.hpp> |
| 23 | #include <utility> |
| 24 | |
| 25 | namespace QuantLib { |
| 26 | |
| 27 | Forward::Forward(DayCounter dayCounter, |
| 28 | Calendar calendar, |
| 29 | BusinessDayConvention businessDayConvention, |
| 30 | Natural settlementDays, |
| 31 | ext::shared_ptr<Payoff> payoff, |
| 32 | const Date& valueDate, |
| 33 | const Date& maturityDate, |
| 34 | Handle<YieldTermStructure> discountCurve) |
| 35 | : dayCounter_(std::move(dayCounter)), calendar_(std::move(calendar)), |
| 36 | businessDayConvention_(businessDayConvention), settlementDays_(settlementDays), |
| 37 | payoff_(std::move(payoff)), valueDate_(valueDate), maturityDate_(maturityDate), |
| 38 | discountCurve_(std::move(discountCurve)) { |
| 39 | |
| 40 | maturityDate_ = calendar_.adjust(maturityDate_, |
| 41 | convention: businessDayConvention_); |
| 42 | |
| 43 | registerWith(h: Settings::instance().evaluationDate()); |
| 44 | registerWith(h: discountCurve_); |
| 45 | } |
| 46 | |
| 47 | |
| 48 | Date Forward::settlementDate() const { |
| 49 | Date d = calendar_.advance(Settings::instance().evaluationDate(), |
| 50 | n: settlementDays_, unit: Days); |
| 51 | return std::max(a: d,b: valueDate_); |
| 52 | } |
| 53 | |
| 54 | |
| 55 | bool Forward::isExpired() const { |
| 56 | return detail::simple_event(maturityDate_) |
| 57 | .hasOccurred(refDate: settlementDate()); |
| 58 | } |
| 59 | |
| 60 | |
| 61 | Real Forward::forwardValue() const { |
| 62 | calculate(); |
| 63 | return (underlyingSpotValue_ - underlyingIncome_ )/ |
| 64 | discountCurve_->discount(d: maturityDate_); |
| 65 | } |
| 66 | |
| 67 | |
| 68 | InterestRate Forward::impliedYield(Real underlyingSpotValue, |
| 69 | Real forwardValue, |
| 70 | Date settlementDate, |
| 71 | Compounding comp, |
| 72 | const DayCounter& dayCounter) { |
| 73 | |
| 74 | Time t = dayCounter.yearFraction(d1: settlementDate,d2: maturityDate_) ; |
| 75 | Real compoundingFactor = forwardValue/ |
| 76 | (underlyingSpotValue-spotIncome(incomeDiscountCurve: incomeDiscountCurve_)) ; |
| 77 | return InterestRate::impliedRate(compound: compoundingFactor, |
| 78 | resultDC: dayCounter, comp, freq: Annual, |
| 79 | t); |
| 80 | } |
| 81 | |
| 82 | |
| 83 | void Forward::performCalculations() const { |
| 84 | |
| 85 | QL_REQUIRE(!discountCurve_.empty(), |
| 86 | "null term structure set to Forward" ); |
| 87 | |
| 88 | ext::shared_ptr<ForwardTypePayoff> ftpayoff = |
| 89 | ext::dynamic_pointer_cast<ForwardTypePayoff>(r: payoff_); |
| 90 | Real fwdValue = forwardValue(); |
| 91 | NPV_ = (*ftpayoff)(fwdValue) * discountCurve_->discount(d: maturityDate_); |
| 92 | } |
| 93 | |
| 94 | } |
| 95 | |