QuantLib: Exchange Rates

In the previous article I spoke about Money. QuantLib supports calculations with Money of different currencies. In order to compute an expression with different currencies, exchange rates have to be defined. This is handled by the ExchangeRate class. This class links two currencies with an exchange rate, so that conversion can be performed.

Creating an ExchangeRate object couldn’t be simpler. The constructor takes two currencies and a decimal for the exchange rate.

ExchangeRate(const Currency& source,
             const Currency& target, 
             Decimal rate);

The first currency is the source and the second currency is the target. In practice defining an exchange rate would look like this

Currency usd = USDCurrency();
Currency gbp = GBPCurrency();
ExchangeRate usdXgbp(usd, gbp, 0.6289);

The rate is defined in such a way that it defines the conversion from the source to the target. In the above example one USD would be equivalent to GBP 0.6289.

Once an exchange rate is defined it can be used to convert money from one currency to another. This is done using the method exchange

Money exchange(Money m);

The conversion works both ways and does not have to be from source to target.

Money m_usd = 1.30*usd;
Money m_gbp = 5.32*gbp;
std::cout << "Converting from USD: " 
  << m_usd << " = " << usdXgbp.exchange(m_usd)
  << std::endl;
std::cout << "Converting from GBP: "
  << m_gbp << " = " << usdXgbp.exchange(m_gbp)
  << std::endl;

This produces the output

Converting from USD: $ 1.30 = £ 0.82
Converting from GBP: £ 5.32 = $ 8.46

The ExchangeRate class also provides a few methods that allow you to access the properties of an instance of the class.

const Currency& source() const;
const Currency& target() const;
Type type() const;
Decimal rate() const;

These methods return the source currency, the target currency and the exchange rate which is stored internally. The type of the currency can be either ExchangeRate::Direct or ExchangeRate::Derived. Exchange rates created with the constructor above are always direct. Derived exchange rates are created by chaining two or more exchange rates together, as discussed in the next section.

Chaining Echange Rates

Two exchange rates can be chained together to produce a new exchange rate. This new exchange rate converts money in a two stage process, by first converting it to a common currency and then converting to the target currency. To create a chained exchange rate you simply have to call the chain method

static ExchangeRate chain(const ExchangeRate& r1,
                          const ExchangeRate& r2);

This will return an ExchangeRate object in which the two rates are chained together. In order for this method to be successful the two exchange rates have to have one common currency. It does not matter which of the currencies match, the two sources, the two targets or one source with one target. The method will find the common currency and create an ExchangeRate that converts between the other two currencies. If no common currency can be found, an exception is thrown.

Currency usd = USDCurrency;
Currency gbp = GBPCurrency;
Currency eur = EURCurrency;

ExchangeRate usdXgbp(usd, gbp, 0.6289);
ExchangeRate usdXeur(usd, eur, 0.7928);
ExchangeRate eurXgbp = ExchangeRate::chain(usdXeur,usdXgbp);

Money m_eur = 500.00 * eur;

std::cout << "Converting EUR to GBP: " << m_eur << " = " << eurXgbp.exchange(m_eur) << std::endl;

This code will produce the following output

Converting EUR to GBP: EUR 500.00 = £ 396.63