book
Checkout our new book! Hands on AI Trading with Python, QuantConnect, and AWS Learn More arrow

Asset Classes

Crypto Futures

Introduction

This page explains how to get historical data for Crypto Futures. Some of the data you can get include prices, margin interest rates, and indicator data.

Trades

To get historical trade data, call the history method with the TradeBar type and a security's Symbol. This method returns a DataFrame with columns for the open, high, low, close, and volume.

Select Language:
class CryptoFutureTradeBarHistoryAlgorithm(QCAlgorithm):

    def initialize(self) -> None:
        self.set_start_date(2024, 12, 19)
        # Get the Symbol of a security.
        symbol = self.add_crypto_future('BTCUSD').symbol
        # Get the 5 trailing daily TradeBar objects of the security in DataFrame format. 
        history = self.history(TradeBar, symbol, 5, Resolution.DAILY)
closehighlowopenvolume
symboltime
BTCUSD2024-12-15101383.8102683.9100563.7101403.816666498.0
2024-12-16104500.0105323.5101209.8101383.926232316.0
2024-12-17106120.0107872.1103320.0104500.044897036.0
2024-12-18106171.2108496.9105369.3106120.041849071.0
2024-12-19100163.2106550.299911.0106171.265325574.0
# Calculate the daily returns.
daily_returns = history.close.pct_change().iloc[1:]
symbol  time      
BTCUSD  2024-12-16    0.030737
        2024-12-17    0.015502
        2024-12-18    0.000482
        2024-12-19   -0.056588
Name: close, dtype: float64

If you intend to use the data in the DataFrame to create TradeBar objects, request that the history request returns the data type you need. Otherwise, LEAN consumes unnecessary computational resources populating the DataFrame. To get a list of TradeBar objects instead of a DataFrame, call the history[TradeBar] method.

# Get the 5 trailing daily TradeBar objects of the security in TradeBar format. 
history = self.history[TradeBar](symbol, 5, Resolution.DAILY)
# Iterate through the TradeBar objects and access their volumes.
for trade_bar in history:
    t = trade_bar.end_time
    volume = trade_bar.volume

Quotes

To get historical quote data, call the history method with the QuoteBar type and a security's Symbol. This method returns a DataFrame with columns for the open, high, low, close, and size of the bid and ask quotes. The columns that don't start with "bid" or "ask" are the mean of the quote prices on both sides of the market.

Select Language:
class CryptoFutureQuoteBarHistoryAlgorithm(QCAlgorithm):

    def initialize(self) -> None:
        self.set_start_date(2024, 12, 19)
        # Get the Symbol of a security.
        symbol = self.add_crypto_future('BTCUSD').symbol
        # Get the 5 trailing minute QuoteBar objects of the security in DataFrame format. 
        history = self.history(QuoteBar, symbol, 5, Resolution.MINUTE)
askcloseaskhighasklowaskopenasksizebidclosebidhighbidlowbidopenbidsizeclosehighlowopen
symboltime
BTCUSD2024-12-19 04:56:00100744.2100753.9100727.5100753.96420.0100744.1100753.8100727.1100753.84237.0100744.15100753.85100727.30100753.85
2024-12-19 04:57:00100727.3100744.2100727.3100744.25674.0100727.2100744.1100727.2100744.1742.0100727.25100744.15100727.25100744.15
2024-12-19 04:58:00100698.5100727.3100653.0100727.36707.0100698.4100727.2100652.8100727.23719.0100698.45100727.25100652.90100727.25
2024-12-19 04:59:00100606.1100698.5100606.1100698.51.0100606.0100698.4100606.0100698.45076.0100606.05100698.45100606.05100698.45
2024-12-19 05:00:00100644.1100655.4100606.1100606.16005.0100644.0100655.3100606.0100606.0611.0100644.05100655.35100606.05100606.05
# Calculate the spread at each minute.
spread = history.askclose - history.bidclose
symbol  time               
BTCUSD  2024-12-19 04:56:00    0.1
        2024-12-19 04:57:00    0.1
        2024-12-19 04:58:00    0.1
        2024-12-19 04:59:00    0.1
        2024-12-19 05:00:00    0.1
