Skip to main content

Building an AI Trading Agent with Claude

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.

https://www.linkedin.com/in/josephflu

Comments

Popular posts from this blog

Unleashing Tableau’s Semantic Layer with AI Agents

⚡ TL;DR I helped built a tool that lets you query Tableau’s semantic layer  using natural language and AI. By integrating a LangChain agent with Tableau’s VizQL Data Service (VDS), we can repurpose Tableau’s trusted data model for conversational analytics . This means you can ask questions in plain English and get answers backed by the same definitions and security that your Tableau dashboards use. In this post, I’ll introduce this open-source agentic tool ( tableau_langchain ), why it’s transformative for analytics, and how it works under the hood. Why Connect LangChain Agents to Tableau? As a user of Tableau, I’ve seen how powerful Tableau’s semantic layer is. It encapsulates our organization’s business logic: things like predefined metrics, calculations, data relationships, and even row-level security rules. Traditionally, that semantic layer is only accessible through Tableau’s interface – you drag and drop fields to build a viz, and Tableau generates the query for you. Rece...

RAM Disks do not speed up Visual Studio

  The limiting factor for Visual Studio is disk IO. I got a tip to speed up Visual Studio from Channel 9 by creating a RAM disk which sounded like a great idea. However, when I ran a thorough set of tests, I found that the performance difference between the Ram disk and the hard disk were not appreciably different. This was a big surprise since RAM is 240,000 times faster than disk (see my previous blog post). But the reason is because Visual Studio and Vista do a lot of caching. So compile times for the same project in RAM disk and on hard disk were pretty similar. I also tested the time it took to search the entire solution for a word, and times to open a solution. There was no discernable difference!   If you still want to try it out and create your own RAM disk, you can download a simple RAMDISK.EXE utility to create a RAM disk in just a few minutes. What is a RAM Disk ?   Ramdisk is a virtual drive created in RAM.   Performance Analysis Creating f...

How to Create and Run Tableau Bridge on Linux Containers

Tableau Bridge is now availble on Linux Containers. Yay! Now what does this mean and how do I build and run Linux Containers? We will discuss the advantages of running Bridge on Linux Containers the steps to build them, and finally, we will provide some automation script ideas for monitoring and scaling Linux Bridge agents. Tableau Bridge Today Until recently, Tableau Bridge was only available as a Windows application running on a Windows VM. It supported only one bridge agent per Virtual or Physical Machine. Advantages of Bridge in Containers Better Hardware Utilization: Linux containers are more efficient than Windows VMs, requiring only about 1/50th of the disk space. Ability to Spin Up Multiple Bridge Agents: With Linux Containers, it becomes easier to spin up multiple bridge agents on a single machine, improving scalability and resource utilization. Infrastructure Automation: Linux Containers enable easier automation of provisioning bridge agents and upgrading Tableau Bridge, the...