Data·OHLCV Data Fetching·Beginner

Bybit OHLCV Fetch

Fetch and store OHLCV candlestick data from Bybit using their REST API, supporting linear and inverse perpetual contracts alongside spot markets across multiple timeframes and trading pairs.

data-engineeringdata-fetching

Bybit Market Data Acquisition Framework

This notebook establishes a standardized protocol for interfacing with the Bybit REST API to retrieve historical OHLCV candle data. It is structured to facilitate programmatic data extraction, transformation, and initial validation for quantitative analysis.

1. Dependency Management and Library Integration

Installation of the pybit library is required for RESTful interaction with the Bybit exchange. The following imports provide the necessary toolkit for data manipulation (pandas), temporal management (datetime), and API client connectivity (HTTP).

[ ]
!pip install pybit

import warnings
warnings.filterwarnings("ignore")
import pandas as pd
from datetime import datetime
from pybit.unified_trading import HTTP
import os
Requirement already satisfied: pybit in c:\users\neurog\appdata\roaming\python\python312\site-packages (5.10.0)
Requirement already satisfied: requests in d:\miniconda3\lib\site-packages (from pybit) (2.32.3)
Requirement already satisfied: websocket-client in c:\users\neurog\appdata\roaming\python\python312\site-packages (from pybit) (1.1.0)
Requirement already satisfied: pycryptodome in c:\users\neurog\appdata\roaming\python\python312\site-packages (from pybit) (3.21.0)
Requirement already satisfied: charset-normalizer<4,>=2 in c:\users\neurog\appdata\roaming\python\python312\site-packages (from requests->pybit) (2.0.12)
Requirement already satisfied: idna<4,>=2.5 in c:\users\neurog\appdata\roaming\python\python312\site-packages (from requests->pybit) (3.10)
Requirement already satisfied: urllib3<3,>=1.21.1 in c:\users\neurog\appdata\roaming\python\python312\site-packages (from requests->pybit) (2.0.7)
Requirement already satisfied: certifi>=2017.4.17 in c:\users\neurog\appdata\roaming\python\python312\site-packages (from requests->pybit) (2024.12.14)
WARNING: Ignoring invalid distribution ~andas (C:\Users\Neurog\AppData\Roaming\Python\Python312\site-packages)
WARNING: Ignoring invalid distribution ~andas (C:\Users\Neurog\AppData\Roaming\Python\Python312\site-packages)
WARNING: Ignoring invalid distribution ~andas (C:\Users\Neurog\AppData\Roaming\Python\Python312\site-packages)
WARNING: Ignoring invalid distribution ~andas (C:\Users\Neurog\AppData\Roaming\Python\Python312\site-packages)

Code Logic: Dependency Management

  • !pip install pybit: Installs the official Bybit SDK for RESTful API interaction.
  • import warnings; warnings.filterwarnings("ignore"): Suppresses runtime warnings to ensure clean standard output.
  • import pandas as pd: Imports the Pandas library with the standard alias for tabular data manipulation.
  • from datetime import datetime: Imports the datetime module for temporal object processing.
  • from pybit.unified_trading import HTTP: Imports the Unified Trading HTTP client for authenticated and public API requests.
[ ]
import os

API_KEY    = os.getenv("BYBIT_API_KEY")
API_SECRET = os.getenv("BYBIT_API_SECRET")

client = HTTP(api_key=API_KEY, api_secret=API_SECRET, testnet=False);

SYMBOL         = "BTCUSDT"
START_DATETIME = datetime.strptime("2024-01-01 00:00:00", "%Y-%m-%d %H:%M:%S")
END_DATETIME   = datetime.strptime("2024-01-02 00:00:00", "%Y-%m-%d %H:%M:%S")

Code Logic: Configuration

  • API_KEY / API_SECRET: Placeholders for cryptographic credentials required for signed API requests.
  • HTTP(api_key, api_secret, testnet=False): Instantiates the authenticated Bybit Unified Trading HTTP client targeting the live environment.
  • SYMBOL: Defines the specific trading pair ticker for the request.
  • datetime.strptime(...): Parses string literals into Python datetime objects using specific format codes.

