Bentrix Docs
HomePricingDashboard

Slippage, latency and spread

Make a workbench whose fills are worse than the recording's — slippage that is fixed, random from a seed or scaled to the market's move, a delay before a market order fills, a wider or session-dependent spread — and see that the same seed gives the same fills.

A laboratory workbench fills an order the way the recording allows: a market order at the next tick's ask or bid, a stop at the first price through it. A real account is filled a little later and a little worse, and on a fast day much worse. An agent trained only on the kind version learns habits that the unkind one punishes.

So Bentrix lets you make a workbench alike another — the same days, markets, account and costs — with other spread and fills. The settings are fixed when the workbench is made; they are part of what that workbench is, so two runs on it are always under the same conditions.

01The settings

One tool, bench_create_alike, takes the workbench to copy and any of these:

SettingWhat it does
slippage={"fixed": {"points": 15}}Every fill that takes the market is 15 points worse.
slippage={"random": {"up_to_points": 30}}Each such fill is worse by a draw between 0 and 30 points, from the seed.
slippage={"volatility": {"k": 0.5}}Worse by half the size of the price's move into the filling tick: more when the market is moving fast.
slippage_tail=[{"from_hour": 13, "to_hour": 14, "points": 40}]Extra slippage in given UTC hours — a news hour, an open.
latency_ms=250A market order waits for the first tick at least this long after the quote it was sent on, rather than the very next tick.
seed=7The seed of every random draw; 1 when omitted.
spread_multiplier=3.0Every recorded spread times three.
spread_fixed_points=25One spread for every tick.
spread_floor_points=40No tick's spread below this.
spread_sessions=[{"from_hour": 21, "to_hour": 23, "points": 60}]A fixed spread in given UTC hours — the thin hours around a rollover, for instance.

Figures are in each market's own points (symbol_info gives a market's point). At least one setting must be given: the tool refuses to make a copy with nothing changed.

What slips and what does not. Slippage is always against the order, and it reaches the fills that take the market: a market order's open and close, a stop order's trigger, a stop-loss. A limit order, a take-profit and the limit of a stop-limit fill at their own price, as they do at a venue.

The spread moves the ask. The bid stays as recorded and the ask moves away from it, so a wider spread makes buys and the exits of shorts dearer.

02See it

The same four market buys, five minutes apart, on seven workbenches:

import tempfile

from bentrix import _native
from bentrix.local import Local

workspace, store = _native.make_tape(tempfile.mkdtemp(prefix="bentrix-practice-"))
engine = Local(workspace=workspace, store=store)
plain = engine.call("get_state")["laboratories"][0]["benches"][0]["uri"]
engine.arm(plain)
symbol = "XAUUSD"


def four_buys(bench):
    """The same four market orders, five minutes apart; the fills' moments and prices."""
    engine.arm(bench)
    engine.next_close(bench, "H1", [symbol])
    for _ in range(4):
        engine.call("order_market", bench=bench, symbol=symbol, side="buy", volume=0.01)
        engine.next_close(bench, "M5", [symbol])
    home, owner, _, name = bench.removeprefix("bentrix://").split("/")
    deals = engine.read("trade_deals", home=home, owner=owner, bench=name)["items"]
    return [(d["time_ms"], round(d["price"], 3)) for d in deals]


def alike(**conditions):
    return engine.call("bench_create_alike", bench=plain, **conditions)["uri"]


as_recorded = four_buys(plain)
slipped_7 = four_buys(alike(slippage={"random": {"up_to_points": 30}}, seed=7))
slipped_7_again = four_buys(alike(slippage={"random": {"up_to_points": 30}}, seed=7))
slipped_8 = four_buys(alike(slippage={"random": {"up_to_points": 30}}, seed=8))
late = four_buys(alike(latency_ms=20_000))
wide = four_buys(alike(spread_multiplier=3.0))
fixed = four_buys(alike(slippage={"fixed": {"points": 15}}, spread_floor_points=40))

