---
title: "Crypto"
description: "Some of the Crypto data you can get include prices, indicators, and universe selection data."
canonical: https://www.quantconnect.com/docs/v2/writing-algorithms/historical-data/asset-classes/crypto
markdown: https://www.quantconnect.com/docs/v2/writing-algorithms/historical-data/asset-classes/crypto.md
language: python
source: https://github.com/QuantConnect/Documentation/tree/master/03%20Writing%20Algorithms/20%20Historical%20Data/04%20Asset%20Classes/03%20Crypto
modified: 2026-10-29
index: https://www.quantconnect.com/llms.txt
---

# Crypto

[Documentation](https://www.quantconnect.com/docs/v2.md) > [Writing Algorithms](https://www.quantconnect.com/docs/v2/writing-algorithms.md) > Historical Data > Asset Classes > Crypto

> Code examples are shown in Python. Append `?lang=csharp` to this URL for the C# version of this page.

## Introduction

This page explains how to get historical data for Crypto. Some of the data you can get include prices, indicators, and universe selection data.

## Trades

To get historical [trade data](https://www.quantconnect.com/docs/v2/writing-algorithms/securities/asset-classes/crypto/handling-data.md#03-Trades), 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.

```python
class CryptoTradeBarHistoryAlgorithm(QCAlgorithm):

    def initialize(self) -> None:
        self.set_start_date(2024, 12, 19)
        self.set_end_date(2024, 12, 31)
        # 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)
```

| symbol | time | close | high | low | open | volume |
| --- | --- | --- | --- | --- | --- | --- |
| BTCUSD | 2024-12-15 | 101399.99 | 102650.00 | 100600.00 | 101423.26 | 4054.541500 |
| BTCUSD | 2024-12-16 | 104439.88 | 105100.00 | 101221.34 | 101400.00 | 7216.743790 |
| BTCUSD | 2024-12-17 | 106099.81 | 107857.79 | 103289.21 | 104445.15 | 22263.157625 |
| BTCUSD | 2024-12-18 | 106150.00 | 108388.88 | 105337.97 | 106099.98 | 11729.293641 |
| BTCUSD | 2024-12-19 | 100150.73 | 106528.13 | 99939.82 | 106147.77 | 21659.470502 |

```python
# Calculate the daily returns.
daily_returns = history.close.pct_change().iloc[1:]
```

```python
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.

```python
# 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](https://www.quantconnect.com/docs/v2/writing-algorithms/securities/asset-classes/crypto/handling-data.md#04-Quotes), 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.

```python
class CryptoQuoteBarHistoryAlgorithm(QCAlgorithm):

    def initialize(self) -> None:
        self.set_start_date(2024, 12, 19)
        self.set_end_date(2024, 12, 31)
        # 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)
```

| symbol | time | askclose | askhigh | asklow | askopen | asksize | bidclose | bidhigh | bidlow | bidopen | bidsize | close | high | low | open |
| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
| 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 |
| BTCUSD | 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 |
| BTCUSD | 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 |
| BTCUSD | 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 |
| BTCUSD | 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 |

```python
# Calculate the spread at each minute.
spread = history.askclose - history.bidclose
```

```python
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.

```python
# 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](https://www.quantconnect.com/docs/v2/writing-algorithms/securities/asset-classes/crypto/handling-data.md#05-Ticks), 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.

```python
class CryptoTickHistoryAlgorithm(QCAlgorithm):

    def initialize(self) -> None:
        self.set_start_date(2024, 12, 19)
        self.set_end_date(2024, 12, 31)
        # 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)
```

| symbol | time | askprice | asksize | bidprice | bidsize | lastprice | quantity |
| --- | --- | --- | --- | --- | --- | --- | --- |
| BTCUSD | 2024-12-17 05:00:00.915569 | 106390.0 | 1.072804 | 106380.0 | 16.521214 | 106385.0 | 0.0 |
| BTCUSD | 2024-12-17 05:00:01.119192 | 106390.0 | 1.302212 | 106380.0 | 16.521214 | 106385.0 | 0.0 |
| BTCUSD | 2024-12-17 05:00:05.981284 | 106390.0 | 1.131937 | 106380.0 | 16.521214 | 106385.0 | 0.0 |
| BTCUSD | 2024-12-17 05:00:07.302974 | 106390.0 | 1.498471 | 106380.0 | 16.521214 | 106385.0 | 0.0 |
| BTCUSD | 2024-12-17 05:00:07.514624 | 106390.0 | 1.631937 | 106380.0 | 16.521214 | 106385.0 | 0.0 |

```python
# 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)
```

| symbol | time | lastprice | quantity |
| --- | --- | --- | --- |
| BTCUSD | 2024-12-17 05:00:13.647 | 106380.0 | 0.201870 |
| BTCUSD | 2024-12-17 05:01:13.130 | 106390.0 | 0.000747 |
| BTCUSD | 2024-12-17 05:01:23.446 | 106380.0 | 0.197494 |
| BTCUSD | 2024-12-17 05:01:31.421 | 106390.0 | 0.031909 |
| BTCUSD | 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.

```python
# 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](https://www.quantconnect.com/docs/v2/writing-algorithms/key-concepts/time-modeling/timeslices.md) data, call the `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.

```python
class SliceHistoryAlgorithm(QCAlgorithm):

    def initialize(self) -> None:
        self.set_start_date(2024, 12, 1)
        self.set_end_date(2024, 12, 31)
        # 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](https://www.quantconnect.com/docs/v2/writing-algorithms/universes/crypto.md), call the `history` method with the `Universe` object. This method doesn't apply your [selection function](https://www.quantconnect.com/docs/v2/writing-algorithms/universes/key-concepts.md#05-Selection-Functions). It returns all of the universe data in a DataFrame with columns for the data point attributes.

```python
class CryptoUniverseHistoryAlgorithm(QCAlgorithm):

    def initialize(self) -> None:
        self.set_start_date(2024, 12, 23)
        self.set_end_date(2024, 12, 31)
        # Add a universe of Cryptocurrencies on Coinbase.
        universe = self.add_universe(
            CryptoUniverse.coinbase(
                lambda constitutents: [c.symbol for c in constitutents]
            )
        )
        # Get 5 days of history for the universe.
        history = self.history(universe, timedelta(5), flatten=True)
```

| time | symbol | close | high | low | open | price | volume | volumeinusd |
| --- | --- | --- | --- | --- | --- | --- | --- | --- |
| 2024-12-19 | 00USD 2XR | 0.050300 | 0.051500 | 0.049200 | 0.051500 | 0.050300 | 3.173495e+06 | 1.596268e+05 |
| 2024-12-19 | 00USDC 2XR | 0.050300 | 0.051500 | 0.049200 | 0.051500 | 0.050300 | 3.173495e+06 | 1.596268e+05 |
| 2024-12-19 | 1INCHBTC 2XR | 0.000004 | 0.000005 | 0.000004 | 0.000005 | 0.000004 | 9.815380e+03 | 4.584371e+03 |
| 2024-12-19 | 1INCHEUR 2XR | 0.446000 | 0.467000 | 0.442000 | 0.465000 | 0.446000 | 9.007325e+04 | 4.215390e+04 |
| 2024-12-19 | 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 |
| 2024-12-23 | ZROUSD 2XR | 5.487000 | 6.363000 | 5.393000 | 5.635000 | 5.487000 | 1.312069e+05 | 7.199321e+05 |
| 2024-12-23 | ZROUSDC 2XR | 5.487000 | 6.363000 | 5.393000 | 5.635000 | 5.487000 | 1.312069e+05 | 7.199321e+05 |
| 2024-12-23 | ZRXUSD 2XR | 0.449944 | 0.509425 | 0.441590 | 0.476001 | 0.449944 | 3.371118e+06 | 1.516814e+06 |
| 2024-12-23 | ZRXUSDC 2XR | 0.449944 | 0.509425 | 0.441590 | 0.476001 | 0.449944 | 3.371118e+06 | 1.516814e+06 |

```python
# 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
```

```python
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.

```python
# 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` method with the dataset `Symbol`. This method returns a DataFrame that contains the data point attributes.

```python
class CryptoAlternativeDataHistoryAlgorithm(QCAlgorithm):

    def initialize(self) -> None:
        self.set_start_date(2024, 12, 19)
        self.set_end_date(2024, 12, 31)
        # 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)
```

| symbol | time | averageblocksize | blockchainsize | costpercentoftransactionvolume | costpertransaction | difficulty | estimatedtransactionvolume | estimatedtransactionvolumeusd | hashrate | marketcapitalization | mediantransactionconfirmationtime | ... | mywalletnumberofusers | numberoftransactionperblock | numberoftransactions | numberoftransactionsexcludingpopularaddresses | numberofuniquebitcoinaddressesused | totalbitcoins | totalnumberoftransactions | totaloutputvolume | totaltransactionfees | totaltransactionfeesusd |
| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
| 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 |
| BTCUSD.BitcoinMetadata | 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 |
| BTCUSD.BitcoinMetadata | 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 |
| BTCUSD.BitcoinMetadata | 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 |
| BTCUSD.BitcoinMetadata | 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 |

```python
# Calculate the growth in market cap.
growth = history.marketcapitalization.pct_change().iloc[1:]
```

```python
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](https://www.quantconnect.com/datasets).

## Indicators

To get historical [indicator](https://www.quantconnect.com/docs/v2/writing-algorithms/indicators/key-concepts.md) values, call the `indicator_history` method with an indicator and the security's `Symbol`.

```python
class CryptoIndicatorHistoryAlgorithm(QCAlgorithm):

    def initialize(self) -> None:
        self.set_start_date(2024, 12, 19)
        self.set_end_date(2024, 12, 31)
        # 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.

```python
# 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 |

```python
# Get the maximum of the SMA values.
sma_max = history_df.current.max()
```

The `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 `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 `indicator_history` method update the indicator with an [alternative price field](https://www.quantconnect.com/docs/v2/writing-algorithms/indicators/automatic-indicators.md#07-Alternative-Price-Fields) instead of the close (or mid-price) of each bar, pass a `selector` argument.

```python
# 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](https://www.quantconnect.com/docs/v2/writing-algorithms/indicators/supported-indicators/beta.md)). In this case, pass a list of the `Symbol` objects to the method.

```python
class CryptoMultiAssetIndicatorHistoryAlgorithm(QCAlgorithm):

    def initialize(self) -> None:
        self.set_start_date(2024, 12, 19)
        self.set_end_date(2024, 12, 31)
        # 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()
```

## Examples

The following examples demonstrate some common practices for trading Cryptos using historical requests.

### Example 1: Risk-Parity Crypto Pairs Portfolio

The following algorithm constructs a monthly rebalance risk-parity portfolio using the top 10 liquid Crypto pairs in the BitFinex market. A risk Parity portfolio can dissipate the risk of each crypto pair to reduce overall drawdown. The position sizing can be optimized by 1-year historical daily return of the universe members.

```python
from Portfolio.RiskParityPortfolioOptimizer import RiskParityPortfolioOptimizer

class CryptoHistoricalDataAlgorithm(QCAlgorithm):

    # Instantiate the optimizer to perform risk-parity optimization.
    # Risk Parity portfolio can dissipate the risk of each crypto pair to reduce overall drawdown.
    _optimizer = RiskParityPortfolioOptimizer(0.0, 1.0)

    def initialize(self) -> None:
        self.set_start_date(2024, 9, 1)
        self.set_end_date(2024, 12, 31)

        # Seed the last price to set the initial price of the BTCUSDT holdings.
        self.settings.seed_initial_prices = True

        # Monthly renewal of the top 10 liquid universe to trade popular crypto pairs.
        self.universe_settings.schedule.on(self.date_rules.month_start())
        self._universe = self.add_universe(CryptoCoarseFundamentalUniverse(Market.BITFINEX, self.universe_settings, self.selection))

        # Set a scheduled event to rebalance the portfolio at the start of every month.
        self.schedule.on(
            self.date_rules.month_start(),
            self.time_rules.at(9, 31),
            self.rebalance
        )

    def selection(self, data: List[CryptoUniverse]) -> List[Symbol]:
        # Filter out the crypto pair with a price below $10 to avoid excessive volatility.
        filtered = [datum for datum in data if datum.volume_in_usd]
        # Select the top 10 dollar volume crypto pairs.
        sorted_by_volume_in_usd = sorted(filtered, key=lambda datum: datum.volume_in_usd, reverse=True)[:10]
        return [datum.symbol for datum in sorted_by_volume_in_usd]

    def rebalance(self) -> None:
        # Historical data request to get 1-year data for optimization.
        symbols = self._universe.selected
        history = self.history(symbols, 253, Resolution.DAILY).close.unstack(0).dropna()
        # Daily return on the universe members to calculate the optimized weights.
        returns = history.pct_change().dropna()

        # Calculate the optimized weights.
        weights = self._optimizer.optimize(returns)

        # Rebalance the portfolio according to the optimized weights.
        targets = [PortfolioTarget(symbol, size if abs(size) > 0.01 else 0) for symbol, size in zip(symbols, weights)]
        self.set_holdings(targets, liquidate_existing_holdings=True)
```
