Welcome back.

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.

Follow me on Twitter if you want to be notified of new posts, or add me to your circles, or subscribe via RSS: the buttons for that are in the footer. Also, make sure to check my Training page.

### Market quotes

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 TimeSeries class, 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 Quote 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 value method, 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 of the Quote interface. Some of them return values which are derived from others; for instance, ImpliedStdDevQuote turns option prices into implied-volatility values. Others adapt other objects; ForwardValueQuote returns forward index fixings as the underlying term structures change, while LastFixingQuote 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 change.

The second is that the original idea was that the Quote 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 SimpleQuote.

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.