BacktestingDevelopersPaper tradingRisk management

How developers can backtest AI trading strategies before going live

Backtesting AI trading strategies requires historical data, a clear signal, and a sandbox. Developers can validate logic and API behavior before real money is at risk.

By the Felix team10 min read
Key takeaways
  • 01Backtesting is a simulation that validates agent logic and API integration, not a guarantee of future profits.
  • 02Reproducible backtests require pinned prompts, frozen data, deterministic model settings, and version control.
  • 03Historical data must span multiple market regimes and avoid lookahead bias, survivorship bias, and over-cleaning.
  • 04Out-of-sample testing and conservative cost assumptions are necessary to detect overfitting before live deployment.
  • 05Live trading requires hard infrastructure limits, including spend caps, drawdown limits, and a kill switch, regardless of backtest performance.

Backtesting an AI trading strategy means running the agent's decision logic against historical market data to see what orders it would have generated without risking real money. Developers should start with a paper trading environment, define clear entry and exit rules, and compare the agent's simulated trades against a simple benchmark. The goal is to catch logical errors, unrealistic assumptions, and overfitting before the agent ever touches a live market. A clean backtest does not promise future profits, but it does prove that the agent understands the instructions and that the integration pipeline works end to end.

What does backtesting an AI trading agent actually involve?

Backtesting is a simulation, not a crystal ball. It replays historical market conditions through your agent's reasoning loop to generate a simulated track record of trades. The agent consumes price, volume, or other signals, applies the rules you encoded in its prompt or code, and emits orders that are caught by a paper execution layer instead of a live venue. This only works if the simulation respects market friction. You must assume that market orders fill at the next available price, not the idealized close, and that limit orders may only partially fill or not fill at all. If your simulation gives the agent perfect fills on every signal, the backtest becomes a work of fiction. You should also account for latency. An agent that decides to trade at 10:00:00 may not see its order acknowledged until seconds later, and the price can move in that interval. On Felix, the paper trading environment uses the same API contracts as live trading, so the agent's path from decision to order does not change when you flip from paper to real. That continuity is critical because it catches integration bugs, serialization errors, and prompt ambiguities under realistic conditions. Whether the target is a stock broker, a perps venue, an options venue, or a prediction market, the paper layer normalizes order sizing in plain US dollars, but the simulation still respects venue-specific rules for trading hours and tick sizes. A backtest that runs on a separate code branch is merely a prototype. It will not tell you whether the production agent will survive when it meets real market noise.

How do you build a reproducible backtesting environment?

Reproducibility separates engineering from guesswork. If you run the same backtest twice and get different trade sequences, you cannot trust the results. Start by pinning every variable. Lock the model version, the prompt text, and the tool definitions in version control. Set the temperature to zero or use deterministic sampling so that the agent's reasoning does not drift between runs. Freeze your market data into a static snapshot with timestamps, rather than letting the agent fetch live feeds that change between tests. Log every reasoning step, every intermediate calculation, and every API call into structured output so you can diff two runs and find the first divergence. Containerize the execution environment if necessary, because dependency shifts in numeric libraries can produce subtle differences in indicator values. Even something as minor as a floating point rounding change can alter a threshold comparison and flip a buy signal to a hold. If your agent uses external tools, mock them or pin their versions too. Reproducibility also makes collaboration possible. Another developer should be able to clone your repo, point the agent at the same frozen data, and reproduce your equity curve exactly. Without that standard, backtesting is just storytelling. When you do encounter non-determinism, trace it methodically. Start with the LLM output, then move to the data feed, then to the execution wrapper. Fix one layer at a time until the runs align. The exact request schema is in the docs; the shape looks like this:

{
  "name": "place_paper_order",
  "arguments": {
    "market": "example-perp",
    "side": "buy",
    "size_usd": 200,
    "order_type": "market"
  }
}

After each run, compare the emitted order sequence against a reference. If a code change does not alter the logic but changes the output, you have introduced a regression. Document the data snapshot hash and the commit hash alongside every result so you can always return to a known state.

What data should you use to validate your agent's logic?

Data quality determines backtest quality. A strategy tested on clean, curated data will collapse when it meets the messy reality of gaps, bad ticks, and delayed prints. Start with historical data that spans multiple market regimes, including periods of high volatility and low liquidity. A mean reversion strategy that looks brilliant during a calm trend may fail when volatility spikes. Do not test only on the asset or market where you plan to deploy. If your agent is meant to trade across stocks, crypto, perps, options, and prediction markets, you need representative data for each category. Stocks have overnight gaps and corporate actions. Perps have funding rates and mark prices. Prediction markets have binary resolutions and expiration dates. Options have path dependence and nonlinear payoff structures. Survivorship bias is another trap. If you test a stock strategy only on companies that still exist today, you erase the bankruptcies and delistings that would have destroyed capital in real time. Use point-in-time databases that include dead instruments. For prediction markets, ensure you have the historical probability estimates and the final resolution outcomes, not just the result. You must also avoid lookahead bias. Never feed the agent information it would not have had at the time of the decision. This sounds obvious, but it creeps in through adjusted close prices, retroactive oracle updates, or future knowledge embedded in training data. Use point-in-time data snapshots. If you are testing a multi-agent system, align the timestamps across all data feeds precisely. A clock skew of a few seconds between a stock broker and a perps venue can create imaginary arbitrage opportunities that never existed. Data granularity matters too. A strategy built on hourly candles may behave differently on minute data because it will see more noise and more entry points. You should test at the same granularity you intend to use in production, or you risk discovering that your signal is only noise at finer time scales.

