Challenged by a young CEO, I wrote a terse CTA (Commodity Trading Advisor) investment strategy in just 10 lines of code. This project discusses the somewhat quirky background of this code fragment and explores how Convex Programming opens the door to deeper insights into the strategy.
- 💰 Implement a hedge fund strategy in just 10 lines of code
- 📊 Boost the Sharpe Ratio through optimization techniques
- 📉 Control both Kurtosis and trading costs
- Python 3.11+
- A POSIX shell. macOS and Linux have one by default.
🪟 Windows users: The
maketargets are written for a POSIX shell and rely on tools likemkdir -p,printf,curl, and[ … ]. They will not run undercmd.exeor PowerShell — you'll see errors such asprocess_begin: CreateProcess(NULL, # Ensure the … folder exists, …) failed.Run the commands below from WSL (recommended) or Git Bash, both of which provide a POSIX shell.
# Clone the repository
git clone https://github.com/tschm/cs.git
cd cs
# Install dependencies
make install# Build the Jupyter Book
make bookDeveloper notes live under docs/development/:
- Parameter optimization — how the Optuna
search in
optimize.pyreuses the notebook signals (the notebooks-are-the-source-of-truth contract). - Sharpe-ratio pins — the pinned regression baselines and how to update them.
- Test-layout parity — why tests mirror the
notebooks (not a
src/tree) and the repo-local gate that enforces it.
Each ExperimentN.py notebook is a self-contained CTA strategy. To search its
parameter space for the configuration that maximizes the portfolio Sharpe ratio,
run the Optuna driver in book/marimo/notebooks/optimize.py:
# Optimize a single experiment (1–5) with an explicit trial budget
python book/marimo/notebooks/optimize.py --experiment 3 --trials 200
# Optimize all five, each with its per-experiment default budget
python book/marimo/notebooks/optimize.py --experiment all
# Reproducible run with Optuna's per-trial logging (short flags)
python book/marimo/notebooks/optimize.py -e 1 -n 50 --seed 7 --verboseEach run prints the baseline Sharpe (notebook defaults), the best Sharpe found, the winning parameters, and the improvement. The driver never re-implements a strategy — it imports each notebook's live signal function, so the notebooks stay the single source of truth. See OPTIMIZATION.md for the full contract.
The headline Sharpe of every experiment is pinned in
tests/expected_sharpe.py and checked to a tight
1e-6 tolerance by both the notebook and optimizer regression tests. A
dependency bump that legitimately shifts the numbers will fail those tests; the
assertion message prints the expected pin next to the freshly computed value:
# See old vs. new values for every experiment
uv run pytest tests/test_notebook_sharpe.py tests/test_optimize.py -vEyeball the deltas, and only if they are acceptable copy the new values into
EXPECTED_SHARPE_RATIOS, then re-run the suite so both test modules agree. The
full review checklist (and when not to re-pin) is in
SHARPE_PINS.md.
# Start Marimo
make marimoWe do not guarantee or take responsibility for the accuracy, completeness, reliability and usefulness of any information.
Dr. Thomas Schmelzer created all material in his personal capacity. The views stated are his own and do not necessarily represent the views of neither ADIA nor Stanford University. The opinion expressed is based on the prevailing market trends and is subject to change.
This project relies on the Rhiza system for its underlying infrastructure and tooling.
This project is licensed under the MIT License - see the LICENSE file for details.