Eleftherios Platis
EngineeringOpen source · 202205 / 06

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.

ContextOpen source · 2022
AreaEngineering
ToolsPython, GitHub, CI/CD

01 — Abstract

As part of a semester-long university open-source course (2022), I contributed to the OpenBB Terminal (formerly Gamestonk Terminal), a widely used open-source investment research platform written in Python. My main contribution was a new options-hedging menu, 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.

02 — The problem

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.

03 — What I built

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.

  • The maths: the investor picks a position to hedge (e.g. a short AAPL call) and two hedging options. The model builds a 2×2 matrix of the options' Gamma and Vega values and inverts it to find the weights that cancel both exposures. A least-squares solution handles singular matrices. It then works out the remaining Delta and offsets it with shares of the underlying, which leaves a Delta-, Gamma- and Vega-neutral portfolio.
  • The commands: pick (position to hedge), list (available strikes), add / rmv (manage hedging options), sop (show positions and neutral weights) and plot (payoff diagram).
  • The engineering: I followed the project's MVC structure (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.

04 — What changed

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.

Presentation

Delta, Gamma, Vega, Zero: Teaching a Trading Terminal to Hedge — slide 1 of 11
01 / 11

Links

View merged PR on GitHub↗Talk to me about this project↗
← PreviousCan We Predict a Breakthrough?Next →Generative AI in UI design