FAILURE MAP
← Case archive

FA-62036 / Currency rounding and FX conversion / Open access

A position flip keeps the old lot's average rate · case 01

A long position flipped short reports the old long average for the new short lot.

Verified by executionVariant 1 · 8 checks per implementationDownload source bundle ↓JSON ↗

ROOT CAUSE

After a reduction that crosses zero the average is not reset to the flipping trade's rate.

VERIFIED REPAIR

When the sign of the position changes, open the remainder at the trade rate.

Unsuccessful approach: Setting avg to the trade rate on every close also reports an average for a flat position.

Case contract

solve(trades, spot): trades are [qty, rate] in foreign units (buy positive, sell negative) at home-per-foreign decimal rates. Adding in the position's direction updates the weighted average rate; reducing realizes closed_qty * (rate - avg) * sign(position) without changing avg; a flip realizes the closed part and opens the remainder at the trade rate; a flat position has no average. Zero-quantity trades are ignored. Unrealized = position * (spot - avg). Return [position, avg to 6 dp or None, realized to 2 dp, unrealized to 2 dp], rounded half-even only at output with unsigned zeros.

Why this case matters

Currency amounts must be rounded at the right stage and in the right unit, or ledgers, quotes and settlements drift by minor units.

1 / The failure

Exit 1
"""Failure Map reference implementation. Python standard library only."""
import json
from decimal import Decimal, ROUND_HALF_EVEN, ROUND_HALF_UP, ROUND_FLOOR, ROUND_CEILING, ROUND_DOWN, ROUND_UP, ROUND_HALF_DOWN
from fractions import Fraction
N = 1
observations = []
def solve(trades, spot):
    def fmt(fr, e):
        d = (Decimal(fr.numerator) / Decimal(fr.denominator)).quantize(Decimal(1).scaleb(-e), rounding=ROUND_HALF_EVEN)
        return format(abs(d) if d == 0 else d, 'f')
    pos, avg, realized = 0, None, Fraction(0)
    for qty, rate in trades:
        r = Fraction(rate)
        if qty == 0: continue
        if pos == 0 or (pos > 0) == (qty > 0):
            avg = r if pos == 0 else (abs(pos) * avg + abs(qty) * r) / (abs(pos) + abs(qty))
            pos += qty
        else:
            closed = min(abs(qty), abs(pos))
            sgn = 1 if pos > 0 else -1
            realized += closed * (r - avg) * sgn
            pos += qty
            if pos == 0: avg = None
    unreal = pos * (Fraction(spot) - avg) if pos else Fraction(0)
    return [pos, None if avg is None else fmt(avg, 6), fmt(realized, 2), fmt(unreal, 2)]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('regression flip-reset 1', ([[100, '1.10'], [-150, '1.00']], '1.05'), [-50, '1.000000', '-10.00', '-2.50']),
  ('regression flip-reset 2', ([[-40, '1.2'], [100, '1.1']], '1.15'), [60, '1.100000', '4.00', '3.00']),
  ('partial repair guard 1', ([[-100, '1.10'], [100, '1.00']], '1.05'), [0, None, '10.00', '0.00']),
  ('partial repair guard 2', ([[300, '1.0711'], [-100, '1.0733'], [-100, '1.0745'], [-100, '1.0719']], '1.07'),
   [0, None, '0.64', '0.00']),
  ('control: add then reduce', ([[100, '1.10'], [100, '1.20'], [-50, '1.30']], '1.25'),
   [150, '1.150000', '7.50', '15.00']),
  ('control: zero trade only', ([[0, '1.10']], '1.05'), [0, None, '0.00', '0.00']),
  ('control: short add', ([[-30, '1.0833'], [-70, '1.0851']], '1.0800'), [-100, '1.084560', '0.00', '0.46']),
  ('control: zero between', ([[50, '1.1'], [-50, '1.2'], [0, '1.3'], [20, '1.4']], '1.5'),
   [20, '1.400000', '5.00', '2.00'])],
 [('regression flip-reset 1', ([[-90, '1.0708'], [540, '1.1315']], '1.0885'), [450, '1.131500', '-5.46', '-19.35']),
  ('regression flip-reset 2', ([[-220, '1.1085'], [310, '1.1162'], [-140, '1.1499']], '1.1267'),
   [-50, '1.149900', '1.34', '1.16']),
  ('partial repair guard 1', ([[-100, '1.10'], [100, '1.00']], '1.05'), [0, None, '10.00', '0.00']),
  ('partial repair guard 2', ([[300, '1.0711'], [-100, '1.0733'], [-100, '1.0745'], [-100, '1.0719']], '1.07'),
   [0, None, '0.64', '0.00']),
  ('control: zero trade only', ([[0, '1.10']], '1.05'), [0, None, '0.00', '0.00']),
  ('control: short add', ([[-30, '1.0833'], [-70, '1.0851']], '1.0800'), [-100, '1.084560', '0.00', '0.46']),
  ('control: zero between', ([[50, '1.1'], [-50, '1.2'], [0, '1.3'], [20, '1.4']], '1.5'),
   [20, '1.400000', '5.00', '2.00']),
  ('control: flip short to long', ([[-40, '1.2'], [100, '1.1']], '1.15'), [60, '1.100000', '4.00', '3.00'])],
 [('regression flip-reset 1',
   ([[0, '1.0601'], [0, '1.0705'], [-110, '1.1230'], [310, '1.0559'], [400, '1.1307']], '1.1024'),
   [600, '1.105767', '7.38', '-2.02']),
  ('regression flip-reset 2',
   ([[160, '1.0719'], [-230, '1.0804'], [-80, '1.1289'], [50, '1.1492'], [570, '1.1139'], [570, '1.1011']], '1.0962'),
   [1040, '1.106885', '-1.55', '-11.11']),
  ('partial repair guard 1', ([[-100, '1.10'], [100, '1.00']], '1.05'), [0, None, '10.00', '0.00']),
  ('partial repair guard 2', ([[300, '1.0711'], [-100, '1.0733'], [-100, '1.0745'], [-100, '1.0719']], '1.07'),
   [0, None, '0.64', '0.00']),
  ('control: zero between', ([[50, '1.1'], [-50, '1.2'], [0, '1.3'], [20, '1.4']], '1.5'),
   [20, '1.400000', '5.00', '2.00']),
  ('control: flip short to long', ([[-40, '1.2'], [100, '1.1']], '1.15'), [60, '1.100000', '4.00', '3.00']),
  ('control: add then reduce', ([[100, '1.10'], [100, '1.20'], [-50, '1.30']], '1.25'),
   [150, '1.150000', '7.50', '15.00']),
  ('control: flip long to short', ([[100, '1.10'], [-150, '1.00']], '1.05'), [-50, '1.000000', '-10.00', '-2.50'])],
 [('regression flip-reset 1',
   ([[-430, '1.0685'], [30, '1.0569'], [100, '1.0804'], [540, '1.0505'], [0, '1.0888'], [-490, '1.0791']], '1.0807'),
   [-250, '1.079100', '11.42', '-0.40']),
  ('regression flip-reset 2', ([[440, '1.0718'], [-570, '1.0726'], [40, '1.1343'], [-160, '1.1482']], '1.1222'),
   [-250, '1.120984', '-2.12', '-0.30']),
  ('partial repair guard 1', ([[-100, '1.10'], [100, '1.00']], '1.05'), [0, None, '10.00', '0.00']),
  ('partial repair guard 2', ([[300, '1.0711'], [-100, '1.0733'], [-100, '1.0745'], [-100, '1.0719']], '1.07'),
   [0, None, '0.64', '0.00']),
  ('control: flip long to short', ([[100, '1.10'], [-150, '1.00']], '1.05'), [-50, '1.000000', '-10.00', '-2.50']),
  ('control: zero trade only', ([[0, '1.10']], '1.05'), [0, None, '0.00', '0.00']),
  ('control: short add', ([[-30, '1.0833'], [-70, '1.0851']], '1.0800'), [-100, '1.084560', '0.00', '0.46']),
  ('control: zero between', ([[50, '1.1'], [-50, '1.2'], [0, '1.3'], [20, '1.4']], '1.5'),
   [20, '1.400000', '5.00', '2.00'])],
 [('regression flip-reset 1',
   ([[-560, '1.1226'], [90, '1.1160'], [300, '1.0734'], [-220, '1.0848'], [500, '1.0820']], '1.1354'),
   [110, '1.082000', '22.87', '5.87']),
  ('regression flip-reset 2', ([[-290, '1.1340'], [220, '1.0570'], [420, '1.0731']], '1.1418'),
   [350, '1.073100', '21.20', '24.04']),
  ('partial repair guard 1', ([[-100, '1.10'], [100, '1.00']], '1.05'), [0, None, '10.00', '0.00']),
  ('partial repair guard 2', ([[300, '1.0711'], [-100, '1.0733'], [-100, '1.0745'], [-100, '1.0719']], '1.07'),
   [0, None, '0.64', '0.00']),
  ('control: short add', ([[-30, '1.0833'], [-70, '1.0851']], '1.0800'), [-100, '1.084560', '0.00', '0.46']),
  ('control: zero between', ([[50, '1.1'], [-50, '1.2'], [0, '1.3'], [20, '1.4']], '1.5'),
   [20, '1.400000', '5.00', '2.00']),
  ('control: flip short to long', ([[-40, '1.2'], [100, '1.1']], '1.15'), [60, '1.100000', '4.00', '3.00']),
  ('control: add then reduce', ([[100, '1.10'], [100, '1.20'], [-50, '1.30']], '1.25'),
   [150, '1.150000', '7.50', '15.00'])]]
