Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

btc-hedge-lab

An open, educational playground for BTC hedging strategies — delta hedging, basis trades, funding capture, protective collars. Vectorized pandas, plottable equity curves, minimal dependencies.

Stars Forks Last Commit Python License PRs Welcome

By @shakeebshaan · MIT · Python 3.11+


Why this exists

Most trading bot repos focus on predicting price. This one focuses on managing risk on a position you already have. If you hold BTC and want to sleep at night, you need to understand hedging. This lab lets you simulate hedging constructs against historical BTC data, plot equity curves, and understand the cost / benefit tradeoff of each.

Not financial advice. Educational only. All strategies assume you understand what you're doing and use paper money first.

Features

  • 📊 Five hedging constructs, each with its own module
  • 🔬 Vectorized pandas — no Python loops, fast on years of minute data
  • 📈 Plot equity curves for naked, hedged, and delta of the two
  • 💸 Realistic cost model — fees, funding, slippage
  • 🧪 Regime split — backtest on bull / bear / chop separately

Hedging strategies included

Construct What it does Cost source Use when
delta_hedge Short perp against spot → flat delta Funding (when negative) Holding structural spot, want fiat-pegged
basis_trade Spot + short dated future Basis convergence Capture annualized basis
funding_capture Neutral spot–perp, collect funding Slippage, occasional inversion Funding consistently positive
protective_put Spot + long OTM put Premium paid Tail protection needed
collar Spot + long put + short call Usually ~zero cost Structural long, bounded upside acceptable

Quickstart

git clone https://github.com/shakeebshaan/btc-hedge-lab.git
cd btc-hedge-lab
pip install -r requirements.txt

# simulate a 50/50 delta hedge on 2022-2024 BTC data
python -m hedge_lab.run --strategy delta_hedge --start 2022-01-01 --end 2024-12-31

Output:

naked buy-and-hold   PnL: +142.3%   MaxDD: -76.8%   Sharpe: 0.81
50% delta-hedged     PnL:  +58.4%   MaxDD: -24.1%   Sharpe: 1.24

Project layout

hedge_lab/
├── data.py              # load BTC spot + perp data
├── costs.py             # fee, funding, slippage models
├── strategies/
│   ├── delta_hedge.py
│   ├── basis_trade.py
│   ├── funding_capture.py
│   ├── protective_put.py
│   └── collar.py
├── metrics.py           # Sharpe, Sortino, DD, Calmar
├── plot.py              # matplotlib equity + PnL attribution
└── run.py               # CLI entry point
examples/
├── 01_naked_vs_hedged.ipynb
├── 02_funding_capture_2024.ipynb
└── 03_collar_on_long_hodl.ipynb
data/
└── README.md            # where to drop your BTC OHLCV + funding rate CSVs

Example: delta hedge

from hedge_lab.data import load_btc
from hedge_lab.strategies.delta_hedge import DeltaHedge
from hedge_lab.costs import RealisticCosts

btc = load_btc("data/btc_1h_2022_2024.csv")
strategy = DeltaHedge(hedge_ratio=0.5, rebalance="daily")
result = strategy.run(btc, costs=RealisticCosts())

result.plot()  # shows naked buy-and-hold vs hedged equity curves
print(result.summary())

Contributing

PRs welcome. Each new strategy goes in hedge_lab/strategies/<name>.py and exports a class inheriting from HedgeStrategy. Must include:

  1. Unit tests with synthetic data
  2. A docstring explaining the strategy's edge and failure modes
  3. An example notebook in examples/

License

MIT — see LICENSE.

See also

Disclaimer

This is educational software. Running these strategies on live capital is your responsibility. Author is not liable for losses. No backtest is a guarantee of forward results. Every strategy can blow up in a regime it hasn't seen.

About

Open educational playground for BTC hedging strategies — delta hedging, basis trades, funding capture, protective collars.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages