QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
money.cpp
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>
22#include <ql/currencies/exchangeratemanager.hpp>
23#include <ql/math/comparison.hpp>
24
25#include <boost/format.hpp>
26
27namespace QuantLib {
28
29 namespace {
30
31 void convertTo(Money& m, const Currency& target) {
32 if (m.currency() != target) {
33 ExchangeRate rate =
35 target);
36 m = rate.exchange(m).rounded();
37 }
38 }
39
40 void convertToBase(Money& m) {
41 const auto & base_currency =
43 QL_REQUIRE(!base_currency.empty(), "no base currency set");
44 convertTo(m, base_currency);
45 }
46
47 }
48
50 const auto & conversion_type = Settings::instance().conversionType();
51 if (currency_ == m.currency_) {
52 value_ += m.value_;
53 } else if (conversion_type == Money::BaseCurrencyConversion) {
54 convertToBase(*this);
55 Money tmp = m;
56 convertToBase(tmp);
57 *this += tmp;
58 } else if (conversion_type == Money::AutomatedConversion) {
59 Money tmp = m;
60 convertTo(tmp, currency_);
61 *this += tmp;
62 } else {
63 QL_FAIL("currency mismatch and no conversion specified");
64 }
65 return *this;
66 }
67
69 const auto & conversion_type = Settings::instance().conversionType();
70 if (currency_ == m.currency_) {
71 value_ -= m.value_;
72 } else if (conversion_type == Money::BaseCurrencyConversion) {
73 convertToBase(*this);
74 Money tmp = m;
75 convertToBase(tmp);
76 *this -= tmp;
77 } else if (conversion_type == Money::AutomatedConversion) {
78 Money tmp = m;
79 convertTo(tmp, currency_);
80 *this -= tmp;
81 } else {
82 QL_FAIL("currency mismatch and no conversion specified");
83 }
84 return *this;
85 }
86
87 Decimal operator/(const Money& m1, const Money& m2) {
88 const auto & conversion_type =
90 if (m1.currency() == m2.currency()) {
91 return m1.value()/m2.value();
92 } else if (conversion_type == Money::BaseCurrencyConversion) {
93 Money tmp1 = m1;
94 convertToBase(tmp1);
95 Money tmp2 = m2;
96 convertToBase(tmp2);
97 return tmp1/tmp2;
98 } else if (conversion_type == Money::AutomatedConversion) {
99 Money tmp = m2;
100 convertTo(tmp, m1.currency());
101 return m1/tmp;
102 } else {
103 QL_FAIL("currency mismatch and no conversion specified");
104 }
105 }
106
107 bool operator==(const Money& m1, const Money& m2) {
108 const auto & conversion_type =
110 if (m1.currency() == m2.currency()) {
111 return m1.value() == m2.value();
112 } else if (conversion_type == Money::BaseCurrencyConversion) {
113 Money tmp1 = m1;
114 convertToBase(tmp1);
115 Money tmp2 = m2;
116 convertToBase(tmp2);
117 return tmp1 == tmp2;
118 } else if (conversion_type == Money::AutomatedConversion) {
119 Money tmp = m2;
120 convertTo(tmp, m1.currency());
121 return m1 == tmp;
122 } else {
123 QL_FAIL("currency mismatch and no conversion specified");
124 }
125 }
126
127 bool operator<(const Money& m1, const Money& m2) {
128 const auto & conversion_type =
130 if (m1.currency() == m2.currency()) {
131 return m1.value() < m2.value();
132 } else if (conversion_type == Money::BaseCurrencyConversion) {
133 Money tmp1 = m1;
134 convertToBase(tmp1);
135 Money tmp2 = m2;
136 convertToBase(tmp2);
137 return tmp1 < tmp2;
138 } else if (conversion_type == Money::AutomatedConversion) {
139 Money tmp = m2;
140 convertTo(tmp, m1.currency());
141 return m1 < tmp;
142 } else {
143 QL_FAIL("currency mismatch and no conversion specified");
144 }
145 }
146
147 bool operator<=(const Money& m1, const Money& m2) {
148 const auto & conversion_type =
150 if (m1.currency() == m2.currency()) {
151 return m1.value() <= m2.value();
152 } else if (conversion_type == Money::BaseCurrencyConversion) {
153 Money tmp1 = m1;
154 convertToBase(tmp1);
155 Money tmp2 = m2;
156 convertToBase(tmp2);
157 return tmp1 <= tmp2;
158 } else if (conversion_type == Money::AutomatedConversion) {
159 Money tmp = m2;
160 convertTo(tmp, m1.currency());
161 return m1 <= tmp;
162 } else {
163 QL_FAIL("currency mismatch and no conversion specified");
164 }
165 }
166
167 bool close(const Money& m1, const Money& m2, Size n) {
168 const auto & conversion_type =
170 if (m1.currency() == m2.currency()) {
171 return close(m1.value(),m2.value(),n);
172 } else if (conversion_type == Money::BaseCurrencyConversion) {
173 Money tmp1 = m1;
174 convertToBase(tmp1);
175 Money tmp2 = m2;
176 convertToBase(tmp2);
177 return close(tmp1,tmp2,n);
178 } else if (conversion_type == Money::AutomatedConversion) {
179 Money tmp = m2;
180 convertTo(tmp, m1.currency());
181 return close(m1,tmp,n);
182 } else {
183 QL_FAIL("currency mismatch and no conversion specified");
184 }
185 }
186
187 bool close_enough(const Money& m1, const Money& m2, Size n) {
188 const auto & conversion_type =
190 if (m1.currency() == m2.currency()) {
191 return close_enough(m1.value(),m2.value(),n);
192 } else if (conversion_type == Money::BaseCurrencyConversion) {
193 Money tmp1 = m1;
194 convertToBase(tmp1);
195 Money tmp2 = m2;
196 convertToBase(tmp2);
197 return close_enough(tmp1,tmp2,n);
198 } else if (conversion_type == Money::AutomatedConversion) {
199 Money tmp = m2;
200 convertTo(tmp, m1.currency());
201 return close_enough(m1,tmp,n);
202 } else {
203 QL_FAIL("currency mismatch and no conversion specified");
204 }
205 }
206
207
208 std::ostream& operator<<(std::ostream& out, const Money& m) {
209 boost::format fmt(m.currency().format());
210 fmt.exceptions(boost::io::all_error_bits ^
211 boost::io::too_many_args_bit);
212 return out << fmt % m.rounded().value()
213 % m.currency().code()
214 % m.currency().symbol();
215 }
216
217
219 {
220 return conversionType_;
221 }
222
224 {
225 return conversionType_;
226 }
227
229 {
230 return baseCurrency_;
231 }
232
234 {
235 return baseCurrency_;
236 }
237
240 return *this;
241 }
242
243 Money::BaseCurrencyProxy::operator Currency() const {
245 }
246
249 return *this;
250 }
251
252 Money::ConversionTypeProxy::operator Money::ConversionType() const {
254 }
255
256}
Currency specification
Definition: currency.hpp:36
const std::string & code() const
ISO 4217 three-letter code, e.g, "USD".
Definition: currency.hpp:142
std::string format() const
output format
Definition: currency.hpp:172
const std::string & symbol() const
symbol, e.g, "$"
Definition: currency.hpp:152
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:218
Money::ConversionType conversionType_
Definition: money.hpp:115
const Currency & baseCurrency() const
Definition: money.cpp:228
amount of cash
Definition: money.hpp:38
Decimal value_
Definition: money.hpp:84
Money & operator+=(const Money &)
Definition: money.cpp:49
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:68
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
detail::percent_holder rate(Rate)
output rates and spreads as percentages
Real Decimal
decimal number
Definition: types.hpp:54
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
bool operator==(const Currency &c1, const Currency &c2)
Definition: currency.hpp:191
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:238
ConversionTypeProxy & operator=(Money::ConversionType)
Definition: money.cpp:247