Strategy Validation

Backtesting API
for AI Trading Agents

Never deploy real capital on an untested strategy. Purple Flea's backtesting API runs your agent's logic against 3 years of historical data with realistic fees, slippage, funding rates, and liquidation mechanics.

Get API Key โ†’ Start Validating

Why Backtesting Is Non-Negotiable

Every strategy looks good in your head. The backtest is where reality meets theory. A momentum strategy that seems compelling โ€” buy breakouts, ride the trend โ€” may produce dismal results in the actual historical data when fees, slippage, and realistic signal timing are accounted for. Or it may work spectacularly. You will not know until you test it.

Purple Flea's backtesting API gives your agent access to 3 years of minute-level OHLCV data, funding rate history, and liquidation price calculations for all 275 markets. You can test your strategy in isolation or in a portfolio context, with realistic assumptions about execution.

The key metrics to evaluate are not just returns. A strategy with a 200% total return but a -60% maximum drawdown may be psychologically and financially impossible to operate through. The Sharpe ratio normalizes returns by risk. Win rate tells you how often your agent is right. Average win-to-loss ratio tells you how much it makes when right versus how much it loses when wrong. All of these are available in the backtest output.

Python: Running a Complete Backtest

Define your strategy as a function and pass it to the backtest runner. The engine calls your function on each historical candle, applies your orders, and tracks all positions with realistic fees and slippage:

import purpleflea

bt = purpleflea.BacktestClient(api_key="YOUR_KEY")

# Define a simple momentum strategy
def momentum_strategy(candle, context):
    """Buy when RSI crosses above 50, sell when below 50."""
    if context['prev_rsi'] < 50 and candle['rsi_14'] >= 50:
        return {"action": "buy", "size_usd": 1000}
    elif context['prev_rsi'] >= 50 and candle['rsi_14'] < 50:
        return {"action": "sell", "size_usd": 1000}
    return {"action": "hold"}

# Run backtest
result = bt.run(
    strategy_fn=momentum_strategy,
    market="BTC-PERP",
    start_date="2023-01-01",
    end_date="2025-12-31",
    initial_capital=10000,
    maker_fee_bps=2,
    taker_fee_bps=5,
    slippage_bps=3,
    include_funding=True
)

print(f"Total return: {result['total_return_pct']:+.1f}%")
print(f"Sharpe ratio: {result['sharpe_ratio']:.2f}")
print(f"Max drawdown: {result['max_drawdown_pct']:.1f}%")
print(f"Win rate: {result['win_rate_pct']:.1f}%")
print(f"Total trades: {result['total_trades']}")

Example output:

Total return: +147.3%
Sharpe ratio: 1.84
Max drawdown: -22.1%
Win rate: 58.3%
Total trades: 247

Backtest Parameters โ€” What to Include

A backtest is only as good as its assumptions. Including all costs makes your results more conservative but far more reliable:

Parameter Recommended Value Why It Matters
maker_fee_bps 2 bps (0.02%) Reflects Purple Flea actual maker fee
taker_fee_bps 5 bps (0.05%) For market orders and aggressive entries
slippage_bps 2โ€“5 bps Real fills differ from backtested prices by this amount
include_funding True Funding paid/received every 8h can significantly alter P&L
include_liquidation True Leveraged strategies must model liquidation risk
data resolution 1-minute candles Higher resolution catches intraday signals accurately

Common Backtesting Mistakes

Avoid these pitfalls โ€” they are the reason most backtested strategies fail in live trading.

Overfitting
Tuning parameters until they perfectly fit historical data creates a strategy that works on the past but fails on new data. Use out-of-sample testing: train on 2023โ€“2024, validate on 2025. If performance collapses out-of-sample, you have overfit.
Look-Ahead Bias
Accidentally using future data in your signals โ€” e.g., calculating a daily close RSI and trading at the open of the same day. The close wasn't known at the open. Purple Flea's backtester enforces strict temporal ordering to prevent this.
Ignoring Fees
A strategy that trades 10 times per day may show 50% annual returns before fees. At 0.05% per trade, that is 10% in annual fee costs โ€” reducing returns by 20% or more. Always include realistic fee estimates, especially for high-frequency strategies.
Survivorship Bias
Backtesting only on markets that exist today excludes delisted tokens. In crypto, many altcoins have gone to zero. Testing on today's survivors overstates expected returns from broad altcoin strategies. Use Purple Flea's full historical market list including delisted assets.
Ignoring Slippage
In a backtest, your market order fills at exactly the close price. In reality, a $10,000 market order in a thin altcoin may move the price against you by 0.1โ€“0.5%. For small-cap perps, always add 5โ€“10 bps of slippage to each trade.
Single-Period Testing
A strategy that works during bull markets may catastrophically fail in bear markets, and vice versa. Test across multiple market regimes: the 2022 bear market, the 2023 recovery, the 2024 bull run. If it works across all three, it is more robust.
3yr
Historical data available for backtesting
275
Markets with full OHLCV + funding history
1min
Minimum candle resolution for precise simulation
12+
Performance metrics in every backtest result
20%
Referral commission on trading fees
Free
Backtesting API included with all plans

From Backtest to Live Trading

The proper workflow from idea to live deployment:

  1. Develop hypothesis โ€” articulate why your strategy should work. "Momentum exists because of slow information diffusion" is a better hypothesis than "RSI crossovers work." A clear thesis makes you less likely to overfit.
  2. In-sample backtest โ€” run on 2023โ€“2024 data. Tune parameters to improve performance, but limit tuning to 3โ€“5 parameter iterations maximum. More iterations risk overfitting.
  3. Out-of-sample test โ€” run on 2025 data without any parameter changes. If Sharpe ratio and returns hold within 30% of in-sample results, the strategy has passed validation.
  4. Paper trade โ€” run the strategy with real-time data but no real capital for 2โ€“4 weeks. Monitor for implementation issues, API errors, and signal timing.
  5. Scale gradually โ€” start with 10% of intended capital. Validate that live performance matches paper trading. Scale up over 4โ€“8 weeks.

Get $1 USDC free to start live: New agents can claim $1 USDC from the Purple Flea faucet to place their first real trades after backtesting. Zero risk for your first experiments with live market data.

Validate Before You Deploy

Backtest Your Strategy
Against 3 Years of Data

Get your API key, run your first backtest in minutes, and deploy only strategies that have proven themselves in simulation.

Get API Key โ†’ Read Best Practices โ†’

Related Pages

Trading API โ†’ Risk Management โ†’ Momentum Strategy โ†’ Mean Reversion โ†’ Kelly Criterion โ†’ Backtesting Blog โ†’