Derive historical open interest and funding rate
Derive is a decentralised options and perpetual futures exchange built on its own OP Stack L2 chain (Derive Chain, chain ID 957). This notebook visualises historical open interest and funding rate data for BTC and ETH perpetual contracts.
Open interest measures the total outstanding perpetual positions (in base currency units, e.g. BTC or ETH). It is read from on-chain state via Multicall3.
Funding rate is the hourly rate paid between longs and shorts to keep the perpetual price anchored to the index price. It is fetched from Derive’s REST API.
For full details on the Derive integration, data sources, API quirks and scan scripts, see README-derive.md.
Populating the DuckDB database
This notebook reads from a local DuckDB database at ~/.tradingstrategy/derive/funding-rates.duckdb. You must populate it before running the notebook by executing the two scan scripts:
1. Scan funding rates (hourly snapshots from Derive REST API, ~2-3 min for full history):
poetry run python scripts/derive/scan-funding-rates.py
2. Scan open interest (daily on-chain snapshots via Multicall3, ~20 min for full backfill):
poetry run python scripts/derive/scan-open-interest.py
Both scripts are incremental — subsequent runs only fetch new data since the last sync. You can limit the sync to specific instruments with INSTRUMENTS=ETH-PERP,BTC-PERP or control the database path with DB_PATH. See README-derive.md for all environment variables and performance benchmarks.
Running this notebook
Run from the command line:
poetry run jupyter execute docs/source/tutorials/derive-open-interest-funding-rate.ipynb --inplace --timeout=900
Or open in Visual Studio Code / JupyterLab.
[1]:
# Set up static PNG chart output
from eth_defi.research.notebook import setup_charting_and_output, OutputMode, set_large_plotly_chart_font
setup_charting_and_output(OutputMode.static, image_format="png", width=1500, height=800, increase_font_size=False)
set_large_plotly_chart_font()
import datetime
import pandas as pd
import plotly.graph_objects as go
from plotly.subplots import make_subplots
from eth_defi.derive.historical import DeriveFundingRateDatabase