dtype: float64

If you intend to use the data in the DataFrame to create QuoteBar objects, request that the history request returns the data type you need. Otherwise, LEAN consumes unnecessary computational resources populating the DataFrame. To get a list of QuoteBar objects instead of a DataFrame, call the history[QuoteBar] method.

# Get the 5 trailing minute QuoteBar objects of the security in QuoteBar format. 
history = self.history[QuoteBar](symbol, 5, Resolution.MINUTE)
# Iterate through each QuoteBar and calculate the dollar volume on the bid.
for quote_bar in history:
    t = quote_bar.end_time
    bid_dollar_volume = quote_bar.last_bid_size * quote_bar.bid.close

Ticks

To get historical tick data, call the history method with a security's Symbol and Resolution.TICK. This method returns a DataFrame that contains data on bids, asks, and last trade prices.

Select Language:
class CryptoFutureTickHistoryAlgorithm(QCAlgorithm):

    def initialize(self) -> None:
        self.set_start_date(2024, 12, 19)
        # Get the Symbol of a security.
        symbol = self.add_crypto_future('BTCUSD').symbol
        # Get the trailing 2 days of ticks for the security in DataFrame format.
        history = self.history(symbol, timedelta(2), Resolution.TICK)
askpriceasksizebidpricebidsizelastpricequantity
symboltime
BTCUSD2024-12-17 05:00:00.132868106537.112342.0106537.035.0106537.050.0
2024-12-17 05:00:00.145098106537.112353.0106537.035.0106537.050.0
2024-12-17 05:00:00.424485106537.112353.0106537.0355.0106537.050.0
2024-12-17 05:00:00.427716106537.112353.0106537.0354.0106537.050.0
2024-12-17 05:00:00.431177106537.112353.0106537.0355.0106537.050.0
# Select the rows in the DataFrame that represent trades. Drop the bid/ask columns since they are NaN.
trade_ticks = history[history.quantity > 0].dropna(axis=1)
lastpricequantity
symboltime
BTCUSD2024-12-17 05:00:01.008106537.12.0
2024-12-17 05:00:02.729106537.02.0
2024-12-17 05:00:02.971106537.05.0
2024-12-17 05:00:05.977106537.04.0
2024-12-17 05:00:14.072106537.156.0

If you intend to use the data in the DataFrame to create Tick objects, request that the history request returns the data type you need. Otherwise, LEAN consumes unnecessary computational resources populating the DataFrame. To get a list of Tick objects instead of a DataFrame, call the history[Tick] method.

# Get the trailing 2 days of ticks for the security in Tick format. 
history = self.history[Tick](symbol, timedelta(2), Resolution.TICK)
# Iterate through each quote tick and calculate the quote size.
for tick in history:
    if tick.tick_type == TickType.Quote:
        t = tick.end_time
        size = max(tick.bid_size, tick.ask_size)

Ticks are a sparse dataset, so request ticks over a trailing period of time or between start and end times.

Slices

To get historical Slice data, call the history method without passing any Symbol objects. This method returns Slice objects, which contain data points from all the datasets in your algorithm. If you omit the resolution argument, it uses the resolution that you set for each security and dataset when you created the subscriptions.

Select Language:
class SliceHistoryAlgorithm(QCAlgorithm):

    def initialize(self) -> None:
        self.set_start_date(2024, 12, 1)
        # Add some securities and datasets.
        self.add_crypto_future('BTCUSD')
        # Get the historical Slice objects over the last 5 days for all the subcriptions in your algorithm.
        history = self.history(5, Resolution.DAILY)
        # Iterate through each historical Slice.
        for slice_ in history:
            # Iterate through each TradeBar in this Slice.
            for symbol, trade_bar in slice_.bars.items():
                close = trade_bar.close

Margin Interest Rates

To get historical margin interest rate data, call the history method with the MarginInterestRate type and a security's Symbol. This method returns a DataFrame with a single column for the interest rate.

