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_indicator_series(symbol, indicator, timeframe, bars=500):
"""Historical indicator values: data.series = [{time, value}, ...]."""
params = {
"symbol": symbol, "indicator": indicator,
"timeframe": timeframe, "limit": bars
}
resp = requests.get(f"{BASE}/indicator/history",
headers=headers, params=params).json()
return resp["data"]["series"]
def get_candles(symbol, timeframe, bars=500):
"""Matching prices: data.candles = [{time, open, high, low, close, volume}]."""
params = {"symbol": symbol, "timeframe": timeframe, "limit": bars}
resp = requests.get(f"{BASE}/ohlc",
headers=headers, params=params).json()
return resp["data"]["candles"]
def _at(ts):
"""/v1/ohlc suffixes its timestamps with 'Z', the series does not."""
return ts.rstrip("Z")
def backtest_rsi(symbol, timeframe="H1", bars=500):
"""Simple RSI mean-reversion backtest."""
# Indicator values and prices are separate endpoints — join them on time.
closes = {_at(c["time"]): c["close"]
for c in get_candles(symbol, timeframe, bars)}
history = [p for p in get_indicator_series(symbol, "RSI_14", timeframe, bars)
if p["value"] is not None and _at(p["time"]) in closes]
trades = []
position = None
for i, bar in enumerate(history):
rsi = bar["value"]
price = closes[_at(bar["time"])]
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
Best Practices
Production hardening
The code above is the happy path. These are the concerns that decide whether it survives contact with a real deployment.
Related Guides
Everything this guide touches, linked directly — so it never dead-ends.