← Journal

RESEARCH PAPER / 03

From a backtest to executable rules

Read text version

Page 1

Stochastly / Working paper 03 / Synthetic methodology 1 STOCHASTLY / WORKING PAPER 03 From a backtest to executable rules State equivalence, risk controls and the accounting of trading costs Mahé · 21 September 2026 Abstract Exporting a strategy requires a specification of its decision state and entry logic. This working paper describes a strategy as a state transition system and separates policy equivalence under a shared event stream from the economic uncertainty of live execution. A deterministic six-return example shows how omitting a cumulative loss stop changes subsequent exposure and reverses the sign of terminal performance. We then stress a declared turnover-cost model and examine cases where OHLC bars leave the order of price-barrier touches unresolved. The examples establish contract requirements and test cases. They do not validate any existing exporter, trading venue or market strategy. A strategy definition can include position size, entry and exit rules, loss limits, cooldowns, session restrictions, portfolio exposure caps and persistent memory. If an exported program reproduces the signal while changing one of these state transitions, the resulting sequence of decisions can diverge before any broker-specific cost appears. This distinction matters when a researcher asks for an exact export. Exactness can be tested under identical inputs and explicitly matched semantics: the same observations, ordering, rounding, state initialization and event timing. Live execution introduces fills, latency, liquidity and venue behaviour that historical bars cannot determine. These need a separate execution model and an observable reconciliation process. The example in this paper has no fitted parameters and no search. We use a short return path to expose a specific contract failure: a loss stop is part of the policy in one replay and absent in another. The sequence is synthetic and intentionally contains a recovery after the stop. Its purpose is to demonstrate state divergence. It provides no general recommendation for or against loss stops. Figure 1. Synthetic unit-exposure replay without costs. Omitting the loss stop changes exposure after the second return. Both paths share the same first two observations.

Page 2

Stochastly / Working paper 03 / Synthetic methodology 2 1. The strategy as a state transition system Let s_t contain the persistent state before decision t, x_t the information then available, and u_t the requested action. A policy produces an action and updated internal state; an execution environment maps orders and market events into fills and account state. These mappings should be named separately in the contract. The state can contain an equity high-water mark, realised daily loss, bars since entry, remaining cooldown, pending orders and the model version. Two programs that hold the same current position can still disagree on these variables and later produce different orders. A comparison restricted to final equity misses that latent divergence. A replay test should compare the ordered event trace. For the replayed events, record observation identity, decision timestamp, policy state before and after, requested quantity, order type, stop and target levels, fills, fees and resulting exposure. Match discrete transitions exactly; compare floating-point values using tolerances justified by tick size, lot size and arithmetic semantics. State equivalence should include restart behaviour. If one program restores a daily-loss latch and another resets it, their next decisions differ even with identical market data. The same applies to timezone boundaries and daylight-saving transitions. Serialization format and reset rules are therefore part of the strategy interface. The contract also needs an unsupported-feature policy. Silently replacing an unavailable risk rule with a superficially similar one changes the strategy. A target exporter should either implement the specified transition and pass a discriminating test or identify the exact unsupported feature. The existence of syntactically valid target-language code does not establish behavioural equivalence. These requirements apply symmetrically to long and short positions, partial exits and reversals. A reversal can represent a close followed by a new entry, with two chargeable legs and different stop initialization. Compressing it into one signed quantity without preserving those semantics can change both the event trace and the cost calculation. Equations in plain text (1) (u_t,s_(t+1)) = policy(s_t,x_t). (fills_t,account_(t+1)) = execution(u_t,account_t,market_t).

Page 3

