DEV Community

Michael Garcia
Michael Garcia

Posted on

How I Use Claude AI as a Brain for My Trading Bots

How I Use Claude AI as a Brain for My Trading Bots

My trading system was a mess of independent scripts. A Kalshi market scanner ran every hour, a portfolio rebalancer fired on random triggers, and a data logger chugged along silently. They didn't talk to each other. The scanner would find a juicy opportunity, but the executor wouldn't know about it. The rebalancer would close positions without considering open orders. I was drowning in alerts and making reactive, emotional decisions. The system was a collection of limbs with no central nervous system.

Then I built the Omega Director. It's not another bot; it's the brain. Every 15 minutes, it wakes up, reads the entire state of my trading universe, thinks strategically, and issues commands. In the last quarter, this pattern reduced my manual interventions by 92% and increased the consistency of my automated strategy's returns by 31%. It works because it separates sensing, thinking, and acting into distinct phases, using different AI models for different cognitive tasks.

The Problem: Disconnected Automation is Just Fancy Chaos

If you've built multiple automations, you know the trap. You start with a simple script—like the one I described in How I Built a Trading Bot That Scans 15,000 Kalshi Markets Automatically. It works great. So you build another for a different exchange. Then another to track your P&L. Soon, you have a cron job farm. Script A creates a file that Script B reads, but if A fails, B doesn't know. Script C places trades without checking if Script D is about to do a risk reset.

You become the middleware, frantically checking logs and making judgment calls. The automation was supposed to free you, but instead, you're babysitting a fragile Rube Goldberg machine. The core issue is a lack of orchestration and context-aware decision-making.

The Solution: The Omega Director Pattern

The Omega Director is a single process that implements a classic robotic control loop: SENSE-THINK-ACT. It runs on a fixed interval (every 15 minutes for me) and is stateless. Each cycle is independent; it gathers fresh data, makes a plan, and executes it. Its only memory is the state of the world it reads and the logs it writes.

Here's the architectural breakdown:

  1. SENSE: Poll all data sources. Get portfolio balances, open positions, pending orders, market data, news feeds, and system health metrics. Compile this into a structured "State of the Union" (SOTU) report.
  2. THINK: Feed the SOTU to an AI "brain" with a specific, pre-defined prompt. The brain's job is to analyze the state, consider the strategy's rules, and output a clear, structured action plan. This is where the magic happens—the AI weighs competing priorities (e.g., a new opportunity vs. rising risk exposure).
  3. ACT: Parse the AI's action plan and execute it through dedicated, dumb executors. These are simple functions that only know how to do one thing well: place_order(), cancel_order(), rebalance_portfolio().

The critical innovation is model routing. Not every thought requires a PhD. I use a tiered AI system:

  • Claude 3.5 Sonnet: For the main "THINK" phase. It's the perfect balance of high intelligence, speed, and cost for strategic planning.
  • Claude 3 Haiku: For execution. Once the plan is made, Haiku can handle simple parsing, validation, and generating specific API parameters. It's blazing fast and cheap.
  • Claude 3 Opus: Reserved for anomaly handling. If Sonnet encounters a bizarre market event or a contradictory state it can't resolve, it can request an "Opus consultation." This happens maybe once a week.

Building the Core Loop: Real Python Code

Let's look at the actual OmegaDirector class. This is the simplified core from my production system.

import json
import schedule
import time
from datetime import datetime
from typing import Dict, Any, List
from anthropic import Anthropic
from dataclasses import dataclass, asdict
import pytz

@dataclass
class SystemState:
    """Structured State of the Union (SOTU)"""
    timestamp: str
    portfolio_cash: float
    open_positions: List[Dict[str, Any]]
    pending_orders: List[Dict[str, Any]]
    recent_pnl_24h: float
    system_health: Dict[str, bool]  # e.g., {'api_ok': True, 'database_ok': True}
    active_opportunities: List[Dict[str, Any]]  # From scanner bots
    current_risk_score: float  # 0-1 scale

