A Python application that collects real-time market data from Hyperliquid exchange via WebSocket connections. This tool subscribes to order book, trades, candles, and asset context data for multiple cryptocurrencies and stores the data in both SQLite databases and CSV files.
- Real-time Data Collection: Subscribe to multiple data streams simultaneously
- Multiple Data Types:
- Order book data (L2 depth)
- Trade data (executed trades)
- Candle data (mark prices)
- Asset context (oracle prices, funding rates, open interest)
- Dual Storage: Data stored in both SQLite databases and CSV files
- Multi-Asset Support: Track multiple cryptocurrencies simultaneously
- Automatic Reconnection: Handles connection drops with automatic reconnection
- Batch Processing: Efficient batch writes to database for better performance
- Error Handling: Robust error handling with configurable retry limits
- Python 3.7+
- Hyperliquid account with API access
- Required Python packages (see
requirements.txt
)
Before running the script, you need to create an API wallet and get your credentials from the Hyperliquid API Management Page.
-
Navigate to API Management:
- Go to https://app.hyperliquid.xyz/API
- Connect your main wallet (e.g., MetaMask) to Hyperliquid
-
Generate API Wallet:
- Enter a descriptive name for your API wallet (e.g., "MyTradingBot")
- Click "Generate" to create a new API wallet
- This generates a wallet address and private key
-
Authorize the API Wallet:
- Click "Authorize API Wallet"
- Sign the transaction with your main wallet
- Set validity period (up to 180 days maximum)
-
Securely Store Credentials:
- Copy the displayed private key
- Store it securely (encrypted file or password manager)
- Never share or commit this key to code
- Use your main wallet address as
account_address
(not the API wallet address) - Use the API wallet's private key as
secret_key
- The main wallet authorizes the API wallet for trading
-
Clone the repository:
git clone <your-repo-url> cd hyperliquid-realtime-data
-
Install dependencies:
pip install -r requirements.txt
-
Set up configuration:
- Copy
config.example.json
toconfig.json
- Edit
config.json
with your Hyperliquid credentials:{ "account_address": "your_main_wallet_address", "secret_key": "your_api_wallet_private_key", "is_mainnet": true }
- Copy
{
"account_address": "0x...",
"secret_key": "your_private_key_here",
"is_mainnet": true
}
Security Note: Never commit your config.json
file to version control. It's already included in .gitignore
.
You can also use environment variables instead of config.json:
export HYPERLIQUID_ACCOUNT_ADDRESS="your_wallet_address"
export HYPERLIQUID_SECRET_KEY="your_private_key"
export HYPERLIQUID_IS_MAINNET="true"
python realtime_data_HL.py
The script:
- Connects to the Hyperliquid WebSocket (wss://api.hyperliquid.xyz/ws or testnet equivalent).
- Validates coins and subscribes to their data streams.
- Stores data in data/{coin}/ folders as SQLite databases and CSV files.
- Data is only collected while the script is running
- When you stop the script (Ctrl+C), data collection stops
- CSV files are saved with data from the last session
- When you restart the script, CSV files are overwritten (not appended)
- Copy CSV files to a backup location before stopping the script
- Use the SQLite databases - they persist data between sessions
- Create regular backups of your
data/
folder
Example backup command:
# Before stopping the script, backup your data
cp -r data/ data_backup_$(date +%Y%m%d_%H%M%S)/
You can easily customize which coins to track by editing the coin list in realtime_data_HL.py
:
# In realtime_data_HL.py, line 376 (main function)
asyncio.run(subscribe_to_data(coins=["BTC", "ETH", "HYPE"]))
# Or in the function definition (line 75)
async def subscribe_to_data(coins=["BTC", "ETH", "HYPE"]):
Examples of custom coin lists:
# Track only major coins
coins=["BTC", "ETH"]
# Track specific meme coins
coins=["HYPE", "FARTCOIN", "kPEPE", "WIF"]
# Track all available coins (within API limits)
coins=["BTC", "ETH", "HYPE", "SOL", "FARTCOIN", "SUI", "SEI", "kPEPE", "WIF", "AAVE", "APT", "CRV", "MATIC", "LINK"]
The script automatically validates coins against the Hyperliquid API.
API Limitations: Be mindful of Hyperliquid's API rate limits and connection limits when adding many coins simultaneously.
All data is organized in a data/
folder with subfolders for each coin:
data/
├── ETH/
│ ├── ETH_main_market_data.db
│ ├── ETH_main_orderbook.csv
│ ├── ETH_main_trades.csv
│ ├── ETH_main_candles.csv
│ ├── ETH_main_asset_context.csv
│ └── ETH_main_orderbook_depth.csv
├── HYPE/
│ ├── HYPE_main_market_data.db
│ ├── HYPE_main_orderbook.csv
│ └── ...
└── ...
For each coin, a database file is created: data/{coin}/{coin}_main_market_data.db
Tables:
orderbook
: Best bid/ask prices and volumestrades
: Executed trades with price, volume, sidecandles
: Mark prices over timeasset_context
: Oracle prices, funding rates, open interestorderbook_depth
: Full order book depth (top 5 levels)
Important: SQLite databases persist data between script sessions, unlike CSV files which are overwritten on restart.
For each coin, CSV files are created in data/{coin}/
:
{coin}_main_orderbook.csv
{coin}_main_trades.csv
{coin}_main_candles.csv
{coin}_main_asset_context.csv
{coin}_main_orderbook_depth.csv
Note: CSV files are overwritten each time the script starts. To preserve historical data, backup these files before stopping the script.
timestamp
: Unix timestampbest_bid
: Best bid pricebid_volume
: Volume at best bidbest_ask
: Best ask priceask_volume
: Volume at best askmid_price
: Mid price ((best_bid + best_ask) / 2)
timestamp
: Unix timestampprice
: Trade pricevolume
: Trade volumeside
: "Buy" or "Sell"notional
: Price × Volume
timestamp
: Unix timestamporacle_price
: Oracle pricefunding_rate
: Current funding rateopen_interest
: Open interest
The current script collects data for perpetual assets using WebSocket channels (l2Book, trades, candle, activeAssetCtx). To collect spot market data:
Modify Subscriptions in realtime_data_2.py:
Replace l2Book with spotL2Book for spot order book data. Replace trades with spotTrades for spot trade data.
Note: candle and activeAssetCtx are not available for spot assets.
Update Coin Names:
Use spot pair names (e.g., PURR/USDC) from the spotMeta.universe field in the meta endpoint.
Example Modification (in subscribe_to_data function):
Update Asset ID Validation: Modify get_asset_id to check spotMeta.universe and return 10000 + index.
Note: Spot data storage can reuse the existing orderbook, trades, and orderbook_depth tables, but you may want to create separate tables (e.g., spot_orderbook) for clarity.
- Never commit credentials: The
config.json
file is gitignored - Use environment variables: Consider using environment variables instead of config files
- Restrict file permissions: Ensure config files have restricted permissions
- Use dedicated wallets: Consider using a dedicated wallet for API access
- Monitor usage: Regularly check your API usage and wallet activity
You can modify example_utils.py
to use environment variables:
import os
def load_config():
return {
"account_address": os.getenv("HYPERLIQUID_ACCOUNT_ADDRESS"),
"secret_key": os.getenv("HYPERLIQUID_SECRET_KEY"),
"is_mainnet": os.getenv("HYPERLIQUID_IS_MAINNET", "true").lower() == "true"
}
The application includes robust error handling:
- Connection drops: Automatic reconnection with 5-second delays
- Invalid coins: Skips invalid coins and continues with valid ones
- Data parsing errors: Logs errors and continues processing
- Rate limiting: Built-in delays between operations
The application provides real-time logging:
- Connection status
- Data reception confirmations
- Error messages
- Data processing statistics
-
"Asset not found" errors:
- Check coin names are correct
- Verify coins are available on Hyperliquid
- Check if using mainnet vs testnet
-
Connection issues:
- Verify internet connection
- Check Hyperliquid API status
- Ensure credentials are correct
-
Permission errors:
- Ensure write permissions in the directory
- Check if files are locked by other processes
The application provides detailed console output for debugging.
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
This software is for educational and research purposes. Use at your own risk. The authors are not responsible for any financial losses or damages resulting from the use of this software.
For issues and questions:
- Check the troubleshooting section
- Review the logs for error messages
- Open an issue on GitHub with detailed information
- Hyperliquid team for providing the API
- Python community for the excellent libraries used