Fix hedge option PnL match by parsing opened_at as Asia/Shanghai.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-07-21 10:21:52 +08:00
parent 67a09b1de8
commit 60ff45f098
3 changed files with 40 additions and 5 deletions
+25
View File
@@ -6,6 +6,7 @@ import unittest
from lib.hedge_plan.hedge_plan_db import get_plan, init_hedge_plan_tables, insert_leg, insert_plan
from lib.hedge_plan.hedge_plan_settle_lib import (
_parse_opened_ms,
backfill_hedge_option_legs_realized_pnl,
resolve_option_leg_realized_pnl,
)
@@ -32,6 +33,30 @@ class HedgeExchangePnlTest(unittest.TestCase):
self.assertEqual(src, "exchange")
self.assertEqual(pnl, 12.0)
def test_opened_at_beijing_wall_clock_not_utc(self):
# 北京 09:19 开仓 → UTC 01:19; 到期结算北京 16:00:31 = UTC 08:00:31
open_ms = _parse_opened_ms("2026-07-20 09:19:30")
self.assertEqual(open_ms, 1784510370000)
leg = {
"inst_id": "ETH-USD_UM-260720-1870-C",
"opened_at": "2026-07-20 09:19:30",
"premium": 6.9,
}
hist = [
{
"instId": "ETH-USD_UM-260720-1870-C",
"uTime": "1784534431512",
"realizedPnl": "-7.1812815",
"pnl": "-6.9",
"type": "2",
}
]
pnl, src = resolve_option_leg_realized_pnl(
leg=leg, hist_rows=hist, fallback=-6.9
)
self.assertEqual(src, "exchange")
self.assertAlmostEqual(pnl, -7.1813, places=4)
def test_backfill_updates_plan_total(self):
conn = sqlite3.connect(":memory:")
conn.row_factory = sqlite3.Row