Fear and Greed Index Trading Bot¶
This bot buys the S&P 500 when investors are scared and sells when they get greedy. Each day an AI agent opens a real web browser, goes to CNN’s Fear & Greed Index page, and reads the score. The score blends seven market signals, like momentum, volatility, and demand for safe assets, into one number from 0 to 100. Buying when others are scared is an old idea: Warren Buffett wrote that he tries “to be fearful when others are greedy and to be greedy only when others are fearful” (1986 Berkshire letter). It is also the simplest example of an AI agent using a browser to read a live website and trade on it.
How it works¶
Research agent opens a real Chrome browser and finds the Fear & Greed score (0 is extreme fear, 100 is extreme greed) for the most recent day before today, on CNN’s page or CNN’s list of past scores.
Trading agent sets how much of your account is in SPY: 100% below 25, 75% from 25 to 44, 50% from 45 to 55, 25% from 56 to 75, and 0% above 75. The rest stays in cash.
If there is no score from the last few days, the trading agent does nothing. The bot repeats this once a day.
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.
Backtest tear sheet¶
GPT-6 Luna, January 5 to 23, 2026, Yahoo daily prices, $100,000 start. Each day the research agent opened CNN’s score history in a real browser and used the score from the day before. Neutral scores put 50% in SPY; when greed pushed the score above 55, the bot cut SPY to 25%. It ended at $100,074, about even with SPY. Cash never went below $50,460.
Open the full tear sheet. A short backtest shows the bot works as written. It is not a promise of future returns.
The code¶
The whole bot is one short file. The prompts are plain English, and they are the strategy.
"""Fear and Greed Index Trading Bot.
Buys the S&P 500 when investors are scared and sells when they are greedy. A
research agent opens a real web browser and reads CNN's Fear & Greed Index. A
trading agent then sets how much of the account sits in SPY: more on fear, less
on greed.
"""
from lumibot.strategies import Strategy
class FearAndGreedTradingBot(Strategy):
parameters = {"symbol": "SPY"}
def initialize(self):
self.sleeptime = "1D"
self.agents.create(
name="researcher",
allow_trading=False,
allow_network=True,
system_prompt=(
"Find the CNN Fear & Greed Index score from 0 to 100 for the most recent day before today. "
"Use a web browser. Today's score is at https://www.cnn.com/markets/fear-and-greed. Past "
"scores are listed day by day at https://production.dataviz.cnn.io/index/fearandgreed/graphdata/ "
"followed by a start date, such as 2026-01-01. Report the score and its date. Do not trade."
),
)
self.agents.create(
name="trader",
allow_trading=True,
system_prompt=(
"Set how much of the account is in the symbol from the Fear & Greed score: 100% below 25, 75% "
"from 25 to 44, 50% from 45 to 55, 25% from 56 to 75, and 0% above 75. Keep the rest in cash. "
"If there is no score from the last few days, do nothing."
),
)
def on_trading_iteration(self):
facts = {"symbol": self.parameters["symbol"]}
research = self.agents["researcher"].run(task_prompt="Read the Fear & Greed Index.", context=facts)
self.agents["trader"].run(
task_prompt="Set the position from the score.", context={**facts, "research": research.summary}
)
if __name__ == "__main__":
from lumibot.credentials import IS_BACKTESTING
if IS_BACKTESTING:
from lumibot.backtesting import YahooDataBacktesting
FearAndGreedTradingBot.backtest(YahooDataBacktesting)
else:
FearAndGreedTradingBot().run_live()
Run it yourself¶
pip install "lumibot[browser]"
patchright install chromium
python -m lumibot.example_strategies.ai_fear_and_greed_trading_bot
Put these in your .env file: OPENAI_API_KEY, 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, and start with a week or two, because every AI call costs a little.
Good to know¶
CNN’s page only shows today’s score, so in a backtest the research agent uses CNN’s list of past scores and takes the day before each test day. The list also shows later days, so this rule lives in the prompt.
Change the percentages in the trading agent’s prompt to make the bot more or less aggressive.
See AI Trading Bot Examples for more AI trading bots and Backtest, paper, or live: choose the runner for backtest and live runs.