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

Crypto Futures

Requesting Data

Introduction

Request Crypto Futures data in your algorithm to receive a feed of asset prices in the on_data method. For more information about the specific datasets we use for backtests, see the Binance Crypto Future Price Data dataset listing. To trade Crypto Futures live, you can use the QuantConnect data provider.

Create Subscriptions

To create a Crypto Futures subscription, in the initialize method, call the add_crypto_future method. The add_crypto_future method returns a CryptoFuture object, which contains a symbol property. Save a reference to the symbol so you can use it in on_data to access the security data in the Slice.

Select Language:
self._symbol = self.add_crypto_future("BTCUSD").symbol

The add_crypto_future method creates a subscription for a single Crypto Futures asset and adds it to your user-defined universe.

To view the supported assets in the Crypto Futures dataset, see Supported Assets.

Resolutions

The following table shows the available resolutions and data formats for Crypto Futures contract subscriptions:

ResolutionTradeBarQuoteBarTrade TickQuote Tick
TICKgreen checkgreen check
SECONDgreen checkgreen check
MINUTEgreen checkgreen check
HOURgreen checkgreen check
DAILYgreen checkgreen check

The default resolution for Crypto Futures subscriptions is Resolution.MINUTE. To change the resolution, pass a resolution argument to the add_crypto_future method.

Select Language:
self._symbol = self.add_crypto_future("BTCUSD", Resolution.DAILY).symbol

To create custom resolution periods, see Consolidating Data.

Supported Markets

The following Market enumeration members are available for Cryptofuture:

Select Language:
self._symbol = self.add_crypto_future("BTCUSD", market=Market.BINANCE).symbol

The brokerage models have a default market for each asset class. If you set a brokerage model, you may not need to specify the market to use.

Fill Forward

Fill forward means if there is no data point for the current slice, LEAN uses the previous data point. Fill forward is the default data setting. If you disable fill forward, you may get stale fills or you may see trade volume as zero.

To disable fill forward for a security, set the fill_forward argument to false when you create the security subscription.

Select Language:
self._symbol = self.add_crypto_future("BTCUSD", fill_forward=False).symbol

Margin and Leverage

LEAN models buying power and margin calls to ensure your algorithm stays within the margin requirements. The amount of leverage available to you depends on the brokerage you use. To change the amount of leverage you can use for a security, pass a leverage argument to the add_crypto_future method.

Select Language:
self._symbol = self.add_crypto_future("BTCUSD", leverage=3).symbol

For more information about the leverage each brokerage provides, see Brokerages.

Data Normalization

The data normalization mode doesn't affect the data that LEAN passes to on_data or the data from history request. If you change the data normalization mode, it won't change the outcome.

Properties

The add_crypto_future method returns a CryptoFuture object, which have the following properties:

Examples

The following examples demonstrate some common practices for requesting Crypto Futures data.

Example 1: Respect Lot Sizes

Like Futures, Crypto Futures contracts have a fixed discrete lot size to trade instead of fractional like spot Crypto pairs. The following algorithm demonstrates how to place orders that respect the lot size to avoid rounding errors.

Select Language:
class CryptoFutureAlgorithm(QCAlgorithm):

    def initialize(self) -> None:
        self.set_start_date(2020, 4, 1)
        # Set brokerage and account type to match your brokerage environment for accurate fee and margin behavior.
        self.set_brokerage_model(BrokerageName.BINANCE, AccountType.MARGIN)
        # In the Binance brokerage, you can't trade with USD.
        # Set the account currency as USDT and add the starting cash.
        self.set_account_currency("USDT", 10000)
        # Subscribe to the BTCUSDT perpetual Future contract.
        self._symbol = self.add_crypto_future("BTCUSDT").symbol
        
    def on_data(self, slice: Slice) -> None:
        # Only place orders when market is open since market on open orders aren't supported.
        if not self.portfolio.invested and self.is_market_open(self._symbol):
            # Get the lot size from the symbol properties. Placing an order that respects the lot size 
            # ensures the order is valid and allows accurate profit and risk calculations for the whole portfolio.
            lot_size = self.securities[self._symbol].symbol_properties.lot_size
            # This example demonstrates an initial desired order size of 2.5 contracts, which will be rounded 
            # to 2 contracts since the lot size is 1.
            quantity = 2.5 // lot_size * lot_size
            self.market_order(self._symbol, quantity)

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: