table of Contents
If not specifically discussed herein, the text of the program are python3 code.
QuantLib financial calculations - ExchangeRateManager basic components of the class
Loading QuantLib:
import QuantLib as ql
print(ql.__version__)
1.15
Outline
QuantLib managing information exchange between currencies category is ExchangeRateManager
, with the Money
corresponding configuration type can be automatically converted currency exchange rates in algebra.
Money
Currency Converter class configuration
Money
Currency Converter class static configuration function setConversionType
implemented available configuration represented by three built integers, respectively:
Configuration Type | meaning |
---|---|
NoConversion |
No conversion |
BaseCurrencyConversion |
Unified converted into a base currency (base currency) |
AutomatedConversion |
Conversion to calculate a first monetary expression occurring |
If you are using BaseCurrencyConversion
, you also need Money
to call a static function class setBaseCurrency
configuration of the base currency.
ExchangeRateManager
ExchangeRateManager
Is a monomer (the Singleton), generally do not directly explicit instance, it is necessary by calling the static function instance
to obtain a unique ExchangeRateManager
example.
ExchangeRateManager.instance()
function
ExchangeRateManager
There are three member functions.
add(ex, start_date, end_date)
: ToExchangeRateManager
add an instance ofExchangeRate
an objectex
is valid starting time of the exchange rate isstart_date
(the default valueDate.minDate()
), the expiration of the exchange rate of the timeend_date
(the default valueDate.maxDate()
).lookup(source, target, date, type)
: Returns an exchange rate target, the source currencysource
, target currencytarget
, datedate
(defaults to the current date), typetype
(default isExchangeRate.Derived
).ExchangeRateManager
Examples will first want to find currency exchange rate recorded at all; if not, then try to return calculate the exchange rate on the shortest path according to the path of the exchange rate in series (in this casetype
must beExchangeRate.Derived
).clear()
: Empty the record exchange rate.
Example,
ql.Money.setConversionType(ql.Money.AutomatedConversion)
usd = ql.USDCurrency()
cny = ql.CNYCurrency()
eur = ql.EURCurrency()
usdXcny = ql.ExchangeRate(usd, cny, 6.912)
usdXeur = ql.ExchangeRate(usd, eur, 0.834)
ql.ExchangeRateManager.instance().add(usdXcny)
ql.ExchangeRateManager.instance().add(
usdXeur,
ql.Date(1, ql.May, 2019),
ql.Date(3, ql.May, 2019))
m_eur = 100 * eur
m_cny = 150 * cny
ql.Settings.instance().evaluationDate = ql.Date(2, ql.May, 2019)
print(m_eur, " + ", m_cny, " = ", m_eur + m_cny)
ql.Settings.instance().evaluationDate = ql.Date(4, ql.May, 2019)
print(m_eur, " + ", m_cny, " = ", m_eur + m_cny)
EUR 100.00 + Y 150.00 = EUR 118.10
RuntimeError: no conversion available from CNY to EUR for May 4th, 2019
The results are automatically rounded according to the type of currency. Note: The exchange rate between the euro and the yuan indirectly by the dollar, when the valuation date ( evaluationDate
) the day when the exchange rate is not available, the system will complain.