How do you measure results without overfitting?

Overfitting is the most common way developers fool themselves in backtesting. It happens when you tweak prompts, parameters, or indicator thresholds until the historical equity curve looks perfect. A perfect backtest is usually a warning sign, not a victory. To avoid this, split your data into an in-sample set for development and an out-of-sample set for final validation. Touch the out-of-sample data only once, at the very end of your cycle. If performance drops sharply, you have fit noise rather than signal. Walk-forward analysis helps by repeatedly testing on a rolling window, which better approximates how the agent will encounter new data in production. Measure risk, not just return. Track the maximum drawdown, the distribution of trade sizes, and the frequency of consecutive losses. Compare the agent against a naive benchmark, such as buying and holding the same exposure. If the agent cannot beat a simple benchmark by a margin that covers transaction costs, it has no edge. Estimate transaction costs conservatively. Assume that every trade pays more in slippage and fees than the venue advertises, because large orders and fast markets always cost more than the top of book. If the strategy cannot survive pessimistic cost assumptions, it will not survive reality. Remember that trading can lose money, including everything. A backtest that shows gains offers no protection against future losses. The market does not owe you the continuation of any historical pattern. You can also run a randomization test. Have the agent trade on scrambled or random data. If it still produces a positive return, your logic is not actually reading the market. It is exploiting a leak in your simulation. True edge should disappear on noise.

What safety controls are required before live trading?

A successful backtest is not a graduation certificate. It is simply permission to start thinking about live deployment with extreme caution. Before you authorize a live key, you must install safety controls that the agent cannot bypass. This is not optional. Developers should set spend caps and drawdown limits that restrict the agent to a budget you can afford to lose entirely. Position limits prevent the agent from concentrating too much capital in a single market or instrument. An exit plan defines when the agent must close positions, such as before a weekend or ahead of a known event. The panic switch must be able to flatten all positions and revoke the API key instantly, without the agent's consent. Trading APIs keep AI agents safe by enforcing these constraints at the infrastructure layer, not inside the agent's prompt. The agent may request a trade, but the API rejects anything that violates the owner-approved guardrails. Because Felix is non-custodial, the agent never holds your funds. They remain in a wallet you control, and withdrawal addresses are owner-approved only. This architecture removes the risk of theft, but it does not remove market risk. You can still lose the entire budget you allocate to the agent. Scoped keys add another layer. You can issue a key that is only valid for specific markets or order types, so even if the agent hallucinates a dangerous trade, the infrastructure blocks it. Budget caps act as a hard ceiling. Once the agent reaches the limit, it cannot place further orders until you reset the allowance.

How do you transition from backtest to live trading responsibly?

The jump from paper to live is where many strategies die. Live markets introduce latency, partial fills, price impact, and psychological pressure that no simulation fully captures. Treat the transition as a second validation phase, not a launch. Start with the smallest capital allocation that still allows the strategy to generate meaningful signals. Run the live agent in parallel with a continued paper instance for at least a week, comparing the two. If the live fills diverge materially from the paper simulation, stop and investigate before adding capital. A developer's guide to agentic trading systems covers patterns for structuring this rollout, including how to shadow trade and how to stage capital increases. Keep a decision log of every anomaly, every manual override, and every unexpected market event. The goal of the first live weeks is not profit. It is to confirm that the agent behaves in the wild the way it behaved in the lab. Only after you have explained every divergence should you consider scaling up. Even then, never remove the kill switch. The most dangerous moment in a strategy's life is the period just after a successful backtest, when overconfidence is highest and risk controls feel unnecessary. If the agent starts trading faster, larger, or more frequently than it did in simulation, treat that as a bug, not a feature. Pull the key, review the logs, and fix the logic before reactivating. Scaling should happen slowly, in steps, with new limits tested at each level of capital.

Frequently asked questions

Can I backtest without writing code?

No. Backtesting requires integrating the agent with the API or MCP tools so that the decision loop and order emission paths are realistic. You can use paper trading to avoid risking capital during development, but the agent still needs a defined logic layer.

How long should my backtest period be?

There is no universal minimum, but a few weeks of data is rarely enough. You should cover multiple market regimes, including periods of high volatility and low liquidity, so the agent encounters conditions that challenge its assumptions.

Does paper trading guarantee that live results will match?

No. Paper trading approximates fills and latency. Live markets have slippage, partial fills, and price impact that simulations cannot replicate perfectly. The purpose of paper trading is to catch logical errors, not to predict exact future returns.

Can I backtest strategies for options and prediction markets?

Yes, but you need historical data that respects the specific mechanics of those instruments. Options have path dependence and nonlinear payoffs, while prediction markets have binary resolutions and expiration logic. The API normalizes order sizing in USD, but the underlying math still matters.

What is the most common backtesting mistake?

Overfitting to historical noise and ignoring transaction costs. Developers often tweak prompts until the equity curve looks perfect on past data, which creates a strategy that fails when the market changes. Trading can lose money, including everything.

Should I go live immediately after a successful backtest?

No. Install spend caps, drawdown limits, position limits, and a kill switch first. Start with the smallest viable capital allocation and run the live agent parallel to paper trading until you have explained every divergence.

Give your agent a key.

One key to trade stocks, crypto, perps, options, and prediction markets. Live after owner authorization.

Keep reading

Not a brokerage, exchange, or investment adviser. Not investment advice. Trading involves risk, including total loss.