Options Pricing in Python

Apologies for that lack of updates, I have been busy working on an options pricing library in Python. I have so far managed to create my first Path Dependent Asian option pricer and it is finally providing me with the correct results. It is not quite ready for implementation as it needs optimisation to run at an acceptable speed on my server.

Although C++ is the predominant language for options pricing, I decided to see how I would fare producing an all-Python based library. Not only would this improve my Python skills, but it would allow a straightforward integration of the library into the site.

The library, which I am tentatively naming PyQuant, is very simple at the moment. It consists of two major components, a set of closed-form solutions for Vanilla calls/puts and the Digitals and a basic Monte Carlo pricer which prices the Double-Digitals and Power Options. In time I will hunt down or derive closed-form solutions for all options that I am able to, but right now I am enjoying the development of the Monte Carlo solver.

The closed form solutions rely on two statistical functions - the Normal Probability Density Function and the Cumulative Normal Distribution Function. A numerical approximation to the CNDF can be found in [1]. In fact, many of the closed form solutions are given in that text, which is where I obtained them from. With the NPDF an CNDF I was able to calculate solutions for the Vanilla Calls and Puts, as well as the common Greeks: Delta, Gamma, Rho, Vega and Theta. I'm still working on closed form solutions for the Digital options.

The Monte Carlo based solutions work differently. There is one module that contains all of the pay-off objects for each type of option - Call, Put, Forward, Digital Call etc. Another module stores option objects. In the case of the Vanilla option, an expiry time and a pay-off are required. The strike is encapsulated in the pay-off object, which ensures code resuability for both pay-offs and options. The final module includes Monte Carlo engines which calculate a large range of stock path evolutions (based on Geometric Brownian Motion) and use these to calculate an expected pay-off of the option. The pay-off is discounted at the risk-free rate and this provides the price.

At this stage it is computationally expensive to re-run the Monte Carlo pricer when changing an input. There are a few ways to optimise this. The first is to make use of SciPy, a Python scientific library. It includes plenty of optimisation strategies - take a look at this article on Performance Python, its performance compared to C++ may just surprise you. The second is to actually write a dedicated C++ library which can be called from Python. However this is contrary to the idea a Python derivatives pricing library.

[1] - Joshi, M., The Concepts and Practice of Mathematical Finance, Cambridge University Press

comments powered by Disqus

Just Getting Started with Quantitative Trading?

3 Reasons to Subscribe to the QuantStart Email List:

No Thanks, I'll Pass For Now

1. Quant Trading Lessons

You'll get instant access to a free 10-part email course packed with hints and tips to help you get started in quantitative trading!

2. All The Latest Content

Every week I'll send you a wrap of all activity on QuantStart so you'll never miss a post again.

3. No Spam

Real, actionable quant trading tips with no nonsense.