Align options review PnL with OKX positions-history realizedPnl.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -173,3 +173,43 @@ def test_reconcile_live_open_trades_reopens_sync_artifact():
|
||||
assert row["realized_pnl"] == -0.5
|
||||
assert row["premium_received"] == 0.74
|
||||
assert row["close_ord_id"] == "pos-1"
|
||||
|
||||
|
||||
def test_backfill_closed_options_realized_pnl_from_history():
|
||||
from lib.options.options_monitor_lib import backfill_closed_options_realized_pnl_from_history
|
||||
|
||||
conn = sqlite3.connect(":memory:")
|
||||
conn.row_factory = sqlite3.Row
|
||||
init_options_tables(conn)
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO options_trades
|
||||
(inst_id, underlying, opt_type, strike, exp_time, sheets, eth_amount,
|
||||
open_quote, close_quote, premium_paid, premium_received, realized_pnl,
|
||||
status, created_at, closed_at, close_ord_id)
|
||||
VALUES (?, 'ETH', 'C', 1860, '', 43, 0.43, 22.4, 50.6, 9.632, 21.758, 12.126,
|
||||
'closed', '2026-07-20 08:02:14', '2026-07-21 01:38:43', 'ord-1')
|
||||
""",
|
||||
("ETH-USD_UM-260721-1860-C",),
|
||||
)
|
||||
conn.commit()
|
||||
hist = [
|
||||
{
|
||||
"instId": "ETH-USD_UM-260721-1860-C",
|
||||
"uTime": "1784564323000",
|
||||
"realizedPnl": "11.64",
|
||||
"closeAvgPx": "48.5",
|
||||
"closeTotalPos": "43",
|
||||
"posId": "pos-x",
|
||||
}
|
||||
]
|
||||
n = backfill_closed_options_realized_pnl_from_history(conn, hist)
|
||||
assert n == 1
|
||||
row = conn.execute(
|
||||
"SELECT realized_pnl, premium_received, close_quote FROM options_trades WHERE id=1"
|
||||
).fetchone()
|
||||
assert row["realized_pnl"] == 11.64
|
||||
assert abs(float(row["premium_received"]) - (9.632 + 11.64)) < 1e-6
|
||||
assert float(row["close_quote"]) == 48.5
|
||||
# idempotent
|
||||
assert backfill_closed_options_realized_pnl_from_history(conn, hist) == 0
|
||||
|
||||
Reference in New Issue
Block a user