print("as recorded      ", as_recorded)
print("random, seed 7   ", slipped_7)
print("seed 7 again     ", slipped_7_again, "- equal:", slipped_7 == slipped_7_again)
print("random, seed 8   ", slipped_8)
print("20 s latency     ", late)
print("3 x spread       ", wide)
print("fixed 15, floor 40", fixed)
engine.close()
as recorded       [(1709514015000, 2102.518), (1709514315000, 2101.869), (1709514615000, 2101.117), (1709514915000, 2100.542)]
random, seed 7    [(1709514015000, 2102.633), (1709514315000, 2102.001), (1709514615000, 2101.34), (1709514915000, 2100.615)]
seed 7 again      [(1709514015000, 2102.633), (1709514315000, 2102.001), (1709514615000, 2101.34), (1709514915000, 2100.615)] - equal: True
random, seed 8    [(1709514015000, 2102.549), (1709514315000, 2101.93), (1709514615000, 2101.378), (1709514915000, 2100.56)]
20 s latency      [(1709514030000, 2102.309), (1709514330000, 2101.618), (1709514630000, 2100.88), (1709514930000, 2100.368)]
3 x spread        [(1709514015000, 2102.918), (1709514315000, 2102.269), (1709514615000, 2101.517), (1709514915000, 2100.942)]
fixed 15, floor 40 [(1709514015000, 2102.868), (1709514315000, 2102.219), (1709514615000, 2101.467), (1709514915000, 2100.892)]

Each pair is a fill's moment (UTC milliseconds) and its price. Compare the rows with the first:

  • Random slippage, seed 7: each buy filled between 0 and 0.30 higher (30 points of 0.01).
  • Seed 7 again, on a second workbench made the same way: the same four prices. Equal seeds give equal fills.
  • Seed 8: four other prices.
  • 20 seconds of latency: each fill is 15 seconds later than as recorded — the first tick at least 20 seconds after the order's quote — and at that later tick's price.
  • Three times the spread: each buy 0.40 higher; the recorded spread of 0.20 became 0.60.
  • Fixed 15 points with a floor of 40: 0.35 higher — 0.20 from the wider spread and 0.15 of slippage.

03Why the same seed gives the same fill

A random draw is not taken from a running sequence. Each draw is computed from the seed and the order's own ticket. Tickets are the same whenever the same orders are sent in the same sequence, so a run, a rewind and a second run all land every fill on the same price — and changing the seed changes all of them. Nothing about your program's timing or the computer's speed enters it.

That is what makes a robustness test meaningful: the same policy over seeds 1 to 20 is twenty different sets of fills, each one reproducible.

04Using it for training and testing

  • Train under mild conditions, test under harsher ones. A policy whose result disappears at twice the spread or with 20 points of slippage depended on fills it will not get.
  • Many seeds. Make one workbench per seed and run many environments at once, or evaluate one policy across them.
  • Your own broker's numbers. With a real account connected, Bentrix can measure how your own orders were filled and make a workbench with that slippage and delay: your own fills.
  • Other tick models. A workbench's ticks can be the recorded ticks, ticks modelled from one-minute candles, the four prices of each minute, or opening prices only — bench_set_tick_model, before the workbench's first fill. Tick models describes each. Comparing a result across them is a robustness test of its own.

05Good to know

  • The settings cannot be changed on an existing workbench, on purpose: a workbench is its conditions. Make another alike.
  • The new workbench's name says what was changed, so the rail and a results table show the conditions.
  • A slipped price is the recorded price plus the draw; it is not rounded to the market's price step in this version.
  • Commission and swap are not part of this tool. A workbench's own commission is set when it is made (bench_create with commission).
  • Not in this version: partial fills, a chance of rejection, a limit on size from the market's volume, and market impact on the fill price. What the engine supports lists them.

Bentrix 0.1.382Every number, name and default on these pages is read from the application itself.What is comingWhat changed