Skip to content
arielb57Public

About

Decide whether a prop-firm drawdown rule was certainly, possibly, or never breached from OHLC bars

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

6 Commits

Folders and files

Repository files navigation

ddbreach

Decide whether a prop-firm drawdown rule was certainly, possibly, or never breached, using only OHLC bars.

The problem

Prop firms enforce static, trailing and daily-loss limits against equity tick by tick, but most traders only have OHLC bars for their history or their backtest. Inside a bar the order of the high and the low is unknown, and for a trailing rule that order decides the answer: a high that comes first lifts the floor that the low then crosses. Backtesters that assume one order (usually open-high-low-close) print a yes/no that is wrong in either direction, and a trader sizing a funded-account challenge has no way to tell which bars the answer hinges on.

ddbreach reports, per bar and per rule, one of three verdicts that are provably correct for every tick path consistent with the bars:

  • CERTAIN: every such path breaches in this bar.
  • POSSIBLE: some path breaches and some does not.
  • SAFE: no path breaches.

It also finds the largest multiple of your position size for which every bar stays SAFE.

How it works

Rules as state machines over equity. Each rule is a floor that equity must not go under (touching the floor is allowed):

rule floor
static:L balance - L
trailing-intraday:L[,lock=K] min(highest equity ever touched - L, K)
trailing-eod:L[,lock=K] min(highest equity at a daily reset - L, K)
daily-loss:L max(balance, equity at the last daily reset) - L

Daily resets use a fixed UTC offset and reset time (tz=-05:00,reset=17:00).

Two orderings, one pass. With one instrument, equity is a linear function of price, so the equity high and low of a bar are attained, and the only thing a path can change is whether the high comes before the low. The adverse ordering is open, high, low, close: it raises the high-water mark as far as possible before the lowest point. The favourable ordering is open, low, high, close. The engine (src/engine.rs) carries two copies of the rule state through the series, one driven along each ordering, and does four comparisons per bar on each:

  • breach on the favourable side: every path breaches, CERTAIN;
  • breach on the adverse side only: POSSIBLE;
  • neither: SAFE.

The favourable state carries forward too, because the lowest high-water mark any path can enter the next bar with is the one the favourable path produced. Cost is O(bars x rules), with no allocation when only the worst verdict is needed.

A worked example, from the crate docs: balance and open at 100,000, bar high 104,000, low 99,500, close 100,000, trailing-intraday:4000. High first, the floor rises to 100,000 and the low breaches it. Low first, the floor is still 96,000 when the low prints, and the later high lifts the floor to 100,000, which the close touches without crossing. Verdict: POSSIBLE.

Portfolios: interval bounds. Positions on the same instrument are netted into one linear coefficient, so they stay exact. With exposure on two or more instruments, the equity low and high are sums of per-instrument extremes that need not happen at the same instant. The adverse side uses those sums (the widest range any path could reach). The favourable side uses guaranteed levels: every path visits each instrument's low at some instant, while the other instruments contribute at most their highs, so equity is certainly at or below sum of highs - widest single range at some point. That keeps SAFE and CERTAIN sound. POSSIBLE may then include bars that every path actually breaches.

Max safe size: exponential search then bisection. Scaling every position by n scales every equity offset by n. When every lock is at or below the balance, each floor comparison is monotone in n, so "all bars SAFE" is monotone and the boundary is found in O(log n) passes. A lock above the balance breaks monotonicity (a bigger winner can stay above a fixed lock that a smaller one dips under), and the tool says so instead of returning a wrong number.

Checking against brute force. tests/common/mod.rs is an independent oracle: it walks every tick-by-tick path consistent with each bar (every price one tick at a time inside the range, touching every high and low, ending at the closes) and records whether some or all paths breach. Property tests (tests/oracle.rs) generate random rules, clocks, locks, gaps and bars and require the engine to match the oracle exactly for one instrument (400 cases) and to be sound for two instruments (120 cases). Further properties check that CERTAIN implies POSSIBLE, that verdicts never get worse as the limit grows or better as size grows, that the reported max safe size is the exact boundary, and that synthetic tick paths agree with the verdicts on the bars built from them.

Install and usage

The library and CLI build on Rust 1.85 or newer (CI checks this) and have no dependencies. The test suite uses proptest, which may need a newer toolchain. From a checkout:

cargo test
cargo install --path .    # puts `ddbreach` on your PATH