3. Data Extraction and Transformation Logic

The fetch_ohlc function encapsulates the data pipeline: timestamp normalization, paginated API requests via the get_kline endpoint, schema mapping to a standardized DataFrame, and precision-safe type casting.

[ ]
def fetch_ohlc(symbol, start_datetime, end_datetime):
    start_ms = int(start_datetime.timestamp() * 1000)
    end_ms   = int(end_datetime.timestamp() * 1000)

    # Be aware of API limits (e.g., 1000 candles per request for Bybit kline).

    resp = client.get_kline(
        category="linear",
        symbol=symbol,
        interval=1,
        start=start_ms,
        end=end_ms,
        limit=1000, # Use maximum limit to fetch as much data as possible in one go
    )

    candles = resp["result"]["list"]

    if not candles:
        return pd.DataFrame()

    df = pd.DataFrame(
        candles,
        columns=["timestamp", "open", "high", "low", "close", "volume", "turnover"],
    )

    df[["timestamp"]]                         = df[["timestamp"]].astype("int64")
    df[["open","high","low","close","volume"]] = df[["open","high","low","close","volume"]].astype("float64")
    df = df.drop_duplicates("timestamp").sort_values("timestamp", ignore_index=True)

    return df[["timestamp", "open", "high", "low", "close", "volume"]]

Code Logic: Data Extraction Function

  • int(start_datetime.timestamp() * 1000): Converts Python datetime objects to Unix timestamps in milliseconds as required by the Bybit API.
  • client.get_kline(category="linear", ...): Executes a REST request to the Bybit futures kline endpoint, fetching up to limit candles.
  • pd.DataFrame(all_data, columns=[...]): Constructs a structured DataFrame from the accumulated raw nested list response.
  • .astype("int64") / .astype("float64"): Casts string-encoded API responses into typed columns to enable mathematical computation.
  • .drop_duplicates("timestamp").sort_values(...): Removes any duplicate candles based on timestamp and sorts chronologically.

4. Data Ingestion Protocol

Execution of the authenticated request retrieves the specified candlestick data. The resulting dataset is loaded into memory as a Pandas DataFrame for downstream processing.

[ ]
df = fetch_ohlc(SYMBOL, START_DATETIME, END_DATETIME)

Code Logic: Execution

  • fetch_ohlc(SYMBOL, START_DATETIME, END_DATETIME): Invokes the defined pipeline using the global configuration variables and the authenticated client instance.

5. Integrity Verification and Data Inspection

Validation procedures include schema auditing and non-null verification. The info() and head() methods ensure the ingested data aligns with expected financial data models and arithmetic precision requirements.

[ ]
print("--- Fetched OHLCV Data ---")
display(df.head())

print("\n--- Schema Summary ---")
df.info()
--- Fetched OHLCV Data ---
timestamp open high low close volume
0 1704075660000 42610.0 42610.0 42603.5 42603.5 8.894
1 1704075720000 42603.5 42627.7 42591.5 42627.7 73.762
2 1704075780000 42627.7 42627.7 42610.0 42610.0 6.035
3 1704075840000 42610.0 42610.1 42600.0 42600.0 5.311
4 1704075900000 42600.0 42602.3 42599.8 42602.1 21.220

--- Schema Summary ---
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1000 entries, 0 to 999
Data columns (total 6 columns):
 #   Column     Non-Null Count  Dtype  
---  ------     --------------  -----  
 0   timestamp  1000 non-null   int64  
 1   open       1000 non-null   float64
 2   high       1000 non-null   float64
 3   low        1000 non-null   float64
 4   close      1000 non-null   float64
 5   volume     1000 non-null   float64
dtypes: float64(5), int64(1)
memory usage: 47.0 KB

Conclusion

This notebook provides a robust framework for acquiring Bybit OHLCV data, encompassing dependency management, secure API configuration, and data integrity verification. The fetch_ohlc function efficiently retrieves and processes candlestick data, transforming raw API responses into a structured Pandas DataFrame. This foundation is essential for subsequent quantitative analysis, algorithmic trading strategy development, and backtesting.