Guide
How to build an AI trading bot with Claude, step by step
From a written strategy to a bot on a demo account: the prompt for Claude to write the code, AI as a filter on each signal, and the safeguards.
By the NexoTrade team · Updated October 31, 2026 · 9 min read

Key takeaways
- An AI trading bot has two parts: fixed rules that decide and execute, and the AI, which writes the code or reviews each signal before entry.
- Claude is good at the first (writing and explaining code) and useful for the second (a filter that can say “no”). It shouldn’t decide size or stop.
- Before real money: test on past data, then on the exchange’s demo account for at least a few weeks.
- The exchange key gets trading permission only, never withdrawals, plus a daily loss limit that shuts the bot down.
What an AI trading bot is (and isn’t)
A trading bot is a program that watches the market, applies a set of rules and sends orders to the exchange without you sitting there. “With AI” can mean two very different things, and it helps to keep them apart from the start.
- AI as the programmer. You describe the strategy and Claude writes the bot’s code, explains each part and fixes it when it breaks. Once built, the bot doesn’t use AI at all.
- AI inside the bot. Every time the rules fire a signal, the bot asks Claude whether the trade makes sense given the candles. The AI can block an entry; the rules stay in charge.
What it isn’t: an AI that “knows” where price is going. No model does. An AI bot does what you would do with discipline, without getting tired and without breaking its own rules.
What you need before you start
| What | What for |
|---|---|
| Python 3 on your computer | The language the bot runs in. You don’t need much: Claude explains it. |
| An Anthropic API key | Only if the bot will ask Claude on every signal. You create it in the Anthropic console. |
| The ccxt library | Connects to most exchanges with the same code: candles, balance and orders. |
| Your exchange’s demo account | To test with play money. Bitget, OKX and BingX have one. |
| A written strategy | The exact rules for entry, stop, size and exit. Without this, there’s no bot. |
If you’d rather not type anything yourself, Claude Code works on your computer: it creates the project folder, writes the files, installs what’s missing and tests the bot with you.
And before coding anything, look at the strategy on the chart: mark where it would have entered over the last few weeks and where the stop would have gone.
Step 1: write the strategy as rules
A bot doesn’t understand “buy when it looks strong”. It needs rules that can be checked with numbers. Before opening Claude, write down these five things:
- The pair and timeframe (for example, BTC/USDT on 1-hour candles).
- The exact entry condition.
- Where the stop goes, always, before entering.
- How much you risk per trade: a fixed percentage of the account.
- When you exit: a target, a trailing stop or an opposite condition.
Position size isn’t the AI’s call: it comes from your risk and the distance to the stop. With a 1,000 USDT account and 1% risk, a losing trade costs 10 USDT; if the stop is 2% away, the position is 500 USDT, whatever leverage you use.
Step 2: ask Claude to write the bot
With the rules written down, the prompt almost writes itself. The more specific, the better the code. This is a good starting point:
Write a Python bot for BTC/USDT futures that:
- downloads 1-hour candles with the ccxt library;
- computes the 50- and 200-candle moving averages;
- opens a long when the 50 crosses above the 200
and the close is above both;
- places the stop below the lowest low of the last 10 candles;
- risks 1% of the account per trade (the size comes from the stop);
- runs on the exchange's demo account first (DEMO_MODE = True);
- logs every decision to a file, with its reason.
Don't hardcode any key: read them from the environment.Read what it gives you before running it. Ask it to explain each function and what happens in the odd cases: the exchange doesn’t respond, an order is partially filled, the internet drops with a position open. The answers usually turn into fixes.
Step 3 (optional): Claude as a filter on each signal
This is where AI goes into the bot. When the rules fire a signal, the bot sends Claude a summary of the latest candles and asks for an answer in a fixed format. If it says no, the bot stays out. The AI can remove doubtful trades, but never open one the rules didn’t give.
import json
import anthropic
client = anthropic.Anthropic() # reads ANTHROPIC_API_KEY from the environment
INSTRUCTIONS = """You are the risk filter of a trading bot.
You get a signal and a summary of the latest candles.
Answer only with JSON: {"trade": true|false, "reason": "...",
"invalidation": price}. If in doubt, "trade": false."""
def review(signal: dict, candles: str) -> dict:
r = client.messages.create(
model="claude-sonnet-5-5", # whichever Claude model you have access to
max_tokens=300,
system=INSTRUCTIONS,
messages=[{"role": "user", "content": f"Signal: {signal}\nCandles: {candles}"}],
)
return json.loads(r.content[0].text)Three details matter: that it answers only in JSON (so the bot reads it without parsing prose), that it says no when in doubt, and that its invalidation price is compared with your stop. If they’re far apart, something is off.
Step 4: test it on past data and in demo
A bot that was never tested isn’t a bot, it’s a bet. There are two tests, in this order:
- On past data (backtesting). Ask Claude to adapt the bot to run over old candles and report the number of trades, the win rate, the longest losing streak and the maximum drawdown. Include the exchange’s fees.
- On the demo account. A few weeks with play money and the real market. This is where the failures a backtest can’t see show up: orders that don’t fill, slippage and connection errors.
If the backtest looks too good, be suspicious: the strategy is almost always fitted to that data, not to what comes next.
Step 5: the safeguards before real money
- An exchange key without withdrawal permission and, if the exchange allows it, restricted to the bot’s IP address.
- Keys out of the code, in environment variables. Never in a file you upload anywhere.
- A daily loss limit that shuts the bot down when hit, and a maximum number of open trades at once.
- A log of every decision, with its reason: it’s the only thing that tells you why the bot did what it did.
- Start small. With a part of the account you can afford to lose entirely, even if the demo went well.
The most common mistakes
- Letting the AI decide the size or move the stop. Those two always live in the rules.
- Asking Claude on every candle. Asking only when there’s a signal is faster and uses fewer API calls.
- Trusting numbers from the AI without checking them. If a level isn’t in your data, don’t use it.
- Changing the strategy after every loss. A bad streak is normal; change the rules only with data.
Want to see what an AI reads in a chart before building anything? It’s covered in how to analyze a trading chart with AI.
Common questions
Keep reading
Guide · 7 min read
How to analyze a trading chart with AI (and what AI can’t see)
What an AI reads well in a chart screenshot, where it gets it wrong, and how to turn its analysis into a trade plan with a stop.
Educational content. Not financial advice or an investment recommendation. Trading with leverage can make you lose all the capital you put in.