Select Language:
class CryptoFutureMarginInterestRateHistoryAlgorithmHistoryAlgorithm(QCAlgorithm):

    def initialize(self) -> None:
        self.set_start_date(2024, 12, 19)
        # Get the Symbol of a security.
        symbol = self.add_crypto_future('BTCUSD').symbol
        # Get the MarginInterestRate data of the security over the last 2 trading days in DataFrame format. 
        history = self.history(MarginInterestRate, symbol, 2, Resolution.DAILY)
interestrate
symboltime
BTCUSD2024-12-17 08:00:000.0001
2024-12-17 16:00:000.0001
2024-12-18 00:00:000.0001
2024-12-18 08:00:000.0001
2024-12-18 16:00:000.0001
2024-12-19 00:00:000.0001
# Calculate the change in interest rates.
delta = history.interestrate.diff()[1:]
symbol  time               
BTCUSD  2024-12-17 16:00:00    0.0
        2024-12-18 00:00:00    0.0
        2024-12-18 08:00:00    0.0
        2024-12-18 16:00:00    0.0
        2024-12-19 00:00:00    0.0
Name: interestrate, dtype: float64

If you intend to use the data in the DataFrame to create MarginInterestRate objects, request that the history request returns the data type you need. Otherwise, LEAN consumes unnecessary computational resources populating the DataFrame. To get a list of MarginInterestRate objects instead of a DataFrame, call the history[MarginInterestRate] method.

# Get the MarginInterestRate data of the security over the last 2 trading days in MarginInterestRate format. 
history = self.history[MarginInterestRate](symbol, 2, Resolution.DAILY)
# Iterate through the MarginInterestRate objects and access their values
for margin_interest_rate in history:
    t = margin_interest_rate.end_time
    interest_rate = margin_interest_rate.interest_rate

Indicators

To get historical indicator values, call the indicator_history method with an indicator and the security's Symbol.

Select Language:
class CryptoFutureIndicatorHistoryAlgorithm(QCAlgorithm):

    def initialize(self) -> None:
        self.set_start_date(2024, 12, 19)
        # Get the Symbol of a security.
        symbol = self.add_crypto_future('BTCUSD').symbol
        # Get the 21-day SMA values of the security for the last 5 trading days. 
        history = self.indicator_history(SimpleMovingAverage(21), symbol, 5, Resolution.DAILY)

To organize the data into a DataFrame, use the data_frame property of the result.

# Organize the historical indicator data into a DataFrame to enable pandas wrangling.
history_df = history.data_frame
currentrollingsum
2024-12-1597759.0904762052940.9
2024-12-1698065.3428572059372.2
2024-12-1798689.0380952072469.8
2024-12-1899363.7380952086638.5
2024-12-1999563.5761902090835.1
# Get the maximum of the SMA values.
sma_max = history_df.current.max()

The indicator_history method resets your indicator, makes a history request, and updates the indicator with the historical data. Just like with regular history requests, the indicator_history method supports time periods based on a trailing number of bars, a trailing period of time, or a defined period of time. If you don't provide a resolution argument, it defaults to match the resolution of the security subscription.

To make the indicator_history method update the indicator with an alternative price field instead of the close (or mid-price) of each bar, pass a selector argument.

Select Language:
# Get the historical values of an indicator over the last 30 days, applying the indicator to the security's volume.
history = self.indicator_history(indicator, symbol, timedelta(30), selector=Field.VOLUME)

Some indicators require the prices of two securities to compute their value (for example, Beta). In this case, pass a list of the Symbol objects to the method.

Select Language:
class CryptoFutureMultiAssetIndicatorHistoryAlgorithm(QCAlgorithm):

    def initialize(self) -> None:
        self.set_start_date(2024, 12, 19)
        # Add the target and reference securities.
        target_symbol = self.add_crypto_future('DOGEUSD').symbol
        reference_symbol = self.add_crypto_future('BTCUSD').symbol
        # Create a 21-period Beta indicator.
        beta = Beta("", target_symbol, reference_symbol, 21)
        # Get the historical values of the indicator over the last 10 trading days.
        history = self.indicator_history(beta, [target_symbol, reference_symbol], 10, Resolution.DAILY)
        # Get the average Beta value.
        beta_avg = history.data_frame.mean()