Stochastly / Working paper 03 / Synthetic methodology 3 2. A loss stop changes the policy The synthetic gross return sequence is -1%, -2%, +4%, +1%, -1%, +2%. The initial target is unit long exposure. One policy stops after observing cumulative equity at or below 97.5% of initial equity and remains flat for the rest of the example. The comparison policy holds through all six returns. There is no intrabar stop in this model; the decision follows an observed close. Without costs, the first two returns reduce equity to 0.99 times 0.98 = 0.9702. The stopped policy therefore has zero exposure from the third return onward. The unrestricted policy participates in the subsequent recovery. Their terminal equity values are obtained by replaying the six returns with their respective exposure histories. Table I. Terminal equity for every cost level Initial equity 1; total turnover 2 for both policies. Cost (bps) With stop Without stop 0 0.970200 1.029085 5 0.969225 1.028051 10 0.968251 1.027018 20 0.966304 1.024952 At five basis points per unit of turnover, the stopped path ends near 0.969225 and the unrestricted path near 1.028051. Both paths have total unit turnover two: one initial entry and one exit. Equal turnover does not imply equivalent decisions. The different exposure history is the primary source of the difference here. Our convention deducts entry turnover cost in the same one-period return factor as the held exposure. A stop triggered at a close changes the next period's exposure; its flattening cost is charged at that next boundary. An open position is liquidated at the final boundary, with an explicit final charge. These conventions are implemented in experiments.py and should be read before reusing the numbers. This example shows that matching entry signals can coexist with different exposure histories. It does not prove that an actual Stochastly export omits this stop, nor that any current export passes the full contract. Those claims require target-specific replay evidence. This paper defines the comparison that such evidence would need to satisfy.

Page 4

Stochastly / Working paper 03 / Synthetic methodology 4 3. Costs belong to a declared accounting model For a simplified fractional-exposure model, let q_t be the position held over a period, r_t its gross return and c the cost per unit of turnover. The recurrence below charges changes in exposure and compounds the resulting period return. Financing, spread asymmetry and market impact are outside this particular example. We evaluate four predeclared cost levels: zero, five, ten and twenty basis points. Both policies are rerun at the four levels, so a cost change is allowed to affect a state-dependent risk trigger. On this path the stop remains triggered after the second return at each level. A different path near the threshold could change its trigger time. Figure 2. Synthetic terminal equity across all declared turnover-cost levels. Each policy is replayed at each cost; neither curve is a live or historical market performance record. Backtests need cost assumptions because they change the economic meaning of the observed return stream. Live programs then reconcile actual fills and broker charges. Deducting the same modeled commission again from already net account equity would double count it. At the same time, removing costs from the research replay would make its evaluation economically incomplete. The interface must distinguish gross price movement, simulated deductions and actual ledger entries. Transaction-cost models can depend on size, spread, volatility and participation. The optimal-execution literature formalizes a trade-off between trading costs and price risk [1]. We do not fit such a model here. A constant basis-point charge is a controlled sensitivity parameter, without a capacity claim or calibration to a venue. For a deployable policy, specify whether risk limits read gross simulated equity, net simulated equity, realised broker equity or a reconciled account measure. A daily loss limit evaluated on one basis in research and another in production can change the permitted trades. Costs and policy state are connected whenever a policy consumes an account variable affected by execution. Equations in plain text (2) E_(t+1) = E_t [1+q_t r_t-c abs(q_t-q_(t-1))], with q_(-1)=0.

Page 5

