This Julia package provides an implementation of automatic options hedging using automatic differentiation for obtaining the Greeks. The package allows users to easily create and backtest complicated hedging strategies for a portfolio of European Options and thier underlying asset.
To install the package, simply run the following command in the Julia REPL:
using Pkg
Pkg.add(url="https://github.com/kgeoffrey/AutoHedge.jl")
To use the automatic options hedging functionality provided by this package, you can import the package into your Julia session:
using AutoHedge
Say you have a portfolio of 10 call options and you want to make your portfolio delta neutral. One way of achieving this is to buy or sell specific quantities of underlying stock - this is referred to as Delta Hedging1. First create a portfolio, define the asset to be hedged (10 call options), and the array of hedging instruments (the underlying stock) and hedging strategies (delta in this case):
call_option = CallOption(100, 130., 5., 0.01, 0.2, 0.)
stock = UnderlyingStock()
portfolio = Portfolio(call_option, 10, [stock], ["delta"])
CallOption takes the arguments S, K, T, r, v, q - which stand for spot price, strike price, passage of time, risk free rate, volatility and continuously compounded dividend yield. The S, T and q can be random upon initiation, as they will be updated during the simulation. Furthermore it is important that the number of hedging strategies is the same as the number of hedging instruments, this will be explained later below. Next we simulate the price of the underlying stock:
using Plots
stock_price = randomwalk(100, 500, 1)
plot(stock_price, xlabel="Time", ylabel="Spot Price")
Now that we have the evolution of the stock price of the underlying we can run a backtest of the hedging strategy and obtain the borrowing to fund hedging purchases, holdings of hedging instruments and tracking error over time. For the backtest, we also need to define the rebalancing frequency:
rebalancing_frequency = 5 # we rebalance every 5 periods
borrowing, volumes, tracking_errors = backtest(stock_price, rebalancing_frequency, portfolio)
As simple as that! Next we plot the results:
plot(borrowing, xlabel="Time", ylabel = "Cash Borrowing")
plot(volumes, xlabel="Time", ylabel = "Volume", labels=permutedims(string.(typeof.(portfolio.hedging_instruments))))
plot(tracking_errors, xlabel="Time", ylabel = "Tracking Error (Value of Portfolio)")
For this example you could experiment with rebalancing frequencies to reduce the tracking error even further. In the next example you see how you can hedge other greeks
In this example we will construct a portfolio that has neutral Delta, Theta, Charm and Speed. We have 100 call options in our portfolio and need 3 additional hedging instruments for our strategy:
call_option = CallOption(100, 130., 5., 0.01, 0.2, 0.)
stock = UnderlyingStock()
f1 = CallOption(100., 130., 5., 0.01, 0.2, 0.)
f2 = PutOption(100., 120., 5., 0.01, 0.2, 0.)
portfolio = Portfolio(call_option, 10, [stock, f1, f2], ["delta", "theta", "vega"])
We use the same simulated stock price as before and create the backtest:
rebalancing_frequency = 5
borrowing, volumes, tracking_errors = backtest(stock_price, rebalancing_frequency, portfolio)
As you can see, depending on how many greeks you want to hedge, borrowing and asset volume can explode. Furthermore you should be wary of the Moneyness of your hedging instruments with regard to the simulated stock price, especially when you select many Greeks to hedge this can lead to numerical errors.
AutoHedge uses ForwardDiff.jl to get the Greeks of European Options. Next a system of linear equations is solved to obtain the cash borrowings, and hedging instrument volumes. The simple Delta-Hedge shown in Example #1 needs to fulfill 2 requirements, where B is the borrowing and
and it needs to be Delta Neutral as per definition:
By rearranging, we can write the equations in matrix form, so they can be solved easily:
where,
Similarly, we can add other Greeks, but for A to be invertible and square, we cannot have an overdetermined system (more equations to balance than variables). Thus for each new greek we want to hedge we need to add a unique hedging instrument to the portfolio. We take Example #2, where we hedge portfolio delta, theta and vega:
Contributions to this package are welcome! If you find a bug or have a feature request, please create an issue on the GitHub repository. If you would like to contribute code, please fork the repository and create a pull request.
This package is licensed under the MIT License.