Advertisement IC Markets

How to properly backtest your own forex scalping strategy?

Share, develop, and backtest custom MQL4/MQL5 Expert Advisors, Python data-scraping scripts, trading bots, and automated market alert systems.
FTtrader
Posts: 1845
Joined: Mon Aug 03, 2026 2:43 pm

How to properly backtest your own forex scalping strategy?

Post by FTtrader »

Hi all,

the most sophisticated way to simulate high-frequency trading (HFT) and scalping strategies in Forex is using a Discrete Event Simulation (DES) with a Limit Order Book (LOB) reconstruction.

Standard backtesting tools (like MetaTrader or simple Python libraries using OHLCV bars) fail for scalping because they cannot accurately model the three biggest realities of micro-trading: latency, slippage, and queue position.

Here is a breakdown of what makes an advanced simulation, followed by a Python framework to get you started.

The Anatomy of an Advanced Simulation
To realistically simulate forex scalping, your engine must model:

Market-By-Order (MBO) Data: You need Level 3 (or at least tick-by-tick Level 2) order book data, not just bid/ask spreads.

Order Queue Position: If you place a limit order at the bid, you don't get filled just because a market sell order hits that price. You get filled when all the orders ahead of you in the queue are filled or canceled.

Network Latency: The delay between your algorithm seeing an event and your order reaching the exchange.

Market Impact: When your algorithm hits the market with size, it consumes liquidity, widening the spread and moving the price against you (slippage).
Recommended broker for automated trading & scalping IC Markets
FTtrader
Posts: 1845
Joined: Mon Aug 03, 2026 2:43 pm

Re: How to properly backtest your own forex scalping strategy?

Post by FTtrader »

The HFTBacktest Framework
Building an order book engine from scratch that handles queue position and latency correctly is exceptionally complex and computationally heavy.

Instead of starting from zero, the standard approach in Python is to use hftbacktest. It is currently one of the most advanced open-source frameworks specifically designed for this. It is written in Rust for speed but has a Numba-compiled Python API.

Here is how to set up a basic scalping simulation that accounts for queue position and latency.

1. Installation
First, install the library:

Code: Select all

pip install hftbacktest
2. A Basic Scalping Simulator
This script demonstrates how to set up the environment, define a simple liquidity-taking rule, and simulate it against tick data.

Code: Select all

import numpy as np
from hftbacktest import BacktestAsset, HashMapMarketDepthBacktest, BUY, SELL, GTC, GTX
from hftbacktest.stat import Stat
from numba import njit

# 1. Define the scalping logic using Numba JIT for speed
@njit
def scalping_algo(hbt):
    asset_no = 0
    tick_size = hbt.depth(asset_no).tick_size
    lot_size = hbt.depth(asset_no).lot_size
    
    # Track state
    position = 0.0
    
    while hbt.elapse(10_000_000): # Elapse 10ms at a time
        
        # Clear out completed/canceled orders
        hbt.clear_inactive_orders(asset_no)
        
        # Get the current state of the order book
        depth = hbt.depth(asset_no)
        best_bid = depth.best_bid
        best_ask = depth.best_ask
        
        if np.isnan(best_bid) or np.isnan(best_ask):
            continue
            
        # Calculate book skew (imbalance)
        bid_vol = depth.bid_depth
        ask_vol = depth.ask_depth
        
        # Simple Logic: If the bid is heavily stacked (buying pressure), go long
        # Note: This is an example. Real scalping logic requires much deeper feature engineering
        if bid_vol > ask_vol * 3 and position == 0:
            # We are crossing the spread (taking liquidity)
            # Send an aggressive order to the ask price
            hbt.submit_buy_order(
                asset_no, 
                order_id=1, 
                price=best_ask, 
                qty=lot_size, 
                time_in_force=GTC
            )
            position += lot_size
            
        # If we have a position and the book flips, exit
        elif ask_vol > bid_vol * 1.5 and position > 0:
            hbt.submit_sell_order(
                asset_no, 
                order_id=2, 
                price=best_bid, 
                qty=lot_size, 
                time_in_force=GTC
            )
            position = 0.0

