DEV Community

Hopkins Jesse
Hopkins Jesse

Posted on

Building an AI Trading Agent That Actually Works (Lessons from 2,296 Trades)

Building an AI Trading Agent That Actually Works (Lessons from 2,296 Trades)

I didn't think it would work. When I started building an AI agent to trade crypto futures, I expected it to blow up my account within a week. The hype around "AI trading bots" is mostly marketing nonsense — everyone's selling a dream while quietly ignoring the part where most of these things lose money.

But here's the thing: after running this agent for three months and watching it execute 2,296 trades, I'm genuinely surprised. Not because it made me rich (it didn't), but because it revealed something interesting about where AI agents actually add value versus where they're just expensive gimmicks.

The Setup

I built this on top of OKX's API using their Model Context Protocol (MCP) integration. The idea was simple: instead of writing hardcoded trading logic, I'd let an AI agent make decisions based on real-time market data, account balance, and position info.

The agent has access to 107 tools across 8 modules — tickers, order books, candlestick data, position management, order placement, you name it. It can query technical indicators (RSI, MACD, Bollinger Bands, even the AHR999 index for Bitcoin), place market or limit orders, set stop losses, and track PnL in real time.

Here's what the tool registration looks like:

// packages/core/src/tools/swap-trade.ts
export function registerSwapTools(): ToolSpec[] {
  return [
    {
      name: 'swap_place_order',
      module: 'swap',
      description: 'Place a perpetual contract order',
      inputSchema: {
        type: 'object',
        properties: {
          instId: { type: 'string', description: 'Instrument ID, e.g., BTC-USDT-SWAP' },
          side: { type: 'string', enum: ['buy', 'sell'] },
          ordType: { type: 'string', enum: ['market', 'limit', 'post_only'] },
          sz: { type: 'string', description: 'Order size in contracts' },
          px: { type: 'string', description: 'Price (required for limit orders)' },
        },
      },
      isWrite: true,
      handler: async (args, ctx) => {
        return ctx.client.privatePost('/api/v5/trade/order', args);
      },
    },
    // ... more tools
  ];
}
Enter fullscreen mode Exit fullscreen mode

The agent runs as a local Node.js process — no cloud servers, no API keys leaving my machine. It talks to Claude Desktop via stdio JSON-RPC, which means I can literally chat with my trading bot like a coworker.

What Actually Worked

The agent's win rate settled at 53.7% over those 2,296 trades. That's barely above the 51.02% break-even point when you factor in fees, but it's enough. The Sharpe ratio came out to 3.82 (annualized), which — if you trade traditional finance — is the kind of number that makes portfolio managers jealous.

But here's what surprised me: the agent wasn't "smart" in the way I expected. It didn't discover some hidden pattern in the market. It didn't predict black swan events. What it did well was discipline.

Humans are terrible at following their own rules. You backtest a strategy, it looks solid, then you go live and suddenly you're revenge trading at 3 AM because Bitcoin dumped 5% while you were watching Netflix. The agent doesn't get tired. It doesn't tilt. It doesn't FOMO into a pump because Twitter is hyping it.

The edge came from consistency, not genius.

Where AI Agents Fail (Hard)

I need to be clear about something: this agent is not a money printer. The average daily PnL was $1.05. At that rate, I'd make about $380 a year if I ran it nonstop. That's not life-changing money. That's "maybe covers the server bill" money.

The real value wasn't the profit — it was the data. Running this thing taught me where AI agents actually belong in a trading workflow:

  1. Data gathering and synthesis — The agent excels at pulling together ticker data, position info, and account balance into a coherent snapshot. I can ask "what's my current exposure?" and get an instant answer without clicking through five dashboard tabs.

  2. Routine execution — Placing a stop loss, rebalancing positions, closing out a winner at a target level. These are mechanical tasks that don't require intuition.

  3. Audit trails — Every action the agent takes is logged. I can reconstruct exactly why a trade happened, what data it had access to, and what it was "thinking" (via the conversation history).

Where it fails:

  1. Strategy design — The agent didn't invent a winning strategy. I gave it one. It can't reason about market regimes or adapt to structural changes.

  2. Risk judgment — It doesn't "feel" when something is off. If the API starts returning weird data or the market behaves abnormally, the agent just keeps trading.

  3. Black swans — When FTX collapsed, my human brain knew to pause everything. An AI agent would've kept trading through the chaos until the API stopped responding.

The Architecture Nobody Talks About

Most "AI trading bot" tutorials skip the boring parts. Here's what actually matters:

Rate limiting is non-negotiable. OKX allows a certain number of requests per second depending on the endpoint. I built a token bucket rate limiter that queues requests and waits for available tokens:

// packages/core/src/utils/rate-limiter.ts
class RateLimiter {
  private tokens: number;
  private lastRefill: number;
  private readonly capacity: number;
  private readonly refillPerSecond: number;

  async acquire(key: string): Promise<void> {
    const now = Date.now();
    const elapsed = (now - this.lastRefill) / 1000;
    this.tokens = Math.min(this.capacity, this.tokens + elapsed * this.refillPerSecond);
    this.lastRefill = now;

    if (this.tokens < 1) {
      const waitTime = (1 - this.tokens) / this.refillPerSecond * 1000;
      await sleep(waitTime);
    }

    this.tokens -= 1;
  }
}
Enter fullscreen mode Exit fullscreen mode

Without this, you'll get rate-limited by the exchange, and your agent will start failing at the worst possible moments.

Error handling needs to be explicit. Network errors, authentication failures, invalid order sizes — each requires different handling. I learned this the hard way when a malformed order caused the agent to crash mid-trade.

Local-first is the only sane approach. I've seen people deploy trading bots to cloud servers with API keys in environment variables. That's a disaster waiting to happen. My agent runs as a local process, keys stay on my machine, and the MCP protocol means it only has access to what I explicitly grant.

The Verdict

Would I run this agent with real money again? Yes, but with caveats:

  • Small position sizes only (we're talking "can afford to lose it all" small)
  • Human oversight for major decisions
  • Clear kill switches and circuit breakers
  • Continuous monitoring of the logs

The agent isn't a replacement for a trader. It's a tool — like a power drill is a tool. You still need to know where to drill, how deep, and when to stop.

The most valuable thing I got from this project wasn't the trading profits. It was understanding where AI agents fit into a real workflow. They're not autonomous geniuses. They're tireless assistants that execute boring tasks consistently while you focus on the stuff that actually requires judgment.

If you're thinking about building something similar, start small. Get the rate limiting right. Log everything. And for the love of god, don't skip the error handling. Your future self will thank you when the market gaps 10% at 4 AM and your agent doesn't lose its mind.


The OKX Agent Trade Kit is open source under MIT. If you want to poke around the code, it's at github.com/okx/agent-tradekit. I'm not affiliated with OKX — I just built on their API because it's well-documented and doesn't require a PhD to understand.

Top comments (0)