DEV Community

Cover image for How I built an AI stock trading system with Claude API (open source)
Davis Wang
Davis Wang

Posted on

How I built an AI stock trading system with Claude API (open source)

I spent the last few days building a semi-automated stock analysis
system powered by Claude AI. Here's what I built and how it works.

The Problem

Most retail traders make emotional decisions. I wanted to build
something that combines the pattern-recognition of technical analysis
with the reasoning ability of a large language model — and forces both
to agree before making any move.

System Architecture

The system has 7 core modules:

  • Stock Scanner — scans 14 tech stocks daily, filters by RSI, MA20, MA50, volume
  • Strategy Engine — generates rule-based signals (BUY / WATCHLIST_BUY / HOLD / SELL)
  • News Service — fetches real-time news via yfinance, scores relevance
  • Claude AI Analyzer — independently evaluates each candidate stock
  • Decision Engine — combines rule signal + AI signal with confidence thresholds
  • Risk Manager — enforces stop loss (5%), take profit (10%), max hold days (10)
  • Memory System — feeds historical performance back to Claude on each run

The Dual Signal Engine

The key insight: neither pure technical analysis nor pure AI is reliable alone.

# Only BUY when both agree
if rule_signal == "WATCHLIST_BUY" and ai_signal == "BUY" and confidence >= "Medium":
    final_action = "BUY"
Enter fullscreen mode Exit fullscreen mode

This filters out most false positives.

Memory System

Claude has no memory between API calls. So I built an external memory
layer that summarizes recent performance and injects it into every prompt:
System Memory (last 14 days):
NVDA: RSI>85 observed 12 times without SELL trigger — pullback risk elevated
AAPL: WATCHLIST_BUY + AI HOLD pattern occurred 3 times — low conviction setup

This makes Claude's analysis context-aware over time.

Real Output Example

=== Stock Scanner ===
[WATCHLIST_BUY] AAPL @ $273 | RSI=67 | Score=3
[WATCHLIST_BUY] CRM @ $189 | RSI=53 | Score=3
Analyzing CRM...
Rule Signal: WATCHLIST_BUY | AI Signal: BUY | Confidence: Medium
Final Action: BUY
[SIMULATED BUY] CRM x5.27 @ $189.80 = $1000

Tech Stack

  • Python 3.11
  • Anthropic Claude API (claude-sonnet-4-6)
  • yfinance for price data and news
  • Custom news relevance scoring algorithm
  • schedule for automated daily runs

Results So Far

Running in simulation mode with $10,000 virtual capital.
The system correctly identified CRM before a 1.4% gain and avoided
NVDA when RSI hit 91 (extreme overbought).

Links

Happy to answer questions about the Claude API integration or the
decision engine logic!

Top comments (0)