Delta, Gamma, Vega, Zero: Teaching a Trading Terminal to Hedge
How I built and merged an options-hedging feature into OpenBB Terminal, using linear algebra to bring a portfolio’s exposure to price, speed and volatility down to zero.
How I built and merged an options-hedging feature into OpenBB Terminal, using linear algebra to bring a portfolio’s exposure to price, speed and volatility down to zero.
hedge. It calculates the positions in options and underlying shares needed to neutralise a portfolio's exposure to three Black-Scholes Greeks: Delta, Gamma and Vega. The feature solves a small system of linear equations to find the hedge weights, with a least-squares fallback for cases where the matrix cannot be inverted. I built it within the project's MVC architecture, added a set of interactive terminal commands, checked the calculations by hand, and documented both the implementation and the underlying maths. The feature was merged into the main repository through pull request #1768, alongside a separate bug fix (#1733). The project also gave me first-hand experience of professional open-source development, including code review, collaborating with maintainers, and adapting to an established codebase's conventions.Options traders are exposed to more than the direction of the market. A position's value also shifts with how fast prices move (Gamma) and with changes in volatility (Vega). Neutralising all three exposures means finding the right mix of offsetting options and shares. That calculation is beyond what most retail investors can do quickly. OpenBB Terminal, an open-source alternative to expensive professional trading terminals, had options analytics but no way to compute a hedge.
A fellow contributor suggested a tool to calculate hedge weights. I took the idea further and built a dedicated hedge menu inside OpenBB's options module. It came to almost 900 lines of Python and documentation, and it builds on Roman Paolucci's Algorithmic Portfolio Hedging, which I credited in the PR.
pick (position to hedge), list (available strikes), add / rmv (manage hedging options), sop (show positions and neutral weights) and plot (payoff diagram).hedge_controller, hedge_model), handled the sign logic for long and short calls and puts, integrated the menu into the options workflow, and wrote the Hugo documentation. I also wrote a separate technical report that walks through the full calculation on a worked example.The feature was merged into the main OpenBB repository (PR #1768), along with a separate bug fix that corrected a misleading log message (PR #1733). Getting it there was a real collaboration. It took more than 250 messages with 6 contributors on Discord. A maintainer then added a view layer, combined it with another contributor's payoff-diagram menu, and updated the tests. The review process shaped how I write code today. I learned to use dictionaries over positional lists for readability, document complex functions properly, run linters (Pylint, Black) through pre-commit hooks before committing, and work within a large codebase's conventions instead of around them.
