This project provides a suite of Python-based tools for backtesting and analyzing a delta-neutral funding rate arbitrage strategy for the "HYPE" cryptocurrency on the Hyperliquid exchange.
The core of this project is a trading strategy that aims to generate profit from cryptocurrency funding rates while neutralizing exposure to price volatility. It accomplishes this by simultaneously shorting perpetual futures contracts and buying the underlying spot asset.
The project is structured into four main stages:
- Data Discovery: Utility scripts to scan for available data on the exchange.
- Data Fetching: Scripts to download historical and live data from the Hyperliquid API.
- Backtesting: A simulation engine to test the trading strategy on historical data.
- Reporting & Analysis: Scripts to generate financial reports and visualizations from the backtest results.
The delta-neutral strategy is designed to be market-neutral.
- Entry Condition: A trade is initiated when the perpetual futures price is higher than the spot price, and the funding rate is positive and above a certain threshold.
- Exit Conditions: A position is closed if the stop-loss is triggered, if the premium between the perpetual and spot price collapses, or if the funding rate drops below the entry threshold.
These scripts are used to check for data availability on Hyperliquid before fetching.
find_first_candle.py: Finds the earliest available candle data for "HYPE".scan_candles_range.py: Scans a date range to identify periods with available candle data.funding_data_range_check.py: Checks the available date range for funding rate data.
These scripts collect data from the Hyperliquid API.
fetch_funding_data.py: Fetches historical funding rate data and saves it tohype_funding_rates_1min.csv.fetch_candles.py: Fetches historical candle data for a specific one-hour window.fetch_price_data.py: Fetches live price data for monitoring.fetch.py: A general-purpose script for fetching historical candle data.
The core logic for simulating the trading strategy resides in these scripts.
end.py: The main backtesting engine. It reads a combined data file (data (1).csv), simulates the strategy, and producestrades.csvas output.endi.py: A more detailed, interactive version of the backtester with extensive analysis and plotting capabilities. It also readsdata (1).csv.
IMPORTANT: The backtesting engine relies on a pre-processed file named data (1).csv, which is not generated by any script in this repository. This file must be created manually and should contain time-aligned spot prices, perpetual prices, and funding rates.
These scripts are used to analyze the results of the backtest.
generate_report.py: Readstrades.csvand generates a detailed backtest report in markdown format.main.py&funding_analysis.py: These are identical scripts that perform statistical analysis on 15-minute funding rate data fromhype_funding_rates_15min.csv.resample_funding_data.py: Resamples 15-minute funding data into 24-hour intervals and generates a plot.
- Prepare the Data: Create the
data (1).csvfile withspot_open,perp_open, andfunding_fundingRatecolumns. - Run the Backtest:
This will generate the
python end.py
trades.csvfile. - Generate the Report:
This will create a
python generate_report.py
report.mdfile with a detailed analysis of the backtest results.
MIT License