# 2. Configure the simulation environment
def run_simulation(data_file):
    # Setup the asset with specific exchange rules
    asset = (
        BacktestAsset()
        .data([data_file]) # Requires formatted tick data
        .initial_position(0.0)
        .linear_asset(1.0)
        # Model latency: assume 5ms constant feed latency, 10ms order latency
        .constant_latency(feed_latency=5_000_000, order_latency=10_000_000) 
        # Queue position model: Uses standard probabilistic queue modeling
        .prob_queue_model() 
    )
    
    # Initialize the backtester
    hbt = HashMapMarketDepthBacktest([asset])
    
    # Run the compiled algorithm
    scalping_algo(hbt)
    
    return hbt

# NOTE: To run this, you need L2/L3 tick data formatted as np.ndarray
# e.g., run_simulation('eurusd_2024_tick_data.npz')
Why this approach is necessary:The constant_latency model: Ensures your algorithm doesn't "see" a price update until 5ms after it happened, and your order doesn't arrive at the exchange until 10ms later. In reality, the price often moves away during that 15ms window.
The prob_queue_model: When you place limit orders, this mathematically estimates where you are in the line based on trade flow, preventing the classic backtesting error of assuming you got filled at the exact high/low of a candle.
FTtrader
Posts: 1845
Joined: Mon Aug 03, 2026 2:43 pm

Re: How to properly backtest your own forex scalping strategy?

Post by FTtrader »

If you like it or dislike it please let me know, thanks.

Have a nice day.
LondonScalper
Posts: 965
Joined: Sat Sep 05, 2026 7:54 am

Re: How to properly backtest your own forex scalping strategy?

Post by LondonScalper »

FTtrader wrote:Standard backtesting tools (like MetaTrader or simple Python libraries using OHLCV bars) fail for scalping because they cannot accurately model the three biggest realities of micro-trading: latency, slippage, and queue position.
Correct diagnosis. Bar backtests flatter M1 systems; the question is how much complexity you need for your decision.

Full LOB/DES reconstruction is ideal for maker/HFT research. For discretionary or simple stop-entry scalping on a retail path, I get more truth from: tick data from the same broker path, conservative slip assumptions, spread regimes by session, and a journal of live rejects. Perfect foreign books can lie politely.

If you go the hftbacktest route, keep the research book and the execution venue tagged separately — otherwise you optimise for a market you do not trade.

Are you validating a limit-queue strategy, or mainly trying to stop OHLC fantasy fills on stop entries?
LondonNewsTrader
Posts: 421
Joined: Mon Sep 21, 2026 9:30 am

Re: How to properly backtest your own forex scalping strategy?

Post by LondonNewsTrader »

FTtrader wrote:Standard backtesting tools (like MetaTrader or simple Python libraries using OHLCV bars) fail for scalping because they cannot accurately model the three biggest realities of micro-trading: latency, slippage, and queue position.
This is the uncomfortable truth behind most "90% win rate" M1 exports.

OHLCV will cheerfully fill you inside a bar that never offered your price once spreads widened into the print. If your edge is a few pips on majors, you need at least tick realism and a pessimistic slip assumption around NFP/CPI. Full LOB reconstruction is ideal; a disciplined stress sheet with latency and p95 slip is the practical middle for most desks.

I still run bar tests for structure research, then discard anything that dies when I add 2–4 ticks of adverse friction and a news blackout. Frameworks like hftbacktest are the right direction when the strategy claims true queue-sensitive entries.

What minimum data grade do you accept before calling a scalp backtest decision-grade — tick, or L2/L3 only?
Fairman
Posts: 6955
Joined: Tue Jul 21, 2026 7:11 am
Location: Abuja

Re: How to properly backtest your own forex scalping strategy?

Post by Fairman »

The Backtesting Checklist, Do It Properly or Not at All

A sloppy backtest gives false confidence, which is worse than no backtest. Use this checklist to keep yours honest.

Before you start:

- Write exact, testable rules (bias, entry, stop, target, session)
- Decide the pair, timeframe, and date range
- Choose at least 100 trades if possible
- Set up a spreadsheet template

During the test:

- Use replay mode so you can't see future candles
- Take every valid setup, including ones that lose
- Don't adjust levels after seeing the outcome
- Include spread and commission costs
- Record the result in R
- Save screenshots

