Backtest Your Strategy
Use historical indicator data to validate your strategies before risking real capital. This guide covers building a simple backtesting framework with Python.
Simple RSI Backtest
This example tests a mean-reversion strategy: buy when RSI drops below 30, sell when it recovers above 50.
import requests
from datetime import datetime
API_KEY = "YOUR_API_KEY"
BASE = "https://tickatlas.com/v1"
headers = {"X-API-Key": API_KEY}
def get_historical(symbol, indicator, timeframe, bars=500):
"""Fetch historical indicator values."""
params = {
"symbol": symbol, "indicator": indicator,
"timeframe": timeframe, "bars": bars
}
return requests.get(f"{BASE}/indicator",
headers=headers, params=params).json()
def backtest_rsi(symbol, timeframe="H1", bars=500):
"""Simple RSI mean-reversion backtest."""
data = get_historical(symbol, "RSI_14", timeframe, bars)
history = data.get("history", [])
trades = []
position = None
for i, bar in enumerate(history):
rsi = bar["value"]
price = bar["ohlc"]["close"]
if position is None and rsi < 30:
position = {"entry": price, "entry_rsi": rsi, "bar": i}
elif position and rsi > 50:
pnl = price - position["entry"]
trades.append({
"entry": position["entry"], "exit": price,
"pnl_pips": pnl * 10000, "bars_held": i - position["bar"]
})
position = None
wins = [t for t in trades if t["pnl_pips"] > 0]
total = len(trades)
win_rate = len(wins) / total * 100 if total else 0
avg_pnl = sum(t["pnl_pips"] for t in trades) / total if total else 0
print(f"Symbol: {symbol} | Timeframe: {timeframe}")
print(f"Total trades: {total} | Win rate: {win_rate:.1f}%")
print(f"Avg PnL: {avg_pnl:.1f} pips")
return trades
# Run backtest
trades = backtest_rsi("EURUSD", "H1", 500) Key Metrics to Track
Win Rate
Percentage of profitable trades. Above 50% is good for mean-reversion.
Profit Factor
Gross profit / gross loss. Above 1.5 indicates a viable strategy.
Max Drawdown
Largest peak-to-trough decline. Keep below 20% of account.
Sharpe Ratio
Risk-adjusted return. Above 1.0 is acceptable; above 2.0 is excellent.
Best Practices
- Test across multiple symbols and timeframes to avoid curve-fitting
- Use out-of-sample data for final validation (don't optimize on all data)
- Account for spread and slippage in your PnL calculations
- Include transaction costs in profitability analysis
- Test in both trending and ranging market conditions
Related
Related Guides
- Guide
How to Build a Trading Bot
Step-by-step Python bot with RSI + MACD strategy
Read more - Guide
Combine Multiple Indicators
Confluence strategies using RSI + MACD + BB
Read more - Guide
Multi-Timeframe Analysis
Combine M30, H1, H4, D1 for stronger signals
Read more - Indicator
Relative Strength Index (RSI)
The Relative Strength Index (RSI) is a momentum oscillator that measures the speed and magnitude of price movements on a scale from 0 to 100.
Read more