fix开仓价卖回价与真盈亏一次冻结,复盘同步不再反复改写。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-10-13 10:56:53 +08:00
parent 1aa279f191
commit aa8b16ce27
5 changed files with 106 additions and 64 deletions
+52 -4
View File
@@ -6,6 +6,54 @@ import unittest
class ReviewLocalSyncFreezeTests(unittest.TestCase):
def test_settled_true_pnl_not_rewritten_on_resync(self):
"""已冻结真盈亏只读;同步不得因桥/指数再改写."""
from lib.options.options_db import init_options_tables
from lib.options.options_review_lib import sync_options_from_local_trades
from lib.sim.mode_lib import MODE_SIM, TRADING_MODE_KEY
from lib.instance.runtime_settings_lib import ensure_runtime_settings_table, runtime_set
conn = sqlite3.connect(":memory:")
conn.row_factory = sqlite3.Row
init_options_tables(conn)
ensure_runtime_settings_table(conn)
runtime_set(conn, TRADING_MODE_KEY, MODE_SIM)
conn.execute(
"""
INSERT INTO options_trades(
inst_id, underlying, opt_type, sheets, eth_amount,
premium_paid, premium_received, realized_pnl, premium_ccy,
open_fee, close_fee, status, realized_pnl_usdt,
open_index_px, close_index_px, pnl_settled,
created_at, closed_at
) VALUES (
'BTC-USD-261008-86500-P', 'BTC', 'P', 1, 0.01,
0.01, 0.02, 0.01, 'BTC',
0, 0, 'closed', 700.0,
90000.0, 80000.0, 1,
'2026-10-05 05:11:30', '2026-10-07 10:03:53'
)
"""
)
conn.commit()
sync_options_from_local_trades(conn, None)
sync_options_from_local_trades(conn, None)
trade = conn.execute(
"SELECT realized_pnl_usdt, open_index_px, close_index_px, pnl_settled, realized_pnl, premium_paid FROM options_trades WHERE id=1"
).fetchone()
self.assertAlmostEqual(float(trade["realized_pnl_usdt"]), 700.0, places=2)
self.assertAlmostEqual(float(trade["open_index_px"]), 90000.0, places=2)
self.assertAlmostEqual(float(trade["close_index_px"]), 80000.0, places=2)
self.assertEqual(int(trade["pnl_settled"]), 1)
self.assertAlmostEqual(float(trade["realized_pnl"]), 0.01, places=8)
self.assertAlmostEqual(float(trade["premium_paid"]), 0.01, places=8)
row = conn.execute(
"SELECT realized_pnl_total FROM options_review_trades WHERE history_key=?",
("local_opt:1",),
).fetchone()
self.assertAlmostEqual(float(row["realized_pnl_total"]), 700.0, places=2)
conn.close()
def test_premium_usdt_move_formula(self):
from lib.options.options_db import init_options_tables
from lib.options.options_review_lib import sync_options_from_local_trades
@@ -32,9 +80,9 @@ class ReviewLocalSyncFreezeTests(unittest.TestCase):
) VALUES (
'BTC-USD-261008-86500-P', 'BTC', 'P', 1, 0.01,
0.01, 0.02, 0.01, 'BTC',
0, 0, 'closed', -51.0,
0, 0, 'closed', NULL,
90000.0, 80000.0,
1000.0, 2506.0, 1,
1000.0, 2506.0, 0,
'2026-10-05 05:11:30', '2026-10-07 10:03:53'
)
"""
@@ -65,7 +113,7 @@ class ReviewLocalSyncFreezeTests(unittest.TestCase):
init_options_tables(conn)
ensure_runtime_settings_table(conn)
runtime_set(conn, TRADING_MODE_KEY, MODE_SIM)
# 无开仓/卖回价 → 无法按新公式更新
# 未冻结且无开仓/卖回价 → 缺价未核(不瞎写)
conn.execute(
"""
INSERT INTO options_trades(
@@ -76,7 +124,7 @@ class ReviewLocalSyncFreezeTests(unittest.TestCase):
) VALUES (
'BTC-USD-261008-86500-P', 'BTC', 'P', 1, 0.01,
0.01, 0.02, 0.01, 'BTC',
'closed', -51.0, 1,
'closed', NULL, 0,
'2026-10-05 05:11:30', '2026-10-07 10:03:53'
)
"""