Claude Code can write a trading strategy in seconds. The hard part is knowing whether the backtest it shows you is real. This guide connects Claude Code to OptAlgo, so the code your agent writes runs on a backtest engine that grades its own honesty, and walks you from an idea to a bot running on paper. Everything here works on the Free plan, with no card.
What you need
- Claude Code, installed in your terminal.
- A free OptAlgo account: sign up here.
- An idea you can say in one sentence, for example “buy BTC on a new 20-bar high on 4h with a 2 ATR stop”.
You do not need an exchange account to start. Every step below runs on paper.
Step 1: Connect Claude Code to OptAlgo
The quickest way is the OptAlgo MCP connector. Add it once from your terminal:
claude mcp add --transport http optalgo https://mcp.optalgo.com/mcp
Then type /mcp inside Claude Code, pick optalgo, sign in to OptAlgo and approve. It uses OAuth, so no key is pasted anywhere. The connector is paper-only by design: it can backtest and run paper bots, and it can never place a live order or change a live bot.
The other way is the REST API with a device login. Ask Claude Code: “Read https://docs.optalgo.com/optalgo-llm.md and connect to my OptAlgo account.” Your agent starts a login, you check the code and approve it in the OptAlgo app, and the key is written straight into a file on your machine that only you can read. It is never printed and never enters the chat. On the Free plan a key can read, backtest and run paper bots. Live trading needs Plus or Pro and a key with the trade scope.
Step 2: Describe the strategy
Tell Claude Code the idea in plain words. It writes a strategy.py with the OptAlgo SDK: vectorised Python with numpy, plain code you can read, download and keep. A few prompts that work:
- “Backtest a BTC breakout: buy a new 20-bar high on 4h, 2 ATR stop, trail it.”
- “Short ETH on 4h when RSI is above 75, 2% stop, take profit at 4%.”
- “Test the same breakout on BTC, ETH and SOL together.”
Strategy code may import only numpy, math and the OptAlgo SDK. It runs in a sandbox, and code that tries to reach files, the network or system modules is refused and counted as a security strike.
Step 3: Validate for free
Before anything costs a unit, your agent validates. Validation queues nothing, loads no market data and costs nothing. It runs your code once on synthetic bars and lists every problem at once: code, symbols, data for the window, plan limits and quota, plus lookahead and non-deterministic code. Each finding comes with its fix, so your agent applies them all in one edit and validates again until it passes.
Step 4: Backtest, and read the grade first
A backtest is charged by run time: one unit is up to 10 seconds. The Free plan has 50 units a month, 3 coins per run and one run at a time. Every result comes back with three things, in this order:
- An honesty grade from A to D. A means every fill settled on real data with fees, funding and exchange rules. D means not realistic: fix it and rerun.
- A search verdict. OptAlgo counts how many variants of this strategy you ran on overlapping data in the last 30 days and checks whether the Sharpe ratio survives that many tries (a deflated Sharpe ratio). The verdict is ok, caution, likely overfit, or no edge when the run lost money.
- Then the numbers: return after fees and funding, max drawdown, trades, and a chart of every entry and exit.
Ask Claude Code to report the grade and the verdict before any return figure. That order is the whole point. A run graded D, or one with no edge, is not something to tune: change the idea instead.
Step 5: Stop tuning by hand
The fastest way to fool yourself is to ask your agent for “a few more variations” fifty times on the same data. Every run is a try, and the best of fifty looks good partly by luck. Ask for one research run instead: a walk-forward that optimises on a training window and judges the result on test windows the optimiser never saw. It answers robust, inconclusive or likely overfit. The overfitting guide explains how to read it, and how to keep a holdout you never touch.
Step 6: Run it on paper
Deploy a backtest graded A to C and OptAlgo runs your code at every bar close in a sandbox, on paper, as a bot in My Bots marked Runs on OptAlgo. The deploy gate always refuses a losing backtest, and refuses one judged likely overfit unless an out-of-sample research run of the same code passed. The Free plan includes one hosted strategy on paper.
While it runs, your agent can use Compare with backtest: a backtest of the same code over the period the bot ran, matched against its real paper trades, trade by trade.
Step 7: Live is your decision
When the paper record holds up, you switch the strategy live in the OptAlgo app, on Plus or Pro. Your agent can suggest it; only you can confirm it. A key that may trade live carries limits the server enforces, such as max allocation, max leverage and a daily loss limit, and every call is logged.
Rules worth giving your agent
- Report the honesty grade and the search verdict before any return.
- Call a single backtest in-sample, never proof.
- Judge results net of fees: a strategy that trades several times a day pays the fee on every entry and exit.
- Never paste, print or copy the API key.
The agent guide at docs.optalgo.com already teaches these rules, so in practice you only need to hold your agent to them.