FAILURE MAP
← Case archive

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

Realized P&L is rounded to cents after every closing trade · case 01

Several small closes accumulate a cent-level discrepancy against the exact realized total.

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

ROOT CAUSE

The running realized amount is quantized to two decimals after each reduction.

VERIFIED REPAIR

Accumulate realized P&L exactly and round only when reporting.

Unsuccessful approach: Rounding the running total to four decimals still compounds per-trade rounding error.

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
            realized = Fraction(fmt(realized, 2))
            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 realized-accumulation-rounding 1', ([[3, '1'], [-1, '1.005'], [-1, '1.005'], [-1, '1.005']], '1'),
   [0, None, '0.02', '0.00']),
  ('regression realized-accumulation-rounding 2', ([[2, '1'], [-1, '1.00252'], [-1, '1.00251']], '1'),
   [0, None, '0.01', '0.00']),
  ('partial repair guard 1', ([[1, '1'], [-1, '1.00502']], '1'), [0, None, '0.01', '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: flip long to short', ([[100, '1.10'], [-150, '1.00']], '1.05'), [-50, '1.000000', '-10.00', '-2.50']),
  ('control: close short', ([[-100, '1.10'], [100, '1.00']], '1.05'), [0, None, '10.00', '0.00']),
  ('control: zero trade only', ([[0, '1.10']], '1.05'), [0, None, '0.00', '0.00'])],
 [('regression realized-accumulation-rounding 1',
   ([[-160, '1.1445'], [370, '1.0518'], [470, '1.1207'], [-420, '1.1186'], [0, '1.1436'], [-430, '1.0692']], '1.1002'),
   [-170, '1.069200', '15.03', '-5.27']),
  ('regression realized-accumulation-rounding 2',
   ([[-440, '1.1376'], [320, '1.1062'], [-500, '1.1066'], [0, '1.0745'], [-450, '1.0551'], [560, '1.0788']], '1.1271'),
   [-510, '1.088418', '15.43', '-19.73']),
  ('partial repair guard 1', ([[1, '1'], [-1, '1.00502']], '1'), [0, None, '0.01', '0.00']),
  ('partial repair guard 2', ([[2, '1'], [-1, '1.00252'], [-1, '1.00251']], '1'), [0, None, '0.01', '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: three reductions', ([[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'])],
 [('regression realized-accumulation-rounding 1', ([[500, '1.1350'], [-210, '1.0625'], [-530, '1.1133']], '1.1442'),
   [-240, '1.113300', '-21.52', '-7.42']),
  ('regression realized-accumulation-rounding 2',
   ([[420, '1.0549'], [-340, '1.1155'], [-600, '1.1019'], [120, '1.0728'], [120, '1.0952'], [300, '1.0728']], '1.1408'),
   [20, '1.072800', '36.81', '1.36']),
  ('partial repair guard 1', ([[1, '1'], [-1, '1.00502']], '1'), [0, None, '0.01', '0.00']),
  ('partial repair guard 2', ([[2, '1'], [-1, '1.00252'], [-1, '1.00251']], '1'), [0, None, '0.01', '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 realized-accumulation-rounding 1',
   ([[-330, '1.0644'], [460, '1.1022'], [130, '1.1361'], [0, '1.0789'], [-160, '1.0850']], '1.0640'),
   [100, '1.119150', '-17.94', '-5.52']),
  ('regression realized-accumulation-rounding 2', ([[10, '1.1411'], [-370, '1.0726'], [50, '1.0616']], '1.1081'),
   [-310, '1.072600', '-0.14', '-11.00']),
  ('partial repair guard 1', ([[1, '1'], [-1, '1.00502']], '1'), [0, None, '0.01', '0.00']),
  ('partial repair guard 2', ([[2, '1'], [-1, '1.00252'], [-1, '1.00251']], '1'), [0, None, '0.01', '0.00']),
  ('control: flip long to short', ([[100, '1.10'], [-150, '1.00']], '1.05'), [-50, '1.000000', '-10.00', '-2.50']),
  ('control: close short', ([[-100, '1.10'], [100, '1.00']], '1.05'), [0, None, '10.00', '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'])],
 [('regression realized-accumulation-rounding 1',
   ([[120, '1.1265'], [-100, '1.1177'], [270, '1.1372'], [-530, '1.1150'], [510, '1.0931'], [-490, '1.0584']],
    '1.1400'),
   [-220, '1.058400', '-11.22', '-17.95']),
  ('regression realized-accumulation-rounding 2',
   ([[460, '1.0523'], [340, '1.1064'], [-460, '1.1068'], [0, '1.1268'], [-560, '1.0903'], [190, '1.0925']], '1.0886'),
   [-30, '1.090300', '19.18', '0.05']),
  ('partial repair guard 1', ([[1, '1'], [-1, '1.00502']], '1'), [0, None, '0.01', '0.00']),
  ('partial repair guard 2', ([[2, '1'], [-1, '1.00252'], [-1, '1.00251']], '1'), [0, None, '0.01', '0.00']),
  ('control: short add', ([[-30, '1.0833'], [-70, '1.0851']], '1.0800'), [-100, '1.084560', '0.00', '0.46']),
  ('control: three reductions', ([[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'])]]
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 realized-accumulation-rounding 1[0, None, '0.00', '0.00'][0, None, '0.02', '0.00']Failed
regression realized-accumulation-rounding 2[0, None, '0.00', '0.00'][0, None, '0.01', '0.00']Failed
partial repair guard 1[0, None, '0.01', '0.00'][0, None, '0.01', '0.00']Passed
control: add then reduce[150, '1.150000', '7.50', '15.00'][150, '1.150000', '7.50', '15.00']Passed
control: flip long to short[-50, '1.000000', '-10.00', '-2.50'][-50, '1.000000', '-10.00', '-2.50']Passed
control: close short[0, None, '10.00', '0.00'][0, None, '10.00', '0.00']Passed
control: zero trade only[0, None, '0.00', '0.00'][0, None, '0.00', '0.00']Passed

SHA-256 / 065e7b7bdcbd520e824f3a8706452a86dd1499ad11d5ed3487e92146ec0d73e7

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
            realized = Fraction(fmt(realized, 4))
            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 realized-accumulation-rounding 1', ([[3, '1'], [-1, '1.005'], [-1, '1.005'], [-1, '1.005']], '1'),
   [0, None, '0.02', '0.00']),
  ('regression realized-accumulation-rounding 2', ([[2, '1'], [-1, '1.00252'], [-1, '1.00251']], '1'),
   [0, None, '0.01', '0.00']),
  ('partial repair guard 1', ([[1, '1'], [-1, '1.00502']], '1'), [0, None, '0.01', '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: flip long to short', ([[100, '1.10'], [-150, '1.00']], '1.05'), [-50, '1.000000', '-10.00', '-2.50']),
  ('control: close short', ([[-100, '1.10'], [100, '1.00']], '1.05'), [0, None, '10.00', '0.00']),
  ('control: zero trade only', ([[0, '1.10']], '1.05'), [0, None, '0.00', '0.00'])],
 [('regression realized-accumulation-rounding 1',
   ([[-160, '1.1445'], [370, '1.0518'], [470, '1.1207'], [-420, '1.1186'], [0, '1.1436'], [-430, '1.0692']], '1.1002'),
   [-170, '1.069200', '15.03', '-5.27']),
  ('regression realized-accumulation-rounding 2',
   ([[-440, '1.1376'], [320, '1.1062'], [-500, '1.1066'], [0, '1.0745'], [-450, '1.0551'], [560, '1.0788']], '1.1271'),
   [-510, '1.088418', '15.43', '-19.73']),
  ('partial repair guard 1', ([[1, '1'], [-1, '1.00502']], '1'), [0, None, '0.01', '0.00']),
  ('partial repair guard 2', ([[2, '1'], [-1, '1.00252'], [-1, '1.00251']], '1'), [0, None, '0.01', '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: three reductions', ([[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'])],
 [('regression realized-accumulation-rounding 1', ([[500, '1.1350'], [-210, '1.0625'], [-530, '1.1133']], '1.1442'),
   [-240, '1.113300', '-21.52', '-7.42']),
  ('regression realized-accumulation-rounding 2',
   ([[420, '1.0549'], [-340, '1.1155'], [-600, '1.1019'], [120, '1.0728'], [120, '1.0952'], [300, '1.0728']], '1.1408'),
   [20, '1.072800', '36.81', '1.36']),
  ('partial repair guard 1', ([[1, '1'], [-1, '1.00502']], '1'), [0, None, '0.01', '0.00']),
  ('partial repair guard 2', ([[2, '1'], [-1, '1.00252'], [-1, '1.00251']], '1'), [0, None, '0.01', '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 realized-accumulation-rounding 1',
   ([[-330, '1.0644'], [460, '1.1022'], [130, '1.1361'], [0, '1.0789'], [-160, '1.0850']], '1.0640'),
   [100, '1.119150', '-17.94', '-5.52']),
  ('regression realized-accumulation-rounding 2', ([[10, '1.1411'], [-370, '1.0726'], [50, '1.0616']], '1.1081'),
   [-310, '1.072600', '-0.14', '-11.00']),
  ('partial repair guard 1', ([[1, '1'], [-1, '1.00502']], '1'), [0, None, '0.01', '0.00']),
  ('partial repair guard 2', ([[2, '1'], [-1, '1.00252'], [-1, '1.00251']], '1'), [0, None, '0.01', '0.00']),
  ('control: flip long to short', ([[100, '1.10'], [-150, '1.00']], '1.05'), [-50, '1.000000', '-10.00', '-2.50']),
  ('control: close short', ([[-100, '1.10'], [100, '1.00']], '1.05'), [0, None, '10.00', '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'])],
 [('regression realized-accumulation-rounding 1',
   ([[120, '1.1265'], [-100, '1.1177'], [270, '1.1372'], [-530, '1.1150'], [510, '1.0931'], [-490, '1.0584']],
    '1.1400'),
   [-220, '1.058400', '-11.22', '-17.95']),
  ('regression realized-accumulation-rounding 2',
   ([[460, '1.0523'], [340, '1.1064'], [-460, '1.1068'], [0, '1.1268'], [-560, '1.0903'], [190, '1.0925']], '1.0886'),
   [-30, '1.090300', '19.18', '0.05']),
  ('partial repair guard 1', ([[1, '1'], [-1, '1.00502']], '1'), [0, None, '0.01', '0.00']),
  ('partial repair guard 2', ([[2, '1'], [-1, '1.00252'], [-1, '1.00251']], '1'), [0, None, '0.01', '0.00']),
  ('control: short add', ([[-30, '1.0833'], [-70, '1.0851']], '1.0800'), [-100, '1.084560', '0.00', '0.46']),
  ('control: three reductions', ([[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'])]]
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 realized-accumulation-rounding 1[0, None, '0.02', '0.00'][0, None, '0.02', '0.00']Passed
regression realized-accumulation-rounding 2[0, None, '0.00', '0.00'][0, None, '0.01', '0.00']Failed
partial repair guard 1[0, None, '0.00', '0.00'][0, None, '0.01', '0.00']Failed
control: add then reduce[150, '1.150000', '7.50', '15.00'][150, '1.150000', '7.50', '15.00']Passed
control: flip long to short[-50, '1.000000', '-10.00', '-2.50'][-50, '1.000000', '-10.00', '-2.50']Passed
control: close short[0, None, '10.00', '0.00'][0, None, '10.00', '0.00']Passed
control: zero trade only[0, None, '0.00', '0.00'][0, None, '0.00', '0.00']Passed

SHA-256 / 106b4b6f4a5a547a4653133f4e8ab9efc9d3cd665c65e9ee61b51bd72b8422bd

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 realized-accumulation-rounding 1', ([[3, '1'], [-1, '1.005'], [-1, '1.005'], [-1, '1.005']], '1'),
   [0, None, '0.02', '0.00']),
  ('regression realized-accumulation-rounding 2', ([[2, '1'], [-1, '1.00252'], [-1, '1.00251']], '1'),
   [0, None, '0.01', '0.00']),
  ('partial repair guard 1', ([[1, '1'], [-1, '1.00502']], '1'), [0, None, '0.01', '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: flip long to short', ([[100, '1.10'], [-150, '1.00']], '1.05'), [-50, '1.000000', '-10.00', '-2.50']),
  ('control: close short', ([[-100, '1.10'], [100, '1.00']], '1.05'), [0, None, '10.00', '0.00']),
  ('control: zero trade only', ([[0, '1.10']], '1.05'), [0, None, '0.00', '0.00'])],
 [('regression realized-accumulation-rounding 1',
   ([[-160, '1.1445'], [370, '1.0518'], [470, '1.1207'], [-420, '1.1186'], [0, '1.1436'], [-430, '1.0692']], '1.1002'),
   [-170, '1.069200', '15.03', '-5.27']),
  ('regression realized-accumulation-rounding 2',
   ([[-440, '1.1376'], [320, '1.1062'], [-500, '1.1066'], [0, '1.0745'], [-450, '1.0551'], [560, '1.0788']], '1.1271'),
   [-510, '1.088418', '15.43', '-19.73']),
  ('partial repair guard 1', ([[1, '1'], [-1, '1.00502']], '1'), [0, None, '0.01', '0.00']),
  ('partial repair guard 2', ([[2, '1'], [-1, '1.00252'], [-1, '1.00251']], '1'), [0, None, '0.01', '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: three reductions', ([[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'])],
 [('regression realized-accumulation-rounding 1', ([[500, '1.1350'], [-210, '1.0625'], [-530, '1.1133']], '1.1442'),
   [-240, '1.113300', '-21.52', '-7.42']),
  ('regression realized-accumulation-rounding 2',
   ([[420, '1.0549'], [-340, '1.1155'], [-600, '1.1019'], [120, '1.0728'], [120, '1.0952'], [300, '1.0728']], '1.1408'),
   [20, '1.072800', '36.81', '1.36']),
  ('partial repair guard 1', ([[1, '1'], [-1, '1.00502']], '1'), [0, None, '0.01', '0.00']),
  ('partial repair guard 2', ([[2, '1'], [-1, '1.00252'], [-1, '1.00251']], '1'), [0, None, '0.01', '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 realized-accumulation-rounding 1',
   ([[-330, '1.0644'], [460, '1.1022'], [130, '1.1361'], [0, '1.0789'], [-160, '1.0850']], '1.0640'),
   [100, '1.119150', '-17.94', '-5.52']),
  ('regression realized-accumulation-rounding 2', ([[10, '1.1411'], [-370, '1.0726'], [50, '1.0616']], '1.1081'),
   [-310, '1.072600', '-0.14', '-11.00']),
  ('partial repair guard 1', ([[1, '1'], [-1, '1.00502']], '1'), [0, None, '0.01', '0.00']),
  ('partial repair guard 2', ([[2, '1'], [-1, '1.00252'], [-1, '1.00251']], '1'), [0, None, '0.01', '0.00']),
  ('control: flip long to short', ([[100, '1.10'], [-150, '1.00']], '1.05'), [-50, '1.000000', '-10.00', '-2.50']),
  ('control: close short', ([[-100, '1.10'], [100, '1.00']], '1.05'), [0, None, '10.00', '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'])],
 [('regression realized-accumulation-rounding 1',
   ([[120, '1.1265'], [-100, '1.1177'], [270, '1.1372'], [-530, '1.1150'], [510, '1.0931'], [-490, '1.0584']],
    '1.1400'),
   [-220, '1.058400', '-11.22', '-17.95']),
  ('regression realized-accumulation-rounding 2',
   ([[460, '1.0523'], [340, '1.1064'], [-460, '1.1068'], [0, '1.1268'], [-560, '1.0903'], [190, '1.0925']], '1.0886'),
   [-30, '1.090300', '19.18', '0.05']),
  ('partial repair guard 1', ([[1, '1'], [-1, '1.00502']], '1'), [0, None, '0.01', '0.00']),
  ('partial repair guard 2', ([[2, '1'], [-1, '1.00252'], [-1, '1.00251']], '1'), [0, None, '0.01', '0.00']),
  ('control: short add', ([[-30, '1.0833'], [-70, '1.0851']], '1.0800'), [-100, '1.084560', '0.00', '0.46']),
  ('control: three reductions', ([[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'])]]
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 realized-accumulation-rounding 1[0, None, '0.02', '0.00'][0, None, '0.02', '0.00']Passed
regression realized-accumulation-rounding 2[0, None, '0.01', '0.00'][0, None, '0.01', '0.00']Passed
partial repair guard 1[0, None, '0.01', '0.00'][0, None, '0.01', '0.00']Passed
control: add then reduce[150, '1.150000', '7.50', '15.00'][150, '1.150000', '7.50', '15.00']Passed
control: flip long to short[-50, '1.000000', '-10.00', '-2.50'][-50, '1.000000', '-10.00', '-2.50']Passed
control: close short[0, None, '10.00', '0.00'][0, None, '10.00', '0.00']Passed
control: zero trade only[0, None, '0.00', '0.00'][0, None, '0.00', '0.00']Passed

SHA-256 / c86f188a9d4c47aa91a5a3fccc86736ce29d798fc00672a4ee54568cb412e212

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:01.026588+00:00.

Case digest / 48b9d8ba21dca9bed01491ea8d3ee16ab0b15ab7f8b664b92c479e67765a2f1c