for label, args, expected in cases[N - 1]:
    check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)
Boundary fixtureActualExpectedOutcome
regression flip-reset 1[-50, '1.100000', '-10.00', '2.50'][-50, '1.000000', '-10.00', '-2.50']Failed
regression flip-reset 2[60, '1.200000', '4.00', '-3.00'][60, '1.100000', '4.00', '3.00']Failed
partial repair guard 1[0, None, '10.00', '0.00'][0, None, '10.00', '0.00']Passed
partial repair guard 2[0, None, '0.64', '0.00'][0, None, '0.64', '0.00']Passed
control: add then reduce[150, '1.150000', '7.50', '15.00'][150, '1.150000', '7.50', '15.00']Passed
control: zero trade only[0, None, '0.00', '0.00'][0, None, '0.00', '0.00']Passed
control: short add[-100, '1.084560', '0.00', '0.46'][-100, '1.084560', '0.00', '0.46']Passed
control: zero between[20, '1.400000', '5.00', '2.00'][20, '1.400000', '5.00', '2.00']Passed

SHA-256 / 127a188c654ae9542c547d41080d6abff481c4b9d14fef825afd2bb171902e3c

2 / The unsuccessful fix

Exit 1
"""Failure Map reference implementation. Python standard library only."""
import json
from decimal import Decimal, ROUND_HALF_EVEN, ROUND_HALF_UP, ROUND_FLOOR, ROUND_CEILING, ROUND_DOWN, ROUND_UP, ROUND_HALF_DOWN
from fractions import Fraction
N = 1
observations = []
def solve(trades, spot):
    def fmt(fr, e):
        d = (Decimal(fr.numerator) / Decimal(fr.denominator)).quantize(Decimal(1).scaleb(-e), rounding=ROUND_HALF_EVEN)
        return format(abs(d) if d == 0 else d, 'f')
    pos, avg, realized = 0, None, Fraction(0)
    for qty, rate in trades:
        r = Fraction(rate)
        if qty == 0: continue
        if pos == 0 or (pos > 0) == (qty > 0):
            avg = r if pos == 0 else (abs(pos) * avg + abs(qty) * r) / (abs(pos) + abs(qty))
            pos += qty
        else:
            closed = min(abs(qty), abs(pos))
            sgn = 1 if pos > 0 else -1
            realized += closed * (r - avg) * sgn
            pos += qty
            if pos == 0 or (pos > 0) != (sgn > 0): avg = r
    unreal = pos * (Fraction(spot) - avg) if pos else Fraction(0)
    return [pos, None if avg is None else fmt(avg, 6), fmt(realized, 2), fmt(unreal, 2)]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('regression flip-reset 1', ([[100, '1.10'], [-150, '1.00']], '1.05'), [-50, '1.000000', '-10.00', '-2.50']),
  ('regression flip-reset 2', ([[-40, '1.2'], [100, '1.1']], '1.15'), [60, '1.100000', '4.00', '3.00']),
  ('partial repair guard 1', ([[-100, '1.10'], [100, '1.00']], '1.05'), [0, None, '10.00', '0.00']),
  ('partial repair guard 2', ([[300, '1.0711'], [-100, '1.0733'], [-100, '1.0745'], [-100, '1.0719']], '1.07'),
   [0, None, '0.64', '0.00']),
  ('control: add then reduce', ([[100, '1.10'], [100, '1.20'], [-50, '1.30']], '1.25'),
   [150, '1.150000', '7.50', '15.00']),
  ('control: zero trade only', ([[0, '1.10']], '1.05'), [0, None, '0.00', '0.00']),
  ('control: short add', ([[-30, '1.0833'], [-70, '1.0851']], '1.0800'), [-100, '1.084560', '0.00', '0.46']),
  ('control: zero between', ([[50, '1.1'], [-50, '1.2'], [0, '1.3'], [20, '1.4']], '1.5'),
   [20, '1.400000', '5.00', '2.00'])],
 [('regression flip-reset 1', ([[-90, '1.0708'], [540, '1.1315']], '1.0885'), [450, '1.131500', '-5.46', '-19.35']),
  ('regression flip-reset 2', ([[-220, '1.1085'], [310, '1.1162'], [-140, '1.1499']], '1.1267'),
   [-50, '1.149900', '1.34', '1.16']),
  ('partial repair guard 1', ([[-100, '1.10'], [100, '1.00']], '1.05'), [0, None, '10.00', '0.00']),
  ('partial repair guard 2', ([[300, '1.0711'], [-100, '1.0733'], [-100, '1.0745'], [-100, '1.0719']], '1.07'),
   [0, None, '0.64', '0.00']),
  ('control: zero trade only', ([[0, '1.10']], '1.05'), [0, None, '0.00', '0.00']),
  ('control: short add', ([[-30, '1.0833'], [-70, '1.0851']], '1.0800'), [-100, '1.084560', '0.00', '0.46']),
  ('control: zero between', ([[50, '1.1'], [-50, '1.2'], [0, '1.3'], [20, '1.4']], '1.5'),
   [20, '1.400000', '5.00', '2.00']),
  ('control: flip short to long', ([[-40, '1.2'], [100, '1.1']], '1.15'), [60, '1.100000', '4.00', '3.00'])],
 [('regression flip-reset 1',
   ([[0, '1.0601'], [0, '1.0705'], [-110, '1.1230'], [310, '1.0559'], [400, '1.1307']], '1.1024'),
   [600, '1.105767', '7.38', '-2.02']),
  ('regression flip-reset 2',
   ([[160, '1.0719'], [-230, '1.0804'], [-80, '1.1289'], [50, '1.1492'], [570, '1.1139'], [570, '1.1011']], '1.0962'),
   [1040, '1.106885', '-1.55', '-11.11']),
  ('partial repair guard 1', ([[-100, '1.10'], [100, '1.00']], '1.05'), [0, None, '10.00', '0.00']),
  ('partial repair guard 2', ([[300, '1.0711'], [-100, '1.0733'], [-100, '1.0745'], [-100, '1.0719']], '1.07'),
   [0, None, '0.64', '0.00']),
  ('control: zero between', ([[50, '1.1'], [-50, '1.2'], [0, '1.3'], [20, '1.4']], '1.5'),
   [20, '1.400000', '5.00', '2.00']),
  ('control: flip short to long', ([[-40, '1.2'], [100, '1.1']], '1.15'), [60, '1.100000', '4.00', '3.00']),
  ('control: add then reduce', ([[100, '1.10'], [100, '1.20'], [-50, '1.30']], '1.25'),
   [150, '1.150000', '7.50', '15.00']),
  ('control: flip long to short', ([[100, '1.10'], [-150, '1.00']], '1.05'), [-50, '1.000000', '-10.00', '-2.50'])],
 [('regression flip-reset 1',
   ([[-430, '1.0685'], [30, '1.0569'], [100, '1.0804'], [540, '1.0505'], [0, '1.0888'], [-490, '1.0791']], '1.0807'),
   [-250, '1.079100', '11.42', '-0.40']),
  ('regression flip-reset 2', ([[440, '1.0718'], [-570, '1.0726'], [40, '1.1343'], [-160, '1.1482']], '1.1222'),
   [-250, '1.120984', '-2.12', '-0.30']),
  ('partial repair guard 1', ([[-100, '1.10'], [100, '1.00']], '1.05'), [0, None, '10.00', '0.00']),
  ('partial repair guard 2', ([[300, '1.0711'], [-100, '1.0733'], [-100, '1.0745'], [-100, '1.0719']], '1.07'),
   [0, None, '0.64', '0.00']),
  ('control: flip long to short', ([[100, '1.10'], [-150, '1.00']], '1.05'), [-50, '1.000000', '-10.00', '-2.50']),
  ('control: zero trade only', ([[0, '1.10']], '1.05'), [0, None, '0.00', '0.00']),
  ('control: short add', ([[-30, '1.0833'], [-70, '1.0851']], '1.0800'), [-100, '1.084560', '0.00', '0.46']),
  ('control: zero between', ([[50, '1.1'], [-50, '1.2'], [0, '1.3'], [20, '1.4']], '1.5'),
   [20, '1.400000', '5.00', '2.00'])],
 [('regression flip-reset 1',
   ([[-560, '1.1226'], [90, '1.1160'], [300, '1.0734'], [-220, '1.0848'], [500, '1.0820']], '1.1354'),
   [110, '1.082000', '22.87', '5.87']),
  ('regression flip-reset 2', ([[-290, '1.1340'], [220, '1.0570'], [420, '1.0731']], '1.1418'),
   [350, '1.073100', '21.20', '24.04']),
  ('partial repair guard 1', ([[-100, '1.10'], [100, '1.00']], '1.05'), [0, None, '10.00', '0.00']),
  ('partial repair guard 2', ([[300, '1.0711'], [-100, '1.0733'], [-100, '1.0745'], [-100, '1.0719']], '1.07'),
   [0, None, '0.64', '0.00']),
  ('control: short add', ([[-30, '1.0833'], [-70, '1.0851']], '1.0800'), [-100, '1.084560', '0.00', '0.46']),
  ('control: zero between', ([[50, '1.1'], [-50, '1.2'], [0, '1.3'], [20, '1.4']], '1.5'),
   [20, '1.400000', '5.00', '2.00']),
  ('control: flip short to long', ([[-40, '1.2'], [100, '1.1']], '1.15'), [60, '1.100000', '4.00', '3.00']),
  ('control: add then reduce', ([[100, '1.10'], [100, '1.20'], [-50, '1.30']], '1.25'),
   [150, '1.150000', '7.50', '15.00'])]]
for label, args, expected in cases[N - 1]:
    check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)
Boundary fixtureActualExpectedOutcome
regression flip-reset 1[-50, '1.000000', '-10.00', '-2.50'][-50, '1.000000', '-10.00', '-2.50']Passed
regression flip-reset 2[60, '1.100000', '4.00', '3.00'][60, '1.100000', '4.00', '3.00']Passed
partial repair guard 1[0, '1.000000', '10.00', '0.00'][0, None, '10.00', '0.00']Failed
partial repair guard 2[0, '1.071900', '0.64', '0.00'][0, None, '0.64', '0.00']Failed
control: add then reduce[150, '1.150000', '7.50', '15.00'][150, '1.150000', '7.50', '15.00']Passed
control: zero trade only[0, None, '0.00', '0.00'][0, None, '0.00', '0.00']Passed
control: short add[-100, '1.084560', '0.00', '0.46'][-100, '1.084560', '0.00', '0.46']Passed
control: zero between[20, '1.400000', '5.00', '2.00'][20, '1.400000', '5.00', '2.00']Passed

SHA-256 / 8754593baa12dcaad25849fa953bc14a3bfb24556db2b73761dfc57ae1901512

3 / The verified repair

Exit 0
"""Failure Map reference implementation. Python standard library only."""
import json
from decimal import Decimal, ROUND_HALF_EVEN, ROUND_HALF_UP, ROUND_FLOOR, ROUND_CEILING, ROUND_DOWN, ROUND_UP, ROUND_HALF_DOWN
from fractions import Fraction
N = 1
observations = []
def solve(trades, spot):
    def fmt(fr, e):
        d = (Decimal(fr.numerator) / Decimal(fr.denominator)).quantize(Decimal(1).scaleb(-e), rounding=ROUND_HALF_EVEN)
        return format(abs(d) if d == 0 else d, 'f')
    pos, avg, realized = 0, None, Fraction(0)
    for qty, rate in trades:
        r = Fraction(rate)
        if qty == 0: continue
        if pos == 0 or (pos > 0) == (qty > 0):
            avg = r if pos == 0 else (abs(pos) * avg + abs(qty) * r) / (abs(pos) + abs(qty))
            pos += qty
        else:
            closed = min(abs(qty), abs(pos))
            sgn = 1 if pos > 0 else -1
            realized += closed * (r - avg) * sgn
            pos += qty
            if pos == 0: avg = None
            elif (pos > 0) != (sgn > 0): avg = r
    unreal = pos * (Fraction(spot) - avg) if pos else Fraction(0)
    return [pos, None if avg is None else fmt(avg, 6), fmt(realized, 2), fmt(unreal, 2)]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('regression flip-reset 1', ([[100, '1.10'], [-150, '1.00']], '1.05'), [-50, '1.000000', '-10.00', '-2.50']),
  ('regression flip-reset 2', ([[-40, '1.2'], [100, '1.1']], '1.15'), [60, '1.100000', '4.00', '3.00']),
  ('partial repair guard 1', ([[-100, '1.10'], [100, '1.00']], '1.05'), [0, None, '10.00', '0.00']),
  ('partial repair guard 2', ([[300, '1.0711'], [-100, '1.0733'], [-100, '1.0745'], [-100, '1.0719']], '1.07'),
   [0, None, '0.64', '0.00']),
  ('control: add then reduce', ([[100, '1.10'], [100, '1.20'], [-50, '1.30']], '1.25'),
   [150, '1.150000', '7.50', '15.00']),
  ('control: zero trade only', ([[0, '1.10']], '1.05'), [0, None, '0.00', '0.00']),
  ('control: short add', ([[-30, '1.0833'], [-70, '1.0851']], '1.0800'), [-100, '1.084560', '0.00', '0.46']),
  ('control: zero between', ([[50, '1.1'], [-50, '1.2'], [0, '1.3'], [20, '1.4']], '1.5'),
   [20, '1.400000', '5.00', '2.00'])],
 [('regression flip-reset 1', ([[-90, '1.0708'], [540, '1.1315']], '1.0885'), [450, '1.131500', '-5.46', '-19.35']),
  ('regression flip-reset 2', ([[-220, '1.1085'], [310, '1.1162'], [-140, '1.1499']], '1.1267'),
   [-50, '1.149900', '1.34', '1.16']),
  ('partial repair guard 1', ([[-100, '1.10'], [100, '1.00']], '1.05'), [0, None, '10.00', '0.00']),
  ('partial repair guard 2', ([[300, '1.0711'], [-100, '1.0733'], [-100, '1.0745'], [-100, '1.0719']], '1.07'),
   [0, None, '0.64', '0.00']),
  ('control: zero trade only', ([[0, '1.10']], '1.05'), [0, None, '0.00', '0.00']),
  ('control: short add', ([[-30, '1.0833'], [-70, '1.0851']], '1.0800'), [-100, '1.084560', '0.00', '0.46']),
  ('control: zero between', ([[50, '1.1'], [-50, '1.2'], [0, '1.3'], [20, '1.4']], '1.5'),
   [20, '1.400000', '5.00', '2.00']),
  ('control: flip short to long', ([[-40, '1.2'], [100, '1.1']], '1.15'), [60, '1.100000', '4.00', '3.00'])],
 [('regression flip-reset 1',
   ([[0, '1.0601'], [0, '1.0705'], [-110, '1.1230'], [310, '1.0559'], [400, '1.1307']], '1.1024'),
   [600, '1.105767', '7.38', '-2.02']),
  ('regression flip-reset 2',
   ([[160, '1.0719'], [-230, '1.0804'], [-80, '1.1289'], [50, '1.1492'], [570, '1.1139'], [570, '1.1011']], '1.0962'),
   [1040, '1.106885', '-1.55', '-11.11']),
  ('partial repair guard 1', ([[-100, '1.10'], [100, '1.00']], '1.05'), [0, None, '10.00', '0.00']),
  ('partial repair guard 2', ([[300, '1.0711'], [-100, '1.0733'], [-100, '1.0745'], [-100, '1.0719']], '1.07'),
   [0, None, '0.64', '0.00']),
  ('control: zero between', ([[50, '1.1'], [-50, '1.2'], [0, '1.3'], [20, '1.4']], '1.5'),
   [20, '1.400000', '5.00', '2.00']),
  ('control: flip short to long', ([[-40, '1.2'], [100, '1.1']], '1.15'), [60, '1.100000', '4.00', '3.00']),
  ('control: add then reduce', ([[100, '1.10'], [100, '1.20'], [-50, '1.30']], '1.25'),
   [150, '1.150000', '7.50', '15.00']),
  ('control: flip long to short', ([[100, '1.10'], [-150, '1.00']], '1.05'), [-50, '1.000000', '-10.00', '-2.50'])],
 [('regression flip-reset 1',
   ([[-430, '1.0685'], [30, '1.0569'], [100, '1.0804'], [540, '1.0505'], [0, '1.0888'], [-490, '1.0791']], '1.0807'),
   [-250, '1.079100', '11.42', '-0.40']),
  ('regression flip-reset 2', ([[440, '1.0718'], [-570, '1.0726'], [40, '1.1343'], [-160, '1.1482']], '1.1222'),
   [-250, '1.120984', '-2.12', '-0.30']),
  ('partial repair guard 1', ([[-100, '1.10'], [100, '1.00']], '1.05'), [0, None, '10.00', '0.00']),
  ('partial repair guard 2', ([[300, '1.0711'], [-100, '1.0733'], [-100, '1.0745'], [-100, '1.0719']], '1.07'),
   [0, None, '0.64', '0.00']),
  ('control: flip long to short', ([[100, '1.10'], [-150, '1.00']], '1.05'), [-50, '1.000000', '-10.00', '-2.50']),
  ('control: zero trade only', ([[0, '1.10']], '1.05'), [0, None, '0.00', '0.00']),
  ('control: short add', ([[-30, '1.0833'], [-70, '1.0851']], '1.0800'), [-100, '1.084560', '0.00', '0.46']),
  ('control: zero between', ([[50, '1.1'], [-50, '1.2'], [0, '1.3'], [20, '1.4']], '1.5'),
   [20, '1.400000', '5.00', '2.00'])],
 [('regression flip-reset 1',
   ([[-560, '1.1226'], [90, '1.1160'], [300, '1.0734'], [-220, '1.0848'], [500, '1.0820']], '1.1354'),
   [110, '1.082000', '22.87', '5.87']),
  ('regression flip-reset 2', ([[-290, '1.1340'], [220, '1.0570'], [420, '1.0731']], '1.1418'),
   [350, '1.073100', '21.20', '24.04']),
  ('partial repair guard 1', ([[-100, '1.10'], [100, '1.00']], '1.05'), [0, None, '10.00', '0.00']),
  ('partial repair guard 2', ([[300, '1.0711'], [-100, '1.0733'], [-100, '1.0745'], [-100, '1.0719']], '1.07'),
   [0, None, '0.64', '0.00']),
  ('control: short add', ([[-30, '1.0833'], [-70, '1.0851']], '1.0800'), [-100, '1.084560', '0.00', '0.46']),
  ('control: zero between', ([[50, '1.1'], [-50, '1.2'], [0, '1.3'], [20, '1.4']], '1.5'),
   [20, '1.400000', '5.00', '2.00']),
  ('control: flip short to long', ([[-40, '1.2'], [100, '1.1']], '1.15'), [60, '1.100000', '4.00', '3.00']),
  ('control: add then reduce', ([[100, '1.10'], [100, '1.20'], [-50, '1.30']], '1.25'),
   [150, '1.150000', '7.50', '15.00'])]]
