QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
money.cpp
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) 2004 Decillion Pty(Ltd)
5 Copyright (C) 2007 StatPro Italia srl
6
7 This file is part of QuantLib, a free-software/open-source library
8 for financial quantitative analysts and developers - http://quantlib.org/
9
10 QuantLib is free software: you can redistribute it and/or modify it
11 under the terms of the QuantLib license. You should have received a
12 copy of the license along with this program; if not, please email
13 <quantlib-dev@lists.sf.net>. The license is also available online at
14 <http://quantlib.org/license.shtml>.
15
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the license for more details.
19*/
20
21#include <ql/money.hpp>
24
25namespace QuantLib {
26
27 namespace {
28
29 void convertTo(Money& m, const Currency& target) {
30 if (m.currency() != target) {
31 ExchangeRate rate =
33 target);
34 m = rate.exchange(m).rounded();
35 }
36 }
37
38 void convertToBase(Money& m) {
39 const auto & base_currency =
41 QL_REQUIRE(!base_currency.empty(), "no base currency set");
42 convertTo(m, base_currency);
43 }
44
45 template< typename ReturnValue, typename Function >
46 ReturnValue apply(const Money& m1, const Money& m2, const Function f) {
47 const auto & conversion_type =
49 if (m1.currency() == m2.currency()) {
50 return f(m1.value(), m2.value());
51 } else if (conversion_type == Money::BaseCurrencyConversion) {
52 Money tmp1 = m1;
53 convertToBase(tmp1);
54 Money tmp2 = m2;
55 convertToBase(tmp2);
56 return f(tmp1.value(), tmp2.value());
57 } else if (conversion_type == Money::AutomatedConversion) {
58 Money tmp = m2;
59 convertTo(tmp, m1.currency());
60 return f(m1.value(), tmp.value());
61 } else {
62 QL_FAIL("currency mismatch and no conversion specified");
63 }
64 }
65 }
66
68 const auto & conversion_type = Settings::instance().conversionType();
69 if (currency_ == m.currency_) {
70 value_ += m.value_;
71 } else if (conversion_type == Money::BaseCurrencyConversion) {
72 convertToBase(*this);
73 Money tmp = m;
74 convertToBase(tmp);
75 *this += tmp;
76 } else if (conversion_type == Money::AutomatedConversion) {
77 Money tmp = m;
78 convertTo(tmp, currency_);
79 *this += tmp;
80 } else {
81 QL_FAIL("currency mismatch and no conversion specified");
82 }
83 return *this;
84 }
85
87 return *this += (-m);
88 }
89
90 Decimal operator/(const Money& m1, const Money& m2) {
91 return apply<Decimal>(
92 m1, m2,
93 [](const Real x, const Real y) { return x / y; });
94 }
95
96 bool operator==(const Money& m1, const Money& m2) {
97 return apply<bool>(
98 m1, m2,
99 [](const Real x, const Real y) { return x == y; });
100 }
101
102 bool operator<(const Money& m1, const Money& m2) {
103 return apply<bool>(
104 m1, m2,
105 [](const Real x, const Real y) { return x < y; });
106 }
107
108 bool operator<=(const Money& m1, const Money& m2) {
109 return apply<bool>(
110 m1, m2,
111 [](const Real x, const Real y) { return x <= y; });
112 }
113
114 bool close(const Money& m1, const Money& m2, Size n) {
115 return apply<bool>(
116 m1, m2,
117 [n](const Real x, const Real y) { return close(x, y, n); });
118 }
119
120 bool close_enough(const Money& m1, const Money& m2, Size n) {
121 return apply<bool>(
122 m1, m2,
123 [n](const Real x, const Real y) { return close_enough(x, y, n); });
124 }
125
126 std::ostream& operator<<(std::ostream& out, const Money& m) {
127 return out << m.rounded().value() << " " << m.currency().code();
128 }
129
131 {
132 return conversionType_;
133 }
134
136 {
137 return conversionType_;
138 }
139
141 {
142 return baseCurrency_;
143 }
144
146 {
147 return baseCurrency_;
148 }
149
152 return *this;
153 }
154
155 Money::BaseCurrencyProxy::operator Currency() const {
157 }
158
161 return *this;
162 }
163
164 Money::ConversionTypeProxy::operator Money::ConversionType() const {
166 }
167
168}
Currency specification
Definition: currency.hpp:36
const std::string & code() const
ISO 4217 three-letter code, e.g, "USD".
Definition: currency.hpp:178
ExchangeRate lookup(const Currency &source, const Currency &target, Date date=Date(), ExchangeRate::Type type=ExchangeRate::Derived) const
const Money::ConversionType & conversionType() const
Definition: money.cpp:130
Money::ConversionType conversionType_
Definition: money.hpp:115
const Currency & baseCurrency() const
Definition: money.cpp:140
amount of cash
Definition: money.hpp:38
Decimal value_
Definition: money.hpp:84
Money & operator+=(const Money &)
Definition: money.cpp:67
Money rounded() const
Definition: money.hpp:181
const Currency & currency() const
Definition: money.hpp:173
Decimal value() const
Definition: money.hpp:177
Money & operator-=(const Money &)
Definition: money.cpp:86
Currency currency_
Definition: money.hpp:85
@ BaseCurrencyConversion
Definition: money.hpp:73
@ AutomatedConversion
Definition: money.hpp:76
static ExchangeRateManager & instance()
access to the unique instance
Definition: singleton.hpp:104
floating-point comparisons
const DefaultType & t
#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
exchange-rate repository
detail::percent_holder rate(Rate)
output rates and spreads as percentages
QL_REAL Real
real number
Definition: types.hpp:50
Real Decimal
decimal number
Definition: types.hpp:54
std::size_t Size
size of a container
Definition: types.hpp:58
cash amount in a given currency
Array apply(const Array &x, const ext::function< Real(Real)> &f)
Definition: any.hpp:35
bool operator==(const Currency &c1, const Currency &c2)
Definition: currency.hpp:231
bool operator<(const Quantity &m1, const Quantity &m2)
Definition: quantity.cpp:125
std::ostream & operator<<(std::ostream &out, GFunctionFactory::YieldCurveModel type)
bool close(const Quantity &m1, const Quantity &m2, Size n)
Definition: quantity.cpp:163
bool operator<=(const Quantity &m1, const Quantity &m2)
Definition: quantity.cpp:144
bool close_enough(const Quantity &m1, const Quantity &m2, Size n)
Definition: quantity.cpp:182
Real operator/(const Quantity &m1, const Quantity &m2)
Definition: quantity.cpp:86
BaseCurrencyProxy & operator=(const Currency &)
Definition: money.cpp:150
ConversionTypeProxy & operator=(Money::ConversionType)
Definition: money.cpp:159