Reconstruct trade-only position basis and realized results

This commit is contained in:
2026-12-05 04:55:24 +08:00
parent 8b4e2c4fa3
commit 76f721786c
8 changed files with 246 additions and 5 deletions
+29
View File
@@ -4293,3 +4293,32 @@ fn public_positions_walk_a_mergeable_pair_and_held_settlement() {
let closed = read(&e, json!({"user":"alice","status":"CLOSED"})).unwrap();
assert_eq!(closed["data"].as_array().unwrap().len(), 2);
}
#[test]
fn public_positions_reconstructs_trade_basis_after_a_partial_sale() {
let mut e = Exchange::open(":memory:").unwrap();
setup(&mut e);
for cmd in [
json!({"op":"fund","id":"fund-trader","account":"trader","cents":2000}),
json!({"op":"open","id":"open","market":"e"}),
order("ask", "alice", "sell", 40, 10),
order("entry", "trader", "buy", 40, 10),
order("exit", "trader", "sell", 80, 3),
order("buyer", "bob", "buy", 80, 3),
] {
e.execute(cmd).unwrap();
}
let read = |user| {
e.public_positions(serde_json::from_value(json!({"user":user})).unwrap())
.unwrap()
};
let page = read("trader");
let row = &page["data"][0];
assert_eq!(row["current_size"], 7);
assert_eq!(row["total_size"], 10);
assert_eq!(row["entry_cost_usdc"], 2.8);
assert_eq!(row["realized_pnl"], 1.2);
assert_eq!(row["unrealized_pnl"], 2.8);
assert_eq!(row["total_pnl"], 4);
assert!(read("alice")["data"][0]["entry_cost_usdc"].is_null());
}