Requesting Crypto Data in Your Algorithm

·

When building algorithmic trading strategies, accessing accurate and timely cryptocurrency data is essential. This guide walks you through how to request crypto data in your algorithm using QuantConnect’s LEAN engine, ensuring you receive real-time price feeds in the OnData (or on_data) method. Whether you're backtesting or preparing for live trading, understanding how to properly subscribe to crypto assets will improve the reliability and performance of your strategy.

For detailed information on the datasets used during backtesting, refer to the CoinAPI datasets. If you're planning to trade live, the QuantConnect data provider offers seamless integration with major crypto exchanges.


Creating Crypto Subscriptions

To begin receiving crypto market data, you must first create a subscription within the Initialize (or initialize) method of your algorithm. This is done using the AddCrypto (or add_crypto) method, which returns a Crypto object containing a Symbol property. It's important to store this symbol reference so you can later access the asset's data in the Slice object during each time step.

Here’s how to subscribe to Bitcoin in USD:

_symbol = AddCrypto("BTCUSD").Symbol;
self._symbol = self.add_crypto("BTCUSD").symbol

This method adds a single crypto asset to your user-defined universe. If you want to dynamically select multiple crypto assets based on filters such as volume or price, consider setting up a Crypto universe.

A full list of supported crypto pairs can be found in the CoinAPI datasets documentation.

👉 Discover how to build powerful crypto trading algorithms with real-time data feeds.


Understanding Fungible Assets

Cryptocurrencies are classified as fungible assets, meaning each unit is interchangeable and indistinguishable from another. For example, one Bitcoin is always equivalent in value and function to any other Bitcoin. This contrasts with non-fungible tokens (NFTs), where each token represents a unique digital asset—such as those in the Bored Ape Yacht Club collection.

Currently, the LEAN engine does not support trading NFTs. However, it provides robust tools for working with fungible cryptocurrencies across various exchanges and markets.


Supported Markets and Exchange Selection

QuantConnect supports multiple cryptocurrency markets through its Market enumeration. When calling AddCrypto, you can specify the exchange (market) from which you'd like to pull data.

For example, to subscribe to BTC/USD on Coinbase:

_symbol = AddCrypto("BTCUSD", market: Market.Coinbase).Symbol;
self._symbol = self.add_crypto("BTCUSD", market=Market.COINBASE).symbol

Many brokerage models come with a default market setting. For instance, if you use the Binance brokerage model, you typically don’t need to manually specify the market—the system automatically uses Binance as the source.


Fill Forward Behavior

By default, QuantConnect enables fill forward for all data subscriptions. This means that if no new data point is available for the current timeslice, the platform will carry forward the last known value. This helps prevent gaps in your data stream and avoids missed events due to irregular exchange updates.

However, if you disable fill forward, there’s a risk of encountering stale fills—executions based on outdated prices—or seeing zero trade volume during periods of inactivity.

To disable this behavior:

_symbol = AddCrypto("BTCUSD", fillForward: false).Symbol;
self._symbol = self.add_crypto("BTCUSD", fill_forward=False).symbol

Use this option carefully, especially in low-frequency strategies where data gaps are more likely.


Margin, Leverage, and Buying Power

The LEAN engine simulates margin requirements and buying power to ensure your algorithm remains compliant with brokerage rules. The amount of leverage available depends on the connected brokerage and account type.

You can customize leverage when adding a crypto asset:

_symbol = AddCrypto("BTCUSD", leverage: 3).Symbol;
self._symbol = self.add_crypto("BTCUSD", leverage=3).symbol

Different brokerages offer varying levels of leverage for crypto trading. Always verify the specific limits in the Brokerages section of the documentation.

👉 Learn how advanced leverage settings can enhance your trading performance.


Data Normalization Clarification

It’s important to note that changing the data normalization mode has no effect on the raw data delivered to the OnData method or retrieved via history requests. Regardless of your normalization settings, the values remain unchanged in both backtesting and live environments.

This means you can focus on price action and volume without worrying about unintended transformations from normalization modes.


Practical Example: Setting Up a Binance Algorithm

Below is a complete example demonstrating how to initialize an algorithm for trading on Binance using BTC/USDT and ETH/USDT pairs.

Since Binance primarily operates with USDT as a quote currency, it's crucial to set your account currency accordingly. Otherwise, orders may fail due to unsupported currency conversions (e.g., USD to USDT).

C# Example

public class CryptoExampleAlgorithm : QCAlgorithm
{
    public override void Initialize()
    {
        // Set brokerage model for accurate fees and margin simulation
        SetBrokerageModel(BrokerageName.Binance, AccountType.Cash);

        // Set account currency to USDT
        SetAccountCurrency("USDT", 100000);

        // Subscribe to BTC/USDT and ETH/USDT
        AddCrypto("BTCUSDT");
        AddCrypto("ETHUSDT");
    }
}

Python Example

class CryptoExampleAlgorithm(QCAlgorithm):
    def initialize(self) -> None:
        # Set brokerage model
        self.set_brokerage_model(BrokerageName.BINANCE, AccountType.CASH)

        # Set account currency to USDT
        self.set_account_currency("USDT", 100000)

        # Subscribe to crypto pairs
        self.add_crypto("BTCUSDT", market=Market.BINANCE)
        self.add_crypto("ETHUSDT", market=Market.BINANCE)

This setup ensures realistic fee modeling, correct margin handling, and proper currency alignment for successful execution.


Frequently Asked Questions

Q: Can I trade NFTs using QuantConnect?
A: No, LEAN currently does not support non-fungible tokens (NFTs). The platform focuses exclusively on fungible cryptocurrency assets.

Q: Do I always need to specify the market when adding a crypto pair?
A: Not necessarily. If you’ve set a brokerage model (like Binance or Coinbase), it often sets the default market automatically.

Q: What happens if I disable fill forward?
A: Disabling fill forward may result in stale fills or missing data points during quiet trading periods. Use this setting only if your strategy specifically requires raw, unfilled data.

Q: Why should I set my account currency to USDT instead of USD on Binance?
A: Binance doesn’t automatically convert USD to USDT. If your trading pairs are quoted in USDT but your account uses USD, orders may fail due to insufficient balance in the correct currency.

Q: Is leverage fixed across all brokerages?
A: No, leverage varies by brokerage and account type. Always check the specific brokerage documentation for details.

Q: Does data normalization affect backtest results?
A: No. Changing normalization modes does not alter the data passed into OnData or historical requests, so it won’t impact your strategy outcomes.


👉 Start building high-performance crypto algorithms with real-time market access today.

By following best practices in data subscription, market selection, and account configuration, you can create reliable and efficient crypto trading algorithms on QuantConnect. Always test thoroughly in backtest mode before going live.