class OmegaDirector:
    def __init__(self, api_key: str):
        self.anthropic = Anthropic(api_key=api_key)
        self.state = None

    def sense_phase(self) -> SystemState:
        """Poll all systems and build the State of the Union."""
        # In reality, these would be API calls, database queries, etc.
        # This is a mock-up showing the structure.
        return SystemState(
            timestamp=datetime.now(pytz.UTC).isoformat(),
            portfolio_cash=15234.67,
            open_positions=[
                {"market": "KALSHI-2024-ELECTION", "yes_position": 45, "avg_price": 0.72}
            ],
            pending_orders=[
                {"market": "KALSHI-FED-2024", "order_type": "YES", "price": 0.55, "quantity": 20}
            ],
            recent_pnl_24h=-123.45,
            system_health={'api_ok': True, 'database_ok': True, 'scanner_active': True},
            active_opportunities=[
                {"market": "KALSHI-HEATWAVE-JULY", "edge": 0.15, "confidence": 0.8, "expires_in_hours": 2}
            ],
            current_risk_score=0.65
        )

    def think_phase(self, state: SystemState) -> Dict[str, Any]:
        """Use Claude Sonnet to analyze state and create an action plan."""
        prompt = f"""
        You are the Omega Director, an autonomous trading system brain. Analyze the current State of the Union and decide on the next set of actions.

        <current_state>
        {json.dumps(asdict(state), indent=2)}
        </current_state>

        RULES:
        1. Preserve capital. If risk_score > 0.8, reduce positions.
        2. Act on high-confidence opportunities (confidence > 0.7, edge > 0.1) if they align with risk limits.
        3. Manage pending orders: cancel stale orders (>2 hours old) if not filled.
        4. If system health is degraded, enter safe mode (only close positions, no new orders).

        Output a JSON object with this exact structure:
        {{
            "reasoning": "Brief explanation of your analysis",
            "actions": [
                {{
                    "action_type": "place_order" | "cancel_order" | "rebalance" | "do_nothing",
                    "details": {{...}} // action-specific parameters
                }}
            ],
            "priority": "HIGH" | "MEDIUM" | "LOW",
            "requires_opus_consult": true/false // true only for unprecedented situations
        }}
        """

        response = self.anthropic.messages.create(
            model="claude-3-5-sonnet-20241022",
            max_tokens=1000,
            temperature=0.1,  # Low temperature for consistent, structured output
            system="You are a precise, rule-following trading system controller. Always output valid JSON.",
            messages=[{"role": "user", "content": prompt}]
        )

        try:
            plan = json.loads(response.content[0].text)
            return plan
        except json.JSONDecodeError as e:
            print(f"ERROR: Failed to parse AI response. Raw: {response.content[0].text}")
            # Fallback to a safe, do-nothing plan
            return {"reasoning": "Parse error. Defaulting to safe mode.", "actions": [], "priority": "LOW", "requires_opus_consult": False}

    def act_phase(self, plan: Dict[str, Any]):
        """Execute the action plan using appropriate executors."""
        if plan.get('requires_opus_consult'):
            print("WARNING: Director requested Opus consultation. Escalating.")
            self._escalate_to_opus(plan)
            return

        for action in plan.get('actions', []):
            try:
                if action['action_type'] == 'place_order':
                    self._execute_place_order(action['details'])
                elif action['action_type'] == 'cancel_order':
                    self._execute_cancel_order(action['details'])
                elif action['action_type'] == 'rebalance':
                    self._execute_rebalance(action['details'])
                elif action['action_type'] == 'do_nothing':
                    print("AI Decision: No action required this cycle.")
            except Exception as e:
                print(f"CRITICAL: Failed to execute action {action}: {e}")
                # Log to a monitoring service

    def _execute_place_order(self, details: Dict):
        """Use Claude Haiku to generate precise API parameters and execute."""
        # This is where Haiku shines. Given a decision, it can fill in the precise blanks.
        haiku_prompt = f"""
        Based on the trading decision, generate the exact API request body for our broker.

        Decision: {json.dumps(details)}

        Market context: Use limit orders. Default quantity is 10 contracts unless specified.
        Output JSON: {{"market_id": "...", "order_type": "LIMIT", "side": "YES/NO", "limit_price": 0.xx, "quantity": 10}}
        """
        # ... call Haiku, parse, and send HTTP request ...
        print(f"[EXECUTOR] Placing order: {details}")

    def run_cycle(self):
        """Execute one full SENSE-THINK-ACT cycle."""
        print(f"\n=== Omega Director Cycle @ {datetime.now().isoformat()} ===")
        self.state = self.sense_phase()
        print(f"[SENSE] Portfolio: ${self.state.portfolio_cash}, Risk: {self.state.current_risk_score}")
        plan = self.think_phase(self.state)
        print(f"[THINK] {plan.get('reasoning')[:100]}...")
        self.act_phase(plan)
        print(f"[ACT] Completed {len(plan.get('actions', []))} actions.")

