A good library design requires a separation of the functionality into modules with an appropriate API. The size of the modules is determined by the application. While for a simple trading application a good API might contain everything from fitting the stochastic processes to pricing an option under a single function, this is completely insufficient for assessing model risk in risk management. In the following, we look at a payoff description in VBA and ThetaML.

# Simple Case: Excel UDF

For many financial applications, a markt-to-market of all financial products is required. This makes a good case for creation of a library, which should concentrate on a clearly separated task. That means that a pricing library just needs to provide prices :-). In order to avoid bugs, the pricing library should provide a stateless function which takes the market parameters and return the option price. An object oriented approach like QuantlibXL which uses state-full object orientation is clearly not useful because it is is hard to maintain. A better example of Excel pricing UDFs are the pricing functions in FinAnSu.

Both, QuantlibXL and FinAnSu define the financial product in a general purpose programming language (C++ resp. C#). Consequently, these UDFs are hard to extend and hard to maintain: a lot of numerical details have to be learned in order to change a pricing function. In order to find out what we can do better, we need to look at whats meant by maintainable.

Maintaining a pricing library means

- Extension of the functionality (e.g. new stochastic models, new derivatives types/payoffs, new applications like greeks, Potential Future Exposure)
- Change of existing functionality (elimination of bugs, improvement of models, introduction of new features like financial transaction tax)
- Understanding, extending and changing the functionality by others, e.g. other quants, controlling and risk management. Even in several years from now!

# Good API Design

A great summary for Java API design is available here. The main features are true for any programming language:

- Easy to learn
- Easy to use, even w/o doc
- Hard to misuse
- Easy to read and maintain code that uses it
- Sufficiently powerful
- Easy to extend
- Appropriate to audience

# Payoff Description Language in VBA

A simplistic payoff description can easily be realized in VBA. A great example for a payoff description in VBA is provided by Kumar in Wilmott magazine. It consists of a parser framework written in VBA which interprets payoff descriptions like the following:

Vanilla Call | (S<=110)*0+(S>110)*(S-110) |

Vanilla Put | (S>=110)*0+(S<110)*(110-S) |

(Source: Wilmott Article: Monte Carlo in Esperanto)

This payoff description is limited to options with a single exercise time and the stochastic process implemented in the parser. But, this kind of description is easy to extend to other payoffs, some Greek computations (Vega, Delta) and even to some Barrier type options.

# ThetaML as Payoff Description Language

The language for financial modeling ThetaML also allows a programming style where ThetaML serves as payoff description:

http://www.thetaris.com/wiki/Payoff_Description_Language

Wilmott Arricle: Computer Aided Finance

The following example shows the definition of a vanilla call using ThetaML. In ThetaML, the payoff description is basically a definition of the potential exercise value “ExcerciseValue_CUR” during model time until maturity. Note that the time is stepped by “Theta” command. The exercise value is zero until maturity, where it is defined as “max(K-S,0)” in currency “CUR”. Just after maturity (another “Theta @dt” time step), the exercise value is zero again:

model EuropeanPut implements IExercisableOption import S "Stock price" import CUR "Currency value" import K "Strike" import T "Maturity" export ExcerciseValue_CUR "Excercise value" ExcerciseValue_CUR = 0 Theta T ExcerciseValue_CUR = max(K-S,0) * CUR % rigtht after Maturity, exercise value is 0 again Theta @dt ExcerciseValue_CUR = 0 end

Similarly, we can define an American Put option which is exercisable at any time by stepping with infinite small timesteps “Theta @dt”:

model AmericanPut implements IExercisableOption import S "Stock price" import CUR "Currency value" import K "Strike" import T "Maturity" export ExcerciseValue_CUR "Excercise value" ExcerciseValue_CUR = max(K-S,0) * CUR loop inf Theta @dt if @time <= roundt(T) ExcerciseValue_CUR = max(K-S,0) * CUR else ExcerciseValue_CUR = 0 end end end

For the actual pricing task, we can use the Theta Suite’s Orchestrator and combine the payoff description with a stochastic process model and an exercise model. In contrast to the VBA payoff description, the stochastic process and the exercise model are written in the domain specific language, too. This way, all components of the pricing are modular (Separation of Concerns) and thus more maintainable. Also, the code required for a pricing function is much shorter than in e.g. FinAnSu or QuantlibXL. This generally means fewer bugs.

# Conclusion

A payoff description can be an integral part of a pricing library. This way, the pricing library is easy to extend and maintainable. A payoff description is easy to understand without the need to learn fundamentals like fast matrix vector multiplication.

ThetaML goes much further then just payoff description. It describes the payoff, the stochastic process, exercise models and how they interact. In a later post, we will look at this interaction more closely.

## édouard

Thank you for the hints on how-to design an option pricing library and reminding the essentiality of the option payoff function of options.