Stochastly / Working paper 03 / Synthetic methodology 5 4. Bar data leaves some event orders unresolved Suppose a long position has a stop at 98 and a target at 104. An OHLC bar opening at 100, reaching a high of 105 and a low of 97, then closing at 101 is compatible with both target-first and stop-first paths. The bar extrema identify touches but not their order. Assigning the favourable order creates information absent from the data. A second bar can open at 95 below the stop. Filling a stop-market order at 98 would ignore the opening gap under a simple next-available-price convention. Actual fills can be worse than the opening print because of liquidity and order handling. Tick data may resolve event ordering but does not by itself provide a complete queue-position or market-impact model. A third case places the stop exactly at the recorded low. Whether equality triggers a trade, which side of the spread is observed and how prices are rounded must be specified. A fourth case has no barrier touch at all; the position should persist unless another rule exits it. Each case should be a separate executable test with the relevant state asserted. For ambiguous same-bar touches, a report can provide a range across admissible event orders, impose a declared conservative convention or require higher-resolution evidence. The choice affects the estimand and should remain fixed across candidate strategies. Changing it selectively after inspecting individual trades introduces an unrecorded discretionary policy. Risk controls should be tested under gaps, fast reversals, repeated stop triggers, partial fills, rejected orders and process restarts. A bounded exposure or a persistent latch can be a testable policy invariant. An exact realised exit price may remain outside the policy's control. A broker can reject an order even when the strategy requests the correct action; the failure path then becomes part of the operational specification. The synthetic replay in this paper uses period returns and close-observed decisions. It deliberately makes no same-bar stop-fill claim. Extending it to OHLC barriers requires an execution resolver and a new set of tests. Keeping this boundary explicit prevents a simple numerical illustration from being mistaken for a venue emulator.

Page 6

Stochastly / Working paper 03 / Synthetic methodology 6 5. Evidence required for an export claim A useful fidelity report starts with the shared fixture and the strategy specification. It records source and target revisions, supported features, input data identity, timezones, numeric conventions and initial state. It then compares the complete trace at the earliest divergence. A terminal-equity match can conceal offsetting errors. Tests should discriminate the feature they claim to validate. A loss-stop fixture needs a path that triggers the stop and then presents an opportunity that would otherwise be traded. A cooldown fixture needs a new signal inside the cooldown. A short-position fixture needs a downside and upside movement. Merely executing code that contains a named parameter does not establish its effect. Metamorphic checks add useful constraints. With zero costs and identical requested exposure, a cost-off replay should match the corresponding gross trace. Raising a constant cost while keeping exposure fixed should reduce terminal equity in this simple model. Once exposure responds to net equity, that monotonicity no longer follows automatically; the test should inspect why the policy changed. For this paper, results.json contains all four cost scenarios, the position vectors and the equity paths. The source script asserts the no-cost stopping boundary and the unrestricted compounded return. No market dataset, proprietary strategy, live account or target-language exporter was exercised. Reusing the document renderer establishes consistent typography; it does not convert these examples into an application export certification. The contract supported by this analysis is precise: under a shared event stream, preserve decision state and declared execution semantics; in live use, reconcile realised execution separately. A cost or fill discrepancy should remain attributable to its cause. An omitted policy rule changes what is being traded and must be treated as a specification failure. References [1] Almgren, R. and Chriss, N. (2001). Optimal Execution of Portfolio Transactions. Journal of Risk, 3(2), 5-39. Author manuscript: https://www.smallake.kr/wp-content/uploads/2016/03/optliq.pdf [2] Reproduction package for this working paper: experiments.py, PROTOCOL.md and results.json, linked from the journal reader. The state-transition definitions and synthetic fixtures in this paper are original methodological examples without empirical market findings. Research disclosures Methodological working paper; not peer reviewed. AI-assisted drafting and computation prepared for Mahe's review. All prices and returns used in examples are synthetic. This document does not certify an exporter, promise live/backtest equality, provide investment advice or claim validated trading performance. Live execution can diverge materially from a historical simulation.

From a backtest to executable rules, page 1. Full text is available above.
Paper 03 · 01 / 06
From a backtest to executable rules, page 2. Full text is available above.
Paper 03 · 02 / 06
From a backtest to executable rules, page 3. Full text is available above.
Paper 03 · 03 / 06
From a backtest to executable rules, page 4. Full text is available above.
Paper 03 · 04 / 06
From a backtest to executable rules, page 5. Full text is available above.
Paper 03 · 05 / 06
From a backtest to executable rules, page 6. Full text is available above.
Paper 03 · 06 / 06