FA-62041 / Currency rounding and FX conversion / Open access
Realized P&L on a short position has the wrong sign · case 01
Buying back a short below its average books a loss.
ROOT CAUSE
The realized amount omits the position sign: closed * (rate - avg).
THE FAILURE
The realized amount omits the position sign: closed * (rate - avg).
Unsuccessful approach: Using the sign of the closing trade instead of the position inverts every realization.
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)
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 short-realized-sign 1', ([[-100, '1.10'], [100, '1.00']], '1.05'), [0, None, '10.00', '0.00']),
('regression short-realized-sign 2', ([[-40, '1.2'], [100, '1.1']], '1.15'), [60, '1.100000', '4.00', '3.00']),
('partial repair guard 2', ([[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: 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'])],
[('regression short-realized-sign 1', ([[-90, '1.0708'], [540, '1.1315']], '1.0885'),
[450, '1.131500', '-5.46', '-19.35']),
('regression short-realized-sign 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'], [-150, '1.00']], '1.05'), [-50, '1.000000', '-10.00', '-2.50']),
('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 short-realized-sign 1',
([[-540, '1.1190'], [-60, '1.0677'], [20, '1.1443'], [-240, '1.1212'], [0, '1.0908']], '1.0791'),
[-820, '1.116015', '-0.61', '30.27']),
('regression short-realized-sign 2',
([[0, '1.0601'], [0, '1.0705'], [-110, '1.1230'], [310, '1.0559'], [400, '1.1307']], '1.1024'),
[600, '1.105767', '7.38', '-2.02']),
('partial repair guard 1', ([[50, '1.1'], [-50, '1.2'], [0, '1.3'], [20, '1.4']], '1.5'),
[20, '1.400000', '5.00', '2.00']),
('partial repair guard 2', ([[-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']),
('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 short-realized-sign 1',
([[-450, '1.1009'], [0, '1.1001'], [230, '1.0743'], [0, '1.0884'], [-190, '1.0956'], [30, '1.0635']], '1.0627'),
[-380, '1.098444', '7.17', '13.58']),
('regression short-realized-sign 2',
([[-40, '1.1445'], [-570, '1.0711'], [530, '1.1387'], [-450, '1.0688'], [-300, '1.1071']], '1.1211'),
[-830, '1.083329', '-33.28', '-31.35']),
('partial repair guard 1',
([[340, '1.0922'], [380, '1.0595'], [0, '1.1288'], [220, '1.0703'], [220, '1.0901'], [-390, '1.1478']], '1.0752'),
[770, '1.076936', '27.64', '-1.34']),
('partial repair guard 2', ([[270, '1.1098'], [-40, '1.1476']], '1.0552'), [230, '1.109800', '1.51', '-12.56']),
('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 short-realized-sign 1',
([[-460, '1.1380'], [-260, '1.0732'], [-130, '1.1234'], [-300, '1.0815'], [210, '1.0636']], '1.0880'),
[-940, '1.106960', '9.11', '17.82']),
('regression short-realized-sign 2',
([[0, '1.0983'], [-530, '1.1416'], [-170, '1.1049'], [280, '1.1333'], [-140, '1.0910'], [0, '1.0954']], '1.0893'),
[-560, '1.122265', '-0.17', '18.46']),
('partial repair guard 1', ([[-90, '1.0708'], [540, '1.1315']], '1.0885'), [450, '1.131500', '-5.46', '-19.35']),
('partial repair guard 2', ([[-220, '1.1085'], [310, '1.1162'], [-140, '1.1499']], '1.1267'),
[-50, '1.149900', '1.34', '1.16']),
('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 fixture | Actual | Expected | Outcome |
|---|---|---|---|
| regression short-realized-sign 1 | [0, None, '-10.00', '0.00'] | [0, None, '10.00', '0.00'] | Failed |
| regression short-realized-sign 2 | [60, '1.100000', '-4.00', '3.00'] | [60, '1.100000', '4.00', '3.00'] | Failed |
| partial repair guard 2 | [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: 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: three reductions | [0, None, '0.64', '0.00'] | [0, None, '0.64', '0.00'] | Passed |
SHA-256 / 056dc6c4e5c76802ae39ecfce8b4b1ecd65f18ed6fd255c0bb29651995de8e63
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) * (1 if qty > 0 else -1)
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 short-realized-sign 1', ([[-100, '1.10'], [100, '1.00']], '1.05'), [0, None, '10.00', '0.00']),
('regression short-realized-sign 2', ([[-40, '1.2'], [100, '1.1']], '1.15'), [60, '1.100000', '4.00', '3.00']),
('partial repair guard 2', ([[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: 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'])],
[('regression short-realized-sign 1', ([[-90, '1.0708'], [540, '1.1315']], '1.0885'),
[450, '1.131500', '-5.46', '-19.35']),
('regression short-realized-sign 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'], [-150, '1.00']], '1.05'), [-50, '1.000000', '-10.00', '-2.50']),
('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 short-realized-sign 1',
([[-540, '1.1190'], [-60, '1.0677'], [20, '1.1443'], [-240, '1.1212'], [0, '1.0908']], '1.0791'),
[-820, '1.116015', '-0.61', '30.27']),
('regression short-realized-sign 2',
([[0, '1.0601'], [0, '1.0705'], [-110, '1.1230'], [310, '1.0559'], [400, '1.1307']], '1.1024'),
[600, '1.105767', '7.38', '-2.02']),
('partial repair guard 1', ([[50, '1.1'], [-50, '1.2'], [0, '1.3'], [20, '1.4']], '1.5'),
[20, '1.400000', '5.00', '2.00']),
('partial repair guard 2', ([[-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']),
('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 short-realized-sign 1',
([[-450, '1.1009'], [0, '1.1001'], [230, '1.0743'], [0, '1.0884'], [-190, '1.0956'], [30, '1.0635']], '1.0627'),
[-380, '1.098444', '7.17', '13.58']),
('regression short-realized-sign 2',
([[-40, '1.1445'], [-570, '1.0711'], [530, '1.1387'], [-450, '1.0688'], [-300, '1.1071']], '1.1211'),
[-830, '1.083329', '-33.28', '-31.35']),
('partial repair guard 1',
([[340, '1.0922'], [380, '1.0595'], [0, '1.1288'], [220, '1.0703'], [220, '1.0901'], [-390, '1.1478']], '1.0752'),
[770, '1.076936', '27.64', '-1.34']),
('partial repair guard 2', ([[270, '1.1098'], [-40, '1.1476']], '1.0552'), [230, '1.109800', '1.51', '-12.56']),
('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 short-realized-sign 1',
([[-460, '1.1380'], [-260, '1.0732'], [-130, '1.1234'], [-300, '1.0815'], [210, '1.0636']], '1.0880'),
[-940, '1.106960', '9.11', '17.82']),
('regression short-realized-sign 2',
([[0, '1.0983'], [-530, '1.1416'], [-170, '1.1049'], [280, '1.1333'], [-140, '1.0910'], [0, '1.0954']], '1.0893'),
[-560, '1.122265', '-0.17', '18.46']),
('partial repair guard 1', ([[-90, '1.0708'], [540, '1.1315']], '1.0885'), [450, '1.131500', '-5.46', '-19.35']),
('partial repair guard 2', ([[-220, '1.1085'], [310, '1.1162'], [-140, '1.1499']], '1.1267'),
[-50, '1.149900', '1.34', '1.16']),
('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 fixture | Actual | Expected | Outcome |
|---|---|---|---|
| regression short-realized-sign 1 | [0, None, '-10.00', '0.00'] | [0, None, '10.00', '0.00'] | Failed |
| regression short-realized-sign 2 | [60, '1.100000', '-4.00', '3.00'] | [60, '1.100000', '4.00', '3.00'] | Failed |
| partial repair guard 2 | [150, '1.150000', '-7.50', '15.00'] | [150, '1.150000', '7.50', '15.00'] | Failed |
| control: flip long to short | [-50, '1.000000', '10.00', '-2.50'] | [-50, '1.000000', '-10.00', '-2.50'] | Failed |
| 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: three reductions | [0, None, '-0.64', '0.00'] | [0, None, '0.64', '0.00'] | Failed |
SHA-256 / 40b6129fabc37a0077b3fa2f5068289e8825781df4105add439d184ba161ba0f
HELD IN THE MEMBER ARCHIVE
The verified repair and its recorded checks are member-only.
This mechanism has 7 recorded checks per implementation. The open-access tier publishes the failure and the unsuccessful fix; the repaired source that passes every check, and the observations that prove it, are available to members.
Every case sharing this mechanism uses the same contract and the same repair, so this one record is held back for all of them.
Member access is invitation-based. Sign in with your invited account to inspect the repair.
Sign in to the archive ↗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.848447+00:00.
Case digest / 5b2fe92fb5ff344d5fb0c57e1c7d7b07531e26784f3703ad7117c5debc484b5b