Asset Classes
Crypto
Trades
To get historical trade data, call the History<TradeBar>
method with a security's Symbol
.
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.
public class CryptoTradeBarHistoryAlgorithm : QCAlgorithm { public override void Initialize() { SetStartDate(2024, 12, 19); // Get the Symbol of a security. var symbol = AddCrypto("BTCUSD").Symbol; // Get the 5 trailing daily TradeBar objects of the security. var history = History<TradeBar>(symbol, 5, Resolution.Daily); // Iterate through each TradeBar and calculate its dollar volume. foreach (var bar in history) { var t = bar.EndTime; var dollarVolume = bar.Close * bar.Volume; } } }
class CryptoTradeBarHistoryAlgorithm(QCAlgorithm): def initialize(self) -> None: self.set_start_date(2024, 12, 19) # Get the Symbol of a security. symbol = self.add_crypto('BTCUSD').symbol # Get the 5 trailing daily TradeBar objects of the security in DataFrame format. history = self.history(TradeBar, symbol, 5, Resolution.DAILY)
close | high | low | open | volume | ||
---|---|---|---|---|---|---|
symbol | time | |||||
BTCUSD | 2024-12-15 | 101399.99 | 102650.00 | 100600.00 | 101423.26 | 4054.541500 |
2024-12-16 | 104439.88 | 105100.00 | 101221.34 | 101400.00 | 7216.743790 | |
2024-12-17 | 106099.81 | 107857.79 | 103289.21 | 104445.15 | 22263.157625 | |
2024-12-18 | 106150.00 | 108388.88 | 105337.97 | 106099.98 | 11729.293641 | |
2024-12-19 | 100150.73 | 106528.13 | 99939.82 | 106147.77 | 21659.470502 |
# Calculate the daily returns. daily_returns = history.close.pct_change().iloc[1:]
symbol time BTCUSD 2024-12-16 0.029979 2024-12-17 0.015894 2024-12-18 0.000473 2024-12-19 -0.056517 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<QuoteBar>
method with a security's Symbol
.
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.
public class CryptoQuoteBarHistoryAlgorithm : QCAlgorithm { public override void Initialize() { SetStartDate(2024, 12, 19); // Get the Symbol of a security. var symbol = AddCrypto("BTCUSD", market: Market.Bitfinex).Symbol; // Get the 5 trailing minute QuoteBar objects of the security. var history = History<QuoteBar>(symbol, 5, Resolution.Minute); // Iterate through the QuoteBar objects and calculate the spread. foreach (var bar in history) { var t = bar.EndTime; var spread = bar.Ask.Close - bar.Bid.Close; } } }
class CryptoQuoteBarHistoryAlgorithm(QCAlgorithm): def initialize(self) -> None: self.set_start_date(2024, 12, 19) # Get the Symbol of a security. symbol = self.add_crypto('BTCUSD', market=Market.BITFINEX).symbol # Get the 5 trailing minute QuoteBar objects of the security in DataFrame format. history = self.history(QuoteBar, symbol, 5, Resolution.MINUTE)
askclose | askhigh | asklow | askopen | asksize | bidclose | bidhigh | bidlow | bidopen | bidsize | close | high | low | open | ||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
symbol | time | ||||||||||||||
BTCUSD | 2024-12-19 04:56:00 | 100830.0 | 100830.0 | 100830.0 | 100830.0 | 0.496728 | 100820.0 | 100820.0 | 100820.0 | 100820.0 | 0.050586 | 100825.0 | 100825.0 | 100825.0 | 100825.0 |
2024-12-19 04:57:00 | 100810.0 | 100830.0 | 100810.0 | 100830.0 | 1.200384 | 100800.0 | 100820.0 | 100800.0 | 100820.0 | 0.130504 | 100805.0 | 100825.0 | 100805.0 | 100825.0 | |
2024-12-19 04:58:00 | 100760.0 | 100810.0 | 100760.0 | 100810.0 | 0.520363 | 100750.0 | 100800.0 | 100750.0 | 100800.0 | 0.275894 | 100755.0 | 100805.0 | 100755.0 | 100805.0 | |
2024-12-19 04:59:00 | 100710.0 | 100760.0 | 100710.0 | 100760.0 | 1.716247 | 100700.0 | 100750.0 | 100700.0 | 100750.0 | 0.270080 | 100705.0 | 100755.0 | 100705.0 | 100755.0 | |
2024-12-19 05:00:00 | 100710.0 | 100710.0 | 100710.0 | 100710.0 | 0.712784 | 100700.0 | 100700.0 | 100700.0 | 100700.0 | 0.389266 | 100705.0 | 100705.0 | 100705.0 | 100705.0 |
# Calculate the spread at each minute. spread = history.askclose - history.bidclose
symbol time BTCUSD 2024-12-19 04:56:00 10.0 2024-12-19 04:57:00 10.0 2024-12-19 04:58:00 10.0 2024-12-19 04:59:00 10.0 2024-12-19 05:00:00 10.0 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<Tick>
method with a security's Symbol
and Resolution.Tick
.
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.
public class CryptoTickHistoryAlgorithm : QCAlgorithm { public override void Initialize() { SetStartDate(2024, 12, 19); // Get the Symbol of a security. var symbol = AddCrypto("BTCUSD", market: Market.Bitfinex).Symbol; // Get the trailing 2 days of ticks for the security. var history = History<Tick>(symbol, TimeSpan.FromDays(2), Resolution.Tick); // Select the ticks that represent trades, excluding the quote ticks. var trades = history.Where(tick => tick.TickType == TickType.Trade); } }
class CryptoTickHistoryAlgorithm(QCAlgorithm): def initialize(self) -> None: self.set_start_date(2024, 12, 19) # Get the Symbol of a security. symbol = self.add_crypto('BTCUSD', market=Market.BITFINEX).symbol # Get the trailing 2 days of ticks for the security in DataFrame format. history = self.history(symbol, timedelta(2), Resolution.TICK)
askprice | asksize | bidprice | bidsize | lastprice | quantity | ||
---|---|---|---|---|---|---|---|
symbol | time | ||||||
BTCUSD | 2024-12-17 05:00:00.915569 | 106390.0 | 1.072804 | 106380.0 | 16.521214 | 106385.0 | 0.0 |
2024-12-17 05:00:01.119192 | 106390.0 | 1.302212 | 106380.0 | 16.521214 | 106385.0 | 0.0 | |
2024-12-17 05:00:05.981284 | 106390.0 | 1.131937 | 106380.0 | 16.521214 | 106385.0 | 0.0 | |
2024-12-17 05:00:07.302974 | 106390.0 | 1.498471 | 106380.0 | 16.521214 | 106385.0 | 0.0 | |
2024-12-17 05:00:07.514624 | 106390.0 | 1.631937 | 106380.0 | 16.521214 | 106385.0 | 0.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)
lastprice | quantity | ||
---|---|---|---|
symbol | time | ||
BTCUSD | 2024-12-17 05:00:13.647 | 106380.0 | 0.201870 |
2024-12-17 05:01:13.130 | 106390.0 | 0.000747 | |
2024-12-17 05:01:23.446 | 106380.0 | 0.197494 | |
2024-12-17 05:01:31.421 | 106390.0 | 0.031909 | |
2024-12-17 05:01:31.460 | 106390.0 | 0.000086 |
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
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.
public class SliceHistoryAlgorithm : QCAlgorithm { public override void Initialize() { SetStartDate(2024, 12, 1); // Add some securities and datasets. AddCrypto("BTCUSD"); // Get the historical Slice objects over the last 5 days for all the subcriptions in your algorithm. var history = History(5, Resolution.Daily); // Iterate through each historical Slice. foreach (var slice in history) { // Iterate through each TradeBar in this Slice. foreach (var kvp in slice.Bars) { var symbol = kvp.Key; var bar = kvp.Value; } } } }
class SliceHistoryAlgorithm(QCAlgorithm): def initialize(self) -> None: self.set_start_date(2024, 12, 1) # Add some securities and datasets. self.add_crypto('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
Universes
To get historical universe data, call the History
method with the Universe
object.
This method doesn't apply your selection function.
It returns all of the universe data.
To get historical universe data, call the history
method with the Universe
object.
This method doesn't apply your selection function.
It returns all of the universe data in a DataFrame with columns for the data point attributes.
public class CryptoUniverseHistoryAlgorithm : QCAlgorithm { public override void Initialize() { SetStartDate(2024, 12, 23); // Add a universe of Cryptocurrencies on Coinbase. var universe = AddUniverse(CryptoUniverse.Coinbase()); // Get 5 days of history for the universe. var history = History(universe, TimeSpan.FromDays(5)); // Iterate through each day of the universe history. foreach (var constituents in history) { // Select the 2 assets with the smallest weights in the ETF on this day. var dailyLargest = constituents.Select(c => c as ETFConstituentData).OrderByDescending(c => c.Weight).Take(2); } } }
class CryptoUniverseHistoryAlgorithm(QCAlgorithm): def initialize(self) -> None: self.set_start_date(2024, 12, 23) # Add a universe of Cryptocurrencies on Coinbase. universe = self.add_universe(CryptoUniverse.coinbase()) # Get 5 days of history for the universe. history = self.history(universe, timedelta(5), flatten=True)
close | high | low | open | price | volume | volumeinusd | ||
---|---|---|---|---|---|---|---|---|
time | symbol | |||||||
2024-12-19 | 00USD 2XR | 0.050300 | 0.051500 | 0.049200 | 0.051500 | 0.050300 | 3.173495e+06 | 1.596268e+05 |
00USDC 2XR | 0.050300 | 0.051500 | 0.049200 | 0.051500 | 0.050300 | 3.173495e+06 | 1.596268e+05 | |
1INCHBTC 2XR | 0.000004 | 0.000005 | 0.000004 | 0.000005 | 0.000004 | 9.815380e+03 | 4.584371e+03 | |
1INCHEUR 2XR | 0.446000 | 0.467000 | 0.442000 | 0.465000 | 0.446000 | 9.007325e+04 | 4.215390e+04 | |
1INCHGBP 2XR | 0.369000 | 0.385000 | 0.366000 | 0.381000 | 0.369000 | 3.932220e+04 | 1.845807e+04 | |
... | ... | ... | ... | ... | ... | ... | ... | ... |
2024-12-23 | ZETAUSDC 2XR | 0.574500 | 0.638600 | 0.562600 | 0.598400 | 0.574500 | 1.522481e+06 | 8.746654e+05 |
ZROUSD 2XR | 5.487000 | 6.363000 | 5.393000 | 5.635000 | 5.487000 | 1.312069e+05 | 7.199321e+05 | |
ZROUSDC 2XR | 5.487000 | 6.363000 | 5.393000 | 5.635000 | 5.487000 | 1.312069e+05 | 7.199321e+05 | |
ZRXUSD 2XR | 0.449944 | 0.509425 | 0.441590 | 0.476001 | 0.449944 | 3.371118e+06 | 1.516814e+06 | |
ZRXUSDC 2XR | 0.449944 | 0.509425 | 0.441590 | 0.476001 | 0.449944 | 3.371118e+06 | 1.516814e+06 |
# Select the 2 assets with the largest dollar volume each day. most_traded = history.groupby('time').apply(lambda x: x.nlargest(2, 'volumeinusd')).reset_index(level=1, drop=True).volumeinusd
time symbol 2024-12-19 BTCUSD 1.245065e+09 BTCUSDC 2XR 1.245065e+09 2024-12-20 BTCUSD 2.169212e+09 BTCUSDC 2XR 2.169212e+09 2024-12-21 BTCUSD 2.231721e+09 BTCUSDC 2XR 2.231721e+09 2024-12-22 BTCUSD 2.077999e+09 BTCUSDC 2XR 2.077999e+09 2024-12-23 BTCUSD 6.169765e+08 BTCUSDC 2XR 6.169765e+08 Name: volumeinusd, dtype: float64
To get the data in the format of the objects that you receive in your universe filter function instead of a DataFrame, use flatten=False
.
This call returns a Series where the values are lists of the universe data objects.
# Get the historical universe data over the last 30 days in a Series where # the values in the series are lists of the universe selection objects. history = self.history(universe, timedelta(30), flatten=False) # Iterate through each day of universe selection. for (universe_symbol, end_time), constituents in history.items(): # Select the 10 assets with the largest dollar volume this day. most_liquid = sorted(constituents, key=lambda c: c.volume_in_usd)[-10:]
Alternative Data
To get historical alternative data, call the History<alternativeDataClass>
method with the dataset Symbol
.
To get historical alternative data, call the history
method with the dataset Symbol
.
This method returns a DataFrame that contains the data point attributes.
public class CryptoAlternativeDataHistoryAlgorithm : QCAlgorithm { public override void Initialize() { SetStartDate(2024, 12, 19); // Get the Symbol of an asset. var symbol = AddEquity("BTCUSD").Symbol; // Add the alternative dataset and save a reference to its Symbol. var datasetSymbol = AddData<BitcoinMetadata>(symbol).Symbol; // Get the trailing 5 days of alternative data for the asset. var history = History<BitcoinMetadata>(datasetSymbol, 5, Resolution.Daily); // Iterate each data point and access its attributes. foreach (var dataPoint in history) { var t = dataPoint.EndTime; var marketCap = dataPoint.MarketCapitalization; } } }
class CryptoAlternativeDataHistoryAlgorithm(QCAlgorithm): def initialize(self) -> None: self.set_start_date(2024, 12, 19) # Get the Symbol of an asset. symbol = self.add_crypto('BTCUSD').symbol # Add the alternative dataset and save a reference to its Symbol. dataset_symbol = self.add_data(BitcoinMetadata, symbol).symbol # Get the trailing 5 days of alternative data for the asset in DataFrame format. history = self.history(dataset_symbol, 5, Resolution.DAILY)
averageblocksize | blockchainsize | costpercentoftransactionvolume | costpertransaction | difficulty | estimatedtransactionvolume | estimatedtransactionvolumeusd | hashrate | marketcapitalization | mediantransactionconfirmationtime | ... | mywalletnumberofusers | numberoftransactionperblock | numberoftransactions | numberoftransactionsexcludingpopularaddresses | numberofuniquebitcoinaddressesused | totalbitcoins | totalnumberoftransactions | totaloutputvolume | totaltransactionfees | totaltransactionfeesusd | ||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
symbol | time | |||||||||||||||||||||
BTCUSD.BitcoinMetadata | 2024-12-15 | 1.5554 | 622522.4629 | 0.9645 | 99.7845 | 1.039196e+14 | 52757.2240 | 5.353586e+09 | 8.213738e+08 | 1.999814e+12 | 7.5167 | ... | 89780467.0 | 3254.6038 | 517482.0 | 517269.0 | 547370.0 | 1.979622e+07 | 1.131425e+09 | 430661.4198 | 11.9933 | 1.216796e+06 |
2024-12-16 | 1.5014 | 622769.7569 | 0.6667 | 118.2001 | 1.039196e+14 | 66749.2576 | 6.822123e+09 | 7.077246e+08 | 2.038652e+12 | 7.8167 | ... | 89784782.0 | 2762.9708 | 378527.0 | 378325.0 | 540459.0 | 1.979658e+07 | 1.131942e+09 | 435592.1612 | 9.5660 | 9.890319e+05 | |
2024-12-17 | 1.5993 | 622975.3876 | 0.2560 | 120.9025 | 1.082427e+14 | 185055.0677 | 1.948991e+10 | 7.963548e+08 | 2.096909e+12 | 7.6167 | ... | 89788382.0 | 2788.8311 | 412747.0 | 412506.0 | 628257.0 | 1.979710e+07 | 1.132320e+09 | 944149.5981 | 12.3276 | 1.295375e+06 | |
2024-12-18 | 1.6135 | 623212.2483 | 0.2910 | 111.2104 | 1.085226e+14 | 150308.1866 | 1.603829e+10 | 7.336779e+08 | 2.099723e+12 | 8.2167 | ... | 89792154.0 | 3086.0809 | 419707.0 | 419484.0 | 587955.0 | 1.979750e+07 | 1.132733e+09 | 899983.4775 | 12.7376 | 1.359282e+06 | |
2024-12-19 | 1.5915 | 623431.5971 | 0.3013 | 116.3402 | 1.085226e+14 | 154047.1023 | 1.600095e+10 | 7.768354e+08 | 2.034833e+12 | 8.3833 | ... | 89797436.0 | 2877.5417 | 414366.0 | 414128.0 | 583495.0 | 1.979795e+07 | 1.133152e+09 | 863124.9378 | 13.1134 | 1.363842e+06 |
# Calculate the growth in market cap. growth = history.marketcapitalization.pct_change().iloc[1:]
symbol time BTCUSD.BitcoinMetadata 2024-12-16 0.019420 2024-12-17 0.028577 2024-12-18 0.001342 2024-12-19 -0.030904 Name: marketcapitalization, dtype: float64
For information on historical data for other alternative datasets, see the documentation in the Dataset Market.
Indicators
To get historical indicator values, call the IndicatorHistory
indicator_history
method with an indicator and the security's Symbol
.
public class CryptoIndicatorHistoryAlgorithm : QCAlgorithm { public override void Initialize() { SetStartDate(2024, 12, 19); // Get the Symbol of a security. var symbol = AddCrypto("BTCUSD").Symbol; // Get the 21-day SMA values of the security for the last 5 trading days. var history = IndicatorHistory(new SimpleMovingAverage(21), symbol, 5, Resolution.Daily); // Get the maximum of the SMA values. var maxSMA = history.Max(indicatorDataPoint => indicatorDataPoint.Current.Value); } }
class CryptoIndicatorHistoryAlgorithm(QCAlgorithm): def initialize(self) -> None: self.set_start_date(2024, 12, 19) # Get the Symbol of a security. symbol = self.add_crypto('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
current | rollingsum | |
---|---|---|
2024-12-15 | 97734.062857 | 2052415.32 |
2024-12-16 | 98039.381905 | 2058827.02 |
2024-12-17 | 98663.181905 | 2071926.82 |
2024-12-18 | 99340.640952 | 2086153.46 |
2024-12-19 | 99540.619048 | 2090353.00 |
# Get the maximum of the SMA values. sma_max = history_df.current.max()
The IndicatorHistory
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 IndicatorHistory
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 IndicatorHistory
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.
// Get the historical values of an indicator over the last 30 days, applying the indicator to the security's volume. var history = IndicatorHistory(indicator, symbol, TimeSpan.FromDays(30), selector: Field.Volume);
# 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.
public class CryptoMultiAssetIndicatorHistoryAlgorithm : QCAlgorithm { public override void Initialize() { SetStartDate(2024, 12, 19); // Add the target and reference securities. var targetSymbol = AddCrypto("DOGEUSD").Symbol; var referenceSymbol = AddCrypto("BTCUSD").Symbol; // Create a 21-period Beta indicator. var beta = new Beta("", targetSymbol, referenceSymbol, 21); // Get the historical values of the indicator over the last 10 trading days. var history = IndicatorHistory(beta, new[] {targetSymbol, referenceSymbol}, 10, Resolution.Daily); // Get the average Beta value. var avgBeta = history.Average(indicatorDataPoint => indicatorDataPoint.Current.Value); } }
class CryptoMultiAssetIndicatorHistoryAlgorithm(QCAlgorithm): def initialize(self) -> None: self.set_start_date(2024, 12, 19) # Add the target and reference securities. target_symbol = self.add_crypto('DOGEUSD').symbol reference_symbol = self.add_crypto('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()
If you already have a list of Slice objects, you can pass them to the IndicatorHistory
indicator_history
method to avoid the internal history request.
var slices = History(new[] {symbol}, 30, Resolution.Daily); var history = IndicatorHistory(indicator, slices);