book
Checkout our new book! Hands on AI Trading with Python, QuantConnect, and AWS Learn More arrow

Asset Classes

Equity Options

Introduction

This page explains how to get historical data for Equity Options. Some of the data you can get include prices, open interest, Greeks, and universe selection data.

Trades

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.

Select Language:
class EquityOptionTradeBarHistoryAlgorithm(QCAlgorithm):

    def initialize(self) -> None:
        self.set_start_date(2024, 12, 19)
        # Get the Symbol of a security.
        equity = self.add_equity('SPY', data_normalization_mode=DataNormalizationMode.RAW)
        symbol = sorted(self.option_chain(equity.symbol), key=lambda c: c.open_interest)[-1].symbol
        # Get the 5 trailing daily TradeBar objects of the security in DataFrame format. 
        history = self.history(TradeBar, symbol, 5, Resolution.DAILY)
closehighlowopenvolume
expirystriketypesymboltime
2025-01-17440.01SPY 32OCGBPPW6DTY|SPY R735QTJ8XC9X2024-12-12 16:00:000.200.230.200.2199.0
2024-12-13 16:00:000.200.210.170.18543.0
2024-12-16 16:00:000.180.190.170.1758.0
2024-12-17 16:00:000.240.240.190.19101.0
2024-12-18 16:00:000.850.850.210.21160.0
# Calculate the daily returns.
daily_returns = history.close.pct_change().iloc[1:]
expiry      strike  type  symbol                              time               
2025-01-17  440.0   1     SPY 32OCGBPPW6DTY|SPY R735QTJ8XC9X  2024-12-13 16:00:00    0.000000
                                                              2024-12-16 16:00:00   -0.100000
                                                              2024-12-17 16:00:00    0.333333
                                                              2024-12-18 16:00:00    2.541667
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

Request minute, hour, or daily resolution data. Otherwise, the history request won't return any data.

Quotes

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.

Select Language:
class EquityOptionsQuoteBarHistoryAlgorithm(QCAlgorithm):

    def initialize(self) -> None:
        self.set_start_date(2024, 12, 19)
        # Get the Symbol of a security.
        equity = self.add_equity('SPY', data_normalization_mode=DataNormalizationMode.RAW)
        symbol = sorted(self.option_chain(equity.symbol), key=lambda c: c.open_interest)[-1].symbol
        # Get the 5 trailing minute QuoteBar objects of the security in DataFrame format. 
        history = self.history(QuoteBar, symbol, 5, Resolution.MINUTE)
askcloseaskhighasklowaskopenasksizebidclosebidhighbidlowbidopenbidsizeclosehighlowopen
expirystriketypesymboltime
2025-01-17440.01SPY 32OCGBPPW6DTY|SPY R735QTJ8XC9X2024-12-18 15:56:000.760.850.710.7572.00.740.740.170.6072.00.750.7950.4400.675
2024-12-18 15:57:000.790.800.760.76143.00.770.780.730.7460.00.780.7900.7450.750
2024-12-18 15:58:000.800.800.780.79169.00.780.780.750.7760.00.790.7900.7650.780
2024-12-18 15:59:000.820.830.790.8072.00.800.800.760.7872.00.810.8150.7750.790
2024-12-18 16:00:000.910.910.820.82346.00.870.890.800.8072.00.890.9000.8100.810
# Calculate the spread at each minute.
spread = history.askclose - history.bidclose
expiry      strike  type  symbol                              time               
2025-01-17  440.0   1     SPY 32OCGBPPW6DTY|SPY R735QTJ8XC9X  2024-12-18 15:56:00    0.02
                                                              2024-12-18 15:57:00    0.02
                                                              2024-12-18 15:58:00    0.02
                                                              2024-12-18 15:59:00    0.02
                                                              2024-12-18 16:00:00    0.04
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

Request minute, hour, or daily resolution data. Otherwise, the history request won't return any data.

Slices

To get historical Slice 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.

Select Language:
class SliceHistoryAlgorithm(QCAlgorithm):

    def initialize(self) -> None:
        self.set_start_date(2024, 12, 1)
        # Add some securities and datasets.
        equity = self.add_equity('SPY', data_normalization_mode=DataNormalizationMode.RAW)
        contract = sorted(self.option_chain(equity.symbol), key=lambda c: c.open_interest)[-1]
        self.add_option_contract(contract.symbol)
        # 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

Open Interest

To get historical open interest data, call the history method with the OpenInterest type and a security's Symbol. This method returns a DataFrame with a single column.

Select Language:
class EquityOptionOpenInterestHistoryAlgorithm(QCAlgorithm):

    def initialize(self) -> None:
        self.set_start_date(2024, 12, 19)
        # Get the Symbol of a security.
        equity = self.add_equity('SPY', data_normalization_mode=DataNormalizationMode.RAW)
        symbol = sorted(self.option_chain(equity.symbol), key=lambda c: c.open_interest)[-1].symbol
        # Get the 5 trailing daily OpenInterest objects of the security in DataFrame format. 
        history = self.history(OpenInterest, symbol, 5, Resolution.DAILY)
