QuantLib financial calculations - ExchangeRateManager basic components of the class

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 Moneycorresponding configuration type can be automatically converted currency exchange rates in algebra.

Money Currency Converter class configuration

MoneyCurrency Converter class static configuration function setConversionTypeimplemented 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 Moneyto call a static function class setBaseCurrencyconfiguration of the base currency.

ExchangeRateManager

ExchangeRateManagerIs a monomer (the Singleton), generally do not directly explicit instance, it is necessary by calling the static function instanceto obtain a unique ExchangeRateManagerexample.

ExchangeRateManager.instance()

function

ExchangeRateManager There are three member functions.

  • add(ex, start_date, end_date): To ExchangeRateManageradd an instance of ExchangeRatean object exis valid starting time of the exchange rate is start_date(the default value Date.minDate()), the expiration of the exchange rate of the time end_date(the default value Date.maxDate()).
  • lookup(source, target, date, type): Returns an exchange rate target, the source currency source, target currency target, date date(defaults to the current date), type type(default is ExchangeRate.Derived). ExchangeRateManagerExamples 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 case typemust be ExchangeRate.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.

Guess you like

Origin www.cnblogs.com/xuruilong100/p/10990585.html
Recommended