Hyperliquid OHLCV Fetch
Download OHLCV candlestick data from Hyperliquid's API, handling their unique candle format and perpetual contract symbols.
Hyperliquid Market Data Acquisition Framework
This notebook provides a structured protocol for interacting with the Hyperliquid REST API to retrieve historical OHLCV (Open, High, Low, Close, Volume) candle data. The framework facilitates programmatic data extraction, transformation, and initial validation, which are essential steps for quantitative analysis.
# Install necessary libraries
# The `requests` library is required for making HTTP requests to the Hyperliquid REST API.
!pip install requests
# Import required libraries
# `pandas` is used for data manipulation and creating DataFrames.
import pandas as pd
# `requests` is used for sending HTTP requests.
import requests
# `datetime`, `timezone`, and `timedelta` are used for handling time-related operations.
from datetime import datetime, timezone, timedeltaRequirement already satisfied: requests in /usr/local/lib/python3.12/dist-packages (2.32.4) Requirement already satisfied: charset_normalizer<4,>=2 in /usr/local/lib/python3.12/dist-packages (from requests) (3.4.7) Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.12/dist-packages (from requests) (3.13) Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.12/dist-packages (from requests) (2.5.0) Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.12/dist-packages (from requests) (2026.4.22)
The initial section of the notebook focuses on setting up the necessary environment and importing the required libraries. This ensures that all dependencies are met before proceeding with data interaction.
Dependency Installation Purpose
- Purpose: To install external Python packages required for the notebook's functionality.
- Code Description: The
!pip install requestscommand executes a shell command within the notebook environment to install therequestslibrary. This library simplifies HTTP communication, which is crucial for interacting with web APIs. - Input: None (installs a package).
- Output: Console output indicating the installation status of the
requestslibrary. - Importance: Establishing HTTP connectivity is foundational for fetching data from the Hyperliquid API.
Library Import Purpose
- Purpose: To make essential Python libraries available for use in subsequent code blocks.
- Code Description: This block imports
pandasfor data structuring and manipulation,requestsfor API calls, and specific modules fromdatetimefor precise time handling. Each import statement loads a module or a specific component into the current namespace, allowing its functions and classes to be called directly. - Input: None (loads libraries).
- Output: None (makes libraries available).
- Importance: These libraries provide the fundamental tools for data processing, API interaction, and temporal management, which are central to the data acquisition process.
# Define the API endpoint for Hyperliquid
# All public data queries are directed to this single REST endpoint.
API_ENDPOINT = "https://api.hyperliquid.xyz/info"
# Define the cryptocurrency symbol for data retrieval
# This represents the asset ticker without a quote-currency suffix (e.g., BTC, ETH).
SYMBOL = "BTC"
# Define the start datetime for data collection
# `datetime.now(timezone.utc)` gets the current UTC time.
# `.replace(second=0, microsecond=0)` normalizes the time to the nearest minute.
# `- timedelta(minutes=2)` sets the start time to 2 minutes before the current minute.
START_DATETIME = datetime.now(timezone.utc).replace(second=0, microsecond=0) - timedelta(minutes=30)
# Define the end datetime for data collection
# Sets the end time to 1 minute after the start time, defining a 1-minute window.
END_DATETIME = datetime.now(timezone.utc).replace(second=0, microsecond=0) # Current minuteThis section configures the primary parameters required for API interaction. Public market data from Hyperliquid does not require an API key, simplifying the authentication process.
Configuration Purpose
- Purpose: To set up essential variables that control the API requests, including the API endpoint, the target cryptocurrency symbol, and the time range for data retrieval.
- Code Description: Variables are initialized to store the Hyperliquid public API endpoint, the desired trading pair symbol (e.g., BTC), and the start and end timestamps for data fetching. The time parameters are calculated relative to the current UTC time, ensuring consistent data querying.
- Input: None (hardcoded values and
datetimecalculations). - Output: Initialized global variables (
API_ENDPOINT,SYMBOL,START_DATETIME,END_DATETIME) that will be used by the data extraction function. - Importance: Proper configuration of these parameters is critical for sending accurate and targeted requests to the Hyperliquid API, ensuring that the correct data is retrieved for the specified asset and time period.
Data Extraction Function: fetch_ohlc
This section defines the fetch_ohlc function, which encapsulates the entire pipeline for retrieving OHLCV candle data from the Hyperliquid API. The function handles timestamp normalization, constructs the API request, processes the response, and transforms the raw data into a standardized Pandas DataFrame.
def fetch_ohlc(symbol, start_datetime, end_datetime):
"""
Fetches OHLCV (Open, High, Low, Close, Volume) candle data for a specified symbol and time range.
Parameters:
symbol (str): The cryptocurrency ticker symbol (e.g., "BTC").
start_datetime (datetime): The UTC datetime object representing the start of the data range.
end_datetime (datetime): The UTC datetime object representing the end of the data range.
Returns:
pandas.DataFrame: A DataFrame containing the OHLCV data with columns for datetime, open, high, low, close, and volume.
Returns an empty DataFrame if no data is found or an error occurs.
"""
# STEP 1: Convert datetime objects to Unix timestamps (milliseconds)
# Unix timestamp = number of seconds since January 1, 1970 (UTC)
# The Hyperliquid API requires timestamps in milliseconds, so we multiply by 1000
start_ms = int(start_datetime.timestamp() * 1000)
end_ms = int(end_datetime.timestamp() * 1000)
# STEP 2: Build the request payload (data to send to the API)
# The API expects a JSON object with a 'type' field and a 'req' field
# 'type': "candleSnapshot" tells the API we want historical candle data
# 'req': contains the specific parameters for our request
payload = {
"type": "candleSnapshot",
"req": {
"coin": symbol.upper(), # Convert to uppercase (e.g., "btc" -> "BTC") as required by the API
"interval": "1m", # Request 1-minute interval candles (options: "1m", "5m", "15m", "1h", etc.)
"startTime": start_ms, # Start time in milliseconds
"endTime": end_ms # End time in milliseconds
}
}
try:
# STEP 3: Send the request to the Hyperliquid API
# requests.post() sends an HTTP POST request to the specified URL
# - API_ENDPOINT: the URL of the Hyperliquid API
# - headers: tells the API we're sending JSON data
# - json: automatically converts our payload dictionary to a JSON string
# - timeout: maximum seconds to wait for a response (prevents hanging indefinitely)
response = requests.post(
API_ENDPOINT,
headers={"Content-Type": "application/json"},
json=payload,
timeout=30,
)
# STEP 4: Check if the request was successful
# raise_for_status() will raise an exception if the status code is 4xx or 5xx (client or server error)
# 200 status code = success
response.raise_for_status()
# STEP 5: Parse the JSON response into a Python list of dictionaries
# Each dictionary in the list represents one minute of candle data
candles = response.json()
# STEP 6: Check if we actually received any data
if not candles:
print(f"No candle data received for {symbol} from {start_datetime} to {end_datetime}.")
# Return an empty DataFrame with the correct column structure
return pd.DataFrame(columns=["datetime", "open", "high", "low", "close", "volume"])
# STEP 7: Extract data from ALL candles (not just the first one!)
# We'll loop through each candle in the response and create a row for it
# This ensures we get ALL minutes of data, not just the first minute
data_rows = [] # Empty list to store each candle's data
for candle in candles: # Loop through every candle returned by the API
# Extract the 6 values we need from each candle dictionary
# Keys in the API response:
# 't' = timestamp (milliseconds)
# 'o' = open price
# 'h' = high price
# 'l' = low price
# 'c' = close price
# 'v' = volume traded
row = [
pd.to_datetime(int(candle["t"]), unit='ms', utc=True), # Convert millisecond timestamp to readable datetime
float(candle["o"]), # Convert string/float to Python float
float(candle["h"]),
float(candle["l"]),
float(candle["c"]),
float(candle["v"])
]
data_rows.append(row) # Add this candle's data to our list
# STEP 8: Create a Pandas DataFrame from all the data rows
# pd.DataFrame converts our list of rows into a table with the specified column names
df = pd.DataFrame(data_rows, columns=["datetime", "open", "high", "low", "close", "volume"])
# STEP 9: Explicitly set correct data types for each column
# This ensures consistency and prevents errors in later calculations
# - datetime64[ns, UTC]: timezone-aware datetime with nanosecond precision
# - float64: decimal numbers (for price and volume calculations)
df = df.astype({
"datetime": "datetime64[ns, UTC]",
"open": "float64",
"high": "float64",
"low": "float64",
"close": "float64",
"volume": "float64"
})
# STEP 10: Return the complete DataFrame with all candles
# The columns are returned in a logical order for easy viewing
return df[["datetime", "open", "high", "low", "close", "volume"]]
# STEP 11: Error handling - catch specific types of request errors
# These help debug what went wrong if the API call fails
except requests.exceptions.HTTPError as http_err:
# HTTP errors: 404 (Not Found), 401 (Unauthorized), 500 (Server Error), etc.
print(f"HTTP error occurred: {http_err}")
except requests.exceptions.ConnectionError as conn_err:
# Connection errors: network issues, API server down, wrong URL, etc.
print(f"Connection error occurred: {conn_err}")
except requests.exceptions.Timeout as timeout_err:
# Timeout errors: API took too long to respond (more than 30 seconds)
print(f"Request timed out: {timeout_err}")
except requests.exceptions.RequestException as req_err:
# Catch-all for any other request-related exceptions
print(f"An error occurred during the request: {req_err}")
except Exception as e:
# Catch any unexpected errors (e.g., data parsing issues, memory errors)
print(f"An unexpected error occurred: {e}")
# If we reach this point, an error occurred above
# Return an empty DataFrame so the rest of the code doesn't break
return pd.DataFrame(columns=["datetime", "open", "high", "low", "close", "volume"])fetch_ohlc Function Logic
- Purpose: To retrieve and standardize OHLCV data from the Hyperliquid API for a specified time window.
- Inputs:
symbol(string): The ticker symbol for the cryptocurrency (e.g., "BTC").start_datetime(datetime object): The beginning of the time range (UTC).end_datetime(datetime object): The end of the time range (UTC).
- Outputs: A Pandas DataFrame containing the
datetime,open,high,low,close, andvolumefor the requested candle. - Logic Description:
- Timestamp Conversion: Converts the input
datetimeobjects (start_datetime,end_datetime) into Unix timestamps in milliseconds, as required by the Hyperliquid API. - Payload Construction: Creates a JSON payload with the
candleSnapshottype andreqparameters including thecoin(symbol),interval(set to "1m" for 1-minute candles),startTime, andendTime. - API Request: Executes an HTTP POST request to the
API_ENDPOINTwith the constructed JSON payload. It includes error handling for various request exceptions (HTTP, connection, timeout, general request). - Response Processing: Parses the JSON response from the API. It checks if
candlesdata is received and prints a message if not. - Data Extraction: Extracts the relevant OHLCV data points from the first candle in the
candleslist. These values are converted to appropriate Python types. - DataFrame Creation: A Pandas DataFrame is created from the extracted single candle data. Column names are explicitly defined.
- Type Casting: Ensures that all columns in the DataFrame have the correct data types (
datetime64[ns, UTC]for datetime andfloat64for numerical values) for precision and consistency. - Return Value: Returns the well-structured and typed DataFrame. In case of any errors during the process, an empty DataFrame is returned.
- Timestamp Conversion: Converts the input
Data Ingestion Protocol
This section describes the process of ingesting the fetched candlestick data. The fetch_ohlc function is executed with the configured parameters, and the resulting dataset is loaded into memory as a Pandas DataFrame for further analysis.
# Execute the data fetching function
# The `fetch_ohlc` function is called with the defined SYMBOL, START_DATETIME, and END_DATETIME.
# The returned DataFrame, containing the OHLCV data, is assigned to the variable `df`.
df = fetch_ohlc(SYMBOL, START_DATETIME, END_DATETIME)Data Ingestion Purpose
- Purpose: To retrieve the OHLCV data using the previously defined function and configuration.
- Code Description: This code block directly invokes the
fetch_ohlcfunction, passing theSYMBOL,START_DATETIME, andEND_DATETIMEvariables that were configured earlier. The output of this function, which is a Pandas DataFrame, is then stored in thedfvariable. - Input: The global configuration variables (
SYMBOL,START_DATETIME,END_DATETIME). - Output: A Pandas DataFrame named
dfpopulated with the fetched OHLCV data. - Importance: This step is the crucial execution phase that transforms the API request definition into actual data, making it available for subsequent processing and analysis within the notebook.
Data Inspection and Integrity Verification
This section focuses on validating the integrity and structure of the ingested data. Procedures include auditing the DataFrame's schema and verifying non-null values. The head() and info() methods are used to quickly inspect the data and confirm that it aligns with expected financial data models and precision requirements.
# Display the first few rows of the fetched OHLCV data
# `display(df.head())` shows the header and the initial entries of the DataFrame.
print("--- Fetched OHLCV Data ---")
display(df.head())
# Print a summary of the DataFrame's schema and data types
# `df.info()` provides a concise summary, including the index dtype, column dtypes, non-null values, and memory usage.
print("\n--- Schema Summary ---")
df.info()--- Fetched OHLCV Data ---
| datetime | open | high | low | close | volume | |
|---|---|---|---|---|---|---|
| 0 | 2026-05-20 07:49:00+00:00 | 77248.0 | 77248.0 | 77244.0 | 77245.0 | 6.76894 |
| 1 | 2026-05-20 07:50:00+00:00 | 77245.0 | 77245.0 | 77231.0 | 77231.0 | 6.68526 |
| 2 | 2026-05-20 07:51:00+00:00 | 77231.0 | 77244.0 | 77207.0 | 77244.0 | 5.59201 |
| 3 | 2026-05-20 07:52:00+00:00 | 77244.0 | 77244.0 | 77243.0 | 77243.0 | 1.94026 |
| 4 | 2026-05-20 07:53:00+00:00 | 77241.0 | 77242.0 | 77233.0 | 77242.0 | 0.39704 |
--- Schema Summary --- <class 'pandas.core.frame.DataFrame'> RangeIndex: 31 entries, 0 to 30 Data columns (total 6 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 datetime 31 non-null datetime64[ns, UTC] 1 open 31 non-null float64 2 high 31 non-null float64 3 low 31 non-null float64 4 close 31 non-null float64 5 volume 31 non-null float64 dtypes: datetime64[ns, UTC](1), float64(5) memory usage: 1.6 KB
Data Visualization
This section provides a visual representation of the fetched OHLCV data. Visualization aids in understanding data trends and patterns, offering immediate insights into market behavior. For a single candle, a basic plot of the closing price against time is presented to demonstrate the visualization capability of the framework.
# Import the necessary library for plotting
# `matplotlib.pyplot` is a widely used library for creating static, animated, and interactive visualizations in Python.
import matplotlib.pyplot as plt
# Purpose: To visualize the closing price of the fetched OHLCV data.
# What the code does: It generates a line plot of the 'close' price over 'datetime'.
# Input: The Pandas DataFrame `df` containing OHLCV data.
# Output: A line plot displayed within the notebook.
# Why this step is important: Visualizing data helps in quick comprehension of market movements and data validity.
# Check if the DataFrame is not empty before attempting to plot
if not df.empty:
# Create a figure and a set of subplots for the plot
fig, ax = plt.subplots(figsize=(10, 6))
# Plot the 'close' price against the 'datetime' column
ax.plot(df['datetime'], df['close'], marker='o', linestyle='-', color='blue')
# Set the title of the plot
ax.set_title(f'Closing Price for {SYMBOL} (1-Minute Candle)', fontsize=14)
# Set the label for the x-axis
ax.set_xlabel('Datetime (UTC)', fontsize=12)
# Set the label for the y-axis
ax.set_ylabel('Close Price', fontsize=12)
# Enable grid for better readability of data points
ax.grid(True, linestyle='--', alpha=0.7)
# Rotate x-axis labels for better visibility if they overlap
plt.xticks(rotation=45, ha='right')
# Adjust layout to prevent labels from being cut off
plt.tight_layout()
# Display the plot
plt.show()
else:
# Inform the user if no data is available for plotting
print("No data available to plot.")Conclusion
This notebook successfully demonstrates a robust framework for acquiring 1-minute OHLCV candle data from the Hyperliquid REST API. It includes dependency installation, parameter configuration, a dedicated data extraction function with error handling, and initial data inspection and visualization. The structured approach ensures data integrity and prepares the data for further quantitative analysis or algorithmic trading strategy development.
Data Visualization
This section provides a visual representation of the fetched OHLCV data. Visualization aids in understanding data trends and patterns, offering immediate insights into market behavior. For a single candle, a basic plot of the closing price against time is presented to demonstrate the visualization capability of the framework.
# Import the necessary library for plotting
# `matplotlib.pyplot` is a widely used library for creating static, animated, and interactive visualizations in Python.
import matplotlib.pyplot as plt
# Purpose: To visualize the closing price of the fetched OHLCV data.
# What the code does: It generates a line plot of the 'close' price over 'datetime'.
# Input: The Pandas DataFrame `df` containing OHLCV data.
# Output: A line plot displayed within the notebook.
# Why this step is important: Visualizing data helps in quick comprehension of market movements and data validity.
# Check if the DataFrame is not empty before attempting to plot
if not df.empty:
# Create a figure and a set of subplots for the plot
fig, ax = plt.subplots(figsize=(10, 6))
# Plot the 'close' price against the 'datetime' column
ax.plot(df['datetime'], df['close'], marker='o', linestyle='-', color='blue')
# Set the title of the plot
ax.set_title(f'Closing Price for {SYMBOL} (1-Minute Candle)', fontsize=14)
# Set the label for the x-axis
ax.set_xlabel('Datetime (UTC)', fontsize=12)
# Set the label for the y-axis
ax.set_ylabel('Close Price', fontsize=12)
# Enable grid for better readability of data points
ax.grid(True, linestyle='--', alpha=0.7)
# Rotate x-axis labels for better visibility if they overlap
plt.xticks(rotation=45, ha='right')
# Adjust layout to prevent labels from being cut off
plt.tight_layout()
# Display the plot
plt.show()
else:
# Inform the user if no data is available for plotting
print("No data available to plot.")Conclusion
This notebook successfully demonstrates a robust framework for acquiring 1-minute OHLCV candle data from the Hyperliquid REST API. It includes dependency installation, parameter configuration, a dedicated data extraction function with error handling, and initial data inspection and visualization. The structured approach ensures data integrity and prepares the data for further quantitative analysis or algorithmic trading strategy development.