Input is two CSV files. Prices are integer ticks and money is integer minor units, so there is no floating-point rounding anywhere.

# bars.csv: time is Unix seconds or "YYYY-MM-DD HH:MM[:SS]" in UTC
time,instrument,open,high,low,close
2026-01-05 10:00,EURUSD,10000,10400,9980,10350
...
# positions.csv: equity = balance + qty * tick_value * (price - entry)
instrument,qty,entry,tick_value
EURUSD,1,10000,10

Every rule variant on the same one-lot history (examples/data):

$ cargo run --release -- --rule static:5000 --rule trailing-eod:5000 --rule trailing-intraday:5000 \
    --rule trailing-intraday:5000,lock=100000 --rule daily-loss:3000 \
    examples/data/bars.csv examples/data/positions.csv
7 bars, 1 instrument(s), exact extremes, balance 100000

time               eq_open    eq_low   eq_high  eq_close  static:5000  trailing-eod:5000  trailing-intraday:5000  trailing-intraday:5000,lock=100000  daily-loss:3000
2026-01-05 10:00    100000     99800    104000    103500  SAFE         SAFE               SAFE                    SAFE                                SAFE
2026-01-05 15:00    103500    100900    106500    102000  SAFE         SAFE               POSSIBLE                SAFE                                SAFE
2026-01-06 10:00    102000    100200    102500    101000  SAFE         SAFE               CERTAIN                 SAFE                                SAFE
2026-01-06 14:00    101000     99500    101200     99800  SAFE         SAFE               CERTAIN                 CERTAIN                             SAFE
2026-01-06 19:00     99800     99600    105000    104800  SAFE         SAFE               CERTAIN                 CERTAIN                             SAFE
2026-01-07 10:00    104800    101700    104900    102000  SAFE         SAFE               SAFE                    SAFE                                CERTAIN
2026-01-07 15:00    102000     99700    102100    100000  SAFE         CERTAIN            CERTAIN                 CERTAIN                             CERTAIN

rule                                         first CERTAIN                first POSSIBLE               max safe size
static:5000                                  none                         none                         10x
trailing-eod:5000                            2026-01-07 15:00 (bar 7)     none                         0x
trailing-intraday:5000                       2026-01-06 10:00 (bar 3)     2026-01-05 15:00 (bar 2)     0x
trailing-intraday:5000,lock=100000           2026-01-06 14:00 (bar 4)     none                         0x
daily-loss:3000                              2026-01-07 10:00 (bar 6)     none                         0x
all rules together: max safe size 0x

sizes are multiples of the quantities in the positions file

Bar 2 is the case a single-ordering backtester gets wrong one way or the other: equity peaks at 106,500 and dips to 100,900. If the peak came first, the trailing floor was 101,500 and the dip breached it; if the dip came first, the floor was 99,000 and nothing happened.

Sizing a position in micro lots (tick value 1 instead of 10) with --summary:

$ cargo run --release -- --summary --rule static:5000 --rule trailing-eod:5000 \
    --rule trailing-intraday:5000 --rule daily-loss:3000 \
    examples/data/bars.csv examples/data/micro.csv
7 bars, 1 instrument(s), exact extremes, balance 100000

rule                                         first CERTAIN                first POSSIBLE               max safe size
static:5000                                  none                         none                         100x
trailing-eod:5000                            none                         none                         9x
trailing-intraday:5000                       none                         none                         7x
daily-loss:3000                              none                         none                         5x
all rules together: max safe size 5x

sizes are multiples of the quantities in the positions file

A two-instrument portfolio (long EURUSD, short XAUUSD) switches to interval bounds and says so:

$ cargo run --release -- --rule static:5000 --rule trailing-intraday:5000 \
    examples/data/portfolio_bars.csv examples/data/portfolio.csv
3 bars, 2 instrument(s), interval-bounded extremes, balance 100000

time               eq_open    eq_low   eq_high  eq_close  static:5000  trailing-intraday:5000
2026-01-05 10:00    100000     95500    104000    100500  SAFE         POSSIBLE
2026-01-05 11:00    100500     91000    101900     93000  CERTAIN      CERTAIN
2026-01-05 12:00     93000     84000     95000     86000  CERTAIN      CERTAIN

rule                                         first CERTAIN                first POSSIBLE               max safe size
static:5000                                  2026-01-05 11:00 (bar 2)     none                         0x
trailing-intraday:5000                       2026-01-05 11:00 (bar 2)     2026-01-05 10:00 (bar 1)     0x
all rules together: max safe size 0x

