BacktestingAgentic tradingDevelopersRisk

How to backtest AI trading strategies through a single API

Backtesting AI trading strategies through a single API lets you test logic across stocks, crypto, perps, options, and prediction markets without risking capital.

By the Felix team9 min read
Key takeaways
  • 01Backtesting through a single API normalizes market data and order sizing so you can test one strategy across stocks, crypto, perps, options, and prediction markets without rewriting logic.
  • 02Paper trading mode uses the same request shape as live trading, which means your agent's code path stays identical while it practices with fake balances.
  • 03You should define your strategy's entry rules, exit conditions, position limits, and budget cap in code before running history, not after you see the results.
  • 04Dollar-based order sizing abstracts away venue-specific contract math, but you still need to check that the backtest respects slippage and liquidity assumptions for each market type.
  • 05A clean backtest is only useful if you log every simulated decision with the same rigor you would use in production, because the goal is to validate the agent, not just the strategy.

Backtesting an AI trading strategy through a single API means running the agent's full decision loop against historical or simulated market data across stocks, crypto, perpetual futures, options, and prediction markets without risking real capital. The API normalizes order sizing, market data, and execution semantics into one shape, so you can test the same logic against every asset class from a single code path. This lets you observe how the agent behaves under past conditions before you authorize it to spend real money.

What does backtesting through a single API actually mean?

A traditional backtest often runs on a single CSV of prices for one symbol. When you work through a unified API, the scope is wider. You are testing whether your agent can interpret signals, allocate capital, and manage orders across five distinct market types without rewriting its core logic for each. Stocks settle through a stock broker. Crypto trades on chain or through a crypto venue. Perpetual futures carry funding rates and margin requirements. Options involve strike prices, expirations, and greeks. Prediction markets resolve to binary outcomes and have unique liquidity dynamics. The API does not erase these structural differences, but it abstracts the interface. Your agent sends orders denominated in plain US dollars, and the API translates those into the contract sizes, lot increments, and margin math that each venue requires. This means your backtest code can treat a 500 dollar stock position and a 500 dollar crypto position similarly at the entry point, even though the underlying risk profiles diverge. The practical benefit is that you spend less time writing adapters and more time refining the agent's decision rules. You can test how a single strategy allocates across correlated assets during a stress event, or how a portfolio rebalancing rule behaves when stocks and crypto move in opposite directions. You can also test how the same risk model behaves when a stock broker halts a symbol while a crypto venue keeps trading, which is difficult to simulate accurately if you maintain separate codebases for each market. All of this happens from one codebase, which makes cross-market tests and portfolio-level risk checks far easier to implement.

How do you set up a paper trading environment for an AI agent?

Paper trading mode is the safest place to start because it exercises the full execution path without touching real capital. In this mode, the agent sends requests to the API exactly as it would in live trading, but the orders are tagged as simulated. The API returns fills, portfolio updates, and PnL as if the trades were real. This parity is deliberate. If your agent works correctly in paper, the only change for live trading is flipping an authorization flag on the key. You should begin by creating a dedicated paper budget and configuring the same guardrails you plan to use in production. Set a daily or total budget cap, define position limits, and attach an exit plan. Even in simulation, you should test the panic switch. Trigger it intentionally and verify that it flattens positions and revokes the agent's access. If you connect through MCP tools from an AI code editor, you can watch the agent reason about trades in real time and intercept mistakes before they become habits. The exact request schema is in the docs; the shape looks like this:

curl -X POST https://api.felix.trade/v1/order \
  -H "Authorization: Bearer YOUR_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "mode": "paper",
    "asset": "BTC",
    "side": "buy",
    "notional_usd": 500
  }'

This example is illustrative. The actual endpoint paths and field names may differ, so consult the official documentation for the current schema. The important pattern is that the agent expresses intent in dollars and the API handles the rest. Log every request and response during this phase. These records become your baseline for comparing paper behavior against live behavior later. You should also compare the paper portfolio state against the agent's internal model of its holdings. If the agent thinks it owns 1000 dollars of an asset but the API reports 950 because of a simulated partial fill, you need to know why. These discrepancies reveal assumptions in your sizing logic that will matter in live trading.

What strategy logic should you define before running history?

The most common backtesting mistake is to let historical results write the strategy. You run a scan, notice a pattern that would have been profitable, and then codify rules that capture that exact pattern. This is overfitting, and it produces agents that collapse when the market shifts. Instead, commit your entry rules, exit conditions, position sizing formula, maximum budget, and allowed drawdown to code before you load any historical data. Treat these parameters as fixed during the test. The API supports scoped keys and budget caps that act as hard infrastructure limits. These are useful because an AI agent can reinterpret prompts or context in ways a traditional bot cannot. If the LLM decides to increase exposure based on a vague headline, a hard API cap blocks the order. This is described in more detail in How to set guardrails for a trading agent without giving up custody. Position sizing deserves particular scrutiny. Dollar-based orders simplify the interface, but they do not normalize risk. A 500 dollar position in a large cap stock and a 500 dollar notional position in a perpetual future carry different volatility and leverage profiles. Define your sizing in terms of risk per trade or portfolio heat, not just notional amount. For a deeper look at sizing errors, see Common position sizing mistakes when letting an AI agent trade real money.

