How to Build an AI Trading Agent in 2026
Traditional trading bots follow rules. AI trading agents reason.
A bot says: "If RSI > 70, sell." An agent says: "NVDA is 47% of my portfolio, earnings are next week, and implied volatility is elevated — I should trim the position and maybe hedge with puts."
The difference is huge. But building an agent that can actually execute trades has been painful — until now.
This guide walks you through building an autonomous trading agent from scratch using broker-cli and an AI coding agent (Codex, Claude Code, or OpenClaw).
What you'll build
A trading agent that can:
- Monitor your portfolio in real-time
- Analyze exposure
- Rebalance positions based on rules you define
- Screen options contracts
- Execute trades autonomously
Total setup time: about 10 minutes.
Prerequisites
- Python 3.12+
- A brokerage account (Interactive Brokers or E*Trade)
- An AI coding agent (Codex, Claude Code, or OpenClaw)
Don't have a brokerage account? You can use paper trading mode to follow along with fake money.
Step 1: Install broker-cli
One command:
curl -fsSL https://brokercli.com/install | bash
This clones the repo, installs dependencies, and adds broker to your PATH.
Verify it worked:
broker --version
Step 2: Run broker setup
Choose your broker provider and configure credentials:
broker setup
This also installs provider-specific dependencies (IB Gateway + IBC for IBKR, or browser auth dependencies for E*Trade).
Step 3: Start the daemon
broker-cli runs a background daemon that maintains your brokerage connection. For development, start in paper trading mode:
broker daemon start --paper
For a live account:
broker daemon start
You'll see: ✓ Connected to Interactive Brokers
The daemon handles authentication, reconnection, and session management. Your agent never deals with OAuth flows or token refreshes.
Step 4: Explore manually
Before handing control to an agent, try some commands yourself:
# Check your portfolio
broker snapshot --symbols AAPL,MSFT
# See exposure breakdown
broker exposure --by symbol
# Get a quote
broker quote AAPL
# Look at option chains
broker chain AAPL --type call --expiry 2026-03 --limit 50 --fields strike,expiry,bid,ask,delta
Every command already returns machine-readable JSON in a stable envelope:
{ok,data,error,meta}. Agents can use meta.request_id to correlate logs and retries.
Step 5: Give your agent SKILL.md
This is where broker-cli is different from every other trading tool.
broker-cli ships with a SKILL.md file — a structured document that describes every command, flag, workflow, and example. AI coding agents read this file automatically and learn the full trading interface without any custom tool definitions or prompt engineering.
If you're using Codex, SKILL.md goes in your project root. For Claude Code, same thing. For OpenClaw, it's loaded as a skill.
The agent reads it once, then knows how to:
- Check positions and P/L
- Analyze exposure by symbol, sector, or asset class
- Place any order type (market, limit, stop, bracket)
- Screen option chains with greeks
- Cancel orders
No integration code. No API wrappers. No boilerplate.
Step 6: Your first autonomous task
Start simple. Ask your agent to check your portfolio:
codex "Check my portfolio and summarize my positions, total value, and P/L"
The agent reads SKILL.md, discovers broker snapshot, runs it, and summarizes the output in plain English.
Step 7: Portfolio monitoring
Now make it useful. Ask your agent to analyze exposure:
codex "Check my portfolio exposure by symbol. Flag any position that exceeds 30% of net liquidation value."
The agent runs broker exposure --by symbol, parses data, and identifies overweight positions. No code to write — it figured out the workflow from SKILL.md.
Step 8: Autonomous rebalancing
Here's where it gets powerful:
codex "Check exposure. If any position exceeds 40% of NLV, trim it to 30%. Use limit orders 0.5% below market price."
The agent:
- Reads SKILL.md
- Runs
broker exposure --by symbol - Identifies NVDA at 47.8% of NLV
- Calculates shares to sell
- Gets the current price with
broker quote NVDA - Runs a safety preview:
broker order sell NVDA 12 --limit 936.29 --dry-run --idempotency-key rebalance-nvda-2026-02-19 - Submits once the dry-run passes
- Confirms the fill
All autonomously. You defined the intent — "trim anything over 40% to 30%" — and the agent figured out the execution.
Step 9: Options screening
Agents are particularly good at options because the analysis is complex but the execution is mechanical:
codex "Find AAPL puts expiring in the next 2 weeks with delta between -0.30 and -0.15. Show me the 3 with the tightest bid-ask spreads."
The agent queries the option chain, filters by greeks, calculates spreads, and presents the results. If you want it to execute:
codex "Buy 1 contract of the AAPL put with the tightest spread from that list."
Safety considerations
Giving an agent access to real money requires safety guardrails:
Start with paper trading. Always. broker daemon start --paper gives you the full experience with simulated funds. Develop and test your strategies here before going live.
Read-only first. Start your agent with read-only commands (portfolio, exposure, quotes) before enabling order placement. Watch how it reasons about your portfolio before trusting it to act.
Review the logs. broker-cli logs every command and order. Review them regularly:
broker audit orders --since 7d
broker audit commands --since 1d
broker audit commands --request-id <request_id_from_meta>
What's next
Once your agent is running reliably in paper trading:
- Backtest your strategies — Review the agent's decisions over time. Would you have made the same calls?
- Add complexity gradually — Start with simple rebalancing, then add options, multi-leg strategies, event-driven triggers
- Go live with small size — Switch to a live account with modest position sizes. Scale up as you build confidence.
- Combine with other tools — Your agent can also write analysis to files, commit trading logs to git, send notifications. The CLI is one tool in a larger toolkit.
Get started
curl -fsSL https://brokercli.com/install | bash
broker setup
broker daemon start --paper
Read the full command reference or check out the GitHub repo.
Give your agent a brokerage account. See what it does.