This AI Trades Stocks on a Live Account While You Sleep
An OpenClaw agent monitors markets and executes trades autonomously using heartbeats. One builder is running it on a real account right now.
What You'll Build
An AI trading agent that monitors market conditions, analyzes trends, and executes buy/sell orders on a live brokerage account. It runs continuously using OpenClaw's heartbeat system, checking in on positions and making decisions without human intervention.
Why This Works
Most retail traders lose money because of emotions. They panic-sell on dips, FOMO-buy at peaks, and check their portfolio 47 times a day. An AI agent doesn't have feelings. It follows the strategy you define, executes consistently, and never revenge-trades after a loss.
The heartbeat pattern is what makes this possible. Instead of running a fragile always-on script, OpenClaw wakes up at regular intervals, evaluates the current state, makes decisions, and goes back to sleep. If something crashes, it picks up where it left off.
Prerequisites
- OpenClaw installed and running
- A brokerage account with API access (Alpaca, Interactive Brokers, or similar)
- Basic understanding of your trading strategy (the AI executes it, you define it)
- Risk tolerance for letting an AI touch real money
Architecture
ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ
β Heartbeat ββββββΆβ Market ββββββΆβ Decision β
β (every 5m) β β Scanner β β Engine β
β β β β β β
β Wake up, β β - Prices β β - Strategy β
β check state β β - Volume β β - Risk mgmt β
β β β - News β β - Position β
β β β - Sentiment β β sizing β
ββββββββββββββββ ββββββββββββββββ ββββββββ¬ββββββββ
β
ββββββββββΌββββββββ
β Executor β
β β
β - Place order β
β - Set stops β
β - Log trade β
β - Alert human β
ββββββββββββββββββ
Step 1: Connect Your Brokerage
Most modern brokerages offer API access. Alpaca is popular for this because it's free and designed for algo trading:
# Store your API keys in OpenClaw's environment
# Never hardcode these in skill files
ALPACA_API_KEY=your_key
ALPACA_SECRET_KEY=your_secret
ALPACA_BASE_URL=https://paper-api.alpaca.markets # Start with paper trading!
Start with paper trading. Seriously. Run this for weeks on a simulated account before touching real money.
Step 2: Define Your Strategy
Tell your agent exactly how to trade. Be specific about entry conditions, exit conditions, and risk management:
## Trading Rules
- Only trade these tickers: SPY, QQQ, AAPL, MSFT
- Max position size: 5% of portfolio per ticker
- Entry: Buy when RSI < 30 and price above 200-day MA
- Exit: Sell when RSI > 70 or stop-loss at -3%
- Never hold more than 3 positions at once
- No trading in first/last 15 minutes of market hours
The more specific your rules, the better the agent performs. Vague strategies lead to vague results.
Step 3: Set Up Heartbeats
Configure OpenClaw to check markets at regular intervals. During market hours, you might want checks every 5 minutes. After hours, once an hour is fine:
The heartbeat checks current positions, scans for new opportunities, and executes any trades that match your criteria. Each heartbeat is a fresh evaluation, so the agent won't get stuck in loops or chase bad trades.
Step 4: Build the Risk Rails
This is the most important step. Your agent needs hard limits it cannot override:
- Max daily loss: Stop trading if portfolio drops more than X% in one day
- Max position size: Never put more than Y% in a single ticker
- Kill switch: A way for you to instantly halt all trading
- Notification triggers: Alert you on any trade above $Z
Think of these as guardrails on a mountain road. The AI drives, but the rails keep it from going off a cliff.
Step 5: Monitor and Adjust
Set up daily reports that summarize:
- Trades executed (entries and exits)
- Current positions and P&L
- Strategy performance vs. benchmark
- Any anomalies or near-miss situations
Review these daily for the first few weeks. Adjust your strategy based on real results, not backtests.
What Could Go Wrong
Let's be honest: this is real money.
- Flash crashes: Your agent might buy into a crash thinking it's a dip. Set hard stop-losses.
- API outages: If your broker's API goes down mid-trade, you could be stuck in a position. Use limit orders, not market orders.
- Strategy drift: What works in a bull market fails in a bear market. Review monthly.
- Over-optimization: Don't keep tweaking your strategy to fit past data. That's curve-fitting, not trading.
The Bottom Line
An AI trading agent won't make you a genius investor. What it will do is execute your strategy with perfect discipline, zero emotion, and 24/7 attention. For anyone who already has a strategy but lacks the time or discipline to follow it consistently, this is a force multiplier.
Just remember: paper trade first, risk what you can afford to lose, and keep a kill switch handy.