openinterest
expirystriketypesymboltime
2025-01-17440.01SPY 250117P004400002024-12-13171751.0
2024-12-16172190.0
2024-12-17172157.0
2024-12-18172147.0
2024-12-19172099.0
# Calculate the daily change in open interest.
oi_delta = history.openinterest.diff().iloc[1:]
expiry      strike  type  symbol                 time      
2025-01-17  440.0   1     SPY   250117P00440000  2024-12-16    439.0
                                                 2024-12-17    -33.0
                                                 2024-12-18    -10.0
                                                 2024-12-19    -48.0
Name: openinterest, dtype: float64

If you intend to use the data in the DataFrame to create OpenInterest 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 OpenInterest objects instead of a DataFrame, call the history[OpenInterest] method.

# Get the 5 trailing daily OpenInterest objects of the security in OpenInterest format. 
history = self.history[OpenInterest](symbol, 5, Resolution.DAILY)
# Iterate through the TradeBar objects and access their volumes.
for oi in history:
    t = oi.end_time
    open_interest = oi.value

Daily Option Chains

To get historical daily Option chain data, call the history method with the Option Symbol object. The data this method returns contains information on all the currently tradable contracts, not just the contracts that pass your filter. If you pass flatten=True, this method returns a DataFrame with columns for the data point attributes.

Select Language:
class EquityOptionDailyOptionChainHistoryAlgorithm(QCAlgorithm):

    def initialize(self) -> None:
        self.set_start_date(2024, 12, 23)
        # Add an Equity Option universe.
        option = self.add_option('SPY')
        # Get the trailing 5 daily Option chains in DataFrame format.
        history = self.history(option.symbol, 5, flatten=True)
closedeltagammahighimpliedvolatilitylowopenopeninterestrhothetaunderlyingvaluevegavolume
timesymbol
2024-12-13SPY YOGVNNCO8QDI|SPY R735QTJ8XC9X484.8151.0000000.000000487.9453.212331484.225486.741539.00.0000000.000000SPY: ¤604.33484.8150.0000000.0
SPY YOGVNNCU72FA|SPY R735QTJ8XC9X474.2101.0000000.000000477.9703.055466474.210476.9126.00.0000000.000000SPY: ¤604.33474.2100.0000000.0
SPY YOGVNND05EH2|SPY R735QTJ8XC9X464.8351.0000000.000000467.9652.910873464.230466.914.00.0000000.000000SPY: ¤604.33464.8350.0000000.0
SPY YOGVNND63QIU|SPY R735QTJ8XC9X454.8451.0000000.000000458.0002.775398454.245456.9218.00.0000000.000000SPY: ¤604.33454.8450.0000000.0
SPY YTG30NXW11QE|SPY R735QTJ8XC9X456.3451.0000000.000000459.2350.687831456.270458.7733.00.0000000.000000SPY: ¤604.33456.3450.0000000.0
................................................
2024-12-19SPY 33899RRUZK23Q|SPY R735QTJ8XC9X314.995-0.8650130.000496317.0450.145628293.900297.421.0-5.8661550.038392SPY: ¤586.28314.9950.5736320.0
SPY 337HP99QFUJJA|SPY R735QTJ8XC9X319.995-0.8749530.000412321.7650.142599299.020302.420.0-5.7540090.039155SPY: ¤586.28319.9950.4522530.0
SPY 33899RVZ5ZO12|SPY R735QTJ8XC9X319.990-0.8688660.000447321.8050.143224298.995302.470.0-5.9540580.039088SPY: ¤586.28319.9900.5111730.0
SPY 337HP95ZTK8HY|SPY R735QTJ8XC9X324.995-0.8760730.000389326.7150.142820304.030307.430.0-5.8424720.040096SPY: ¤586.28324.9950.4280580.0
SPY 33899RS8JPCZQ|SPY R735QTJ8XC9X324.995-0.8684260.000446326.7450.145502303.975307.461.0-6.0483550.039695SPY: ¤586.28324.9950.5176960.0
# Select the 2 contracts with the greatest volume each day.
most_traded = history.groupby('time').apply(lambda x: x.nlargest(2, 'volume')).reset_index(level=1, drop=True).volume
time        symbol                            
2024-12-13  SPY 32NDZOIHRHLRA|SPY R735QTJ8XC9X    166689.0
            SPY 32NDZOIHXFXT2|SPY R735QTJ8XC9X    160265.0
2024-12-14  SPY YOCXVCLWHPT2|SPY R735QTJ8XC9X     137597.0
            SPY YOCXVCM2G1UU|SPY R735QTJ8XC9X     100401.0
2024-12-17  SPY 32NHXGVYLQYG6|SPY R735QTJ8XC9X    104700.0
            SPY YODXBFZKFH46|SPY R735QTJ8XC9X     103635.0
2024-12-18  SPY 32NIWWZBFX1IE|SPY R735QTJ8XC9X    106804.0
            SPY YOEWRJCELK6E|SPY R735QTJ8XC9X      75769.0
