Published 2026-10-14 · Updated 2026-10-25

Connect an AI Agent to Market Data

Give an AI agent a way to pull structured market data, then let it analyse. A runnable walkthrough — the loop from the definition page, closed.


On this page

What this builds

The definition page ended with a question: do you know how to ask — and how to judge whether the answer is trustworthy?

This walks through the first half of that: giving an AI agent a reliable way to pull structured market data, so it can compute and explain instead of guessing.

By the end you will have an agent that can answer “what has this stock done over the last month?” with numbers that came from code, not from the model’s memory.

The rule from the standards page applies throughout: code computes, the model narrates.


Prerequisites

You need:

  • An AI agent that can call tools (Claude Code, Cursor, Codex, or any MCP-compatible client)
  • A market data source the agent can reach
  • About ten minutes

No prior quant background. If you can describe what you want, you can do this.


Step 1 — Pick a data path

There are four ways an agent can get market data. They differ mainly in one thing: can you verify what came back?

Path How it works Verifiable?
Scrape a web page Agent reads HTML ✗ Layout changes break it; numbers are unverifiable
A local data file You download a CSV, agent reads it ✓ but stale the moment you download it
A REST API Agent calls an endpoint, gets JSON ✓ structured and repeatable
An MCP tool Agent calls the data source as a tool ✓ structured, and the agent knows the tool exists

For a first run, a REST API is the shortest path to something verifiable. MCP is the better long-term option once you know what you want.


Step 2 — Pull one number, by hand

Before involving the agent, prove the data source works. This endpoint needs no key:

curl "https://api.ashareapi.com/v1/quote?code=sh600519"

You get back a JSON envelope whose data field is an array of daily bars, newest first — so the latest close is data[0].last. If this returns numbers, the rest is plumbing. If it does not, stop here — no agent can fix a data source that is not answering.

One thing to know before you start looping: anonymous access is rate-limited to five requests a minute, and 250 rows per call. That is enough to follow this page once, and not enough to iterate. Add a key when you start looping.


Step 3 — Give the agent the tool

The point of this step is not the specific tool. It is that the agent should call a tool for numbers, never produce them from memory.

If your client supports MCP, point it at the data source and let it discover the tools:

{
  "mcpServers": {
    "ashareapi": {
      "type": "http",
      "url": "https://api.ashareapi.com/mcp"
    }
  }
}

That is the Claude Code shape. Cursor takes the same object without type; Codex uses TOML ([mcp_servers.ashareapi] + url). Paid tools add "headers": { "Authorization": "Bearer ct-..." }.

If your client does not support MCP, a plain REST call inside a small script works just as well:

import httpx

def quote(code: str) -> dict:
    r = httpx.get("https://api.ashareapi.com/v1/quote", params={"code": code})
    r.raise_for_status()
    return r.json()["data"]

print(quote("sh600519"))

Either way, the numbers now come from a call you can re-run. That is the whole difference between a tool and a story.


Step 4 — Ask a question, require the number

Now ask the agent something that forces it to use the tool:

“Using the quote tool from the data source, pull the last 30 daily bars for sh600519 and tell me the highest close and the date it happened.”

Watch for two things:

  1. Did it call the tool? If it answered without a call, the number is invented.
  2. Can you re-run the call and get the same answer? If yes, the result is reproducible — rule 2 from the standards page.

Step 5 — Check the answer

This is the part most walkthroughs skip. Do not skip it.

Check How
Is the number from a call? Look at the tool call in the transcript. No call, no number.
Is the window stated? “The last 30 daily bars” — not “recently”.
Is the field named? last, not “price”. Different fields mean different things.
Does it reproduce? Re-run the call. Same answer, or it is not a result.

A number you cannot trace back to a call is not data — it is text.


What you have now

You closed the loop: the agent asks, the tool answers with a number, the code computes, the model explains.

That is the first half of the question from the definition page. The second half — judging whether a strategy built on top of this is trustworthy — is a different walkthrough: look-ahead protection, costs, and survivorship bias.

The barrier was never “the tools are weak”. It was “you must learn to program first”. You just did this without writing a strategy — you described what you wanted and checked the answer.


Next

Sources