Wanting to streamline and automate actions relating to derivatives trading I sought out to find a calculator library I could wrap an API around to calculate various outcomes.
A derivative is a contract between two or more parties whose value is based on an agreed-upon underlying financial asset, index or security.
Sadly I found nothing suitable or simple, so I implemented my own called guant using the Black-Scholes pricing model and Newton-Raphson model.
Derivatives come in many forms and thus have many different associated formulas.
In this case we’ll be focusing on Options Contracts, so lets take a look at the related formulas.
- Black-Scholes pricing model
- Binomial options pricing model
- Trinomial options pricing model
- Bjerksund-Stensland Model
Black-Scholes is often used to price European options that can only be exercised on their expiry date, and without consideration to dividends.
The other 3 models take into account both of those factors and more to come up with a higher degree of accuracy when pricing American options.
After much reading on the topic I came across this quant stack exchange article explaining why Black-Scholes is still used as the benchmark, so we’ll implement this model.
Calculating implied volatility has it’s own considerations but we’ll address that specifically later.
Implementation of Black-Scholes
After watching Khan work through the formula we just need to transfer the following into code.
The core of our implementation comes out to these few functions alongside some other helpers for certain values such as time.
Implementation of Newton-Raphson
An important part of pricing options is knowing their Sigma or Implied Volatility(IV). We can calculate IV with the current option and underlying price. This is useful as it will allow us to nice solve for the option at various underlying prices and times. IV is not a constant, it is an approximation and as such liable to change. We can compensate for this by avoiding buying options at higher implied volatilities or too close to events such as earnings.
With this method we choose a starting value for Sigma and iteratively calculate using the option value and underlying price until it converges. This results in a high degree of accuracy.
There are more nuanced approaches we can use such as implementing a closed form solution or secant method and then iterating on that to achieve a higher degree of accuracy.
This youtuber had a pretty solid breakdown of using Newton’s Method so in the interest of being simple but effective we’ll implement the same way.
Pulling it all together
guant with the
go-finance library we can pull options contract values and current stock quotes to calculate options value for a given strike/expiry on a ticker.
We’ll use a Microsft Call Option with a Strike of $190 and an expiry of 2020-06-05.
The results from implementing
guant and running the calculation come out as follows:
For comparison here is the same options contract listed on Robinhood.
We’ll notice that our IV is incredibly close to Robinhoods IV, along with the value of the Call option.
It’s worth noting that these formulas yield approximations, the ultimate value of the contract is determined by the market. Hence the difference between the
Of course theres always ways to improve the results, one such easy addition may be making the time input more granular.
This has been a fun journey into the world of Quantitative finance and learning about some of the underlying mechanisms in derivatives.
Disclaimer for anyone who decides to use this, it is not guranteed at all to be reliable or accurate.