2024-12-19  SPY 32NKVT60AHJDY|SPY R735QTJ8XC9X    109298.0
            SPY YOFW7MPKOBC6|SPY R735QTJ8XC9X      94823.0
Name: volume, dtype: float64

To get the data in the format of OptionUniverse objects instead of a DataFrame, call the history[OptionUniverse] method.

# Get the historical OptionUniverse data over the last 30 days.
history = self.history[OptionUniverse](option.symbol, timedelta(30))
# Iterate through each daily Option chain.
for option_universe in history:
    t = option_universe.end_time
    # Select the contract with the most volume.
    most_traded = sorted(option_universe, key=lambda contract: contract.volume)[-1]

Indicators

To get historical indicator values, call the indicator_history method with an indicator and the security's Symbol.

Select Language:
class EquityOptionIndicatorHistoryAlgorithm(QCAlgorithm):

    def initialize(self) -> None:
        self.set_start_date(2024, 12, 19)
        # Get the Symbol of the Option contract.
        underlying = self.add_equity('SPY').symbol
        symbol = sorted(self.option_chain(underlying), key=lambda c: c.open_interest)[-1].symbol
        # Get the 21-day SMA values of the contract 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
currentrollingsum
2024-12-12 16:00:000.3776197.930
2024-12-13 16:00:000.3661907.690
2024-12-16 16:00:000.3538107.430
2024-12-17 16:00:000.3366677.070
2024-12-18 16:00:000.3564297.485
# 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 instead of the close (or mid-price) of each bar, pass a selector argument.

Select Language:
# 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 multiple securities to compute their value (for example, the indicators for the Greeks and implied volatility). In this case, pass a list of the Symbol objects to the method.

Select Language:
class EquityOptionMultiAssetIndicatorHistoryAlgorithm(QCAlgorithm):

    def initialize(self) -> None:
        self.set_start_date(2024, 12, 19)
        # Get the Symbol of the underlying asset.
        underlying = self.add_equity('SPY').symbol
        # Get the Option contract Symbol.
        option = sorted(self.option_chain(underlying), key=lambda c: c.open_interest)[-1].symbol
        # Get the Symbol of the mirror contract.
        mirror = Symbol.create_option(
            underlying, option.id.market, option.id.option_style, 
            OptionRight.Call if option.id.option_right == OptionRight.PUT else OptionRight.PUT,
            option.id.strike_price, option.id.date
        )
        # Create the indicator.
        indicator = ImpliedVolatility(
            option, self.risk_free_interest_rate_model, DividendYieldProvider(underlying),
            mirror, OptionPricingModelType.FORWARD_TREE
        )
        # Get the historical values of the indicator over the last 60 trading minutes.
        history = self.indicator_history(indicator, [underlying, option, mirror], 60, Resolution.MINUTE)
        # Get the average IV value.
        iv_avg = history.data_frame.current.mean()

Examples

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

Example 1: Trend Following on 0DTE Option Contract

This algorithm strategically trades 0DTE SPY options by analyzing bid and ask volumes shortly after the market opens. Using scheduled events, it effectively executes trades based on historical quote data, optimizing decision-making. The algorithm aims for timely entries and exits, ensuring efficient capital management and quick adaptability in volatile markets.

Select Language:
class ZeroDTEOptionsTradingAlgorithm(QCAlgorithm):
    def initialize(self) -> None:
        self.set_start_date(2024, 12, 1)
        self.set_end_date(2024, 12, 10)  # Limit to a single day for 0DTE
        self.set_cash(100000)  # Starting cash

        # Request SPY option data for trading and signal generation.
        option = self.add_option("SPY")
        # We are interested in ATM 0DTE options since they are the most popular.
        option.set_filter(lambda u: u.include_weeklys().expiration(0, 0).strikes(-3, 3))
        self._option = option.symbol
        
        # Schedule event to enter and exit option contract position.
        self.schedule.on(self.date_rules.every_day(self._option), self.time_rules.after_market_open(self._option, 16), self.trade_option)
        self.schedule.on(self.date_rules.every_day(self._option), self.time_rules.before_market_close(self._option, 15), self.liquidate)

    def trade_option(self) -> None:
        # Get the option chain for SPY to trade.
        option_chain = self.current_slice.option_chains.get(self._option, None)
        if option_chain:
            for option in option_chain:
                # Request historical quote data for signal generation.
                history = self.history(QuoteBar, option.symbol, 15, Resolution.Minute)
                if not history.empty:
                    # Calculate total bid and ask dollar volume to determine the capital directional force.
                    total_bid_volume = (history['bidclose'] * history['bidsize']).sum()
                    total_ask_volume = (history['askclose'] * history['asksize']).sum()

                    # Follow the capital flow to trade.
                    if total_bid_volume > total_ask_volume:
                        self.market_order(option.symbol, 1)
                    else:
                        self.market_order(option.symbol, -1)

You can also see our Videos. You can also get in touch with us via Discord.

Did you find this page helpful?

Contribute to the documentation: