← Changelog

Mean Reversion Oscillator, Correctness Audit

Patch7 June 2026· per-strategy

Audit / Issue Found

A deep review of every variable in this Ornstein-Uhlenbeck mean-reversion strategy turned up three real bugs in the entry and exit accounting.

Before the patch

The entry side was recording the buy price as the ask price, even though the strategy places a post-only buy that actually fills at the bid. Because ask is higher than bid by the full spread, the recorded average cost was inflated. Every dollar-cost-averaging layer that the strategy then opened inherited that bias, and the inflated average cost made all subsequent profit calculations look smaller than reality.

The exit side had the mirror problem. The strategy uses two different exit paths. A normal mean-reversion win is a post-only sell at the ask, which earns the maker rebate. A timed exit when the position has been held too long is a market sell that fills at the bid. The code was using bid for the profit calculation and the audit log on both paths, including the post-only path that actually fills at ask. So every mean-reversion win was under-counted by the spread.

The audit log was also recording a copy of the sell even when the BE_GUARD safety blocked the order from going out. Analytics replaying the log would treat that ghost trade as real.

After the patch

The buy entry now records the bid as the cost basis. Average cost rebuilds from the honest starting point.

The exit logic now picks the correct price based on the path used: bid for the market sell, ask for the post-only sell. The profit calculation and the audit log both use that price consistently.

The audit log is now gated by the BE_GUARD safety, so blocked sells no longer leave phantom entries behind.

Bottom line

Your average entry price, your win-loss record, and the running profit total now match what the exchange actually did. The mean-reversion wins that earn the maker rebate are no longer silently understated by the spread.