Crypto
Requesting Data
Introduction
Request Crypto data in your algorithm to receive a feed of asset prices in the OnData
on_data
method. For more information about the specific datasets we use for backtests, see the CoinAPI datasets. To trade Crypto live, you can use the QuantConnect data provider.
Create Subscriptions
To create a Crypto subscription, in the Initialize
initialize
method, call the AddCrypto
add_crypto
method. The AddCrypto
add_crypto
method returns a Crypto
object, which contains a Symbol
symbol
property. Save a reference to the Symbol
symbol
so you can use it in OnData
on_data
to access the security data in the Slice
.
_symbol = AddCrypto("BTCUSD").Symbol;
self._symbol = self.add_crypto("BTCUSD").symbol
The AddCrypto
add_crypto
method creates a subscription for a single Crypto asset and adds it to your user-defined universe. To create a dynamic universe of Crypto assets, add a Crypto universe.
To view the supported assets, see the CoinAPI datasets.
Fungible Assets
Fungible assets are assets that are interchangeable with each other. For example, every Bitcoin is effectively identical to every other Bitcoin. In contrast, non-fungible tokens (NFTs) are non-fungible because each one is unique. An example of an NFT is the Bored Ape Yacht Club collection. LEAN doesn't currently support trading NFTs.
Resolutions
The following table shows the available resolutions and data formats for Crypto subscriptions:
Resolution | TradeBar | QuoteBar | Trade Tick | Quote Tick |
---|---|---|---|---|
Tick TICK | ||||
Second SECOND | ||||
Minute MINUTE | ||||
Hour HOUR | ||||
Daily DAILY |
The default resolution for Crypto subscriptions is Resolution.Minute
Resolution.MINUTE
. To change the resolution, pass a resolution
argument to the AddCrypto
add_crypto
method.
_symbol = AddCrypto("BTCUSD", Resolution.Daily).Symbol;
self._symbol = self.add_crypto("BTCUSD", Resolution.DAILY).symbol
To create custom resolution periods, see Consolidating Data.
Supported Markets
The following Market
enumeration members are available for Crypto:
To set the market for a security, pass a market
argument to the AddCrypto
add_crypto
method.
_symbol = AddCrypto("BTCUSD", market: Market.Coinbase).Symbol;
self._symbol = self.add_crypto("BTCUSD", market=Market.COINBASE).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 fillForward
fill_forward
argument to false when you create the security subscription.
_symbol = AddCrypto("BTCUSD", fillForward: false).Symbol;
self._symbol = self.add_crypto("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 AddCrypto
add_crypto
method.
_symbol = AddCrypto("BTCUSD", leverage: 3).Symbol;
self._symbol = self.add_crypto("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 OnData
on_data
or the data from history request. If you change the data normalization mode, it won't change the outcome.
Examples
The following examples demonstrate some common practices for requesting Crypto data.
Example 1: Initialize Binance Algorithms
The following algorithm requests data for the BTC/USDT and ETH/USDT Crypto pairs on Binance. The Binance brokerage model provides realistic fee and fill modeling for the backtest. Since the quote currency of both pairs is USDT, set the account currency to USDT instead of USD. Otherwise, some order will fail since the brokerage doesn't automatically convert USD to USDT for you.
public class CryptoExampleAlgorithm : QCAlgorithm { public override void Initialize() { // Set the brokerage and account type for accurate fee and margin behavior. SetBrokerageModel(BrokerageName.Binance, AccountType.Cash); // In some Crypto brokerages, USD is not a valid currency to trade. // Therefore, set the account currency to USDT and add the starting cash. SetAccountCurrency("USDT", 100000); // Subscribe to the Crypto pairs. // You don't need to specify the market because the Binance brokerage model already does that. AddCrypto("BTCUSDT"); AddCrypto("ETHUSDT"); } }
class CryptoExampleAlgorithm(QCAlgorithm): def initialize(self) -> None: # Set the brokerage and account type for accurate fee and margin behavior. self.set_brokerage_model(BrokerageName.BINANCE, AccountType.CASH) # In some Crypto brokerages, USD is not a valid currency to trade. # Therefore, set the account currency to USDT and add the starting cash. self.set_account_currency("USDT", 100000) # Subscribe to the Crypto pairs. # You don't need to specify the market because the Binance brokerage model already does that. self.add_crypto("BTCUSDT", market=Market.BINANCE) self.add_crypto("ETHUSDT", market=Market.BINANCE)