Skip to content
All library documents

Next-Day SPY Iron Condors with a Price-Based Early Exit

Code Lumibot

Summary

This Python strategy sells a short-dated SPY iron condor near the end of each trading day, targeting expiration on the next trading day. It skips a new position when the prior day's VIX close exceeded 25. Otherwise, an AI trading agent selects short put and call options near 0.14 delta and buys options one dollar farther out to cap losses, sizing the trade so the maximum loss is about 3% of the account. The strategy also directs the agent to sell any SPY shares received through exercise.

A separate five-minute check monitors the underlying price relative to the midpoint and half-width of the short strikes. Once the price has moved at least 40% of that half-width toward either short strike, the agent is asked to close the condor. The code includes hooks for backtesting and live operation, but provides no performance results. It does not detail fill assumptions, transaction costs, assignment handling beyond selling shares, or how the options agent verifies available contracts and order execution.

Key ideas

  • The strategy opens next-day SPY iron condors near the close, subject to a prior-day VIX filter.
  • It uses short options near 0.14 delta and protective options one dollar farther out.
  • Position sizing aims to cap maximum loss at about 3% of account value.
  • A five-minute price check requests an early exit after a defined move toward either short strike.
  • The code provides no performance evidence or detailed treatment of execution costs.

Tags

Full text
# ai_iron_condor.py


```py
"""AI-owned iron condor with inexpensive minute observations.

One researcher supplies evidence; one trader owns every trading decision and
order. The trader saves its next attention conditions. Quiet minutes invoke no
model. This example requires the attention API in the installed LumiBot runtime.
Historical Alpaca option bars do not establish executable historical bid/ask.
"""
from datetime import timedelta

from lumibot.strategies import Strategy


class AIIronCondorStrategy(Strategy):
    parameters = {"symbol": "SPY", "model": None, "estimated_contract_fee": 0.65}

    def initialize(self):
        self.sleeptime = "1M"
        self.minutes_before_closing = 0
        self.agents.create(
            name="researcher", allow_trading=False, allow_network=True, model=self.parameters["model"],
            reasoning_effort="high", system_prompt=(
                (
                'Research the supplied underlying for a short iron condor held overnight through the next '
                'trading session. Use dated volatility, trend and event evidence, including the previous '
                'completed VIX close. Focus on material events inside that holding window. Distinguish known '
                'events from unavailable calendar evidence; give the trader a compact dated assessment, '
                'contrary evidence and uncertainty. Do not trade. '
            )
            ),
        )
        self.agents.create(
            name="trader", allow_trading=True, model=self.parameters["model"],
            reasoning_effort="high", system_prompt=(
                (
                'Trade at most one short/net-credit iron condor on the supplied underlying. Consider a new '
                'setup only in the final 15 regular-session minutes, at most once per session, using this '
                "session's setup research. Require the previous completed VIX close at or below 25. Use next- "
                'trading-day expiry, short strikes near 0.14 absolute delta and one-dollar outer wings. '
                'Compare at most three candidates. You choose integer package count; maximum loss including '
                'estimated round-trip per-contract fees must fit 3% of current account value. The supplied '
                'fee is an estimate, not a broker fee claim. '
                'The mandate is routine defined-risk premium collection when the priced range is reasonable. '
                'Compare candidate premiums and risk before deciding; decline for a concrete limit, known '
                'imminent major release or demonstrated breakout/gap risk. Ordinary mixed news or a missing '
                'calendar is not itself a veto. '
                'Set underlying review boundaries 40% of the distance from the short-strike midpoint toward '
                'either short strike; a breach requires your close decision. Close remaining exposure no '
                'later than 30 minutes before the expiration session ends. Leave the next eligible setup '
                'review after closure. Atomic market exits are permitted if a limit cannot meet a required '
                "close deadline. Follow LumiBot's shared options and attention guidance; you own every "
                'trading decision. '
            )
            ),
        )

    def before_starting_trading(self):
        if not self.vars.get("attention_started", False):
            self.agents.attention.watch(owner="lifecycle", watch_id="initial_review",
                operation_id="initial_setup", metric="time", comparison="gte",
                value=(self.get_datetime() + timedelta(seconds=max(
                    0, (self.broker.get_time_to_close() or 0) - 15 * 60))).isoformat(),
                reason="Initial setup review; verify entry eligibility under your policy",
                review_scope="setup")
            self.vars.set("attention_started", True)
        self.on_trading_iteration()

    def on_trading_iteration(self):
        self.agents.attention.poll()
        self.agents.attention.dispatch_team(decision_agent="trader", research_agents=["researcher"],
            context={"symbol": self.parameters["symbol"],
                     "estimated_contract_fee": self.parameters.get("estimated_contract_fee", 0.65),
                     "estimated_contract_fee_units": "USD per contract per side"})

    def on_strategy_end(self):
        self.agents.attention.join()

    def on_abrupt_closing(self):
        self.agents.attention.join()


if __name__ == "__main__":
    from lumibot.credentials import IS_BACKTESTING

    if IS_BACKTESTING:
        from lumibot.backtesting import AlpacaBacktesting
        from lumibot.entities import TradingFee

        fee = TradingFee(per_contract_fee=AIIronCondorStrategy.parameters["estimated_contract_fee"])
        AIIronCondorStrategy.backtest(AlpacaBacktesting, buy_trading_fees=[fee], sell_trading_fees=[fee])
    else:
        AIIronCondorStrategy().run_live()

```

Shown in full with attribution under the source's licence. Licence: GPL-3.0

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