I've been fascinated by agentic AI - systems that don't just answer questions, but take actions autonomously. The challenge? Most tutorials stop short of practical agentic workflows. I wanted to build something that actually does something in the real world.
So I built ai_trader: an autonomous trading system powered by Claude that analyzes market news, generates trading signals with confidence scores, and executes paper trades through Alpaca's API. It's been running for a few weeks now, generating 500+ signals, and I've learned a lot about what it takes to let an AI make decisions on your behalf.
This post covers the architecture, shows the key code, and shares what I learned along the way.
What It Does
The system is a CLI tool that connects Claude's reasoning capabilities to a paper trading account:
$ trader run claude
Analyzing AAPL...
Signal: BUY | Confidence: 0.75
Reason: Strong earnings beat and positive guidance suggest continued momentum
Analyzing TSLA...
Signal: HOLD | Confidence: 0.60
Reason: Mixed signals - delivery numbers strong but margin concerns persist
The --execute flag actually places trades, but the default is always dry-run. Safety first when an AI is making financial decisions.
Full source code: github.com/josephflu/ai_trader
Architecture Overview
The system has five main components:
Why these choices?
Typer + Rich for the CLI: Beautiful terminal output, type hints, auto-generated help
Abstract Strategy base class: Easy to add new strategies without changing execution logic
SQLite: Simple, file-based, no server needed. Perfect for a personal project
Alpaca Paper Trading: Free, realistic API, no real money at risk
The Interesting Part: Claude as Signal Generator
The core of the system is getting Claude to analyze news and return structured trading signals. Each strategy implements a simple interface: analyze(symbol) returns a TradeSignal with a BUY/SELL/HOLD recommendation, confidence score (0.0-1.0), and reasoning.
The Claude strategy fetches recent news via Alpaca's News API, then sends it to Claude with a prompt asking for JSON output:
{
"signal": "buy|sell|hold",
"confidence": 0.0-1.0,
"reason": "brief explanation"
}
Key prompt engineering decisions:
JSON output format: Structured output makes parsing reliable. Claude follows the schema consistently.
Low temperature (0.3): We want consistent, reproducible analysis - not creative writing.
Explicit confidence guidance: Without this, Claude tends to be overly conservative (everything is 0.5).
"Be decisive": I added this after noticing Claude defaulted to HOLD too often. The prompt now encourages action on clear signals.
Here's an example of what Claude actually returns:
{
"signal": "buy",
"confidence": 0.75,
"reason": "Apple's Q4 earnings exceeded expectations with 8% revenue growth.
Strong iPhone 15 sales and growing services revenue suggest continued
momentum. Positive analyst revisions support a bullish short-term outlook."
}
Risk Management: The Guardrails
Letting an AI place trades - even paper trades - without guardrails is asking for trouble. The TradeExecutor class enforces several safety checks before any order is placed:
- Confidence threshold (70%): Only trade when Claude is reasonably confident
- Position size limit (10%): No single position can exceed 10% of portfolio value
- Skip HOLD signals: Only act on definitive BUY/SELL recommendations
- Paper mode default: Real trading requires explicit configuration changes
- Every signal and trade is logged to SQLite for later analysis. This turned out to be invaluable for understanding Claude's behavior patterns over time.
Lessons Learned
After running this system and analyzing 500+ signals, here's what I've learned:
1. Paper Trading Is Essential for AI Systems
Real money would have been a disaster in week one. The system had bugs, Claude's prompts needed tuning, and my confidence thresholds were wrong. Paper trading let me iterate safely.
2. Confidence Scores Need Calibration
My first prompt produced signals where 0.5 meant "I don't know" and 0.8 was rare. After prompt tuning, the distribution shifted: 0.6+ for moderate signals, 0.7+ for strong ones. The prompt literally had to tell Claude to be more decisive.
3. Logging Everything Pays Off
Every signal includes: symbol, signal type, confidence, reason, timestamp, API cost, and token usage. This data revealed patterns - like Claude being more bullish on tech stocks, or certain news sources leading to better signals.
4. Cost Tracking Matters
Each Claude API call costs about
0.04/day or ~$15/year. Cheap! But without tracking, you won't notice when a bug causes infinite API calls.
What's Next
This is a learning project, not a get-rich-quick scheme. Future improvements I'm considering:
- Backtesting: Test strategies against historical data before live trading
- Multiple signal sources: Combine news sentiment with technical indicators
- Better position sizing: Kelly criterion or other sophisticated methods
- Real-time monitoring: Alerts when positions move significantly
Try It Yourself
The full source code is available at github.com/josephflu/ai_trader. It's built with Python 3.13+ and requires API keys for Alpaca (free paper trading) and an Anthropic API Key.
If you're interested in agentic AI, I'd encourage you to build something similar. Not necessarily trading - but something that takes real actions based on AI reasoning. That's where the interesting lessons are.
I'm Joseph Fluckiger, a software architect exploring agentic AI. Connect with me on LinkedIn or check out more at fluckiger.org.
Comments