Coverage Report

Created: 2026-09-28 06:23

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/instruments/fxforward.hpp
Line
Count
Source
1
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*
4
 Copyright (C) 2026 Chirag Desai
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
 <https://www.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 fxforward.hpp
21
    \brief FX Forward instrument
22
*/
23
24
#ifndef quantlib_fx_forward_hpp
25
#define quantlib_fx_forward_hpp
26
27
#include <ql/currency.hpp>
28
#include <ql/instrument.hpp>
29
#include <ql/time/calendar.hpp>
30
#include <ql/time/date.hpp>
31
32
namespace QuantLib {
33
34
    //! %FX Forward instrument
35
    /*! This class represents a foreign exchange forward contract,
36
        which is an agreement to exchange a specified amount of one
37
        currency for another currency at a future date at a
38
        predetermined exchange rate.
39
40
        The instrument supports various settlement conventions:
41
        - Overnight (O/N): settlementDays = 0
42
        - TomNext (T/N): settlementDays = 1
43
        - SpotNext (S/N): settlementDays = 2 (standard spot)
44
45
        The payment date is computed as the evaluation date plus
46
        settlementDays business days according to the specified calendar.
47
48
        The instrument can be valued using DiscountingFxForwardEngine,
49
        which computes the NPV by discounting the source and target
50
        legs using their respective yield curves.
51
52
        \ingroup instruments
53
    */
54
    class FxForward : public Instrument {
55
      public:
56
        class arguments;
57
        class results;
58
        class engine;
59
        //! \name Constructors
60
        //@{
61
        /*! Constructor for FX Forward using nominal amounts.
62
            \param sourceNominal     Notional amount in source(domestic) currency
63
            \param sourceCurrency    Currency of sourceNominal (source currency)
64
            \param targetNominal     Notional amount in target(foreign) currency
65
            \param targetCurrency    Currency of targetNominal (target currency)
66
            \param maturityDate      Settlement date of the forward contract
67
            \param paySourceCurrency If true, pay source currency and receive target currency;
68
                                     if false, receive source currency and pay target currency
69
            \param settlementDays    Number of business days for payment settlement
70
                                     (0=O/N, 1=T/N, 2=Spot, default=2)
71
            \param paymentCalendar   Calendar for computing payment date
72
                                     (defaults to NullCalendar)
73
        */
74
        FxForward(Real sourceNominal,
75
                  const Currency& sourceCurrency,
76
                  Real targetNominal,
77
                  const Currency& targetCurrency,
78
                  const Date& maturityDate,
79
                  bool paySourceCurrency,
80
                  Natural settlementDays = 2,
81
                  const Calendar& paymentCalendar = Calendar());
82
83
        /*! Constructor for FX Forward using exchange rate.
84
            \param sourceNominal     Notional amount in source currency
85
            \param sourceCurrency    Currency of nominal amount
86
            \param targetCurrency    Currency to exchange into
87
            \param forwardRate       The forward exchange rate (target/source)
88
            \param maturityDate      Settlement date of the forward contract
89
            \param paySourceCurrency If true, pay source currency and receive target currency;
90
                                     if false, receive source currency and pay target currency
91
            \param settlementDays    Number of business days for payment settlement
92
                                     (0=O/N, 1=T/N, 2=Spot, default=2)
93
            \param paymentCalendar   Calendar for computing payment date
94
                                     (defaults to NullCalendar)
95
        */
96
        FxForward(Real sourceNominal,
97
                  const Currency& sourceCurrency,
98
                  const Currency& targetCurrency,
99
                  Real forwardRate,
100
                  const Date& maturityDate,
101
                  bool paySourceCurrency,
102
                  Natural settlementDays = 2,
103
                  const Calendar& paymentCalendar = Calendar());
104
        //@}
105
106
        //! \name Inspectors
107
        //@{
108
        //! Source nominal amount
109
0
        Real sourceNominal() const { return sourceNominal_; }
110
        //! Source currency
111
0
        const Currency& sourceCurrency() const { return sourceCurrency_; }
112
        //! Target nominal amount
113
0
        Real targetNominal() const { return targetNominal_; }
114
        //! Target currency
115
0
        const Currency& targetCurrency() const { return targetCurrency_; }
116
        //! Maturity date of the forward contract
117
0
        const Date& maturityDate() const { return maturityDate_; }
118
        //! True if paying source currency
119
0
        bool paySourceCurrency() const { return paySourceCurrency_; }
120
        //! Contracted forward rate (target currency per unit of source currency)
121
0
        Real forwardRate() const { return targetNominal_ / sourceNominal_; }
122
        //! Number of settlement days (0=O/N, 1=T/N, 2=Spot)
123
0
        Natural settlementDays() const { return settlementDays_; }
124
        //! Settlement calendar
125
0
        const Calendar& settlementCalendar() const { return paymentCalendar_; }
126
        //! Settlement date (computed from evaluation date + settlementDays)
127
        Date settlementDate() const;
128
        //@}
129
130
        //! \name Instrument interface
131
        //@{
132
        bool isExpired() const override;
133
        void setupArguments(PricingEngine::arguments*) const override;
134
        void fetchResults(const PricingEngine::results*) const override;
135
        //@}
136
137
        //! \name Additional results
138
        //@{
139
        //! Fair forward rate (targetCurrency/sourceCurrency), the market-implied fair rate
140
        //! computed by the engine
141
        Real fairForwardRate() const;
142
        //! NPV in source currency terms
143
        Real npvSourceCurrency() const;
144
        //! NPV in target currency terms
145
        Real npvTargetCurrency() const;
146
        //@}
147
148
      private:
149
        Real sourceNominal_;
150
        Currency sourceCurrency_;
151
        Real targetNominal_;
152
        Currency targetCurrency_;
153
        Date maturityDate_;
154
        bool paySourceCurrency_;
155
        Natural settlementDays_;
156
        Calendar paymentCalendar_;
157
158
        mutable Real fairForwardRate_;
159
        mutable Real npvSourceCurrency_;
160
        mutable Real npvTargetCurrency_;
161
    };
162
163
    //! Arguments for FX Forward pricing engine
164
    class FxForward::arguments : public virtual PricingEngine::arguments {
165
      public:
166
        Real sourceNominal = Null<Real>();
167
        Currency sourceCurrency;
168
        Real targetNominal = Null<Real>();
169
        Currency targetCurrency;
170
        Date maturityDate;
171
        bool paySourceCurrency = true;
172
        Date settlementDate;
173
        void validate() const override;
174
    };
175
176
    //! Results for FX Forward pricing engine
177
    class FxForward::results : public Instrument::results {
178
      public:
179
        Real fairForwardRate = Null<Real>();
180
        Real npvSourceCurrency = Null<Real>();
181
        Real npvTargetCurrency = Null<Real>();
182
        void reset() override;
183
    };
184
185
    //! Base class for FX Forward pricing engines
186
    class FxForward::engine : public GenericEngine<FxForward::arguments, FxForward::results> {};
187
188
}
189
190
#endif