for label, args, expected in cases[N - 1]:
    check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)
Boundary fixtureActualExpectedOutcome
regression flip-reset 1[-50, '1.000000', '-10.00', '-2.50'][-50, '1.000000', '-10.00', '-2.50']Passed
regression flip-reset 2[60, '1.100000', '4.00', '3.00'][60, '1.100000', '4.00', '3.00']Passed
partial repair guard 1[0, None, '10.00', '0.00'][0, None, '10.00', '0.00']Passed
partial repair guard 2[0, None, '0.64', '0.00'][0, None, '0.64', '0.00']Passed
control: add then reduce[150, '1.150000', '7.50', '15.00'][150, '1.150000', '7.50', '15.00']Passed
control: zero trade only[0, None, '0.00', '0.00'][0, None, '0.00', '0.00']Passed
control: short add[-100, '1.084560', '0.00', '0.46'][-100, '1.084560', '0.00', '0.46']Passed
control: zero between[20, '1.400000', '5.00', '2.00'][20, '1.400000', '5.00', '2.00']Passed

SHA-256 / c6fe2b8dcc06edbee9bb0887496458202102181ba16e447ddacee897533ad743

Verification & scope

A deterministic, bounded teaching model with a stipulated toy contract; it makes no claim of conformance to any real regulation, standard, or institution's rules. This reproducer isolates one failure mechanism. Results cover the supplied fixtures. Variants within a family share a test contract and should remain grouped when constructing evaluation splits. Related mechanisms with a shared evaluation_group must also remain together; these controlled models are not independent production incidents.

Observations recorded using Python 3.12.14 at 2026-09-29T14:47:00.789232+00:00.

Case digest / caea61d4796fee740ed91592fc076bce3ffc698d6dbee502c046c35eabae1fcd