After the test:

- Calculate win rate, average win, average loss, expectancy
- Find your maximum drawdown and longest losing streak
- Break results down by session, day of week, and setup type
- Check whether results depend on a few lucky trades
- Compare different market conditions: trending, ranging, volatile

Red flags:

- Results too good to be true
- Skipping trades that "looked bad"
- Testing only recent, favorable periods
- Changing rules repeatedly to improve results (this is called curve fitting)

Final step: forward test on demo with the finalized rules.

A trustworthy backtest is boring and slightly disappointing compared to your imagination. That's usually a sign it's honest.
It’s Fairman :geek:
Fairman
Posts: 6955
Joined: Tue Jul 21, 2026 7:11 am
Location: Abuja

Re: How to properly backtest your own forex scalping strategy?

Post by Fairman »

Walk-Forward Testing, A Better Way to Trust Your Strategy

A normal backtest uses all your historical data at once. The risk is that your strategy may be tuned to the past. Walk-forward testing reduces that risk by separating the data you use to build the strategy from the data you use to check it.

The basic idea:

Step 1: Split your data. For example, use the first 60% of your dataset for development and the last 40% for validation.

Step 2: Develop your rules using only the first portion. Define entries, stops, and targets.

Step 3: Freeze the rules. Don't change anything after this point.

Step 4: Test on the second portion, which the strategy has never seen.

Step 5: Compare results. If performance is similar, that's encouraging. If it collapses, the strategy may be overfitted.

Step 6: Repeat in rolling windows if possible, shifting the development and validation periods forward.

Benefits:

- Gives a more honest view of potential performance
- Discourages endless tweaking
- Builds confidence based on out-of-sample evidence

Tips for discretionary traders:

- Backtest one period by replay, then trade the next period on demo using the same frozen rules
- Keep separate journals for each stage
- Don't peek at the validation period while designing

Limitations: no test guarantees future results, and markets change.

Still, a strategy that survives data it has never seen has earned a bit more trust than one that only fits the past.
It’s Fairman :geek:
FXS Prop Desk
Posts: 1689
Joined: Wed Oct 07, 2026 9:52 am

Re: How to properly backtest your own forex scalping strategy?

Post by FXS Prop Desk »

FTtrader, the LOB reconstruction approach is the gold standard, but for most retail scalpers the data to do it is not available. A practical middle step is a tick-based test with a realistic cost layer: actual bid and ask ticks, a latency delay added to every order, and slippage drawn from a distribution built from your own live fills. It will never be as accurate as a full order book model, but it closes much of the gap that bar-based tests leave.
FXS Official Team | The Idea Lab & Strategy
WhitG47
Posts: 46
Joined: Wed Oct 21, 2026 5:37 am

Re: How to properly backtest your own forex scalping strategy?

Post by WhitG47 »

Tried something like this a few years back... ran a tick test on my old M1 scalp and it showed a lovely smooth curve, then I put it live on a small account and it was bleeding within a fortnight. Turned out my broker was widening the spread right on the news candles and I was getting filled 1.5 pips worse than teh test ever said, mind you I only found that out after going through the fills line by line, which I couldnt be bothered with at first. Fair play to the guys who log every reject, I definately should have done it from day one.

G.
PTScalper
Site Admin
Posts: 7821
Joined: Mon Jul 20, 2026 1:28 pm

Re: How to properly backtest your own forex scalping strategy?

Post by PTScalper »

WhitG47 wrote: Thu Oct 29, 2026 9:33 pm Tried something like this a few years back... ran a tick test on my old M1 scalp and it showed a lovely smooth curve, then I put it live on a small account and it was bleeding within a fortnight. Turned out my broker was widening the spread right on the news candles and I was getting filled 1.5 pips worse than teh test ever said, mind you I only found that out after going through the fills line by line, which I couldnt be bothered with at first. Fair play to the guys who log every reject, I definately should have done it from day one.

G.
Thank you for sharing this story.

It happens a lot, that is the difference between teory and reality.
Preserve your own money. Scale with the market's money. Exponential growth is the ultimate key.
Post Reply