What is a trading API? How programs talk to your broker
- An API (application programming interface) is a set of web addresses a program can call instead of a person clicking buttons: get my balance, get prices, place this order.
- Each call is a request with a response. Your program proves who it is with a key or token, like a password made for software.
- Trading APIs have two halves: market data (prices coming in) and orders (instructions going out), plus account information.
- Live prices usually arrive over a streaming connection that stays open, rather than repeated requests.
- Treat API keys like the keys to your account, and check your broker's or firm's rules on API use.
When you click Buy on your trading platform, the platform sends a message to your broker saying, in effect, "buy one contract of this market at the market price, for this account". An API lets your own program send that same message. Nothing more mysterious than that. Automated trading is a program that sends those messages based on your rules instead of your clicks.
Requests and responses
Most broker APIs work like the web. Your program sends a request to an address, and the broker sends back a response. A request to place an order might carry this information:
{
"account": "your account id",
"symbol": "MNQ",
"side": "buy",
"quantity": 1,
"type": "market"
}
The response says whether the broker accepted it and gives the order an id, which your program uses later to check on it, change it or cancel it. If something is wrong (the market is closed, the account doesn't have enough margin, the size is over a limit), the response says that instead, and your program has to handle it.
The format above is JSON, a plain-text way of writing labelled values. Almost every modern API uses it, and it's readable by people as well as programs.
Proving who you are: keys and tokens
An API has to know a request really comes from you. Brokers do this with an API key, a long secret string you create in your broker's settings, often exchanged for a short-lived token that goes with every request. Anyone with your key can do whatever the key allows, so:
- Never paste a key into a chat, a forum post, a screenshot or shared code.
- If the broker lets you limit a key (read-only, or no withdrawals), use the smallest permission that does the job.
- Store keys encrypted, and delete a key you no longer use.
The three parts of a trading API
Account information
Balances, open positions, working orders and past fills. Journals use this part: TradeHarbor's broker auto-import (on Pro) reads your fills and positions this way, with a read-only connection where the broker offers one.
Market data
Prices. Historical bars (each 1 or 5-minute bar's open, high, low and close) are usually fetched with a request, which is how a backtester gets its history. Live prices are different: requesting them over and over would be slow and wasteful, so brokers offer a streaming connection that stays open and pushes each new price or bar to your program the moment it happens.
Orders
Place, modify and cancel. Most APIs support the same order types you'd use by hand: market, limit, stop. A common pattern is a bracket: the entry, plus a stop and a target that are placed as soon as the entry fills, with one cancelling the other when either is hit.
Limits and failures
- Rate limits. Brokers cap how many requests you can send per second or minute. A program that loops too fast gets refused.
- Errors. Requests can fail: a timeout, a dropped connection, an outage at the broker. A well-built program checks every response and has a plan for each failure, such as retrying, or flattening and stopping.
- Reconnecting. Streaming connections drop from time to time. The program has to notice, reconnect, and check whether anything filled while it wasn't listening.
Rules about API trading
Not every account may use an API, and not from anywhere. Some prop firms restrict automated or API trading, the places it can run from (such as servers or third-party services), or don't allow it on funded accounts at all. Brokers have their own terms. Read the current rules for your account before you connect a program to it.
How TradeHarbor uses broker APIs
- Importing trades (Pro): reads your fills through a compatible broker API, such as TopstepX. Your credentials are encrypted and can be removed any time.
- Backtesting (Pro): requests historical bars through your own ProjectX connection.
- Automation (beta): streams live bars from your broker and runs your strategy on them, placing its orders on your account, or in shadow mode, recording what it would do without placing any.
- Risk guards (beta): on accounts linked by API, can close positions and cancel orders when one of your own limits trips. This is opt-in.
Next steps
See how these pieces come together for a single trade in how automated trading works, or start from the beginning with how to automate a trading strategy.