note: several instruments carry exposure, so extremes are interval bounds.
SAFE and CERTAIN stay sound; POSSIBLE may hide a breach that every path makes.

sizes are multiples of the quantities in the positions file

Options: --balance N (default 100000), --summary, --size-cap N (default 1000000), and repeatable --rule SPEC. Run ddbreach --help for the full list.

A deterministic synthetic data generator is included, for experiments and for the benchmark:

$ cargo run --release -- gen --bars 3 --seed 7 --instruments EURUSD
time,instrument,open,high,low,close
2026-01-05 00:00,EURUSD,10000,10005,9990,10005
2026-01-05 00:01,EURUSD,10006,10008,9999,9999
2026-01-05 00:02,EURUSD,10002,10002,9992,9992

--ticks FILE also writes the underlying tick paths, so a verdict can be checked against the ticks it summarises.

Results

Throughput, measured with one command:

cargo run --release --example bench

The benchmark generates a seeded random walk (8 ticks per one-minute bar, occasional gaps), maps it to equity for one position, and times each rule. Each figure is the best of three runs. Measured on an 8-thread Mac (macOS, Darwin 25.0), rustc 1.94.1, release profile:

10M one-minute bars, single thread seconds bars/second
price to equity mapping 0.072 139 million
static 0.035 284 million
trailing-eod with lock, New York 17:00 reset 0.042 241 million
trailing-intraday 0.052 193 million
trailing-intraday with lock 0.052 193 million
daily-loss, New York 17:00 reset 0.039 254 million
all five variants 0.218 46 million
trailing-intraday, full per-bar report 0.069 145 million

A year of one-minute bars (525,600) for 50 accounts under all five variants, including the equity mapping: 0.766 s on one thread (171 million rule-bar evaluations per second) and 0.175 s split across 8 threads. Numbers vary by machine; rerun the command above to get yours.

Correctness is the stronger claim, and it is measured by the test suite rather than a table: cargo test runs the brute-force oracle comparison described above on every run.

Design notes

The main decision was to reason about two extreme orderings instead of enumerating paths or sampling them. Enumeration is exponential in bar range, and Monte Carlo over intrabar paths gives a probability that depends on a made-up price model, which is exactly the assumption a trader cannot check. Two orderings give a guarantee with no model at all, at the cost of four comparisons per bar. The less obvious part is that the favourable state is carried across bars on its own rather than derived from the adverse one: a path that stayed under a high-water mark in one bar enters the next with a lower floor, and CERTAIN has to hold for that path too. Subtleties like this are why the brute-force oracle exists; it shares no logic with the engine, so the two agreeing on hundreds of random cases per test run is real evidence.

For portfolios I chose soundness over precision. The exact question (does every joint path of two instruments breach?) needs the joint intrabar ordering, which OHLC bars do not contain. The interval bounds never call a survivable bar CERTAIN or a breachable bar SAFE, and the output flags when they are in use, so the imprecision shows up as more POSSIBLE verdicts rather than as wrong ones. Everything is integer arithmetic (i128 during the price to equity mapping, with overflow reported) so that "touching the floor is not a breach" means exactly that.

Limitations

  • Positions are constant over the whole file. There are no entries, exits, partial closes, commissions, swaps or slippage; run separate files for separate holding periods.
  • Verdicts are per bar and do not stop at the first breach: a bar after a CERTAIN one is still evaluated as if the account were open, which is what you want for sizing and diagnosis but not what a firm would do.
  • A daily reset is applied at the first bar whose timestamp is at or after the reset time, using the previous bar's close as the equity at reset. If a reset falls strictly inside a bar, the true equity at that instant is unknown and this approximation is used without warning.
  • Time zones are fixed UTC offsets. Daylight saving changes are not modelled.
  • With exposure on several instruments, POSSIBLE can hide a certain breach (see above), and max safe size is correspondingly conservative.
  • Rules model the common static, trailing intraday, trailing end-of-day and daily-loss variants. Firm-specific variations (profit targets, consistency rules, trailing on balance rather than equity, floors that lock at a profit threshold rather than a fixed level) are not covered.
  • The CSV reader is deliberately minimal: comma separated, no quoting, integer prices.

License

MIT. See LICENSE.

About

Decide whether a prop-firm drawdown rule was certainly, possibly, or never breached from OHLC bars

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages