AI Iron Condor Trading Bot¶
This bot sells an iron condor on SPY every afternoon. An iron condor is a bet that SPY stays inside a price range until the next day’s close. You collect cash up front, and the protective options cap how much you can lose. It copies the core of our older Iron Condor bot, which sold a one-day condor at 3:45 PM and did best when it closed early once SPY ran toward a strike. Why this trade? Over about 35 years, Cboe’s iron condor index had a worst drop of 19%, compared with 51% for the S&P 500 (Cboe).
How it works¶
At 3:45 PM each trading day, the trading agent checks yesterday’s VIX close. If the VIX is above 25, it skips the day.
Otherwise it sells a SPY iron condor that expires the next trading day: a put and a call near 0.14 delta, each with a protective option $1 farther out. It sizes the trade so the most it can lose is about 3% of the account.
Every 5 minutes, a few lines of plain Python check SPY for free. If SPY moves 40% of the way from the middle of the condor toward a short strike, they wake the trading agent, which closes the whole condor.
Otherwise the condor expires the next afternoon, and the agent sells a new one at 3:45 PM.
Run it on BotSpot¶
Run this bot on BotSpot without installing anything. BotSpot runs LumiBot in the cloud, backtests it, and connects it to your broker.
The code¶
The whole bot is one short file. The prompt is the strategy; the stop is four lines of Python.
"""AI Iron Condor.
Every afternoon at 3:45 PM the AI sells a SPY iron condor that expires the next
trading day and lets it expire. Between those runs, plain Python checks SPY every
5 minutes for free and wakes the AI only when SPY runs too close to one of the
short strikes, so the AI can close the condor early.
"""
from lumibot.strategies import Strategy
class AIIronCondorStrategy(Strategy):
parameters = {"symbol": "SPY", "stop_at": 0.4}
def initialize(self):
self.sleeptime = "5M" # how often plain Python checks the stop (no AI call)
self.minutes_before_closing = 15 # before_market_closes runs at 3:45 PM
self.agents.create(
name="trader",
allow_trading=True,
system_prompt=(
"You trade SPY iron condors that expire the next trading day. When it is time to open one, "
"first check the VIX: if it closed above 25 yesterday, do not trade today. Otherwise sell an "
"iron condor on SPY that expires the next trading day: sell a put and a call near 0.14 delta, "
"and buy a put and a call one dollar farther out to cap the loss. Size it so the most we can "
"lose is about 3% of the account. Then hold it until it expires. If we ever hold SPY shares "
"because an option was exercised, sell them."
),
)
def before_market_closes(self):
self.agents["trader"].run(task_prompt="It is 3:45 PM. Open today's iron condor.")
def on_trading_iteration(self):
# The stop, in plain Python: how far SPY has moved from the middle of the condor toward a short strike.
shorts = [p.asset.strike for p in self.get_positions() if p.asset.asset_type == "option" and p.quantity < 0]
if len(shorts) != 2 or any(order.is_active() for order in self.get_orders()):
return
middle, half_width = (max(shorts) + min(shorts)) / 2, (max(shorts) - min(shorts)) / 2
price = self.get_last_price(self.parameters["symbol"])
if price and abs(price - middle) >= self.parameters["stop_at"] * half_width:
self.agents["trader"].run(
task_prompt=f"SPY is at {price}, close to one of our short strikes. Close the whole iron condor now."
)
if __name__ == "__main__":
from lumibot.credentials import IS_BACKTESTING
if IS_BACKTESTING:
from lumibot.backtesting import AlpacaBacktesting
AIIronCondorStrategy.backtest(AlpacaBacktesting)
else:
AIIronCondorStrategy().run_live()
Run it yourself¶
pip install lumibot
python -m lumibot.example_strategies.ai_iron_condor
Put these in your .env file: OPENAI_API_KEY, a free FRED_API_KEY for the VIX, and your broker keys (for example ALPACA_API_KEY, ALPACA_API_SECRET, and ALPACA_IS_PAPER=true for paper trading). With IS_BACKTESTING=false the bot trades. With IS_BACKTESTING=true it backtests instead; set BACKTESTING_START and BACKTESTING_END to pick the dates. The AI runs about once a day, so a month of backtest costs little. Option backtests use Alpaca’s free history (from February 2024), so paper Alpaca keys are enough.
Good to know¶
Options can lose money fast, and a one-day condor can lose its full width on a sharp move. Paper trade first.
SPY options can be exercised at expiry. If that ever leaves you holding SPY shares, the agent sells them at 3:45 PM.
See AI Trading Bot Examples for more AI trading bots and Backtest, paper, or live: choose the runner for backtest and live runs.