The common member functions are as follows:
name() : returns a string, the name of the currency;
code() : Returns a string, the ISO4217 code of the currency, usually three uppercase English letters;
numericCode() : returns an integer, the number corresponding to the ISO4217 code of the currency;
symbol() : Returns a string, which is a symbol often used to represent the currency in the real world. The dollar is “$” and the yen is “￥”. It should be noted that this function may return Unicode, which may cause the program to fail in Python;
fractionSymbol() : Returns a string, which is a symbol often used to represent the smallest unit of the currency in the real world. Like symbol() , this function may return Unicode, which may cause the program to fail in Python;
fractionsPerUnit() : Returns an integer, the multiple of a unit of currency relative to the smallest unit of the currency, usually 100.
format() : Returns a string, a “format string” used to format the print result.
empty() : Returns a Boolean value. If the object is instantiated by a derived class, it returns True ; if the object is instantiated by Currency , it returns False . After all, the currency information in the base class object is “empty.”
rounding() : Returns a Rounding object, which is the rounding rule of the currency. By default, no rounding is performed.
To make algebraic calculations of currencies in QuantLib, you need to transform the Currency object into a Money object.
cur = ql.EURCurrency() money1 = ql.Money(cur, 100) money2 = ql.Money(100, cur)
Money is not usually constructed explicitly, but is generated by multiplying a floating-point number with a Currency object:
money3 = 100 * cur money3.value()
The class in QuantLib that describes exchange rate information between currencies is ExchangeRate , and every two currencies in the Currency system can generate an ExchangeRate object.
ExchangeRate constructor is very fixed and accepts three parameters: ExchangeRate(source, target, rate)
a Currency object representing the source currency
a Currency object representing the target currency
A floating point number that represents the exchange rate for “source to target”
Common member functions are as follows:
returns the Currency object, which is the source currency;
returns the Currency object, which is the target currency;
returns a floating point number, ie the exchange rate;
returns the built-in integer constant,
equal to 0, indicating that the exchange rate is constructed directly through the constructor;
equal to 1, indicating that the exchange rate is constructed indirectly through other exchange rate objects;
amount is a Money object that converts amount into other currencies of equal value;
- chain(r1, r2)
r1 and r2 are ExchangeRate objects. The currency involved must form a triangular relationship. This function will return an ExchangeRate object to complete the missing side of the triangle relationship.
usd = ql.USDCurrency() eur = ql.EURCurrency() usdToeur = ql.ExchangeRate(eur, usd, 1.14) m_usd = 5 * usd m_eur = 4.39 * eur print( 'Converting from USD: ', m_usd, ' = ', usdToeur.exchange(m_usd)) print( 'Converting from EUR: ', m_eur, ' = ', usdToeur.exchange(m_eur)) print(usdToeur.source()) print(usdToeur.target()) print(usdToeur.rate())