PyQuant News 🐍
PyQuant News 🐍

@pyquantnews

17 Tweets 4 reads Feb 09, 2023
I love Python.
But it's slow.
So I built an options pricing library in C and call it from Python.
Now I can trade like a professional (and you can too).
Here's a dead-simple way you can 45x the performance of your Python code with C (code included):
Python is based on C but it's much slower.
Python has to figure out the type of data assigned to variables when it runs.
C is compiled first so it already knows.
This helps C run up to 45x faster than pure Python.
And there's great news...
It's simple to use C with Python!
I'm going to show you how to do 3 things:
• Build an implied volatility solver in C
• Build the Black-Scholes model in C
• Use Python to call the C code
Then I'll build implied volatility charts.
Put on your seatbelt, here we go!
To start, we need to create a function that gives us the cumulative normal distribution of a variable.
Part of the fun (?) of C is writing all this from scratch.
Then we build C code for the Black-Scholes call option.
This looks pretty familiar to Python.
I've imported sqrt, log, and exp from the math library which is part of the standard C library.
To find implied volatility, we need something called a root finder.
A root finder puts values into a function until the result matches a target.
We try volatility inputs until the Black-Scholes value equals the market price.
This is the volatility "implied" by the market.
The root finder I chose follows Brent's method.
This code was adapted from the book Numerical Recipes in C.
Yes.
It's scary.
If you're interested in learning how to link and compile the C code, drop me a reply!
Now, let's move on to the Python code.
We import a Python module called ctypes to use the C code.
You can see that I point to the shared object file and then load it using CDLL.
Then we wrap the C-based Black-Scholes function in Python.
Using Numpy's vectorize function lets me pass in an array of input values and get an array of output values.
(This comes in handy in a minute.)
We do the same thing for implied volatility.
So what did we accomplish?
You just built an implied volatility solver in C and called it from Python!
Here's the famous implied volatility smile.
The smile shows how implied volatility varies by strike price.
The volatility term structure shows how implied volatility varies by expiration.
Implied volatility smile and term structure are how professional options traders make money.
Instead of Black-Scholes, they might use a proprietary model.
If their model shows a difference from the market, it's an opportunity to trade.
This is what makes C useful.:
Python is too slow to react to market movements in real-time.
The Black-Scholes model is simple and runs pretty fast.
Other models are complicated and run slow.
Building the underlying models in C helps improve performance by up to 45x.
For more on options:
Get the 46-Page Guide to Pricing Options and Implied Volatility.
Here's why:
• Compute Black-Scholes, the greeks, and implied volatility
• Includes a Jupyter Notebook with the code
• How to use Python to analyze the results
pyquantnews.gumroad.com
There's a lot here (especially if you're new to C)!
Your best bet is to retweet the top tweet so you can come back to it later.
To make it easy, click the link below:

Loading suggestions...