Hands-on: Connecting Market Data and Charts
On this page9 items
In 10 seconds
A hands-on example of connecting external data via MCP and verifying indicators and ideas yourself.
Placing orders and automated trading are not covered. Decision-making is discussed in No.69.
No.48 covered building your own server. Here we walk through one concrete example. The subject is market data, but what we’re doing is the general pattern of connecting to an external data provider, fetching data and verifying it.
Let’s define the scope first. This page covers data retrieval, visualization and verification only. Placing orders and automated trading are not covered. Investment decisions themselves are covered in No.69.
Why this subject#
There are three reasons.
External service specifications change often. Data providers’ APIs change without notice. It is a good way to practice designing on the assumption that things will break.
The need for verification is obvious. Everyone knows that good results on past data don’t guarantee the future. It’s practice in not taking AI answers at face value.
It is fully read-only. If you’re not placing orders, you don’t need any tools that write. “Start with read-only” from No.47 applies as is.
How to connect#
I won’t name specific services. Service availability and specifications change frequently, so anything written would be outdated as soon as it’s written. Instead, here is the pattern.
If an MCP server already exists. Check that service’s official documentation and connect using the steps in No.46. Authenticate with read-only permissions.
If not, build your own. Following the pattern in No.48, two or three tools are enough.
@mcp.tool()
async def get_price_history(symbol: str, days: int = 90) -> str:
"""Fetch daily closing prices for the given symbol.
Only closing prices are available. Order book data and trade history are not handled.
Some days may be missing due to the data provider. Missing days are returned explicitly as "none".
Args:
symbol: ticker symbol
days: number of days to fetch. Default 90, maximum 365
"""The key is to write the out-of-scope items and how missing data is handled in the description (No.48). If missing data is silently filled in, the verification itself becomes meaningless.
Usage pattern#
Stage 1: fetch and save to a file.
Fetch the closing prices for (symbol) over the past year and save them to data/prices.csv.
If any days are missing, make a separate list in data/missing.md.As in Chapter 4, No.30, the key is to have it save to a file. That way you can verify later.
Stage 2: visualize.
Read data/prices.csv and chart the closing prices over time.
Output to data/chart.png. Don't connect the line across missing days; break it.That one line about not connecting the line across missing days quietly makes a big difference. If you connect it, data that didn’t exist looks as if it did.
Stage 3: test an idea.
This is the main event.
Using data/prices.csv, test the following idea.
Idea: (the hypothesis you want to test, in one sentence)
・Show the verification steps first, then stop
・Write both the results on past data and the reasons those results may not be reliable
・Treat "these results are not enough to decide" as a possible conclusionThe last line is the most important. Without it, some conclusion will come out regardless. As explained in No.02, Claude tends to produce “a likely answer” rather than “I don’t know.”
What to always ask during verification#
Ask this about the results you get.
About the verification you just did, tell me the following.
・Would the results change with a different data period?
・Would the results change with a different symbol?
・What has this verification not confirmed?
・How likely is it that these results arose by chance?Pick a convenient period, and most hypotheses will appear to hold. These questions are for knocking that down yourself.
And good results on past data don’t guarantee the future. No.69 repeats this as well.
Assume things will break#
External services change. They belong in the freshness ledger (this guide checks them every 60 days).
Things worth building into the design:
- Stop if fetching fails. Don’t continue with stale data
- Save the fetch date and time along with the data
- Have it report when the count differs from what’s expected (“count the records” from Chapter 4, No.33)
The third one pays off. When you ask for a year’s worth and should get 250 records but get 80, if you don’t notice, the whole verification goes wrong.
Try it: design the verification from the refuting side (5 minutes)
You don’t need to actually connect any data. This is design practice.
Prompt
“`
Design a procedure to test the following hypothesis.Hypothesis: (what you want to check, in one sentence)
・First, write “what data would be observed if this hypothesis were wrong”
・Then design a verification procedure that makes that observation possible
・Point out where the conclusion could change depending on which period is chosen
“`Expected result: “what being wrong looks like” comes first, then the procedure. Places where the choice of period could change the conclusion are pointed out.
If it doesn’t work: if it only produces procedures that confirm the hypothesis, the first instruction isn’t working. Ask only for “write what refutation looks like first,” and stop there.
Common pitfalls#
Not noticing missing data. This is the most common failure. Always have it report counts and missing days.
Settling for a convenient period. Re-test with different periods. That alone brings down many hypotheses.
Building tools that write. They aren’t needed within the scope of this page. Reading alone is enough.
Tying verification results directly to decisions. Verification is for understanding the nature of the data. Decisions are the subject of No.69, and even there specific stock recommendations and trade timing are not covered.
Related pages#
- No.48 Building your own MCP server — how to connect
- No.69 Learning about investing and money — the approach to decision-making
- No.80 Working with AI on the assumption it will make mistakes — verification procedures
Summary#
- The scope is retrieval, visualization and verification. Placing orders and automated trading are out of scope
- Write what’s out of scope and how missing data is handled in the description
- State explicitly that “cannot decide” is a possible conclusion
- External services change. Design it to stop when fetching fails