Examples

The following examples demonstrate common practices for trading cryptocurrency futures with historical data.

Example 1: Spot-Future Arbitrage

The example below uses linear regression with historical Spot and Future BTCUSDT data to obtain the cointegrating vector between them. We measure the spread between the two securities and trade mean reversal upon their spread divergence.

Select Language:
from sklearn.linear_model import LinearRegression

class CointegrationVectorRegressionAlgorithm(QCAlgorithm):
    # Threshold of spread divergence to enter position.
    entry_threshold = 2

    def initialize(self) -> None:
        self.set_start_date(2024, 1, 1)
        self.set_end_date(2025, 1, 1)
        self.set_account_currency("USDT", 100000)
        self.set_cash("BTC", 2)
        
        # Request BTCUSDT Crypto and Crypto Future data to form the cointegrating vectors and trade.
        self._future = self.add_crypto_future("BTCUSDT", market=Market.BYBIT).symbol
        self._spot = self.add_crypto("BTCUSDT", market=Market.BYBIT).symbol
        
        # Initialize cointegration vector.
        self.cointegration_vector = np.array([0.0, 0.0])
        self.residual_mean = 0
        self.residual_std = 1

        # Daily recalibration on the cointegrating vectors.
        self.schedule.on(
            self.date_rules.every_day(self._future),
            self.time_rules.at(0, 1),
            self.recalibrate
        )

    def on_data(self, slice: Slice) -> None:
        y = slice.bars.get(self._future)
        x1 = slice.bars.get(self._spot)
        if y and x1:
            # Calculate the spread based on the cointegration vector.
            spread = self.calculate_spread(y.close, x1.close, self.cointegration_vector)
            normalized_spread = (spread - self.residual_mean) / self.residual_std

            # If the spread is too high, short the dependent and long the independent variables based on the weightings of the cointegrating vectors.
            # Else, vice versa.
            total_weights = abs(self.cointegration_vector[1]) + 1
            if normalized_spread > self.entry_threshold and not self.portfolio[self._future].is_short:
                self.set_holdings(self._future, -0.5 / total_weights)
                self.set_holdings(self._spot, 0.5 * self.cointegration_vector[1] / total_weights)
            elif normalized_spread < -self.entry_threshold and not self.portfolio[self._future].is_long:
                self.set_holdings(self._future, 0.5 / total_weights)
                self.set_holdings(self._spot, -0.5 * self.cointegration_vector[1] / total_weights)
            
            # Exit if spread is converged.
            if (self.portfolio[self._future].is_short and normalized_spread < 0) or (self.portfolio[self._future].is_long and normalized_spread > 0):
                self.liquidate()

    def recalibrate(self) -> None:
        # Calculate the cointegration vector from historical data.
        log_price = np.log(self.history([self._future, self._spot], 252, Resolution.DAILY).unstack(0).close.dropna())
        self.cointegration_vector = self.calculate_cointegrating_vector(log_price)
        residual = log_price.apply(lambda x: self.calculate_spread(x[self._future], x[self._spot], self.cointegration_vector), axis=1)
        self.residual_mean, self.residual_std = np.mean(residual), np.std(residual, ddof=1)

    def calculate_cointegrating_vector(self, log_price: pd.DataFrame) -> np.array:
        try:
            # Perform regression.
            lr = LinearRegression().fit(log_price[[self._spot]], log_price[self._future])

            # The coefficients array will contain the coefficients for each independent variable.
            return np.array([lr.intercept_, lr.coef_[0]])
        except:
            return self.cointegration_vector

    def calculate_spread(self, y: float, x1: float, cointegration_vector: np.array) -> float:
        # Using the cointegration vector to calculate the spread.
        return np.log(y) - (cointegration_vector[0] + np.log(x1) * cointegration_vector[1])

You can also see our Videos. You can also get in touch with us via Discord.

Did you find this page helpful?

Contribute to the documentation: