Crypto
Handling Data
Introduction
LEAN passes the data you request to the OnData
on_data
method so you can make trading decisions. The default OnData
on_data
method accepts a Slice
object, but you can define additional OnData
on_data
methods that accept different data types. For example, if you define an OnData
on_data
method that accepts a TradeBar
argument, it only receives TradeBar
objects. The Slice
object that the OnData
on_data
method receives groups all the data together at a single moment in time. To access the Slice
outside of the OnData
on_data
method, use the CurrentSlice
current_slice
property of your algorithm.
All the data formats use DataDictionary
objects to group data by Symbol
and provide easy access to information. The plural of the type denotes the collection of objects. For instance, the TradeBars
DataDictionary
is made up of TradeBar
objects. To access individual data points in the dictionary, you can index the dictionary with the security ticker or Symbol
symbol
, but we recommend you use the Symbol
symbol
.
To view the resolutions that are available for Crypto data, see Resolutions.
Trades
TradeBar
objects are price bars that consolidate individual trades from the exchanges. They contain the open, high, low, close, and volume of trading activity over a period of time.
To get the TradeBar
objects in the Slice
, index the Slice
or index the Bars
bars
property of the Slice
with the security Symbol
symbol
. If the security doesn't actively trade or you are in the same time step as when you added the security subscription, the Slice
may not contain data for your Symbol
symbol
. To avoid issues, check if the Slice
contains data for your security before you index the Slice
with the security Symbol
symbol
.
public override void OnData(Slice slice) { // Check if the symbol is contained in TradeBars object if (slice.Bars.ContainsKey(_symbol)) { // Obtain the mapped TradeBar of the symbol var tradeBar = slice.Bars[_symbol]; } }
def on_data(self, slice: Slice) -> None: # Obtain the mapped TradeBar of the symbol if any trade_bar = slice.bars.get(self._symbol) # None if not found
You can also iterate through the TradeBars
dictionary. The keys of the dictionary are the Symbol
objects and the values are the TradeBar
objects.
public override void OnData(Slice slice) { // Iterate all received Symbol-TradeBar key-value pairs foreach (var kvp in slice.Bars) { var symbol = kvp.Key; var tradeBar = kvp.Value; var closePrice = tradeBar.Close; } }
def on_data(self, slice: Slice) -> None: # Iterate all received Symbol-TradeBar key-value pairs for symbol, trade_bar in slice.bars.items(): close_price = trade_bar.close
TradeBar
objects have the following properties:
Quotes
QuoteBar
objects are bars that consolidate NBBO quotes from the exchanges. They contain the open, high, low, and close prices of the bid and ask. The Open
open
, High
high
, Low
low
, and Close
close
properties of the QuoteBar
object are the mean of the respective bid and ask prices. If the bid or ask portion of the QuoteBar
has no data, the Open
open
, High
high
, Low
low
, and Close
close
properties of the QuoteBar
copy the values of either the Bid
bid
or Ask
ask
instead of taking their mean.
To get the QuoteBar
objects in the Slice
, index the QuoteBars
property of the Slice
with the security Symbol
symbol
. If the security doesn't actively get quotes or you are in the same time step as when you added the security subscription, the Slice
may not contain data for your Symbol
symbol
. To avoid issues, check if the Slice
contains data for your security before you index the Slice
with the security Symbol
symbol
.
public override void OnData(Slice slice) { // Check if the symbol is contained in QuoteBars object if (slice.QuoteBars.ContainsKey(_symbol)) { // Obtain the mapped QuoteBar of the symbol var quoteBar = slice.QuoteBars[_symbol]; } }
def on_data(self, slice: Slice) -> None: # Obtain the mapped QuoteBar of the symbol if any quote_bar = slice.quote_bars.get(self._symbol) # None if not found
You can also iterate through the QuoteBars
dictionary. The keys of the dictionary are the Symbol
objects and the values are the QuoteBar
objects.
public override void OnData(Slice slice) { // Iterate all received Symbol-QuoteBar key-value pairs foreach (var kvp in slice.QuoteBars) { var symbol = kvp.Key; var quoteBar = kvp.Value; var askPrice = quoteBar.Ask.Close; } }
def on_data(self, slice: Slice) -> None: # Iterate all received Symbol-QuoteBar key-value pairs for symbol, quote_bar in slice.quote_bars.items(): ask_price = quote_bar.ask.close
QuoteBar
objects let LEAN incorporate spread costs into your simulated trade fills to make backtest results more realistic.
QuoteBar
objects have the following properties:
Ticks
Tick
objects represent a single trade or quote at a moment in time. A trade tick is a record of a transaction for the security. A quote tick is an offer to buy or sell the security at a specific price.
Trade ticks have a non-zero value for the Quantity
quantity
and Price
price
properties, but they have a zero value for the BidPrice
bid_price
, BidSize
bid_size
, AskPrice
ask_price
, and AskSize
ask_size
properties. Quote ticks have non-zero values for BidPrice
bid_price
and BidSize
bid_size
properties or have non-zero values for AskPrice
ask_price
and AskSize
ask_size
properties. To check if a tick is a trade or a quote, use the TickType
ticktype
property.
In backtests, LEAN groups ticks into one millisecond buckets. In live trading, LEAN groups ticks into ~70-millisecond buckets. To get the Tick
objects in the Slice
, index the Ticks
property of the Slice
with a Symbol
symbol
. If the security doesn't actively trade or you are in the same time step as when you added the security subscription, the Slice
may not contain data for your Symbol
symbol
. To avoid issues, check if the Slice
contains data for your security before you index the Slice
with the security Symbol
symbol
.
public override void OnData(Slice slice) { if (slice.Ticks.ContainsKey(_symbol)) { var ticks = slice.Ticks[_symbol]; foreach (var tick in ticks) { var price = tick.Price; } } }
def on_data(self, slice: Slice) -> None: ticks = slice.ticks.get(self._symbol, []) # Empty if not found for tick in ticks: price = tick.price
You can also iterate through the Ticks
dictionary. The keys of the dictionary are the Symbol
objects and the values are the List<Tick>
list[Tick]
objects.
public override void OnData(Slice slice) { foreach (var kvp in slice.Ticks) { var symbol = kvp.Key; var ticks = kvp.Value; foreach (var tick in ticks) { var price = tick.Price; } } }
def on_data(self, slice: Slice) -> None: for symbol, ticks in slice.ticks.items(): for tick in ticks: price = tick.price
Tick data is raw and unfiltered, so it can contain bad ticks that skew your trade results. For example, some ticks come from dark pools, which aren't tradable. We recommend you only use tick data if you understand the risks and are able to perform your own online tick filtering.
Tick
objects have the following properties:
Examples
The following examples demonstrate some common practices for handling Crypto data.
Example 1: Dollar Cost Average BTC
Dollar cost averaging (DCA) is where you consistently invest a fixed dollar amount into an asset on a regular basis (for example, monthly), regardless of the asset's price. It can reduce the volatility in your PnL due to slowly increasing the position size over time. The following algorithm demonstrates a DCA investment into BTC. It buys $10,000 USD worth of BTC every midnight for 30 consecutive days.
public class CryptoExampleAlgorithm : QCAlgorithm { private Symbol _symbol; // Set a day count variable for counting the days of the DCA trade. private int _dayCount = 0; public override void Initialize() { // Set the brokerage and account type to match your brokerage environment for accurate fee and margin behavior. SetBrokerageModel(BrokerageName.Bitfinex, AccountType.Cash); // For daily DCA purchases, subscribe to daily asset data. _symbol = AddCrypto("BTCUSD", Resolution.Daily).Symbol; } public override void OnData(Slice slice) { // If you haven't invested for 30 consecutive days yet, continue buying. if (slice.Bars.ContainsKey(_symbol) && _dayCount++ < 30) { // Calculate the order size for $10,000 USD using the current price. var size = 10000m / slice.Bars[_symbol].Close; MarketOrder(_symbol, size); } } }
class CryptoExampleAlgorithm(QCAlgorithm): def initialize(self) -> None: # Set a day count variable for counting the days of the DCA trade. self.day_count = 0 # Set the brokerage and account type to match your brokerage environment for accurate fee and margin behavior. self.set_brokerage_model(BrokerageName.BITFINEX, AccountType.CASH) # For daily DCA purchases, subscribe to daily asset data. self._symbol = self.add_crypto("BTCUSD", Resolution.DAILY, Market.BITFINEX).symbol def on_data(self, slice: Slice) -> None: # If you haven't invested for 30 consecutive days yet, continue buying. if self._symbol in slice.bars and self.day_count < 30: # Calculate the order size for $10,000 USD using the current price. size = 10000 / slice.bars[self._symbol].close self.market_order(self._symbol, size) # Increase the day count. self.day_count += 1
Example 2: Inter-Market Spread
There is always a small discrepancy in the price of the same Crypto pair trading on different exchanges. Although you can't currently live trade on algorithm with multiple brokerages, you can study the cointegration pattern and implement 2 live nodes to capture the arbitrage opportunity. The following algorithm demonstrates how to obtain the spread between the BTCUSD pair on Kraken and on Coinbase:
public class CryptoExampleAlgorithm : QCAlgorithm { private Symbol _krakenBtc; private Symbol _coinbaseBtc; public override void Initialize() { // Subscribe to BTC/USD on 2 different exchanges. _krakenBtc = AddCrypto("BTCUSD", market: Market.Kraken).Symbol; _coinbaseBtc = AddCrypto("BTCUSD", market: Market.Coinbase).Symbol; } public override void OnData(Slice slice) { // Only calculate the spread if the prices on both exchanges are in the current Slice. if (slice.Bars.ContainsKey(_krakenBtc) && slice.Bars.ContainsKey(_coinbaseBtc)) { // Calculate the spread between the 2 exchanges, making sure the comparison is always in the same direction. var spread = slice.Bars[_krakenBtc].Close - slice.Bars[_coinbaseBtc].Close; // Plot the spread between the 2 exchanges in a custom plot. Plot("BTC Close Spread", "Spread", spread); } } }
class CryptoExampleAlgorithm(QCAlgorithm): def initialize(self) -> None: # Subscribe to BTC/USD on 2 different exchanges. self.kraken_btc = self.add_crypto("BTCUSD", market=Market.KRAKEN).symbol self.coinbase_btc = self.add_crypto("BTCUSD", market=Market.COINBASE).symbol def on_data(self, slice: Slice) -> None: # Only calculate the spread if the prices on both exchanges are in the current Slice. if self.kraken_btc in slice.bars and self.coinbase_btc in slice.bars: # Calculate the spread between the 2 exchanges, making sure the comparison is always in the same direction. spread = slice.bars[self.kraken_btc].close - slice.bars[self.coinbase_btc].close # Plot the spread between the 2 exchanges in a custom plot. self.plot("BTC Close Spread", "Spread", spread)