DEV Community

Emily
Emily

Posted on

Don’t Trust Your Forex Backtest Until You’ve Used Tick Data (with a Working Forex API Example)

You built a strategy, backtested it on 1-minute candles, and got a beautiful equity curve. Then you went live and it fell apart. Sound familiar?

The problem isn’t your edge. It’s the data you used to validate it.

Candles vs. Ticks: A Fundamental Gap

Candlestick data aggregates an entire minute into four values: open, high, low, close. That works for charting, but it’s terrible for backtesting.

Forex tick data records every quote update — every change in bid and ask price. That’s the raw feed from the market, and it’s the only way to know what actually happened inside a candle.

For example, if price spikes 10 pips and retraces within 30 seconds, a 1-minute candle shows only a long wick. You can’t tell whether your limit order would have been filled, whether your stop was hit, or how much slippage you’d have taken on a market order.

Three Practical Uses for Tick Data in FX Research

  1. Simulate realistic fills

    Replay the exact sequence of price changes and apply your order execution logic to each tick. No more assuming a fill at the candle close.

  2. Measure actual spread costs

    FX quotes have separate bid and ask streams. Tick data lets you compute the real spread you would have paid on every trade.

  3. Capture micro-signals

    Order-book imbalance, short-lived volatility bursts, and quote gaps are only visible at tick granularity. Candles destroy that information.

Common Issues When Working with Tick Data

Tick data is powerful, but it comes with operational challenges. Here’s what you’ll face and how to solve each:

Problem Symptom Solution
Out-of-order ticks Received timestamps are not sequential Sort by event time before processing
Duplicate ticks Same quote appears after reconnection Deduplicate using a unique message ID
Large data volume One day can produce hundreds of thousands of rows Store partitioned by symbol and date

I use ALLTICK API’s WebSocket to pull forex tick data in real time. It’s a practical choice when you don’t want to maintain your own infrastructure.

Below is a minimal Python script that connects to the WebSocket and subscribes to EURUSD. Comments explain the key steps.

import websocket
import json

# WebSocket endpoint and authentication token
WS_URL = "wss://quote.alltick.co/quote-stub"
TOKEN = "your_token_here"
tick_buffer = []

def on_message(ws, message):
    # Parse incoming JSON message
    data = json.loads(message)
    if data.get("data"):
        # Extract relevant tick fields
        tick = {
            "symbol": data["data"].get("code"),
            "bid": data["data"].get("bid_price"),
            "ask": data["data"].get("ask_price"),
            "event_time": data["data"].get("tick_time")
        }
        tick_buffer.append(tick)

def on_open(ws):
    # Build subscription message for EURUSD
    sub_msg = {
        "cmd_id": 22004,
        "seq_id": 1,
        "trace": "fx-sub-1",
        "data": {"symbol_list": [{"code": "EURUSD"}]}
    }
    ws.send(json.dumps(sub_msg))

# Create WebSocket connection
ws = websocket.WebSocketApp(
    f"{WS_URL}?token={TOKEN}",
    on_open=on_open,
    on_message=on_message
)
ws.run_forever()
Enter fullscreen mode Exit fullscreen mode

Once the ticks are collected, I reorder them by event time, then aggregate into bars for coarse analysis — but always keep the raw ticks for fine-grained backtests. The result is a much smaller gap between simulation and live trading.

If you’re doing any kind of serious forex research, stop relying on candles for validation. Tick data is the ground truth.

Top comments (0)