In this post, I’ll look at the implementation of market quotes and take a break from the series on the new finite-difference framework. But before that, a couple of announcements.
First, save the date for the next QuantLib User Meetings; it will be in Düsseldorf on the 7th and 8th of December. I’ll give you more details as they come my way.
Second: remember when I told you that Quaternion announced at the QuantLib user meeting in London that they were going to release their risk engine as open-source code? They just did. The project has a web site at www.opensourcerisk.org, the code is on GitHub, and the goal is to create a global standard for risk management. They’re brilliant people, so they might succeed.
There are at least two possibilities to model quoted values. One is to model quotes as a sequence of static values, each with an associated timestamp, with the current value being the latest; the other is to model the current value as a quoted value that changes dynamically.
Both views are useful; and in fact, both were implemented in the
library. The first model corresponds to the
which I won’t describe in detail here; it is basically a map between
dates and values, with methods to retrieve values at given dates and
to iterate on the existing values, and it was never really used in
other parts of the library. The second resulted in the
class, shown in the following listing.
Its interface is slim enough. The class inherits from the
Observable class, so that it can notify its dependent objects
when its value change. It declares the
isValid method, that
tells whether or not the quote contains a valid value (as opposed to,
say, no value, or maybe an expired value) and the
which returns the current value.
These two methods are enough to provide the needed behavior. Any other object whose behavior or value depends on market values (for example, the bootstrap helpers in this post) can store handles to the corresponding quotes and register with them as an observer. From that point onwards, it will be able to access the current values at any time.
The library defines a number of quotes—that is, of implementations
Quote interface. Some of them return values which are
derived from others; for instance,
option prices into implied-volatility values. Others adapt other
ForwardValueQuote returns forward index fixings as the
underlying term structures change, while
returns the latest value in a time series.
At this time, only one implementation is an genuine source of external
values; that would be the
SimpleQuote class, shown in
the next listing.
It is “simple” in the sense that it doesn’t implement any particular
data-feed interface: new values are set manually by calling the
appropriate method. The latest value (possibly equal to
Null<Real>() to indicate no value) is stored in a data member. The
Quote interface is implemented by having the
value method return
the stored value, and the
isValid method checking whether it’s null.
The method used to feed new values is
setValue; it takes the new
value, notifies its observers if it differs from the latest stored
one, and returns the increment between the old and new values. (The
choice to return the latest increment is kind of unusual; the
idiomatic choice in C and C++ would be to return the old value.)
I’ll conclude this post with a few short notes. The first is that
the type of the quoted values is constrained to
Real. This has
not been a limitation so far, and besides, it’s now too late to define
Quote as a class template; so it’s unlikely that this will ever
The second is that the original idea was that the
interface would act as an adapter to actual data feeds, with different
implementations calling the different API and allowing QuantLib to use
them in a uniform way. So far, however, nobody provided such
implementations; the closer we got was to use data feeds in Excel and
set their values to instances of
The last (and a bit longer) note is that the interface of
SimpleQuote might be modified in future to allow more advanced
uses. When setting new values to a group of related quotes (say, the
quotes interest rates used for bootstrapping a curve) it would be
better to only trigger a single notification after all values are set,
instead of having each quote send a notification when it’s updated.
This behavior would be both faster, since chains of notifications turn
out to be quite the time sink, and safer, since no observer would risk
to recalculate after only a subset of the quotes are updated. The
change (namely, an additional
silent parameter to
setValue that would mute notifications when equal to
true) has already been implemented in a fork of the library,
and could be added to QuantLib too.