How do you account for market differences across five asset classes?

The API normalizes the interface, but it cannot normalize market structure. Your backtest must reflect the reality of each asset class. Stocks have overnight gaps, earnings halts, and opening auctions. Crypto markets trade continuously and can gap on infrastructure issues or protocol news. Perpetual futures accrue funding payments every few hours, which means a flat price can still produce a loss for the holder. Options experience theta decay and nonlinear moves near expiration. Prediction markets have binary payoffs, wide spreads, and liquidity that often concentrates near the event date. If your backtest assumes instant fills at the mid price with no friction, you are building a fantasy. You should model slippage, partial fills, and holding costs for each market type your agent will touch. For example, suppose your agent tries to buy 5000 dollars of a low volume prediction market contract. In reality, that order might clear the order book and move the price significantly. Your simulation should reflect that possibility. One practical approach is to run the agent in paper mode across each asset class individually before combining them. Compare the simulated fills against the quoted prices and use that data to calibrate your slippage assumptions. This step is tedious, but it is what separates a realistic test from an optimistic curve.

What makes a backtest trustworthy enough to act on?

Trust comes from process, not from a green equity curve. A trustworthy backtest is reproducible, conservative, and fully logged. Start by recording every decision the agent makes. Capture the prompt context, the reasoning trace, the market data snapshot, and the resulting order. This audit trail is essential for debugging and for proving that the agent acted within its mandate. How to evaluate audit logs and observability for trading agents through one API explains how to structure this. Next, enforce out-of-sample testing. Reserve the most recent 20 percent of your data for validation and do not let the agent or the developer see it during strategy construction. If the strategy fails on the holdout set, the in-sample results were likely overfit. Walk-forward analysis is stronger still. Parameterize on a rolling window, test on the next block, and repeat. This simulates the experience of adapting to new market regimes. You should also version control everything. Lock the strategy code, the API client version, the dataset date range, and the random seeds if your agent uses probabilistic reasoning. A backtest that cannot be re-run exactly is not a valid test. It is a story. Finally, simulate adverse conditions. Introduce latency spikes, market halts, and conflicting signals to see if the agent respects its guardrails or generates orphaned orders. A backtest that only runs under ideal conditions is not a safety test. It is a marketing slide.

When is it safe to move from paper trading to live capital?

Transition to live capital when the agent's behavior is predictable, not when its paper PnL is highest. Predictable means you can state the strategy in plain language, you have observed it respect limits during stress tests, and your logs prove it acted consistently over a meaningful period. Live trading requires explicit owner authorization of a key. Without that authorization, the API rejects real orders regardless of what the agent requests. This is a non-custodial safety feature. When you do authorize live trading, start with a small budget. How to start an AI agent with a small budget outlines practical steps for this phase. Remember that trading can lose money, including everything. A clean backtest does not guarantee future performance. Markets evolve, liquidity shifts, and an agent that behaved well on paper may encounter real slippage and partial fills that change its economics. The goal of backtesting is to eliminate obvious errors and build confidence in the process, not to predict profits. Scale your capital only after weeks of live trading confirm that the agent behaves as it did in simulation. If the agent diverges, stop, review the logs, and return to paper mode until you understand why. Many developers rush this step because they trust the math. Math is necessary, but markets are not math. They are collections of humans and algorithms reacting to uncertainty. Your backtest must respect that uncertainty, and your live rollout must respect it even more.

Frequently asked questions

Can I use the same API key for paper trading and live trading?

The same key can access paper trading, but live trading requires explicit owner authorization. You should keep the scopes separate in practice to avoid accidental live orders during testing. A dedicated paper key with its own budget cap is the safest approach.

Does paper trading use historical data or live market feeds?

Paper trading can operate against live market feeds with simulated balances, while historical backtests use recorded data. Both are useful, but they test different things. Paper trading validates real-time execution logic and API integration, while historical backtests validate strategy logic against past market regimes.

How do I prevent my agent from overfitting during backtesting?

Define your entry rules, exit conditions, and position sizing before you look at historical results. Test on out-of-sample data and avoid optimizing parameters to fit a single equity curve. If you tweak the strategy after seeing the backtest, you must test it again on fresh data.

Are trading costs included in backtest simulations?

The API can apply estimated fees and slippage, but these are approximations. You should verify that your assumptions match the costs of the specific venues you plan to trade on. Underestimating fees is one of the fastest ways to turn a profitable backtest into a losing live strategy.

Can I backtest options and prediction market strategies the same way as stocks?

The API accepts dollar-sized orders for all five market types, but your strategy must account for different liquidity profiles, expiration behavior, and payoff structures. Do not assume that identical notional sizes mean identical risk. An option near expiration can move dramatically even if the underlying is calm.

What is the difference between backtesting a bot and an AI agent?

A bot follows fixed rules, while an agent can reinterpret prompts or context. Backtesting an agent must include the full LLM reasoning loop and prompt history, because the risk lies in unexpected interpretations, not just bad signals. The API logs help you trace exactly what the agent was thinking when it placed each order.

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.