Skip to content
All library documents

Constructing Long and Short Put Butterfly Spreads with an Options Helper

Article Strategy library · Author: QuantConnect

Summary

This example demonstrates how an options strategy helper can submit a put butterfly as a grouped multi-leg order and later close it with the corresponding short butterfly. It scans put contracts by expiry, requires at least three strikes, selects the strike nearest the underlying price, and chooses lower and higher strikes using the available strike range. If both outer strikes exist, it buys two butterfly spreads. The expected position group contains three put legs: two contracts at each outer strike and a short four-contract position at the middle strike.

The example is focused on order construction and position-group verification, not on a market signal or a tested trading edge. It does not specify a forecast, entry timing rationale, or performance evidence, and the chosen strikes are based on chain availability and distance from the underlying rather than an assessment of volatility or expected payoff. It illustrates how the opposite butterfly order can liquidate the opened position, but does not discuss execution quality, commissions, assignment, or broader risk management.

Key ideas

  • The example uses an options helper to submit a multi-leg put butterfly order.
  • It groups put contracts by expiry and selects strikes around the underlying price.
  • Buying two butterflies produces long outer legs and a larger short position at the middle strike.
  • The inverse short butterfly order is used to close the strategy position.
  • The document demonstrates order construction and position checks, not a profitable signal.

Tags

Full text
# LongAndShortButterflyPutStrategiesAlgorithm


# LongAndShortButterflyPutStrategiesAlgorithm









This algorithm demonstrate how to use OptionStrategies helper class to batch send orders for common strategies. In this case, the algorithm tests the Butterfly Put and Short Butterfly Put strategies.

## Source (Apache-2.0)

```python
# QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
# Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from AlgorithmImports import *

import itertools

from OptionStrategyFactoryMethodsBaseAlgorithm import *

### <summary>
### This algorithm demonstrate how to use OptionStrategies helper class to batch send orders for common strategies.
### In this case, the algorithm tests the Butterfly Put and Short Butterfly Put strategies.
### </summary>
class LongAndShortButterflyPutStrategiesAlgorithm(OptionStrategyFactoryMethodsBaseAlgorithm):

    def expected_orders_count(self) -> int:
        return 6

    def trade_strategy(self, chain: OptionChain, option_symbol: Symbol) -> None:
        put_contracts = (contract for contract in chain if contract.right == OptionRight.PUT)

        for expiry, group in itertools.groupby(put_contracts, lambda x: x.expiry):
            contracts = list(group)
            if len(contracts) < 3:
                continue

            strikes = sorted([contract.strike for contract in contracts])
            atm_strike = min(strikes, key=lambda strike: abs(strike - chain.underlying.price))
            spread = min(atm_strike - strikes[0], strikes[-1] - atm_strike)
            itm_strike = atm_strike + spread
            otm_strike = atm_strike - spread

            if otm_strike in strikes and itm_strike in strikes:
                # Ready to trade
                self._butterfly_put = OptionStrategies.butterfly_put(option_symbol, itm_strike, atm_strike, otm_strike, expiry)
                self._short_butterfly_put = OptionStrategies.short_butterfly_put(option_symbol, itm_strike, atm_strike, otm_strike, expiry)
                self.buy(self._butterfly_put, 2)
                return

    def assert_strategy_position_group(self, position_group: IPositionGroup, option_symbol: Symbol) -> None:
        positions = list(position_group.positions)
        if len(positions) != 3:
            raise AssertionError(f"Expected position group to have 3 positions. Actual: {len(positions)}")

        higher_strike = max(leg.strike for leg in self._butterfly_put.option_legs)
        higher_strike_position = next((position for position in positions
                                      if position.symbol.id.option_right == OptionRight.PUT and position.symbol.id.strike_price == higher_strike),
                                     None)

        if not higher_strike_position or higher_strike_position.quantity != 2:
            raise AssertionError(f"Expected higher strike position quantity to be 2. Actual: {higher_strike_position.quantity}")

        lower_strike = min(leg.strike for leg in self._butterfly_put.option_legs)
        lower_strike_position = next((position for position in positions
                                    if position.symbol.id.option_right == OptionRight.PUT and position.symbol.id.strike_price == lower_strike),
                                   None)

        if not lower_strike_position or lower_strike_position.quantity != 2:
            raise AssertionError(f"Expected lower strike position quantity to be 2. Actual: {lower_strike_position.quantity}")

        middle_strike = [leg.strike for leg in self._butterfly_put.option_legs if leg.strike < higher_strike and leg.strike > lower_strike][0]
        middle_strike_position = next((position for position in positions
                                     if position.symbol.id.option_right == OptionRight.PUT and position.symbol.id.strike_price == middle_strike),
                                    None)

        if not middle_strike_position or middle_strike_position.quantity != -4:
            raise AssertionError(f"Expected middle strike position quantity to be -4. Actual: {middle_strike_position.quantity}")

    def liquidate_strategy(self) -> None:
        # We should be able to close the position using the inverse strategy (a short butterfly put)
        self.buy(self._short_butterfly_put, 2)

```

Shown in full with attribution under the source's licence. Licence: Apache-2.0

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.