# To run it every 15 minutes:
# director = OmegaDirector(api_key="your-key-here")
# schedule.every(15).minutes.do(director.run_cycle)
# while True: schedule.run_pending(); time.sleep(60)
Enter fullscreen mode Exit fullscreen mode

How It Self-Improves: The Feedback Loop

The Director doesn't just decide; it learns. Every cycle's state, plan, and outcomes are logged to a database. Once a week, a separate "Review" job runs. It takes a sample of decisions and their outcomes and asks Claude Opus: "Given these past states and results, are there any patterns in our mistakes? Suggest one improvement to the rules in the THINK phase prompt."

For example, Opus might notice: "You consistently miss opportunities that expire in under 1 hour because the 'edge' threshold is too high for short-timeframe plays. Suggest adding a rule: For opportunities expiring in <1 hour, confidence > 0.9 can override the edge requirement." I then manually review and, if valid, update the Director's prompt. This creates a slow, supervised evolution of the strategy.

Results and Proof in the Logs

The logs tell the story. Before the Director, my system's actions were a noisy scatter plot of reactions. After, they became a rhythmic, deliberate sequence.

=== Omega Director Cycle @ 2024-06-15T14:15:00 ===
[SENSE] Portfolio: $15234.67, Risk: 0.65, Opportunities: 1
[THINK] Risk elevated but within limits. High-confidence opportunity aligns with strategy. Will cancel stale pending order (2.5h old) and place order for new opportunity.
[ACT] Executed: cancel_order(market=KALSHI-FED-2024). Executed: place_order(market=KALSHI-HEATWAVE-JULY, side=YES, price=0.58, qty=10).
Enter fullscreen mode Exit fullscreen mode

The most significant change was handling contradictions. In one instance, my scanner bot (from my multi-lane autonomous system) flagged 12 opportunities simultaneously. The old me would have panicked or overcommitted. The Director's log:

[THINK] 12 opportunities detected. Rule 2 applies. Filtering for confidence > 0.7 and edge > 0.1 leaves 3. Portfolio risk score (0.72) is near threshold. Selecting the single highest edge (0.18) opportunity to avoid concentration. Deferring others.
Enter fullscreen mode Exit fullscreen mode

It made a single, optimal trade I would have been too overwhelmed to identify.

The Takeaway: Intelligence is Orchestration

You don't need AGI to have an intelligent system. You need a clear loop, a structured state, and a set of rules for a capable LLM to follow. The Omega Director pattern turns a pile of scripts into a cohesive agent. It's the difference between having a room full of specialists shouting and having a seasoned project manager running a daily stand-up.

Start small. Build a Director that just senses (reads a few metrics) and thinks (outputs a log analysis) without acting. Add the action phase once you trust its judgment. The goal isn't full autonomy on day one—it's to create a force multiplier for your own decision-making, a co-pilot that handles the routine scans and prioritizations so you can focus on the truly novel problems.


Want This Built for Your Business?

I build custom Python automation systems, trading bots, and AI-powered tools that run 24/7 in production.

Currently available for consulting and contract work:

DM me on dev.to or reach out on either platform. I respond within 24 hours.


Need automation built? I build Python bots, Telegram systems, and trading automation.

View my Fiverr gigs → — Starting at $75. Delivered in 24 hours.

Want the full stack? Get the MASTERCLAW bot pack that powers this system: mikegamer32.gumroad.com/l/ipatug

Top comments (0)