fix模拟盘盈亏:按权利金净额×平仓指数,不再把买币预算差额当盈利。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-10-08 23:49:02 +08:00
parent a16dd4f9cc
commit a0ee1fa8a0
5 changed files with 115 additions and 95 deletions
+19 -35
View File
@@ -73,8 +73,9 @@ class SettleClosedPnlTests(unittest.TestCase):
self.assertAlmostEqual(float(s["realized_pnl_usdt"]), 27.0, places=4)
class SimCashPnlStatsTests(unittest.TestCase):
def test_sim_cash_from_bridge_overrides_index_pnl(self):
class SimPremiumPnlStatsTests(unittest.TestCase):
def test_sim_uses_premium_net_not_bridge_cash(self):
"""卖回−预算会把权利金本金算进盈利;必须用(收回−已付)×平仓指数."""
import sqlite3
from lib.options.options_db import init_options_tables
@@ -85,6 +86,7 @@ class SimCashPnlStatsTests(unittest.TestCase):
conn.row_factory = sqlite3.Row
init_options_tables(conn)
ensure_bridge_table(conn)
# 桥上现金差额 952.96,但权利金净盈亏仅 0.01 ETH × 2500 = 25U
conn.execute(
"""
INSERT INTO options_spot_bridge(
@@ -97,12 +99,12 @@ class SimCashPnlStatsTests(unittest.TestCase):
INSERT INTO options_trades(
inst_id, underlying, opt_type, sheets, eth_amount,
premium_paid, premium_received, realized_pnl, premium_ccy,
status, bridge_id, budget_usdt, realized_pnl_usdt,
status, bridge_id, budget_usdt, realized_pnl_usdt, close_index_px,
created_at, closed_at
) VALUES (
'ETH-USD-261009-2500-P', 'ETH', 'P', 1, 0.01,
0.01, 0.02, 0.01, 'ETH',
'closed', 1, 1000.0, 884.54,
'closed', 1, 1000.0, 1947.05, 2500.0,
'2026-10-06 10:00:00', '2026-10-08 14:00:00'
)
"""
@@ -111,62 +113,44 @@ class SimCashPnlStatsTests(unittest.TestCase):
n = sync_sim_cash_realized_pnl_usdt(conn, None)
self.assertEqual(n, 1)
row = conn.execute(
"SELECT realized_pnl_usdt FROM options_trades WHERE id=1"
"SELECT realized_pnl_usdt, realized_pnl FROM options_trades WHERE id=1"
).fetchone()
self.assertAlmostEqual(float(row["realized_pnl_usdt"]), 952.96, places=2)
# (0.02-0.01)*2500 = 25, 绝不是 1952.96-1000
self.assertAlmostEqual(float(row["realized_pnl_usdt"]), 25.0, places=2)
self.assertAlmostEqual(float(row["realized_pnl"]), 0.01, places=8)
conn.close()
def test_sim_cash_from_ledger_when_bridge_missing_recovered(self):
def test_sim_deducts_fees_from_premium_pnl(self):
import sqlite3
from lib.options.options_db import init_options_tables
from lib.options.options_spot_bridge_lib import ensure_bridge_table
from lib.options.options_stats_lib import sync_sim_cash_realized_pnl_usdt
from lib.sim.db_lib import init_sim_tables
conn = sqlite3.connect(":memory:")
conn.row_factory = sqlite3.Row
init_options_tables(conn)
ensure_bridge_table(conn)
init_sim_tables(conn)
conn.execute(
"""
INSERT INTO options_spot_bridge(
id, underlying, status, budget_usdt, usdt_recovered
) VALUES (1, 'ETH', 'closed', 1000.0, NULL)
"""
)
conn.execute(
"""
INSERT INTO options_trades(
inst_id, underlying, opt_type, sheets, eth_amount,
premium_paid, premium_received, realized_pnl, premium_ccy,
status, bridge_id, budget_usdt, realized_pnl_usdt,
open_fee, close_fee, status, realized_pnl_usdt, close_index_px,
created_at, closed_at
) VALUES (
'ETH-USD-261009-2500-P', 'ETH', 'P', 1, 0.01,
0.01, 0.02, 0.01, 'ETH',
'closed', 1, 1000.0, 884.54,
'2026-10-06 10:00:00', '2026-10-08 14:00:00'
'BTC-USD-261008-86500-P', 'BTC', 'P', 1, 0.01,
0.01, 0.02, 0.01, 'BTC',
0.0001, 0.0001, 'closed', 1947.05, 80000.0,
'2026-10-05 05:11:30', '2026-10-07 10:03:53'
)
"""
)
# 买币 -1000U, 卖回 +1952.96U
conn.execute(
"""
INSERT INTO sim_ledger_entries(kind, amount, ccy, account, note, ts)
VALUES
('convert', -1000.0, 'USDT', 'trading', 'ETH/USDT mkt 2500', '2026-10-06 10:00:05'),
('convert', 1952.96, 'USDT', 'trading', 'from eth', '2026-10-08 14:00:10')
"""
)
conn.commit()
n = sync_sim_cash_realized_pnl_usdt(conn, None)
self.assertEqual(n, 1)
sync_sim_cash_realized_pnl_usdt(conn, None)
row = conn.execute(
"SELECT realized_pnl_usdt FROM options_trades WHERE id=1"
).fetchone()
self.assertAlmostEqual(float(row["realized_pnl_usdt"]), 952.96, places=2)
# (0.02-0.01-0.0002)*80000 = 784
self.assertAlmostEqual(float(row["realized_pnl_usdt"]), 784.0, places=2)
conn.close()
+7 -6
View File
@@ -26,8 +26,8 @@ class ReviewLocalSyncFreezeTests(unittest.TestCase):
created_at, closed_at
) VALUES (
'BTC-USD-261008-86500-P', 'BTC', 'P', 1, 0.01,
0.01, 0.02, 0.01172, 'BTC',
'closed', 1947.05, 85000.0,
0.01, 0.02, 0.01, 'BTC',
'closed', 850.0, 85000.0,
'2026-10-05 05:11:30', '2026-10-07 10:03:53'
)
"""
@@ -45,8 +45,8 @@ class ReviewLocalSyncFreezeTests(unittest.TestCase):
("local_opt:1",),
).fetchone()
self.assertIsNotNone(row)
# 必须是冻结现金 1947.05,不是 0.01172*999999
self.assertAlmostEqual(float(row["realized_pnl_total"]), 1947.05, places=2)
# (0.02-0.01)*85000=850,不是 0.01*999999
self.assertAlmostEqual(float(row["realized_pnl_total"]), 850.0, places=2)
conn.close()
def test_second_sync_stable_when_index_moves(self):
@@ -70,7 +70,7 @@ class ReviewLocalSyncFreezeTests(unittest.TestCase):
) VALUES (
'BTC-USD-261008-86500-P', 'BTC', 'P', 1, 0.01,
0.01, 0.02, 0.01, 'BTC',
'closed', 952.96, 80000.0,
'closed', 800.0, 80000.0,
'2026-10-05 05:11:30', '2026-10-07 10:03:53'
)
"""
@@ -99,7 +99,8 @@ class ReviewLocalSyncFreezeTests(unittest.TestCase):
("local_opt:1",),
).fetchone()["realized_pnl_total"]
)
self.assertAlmostEqual(v1, 952.96, places=2)
# (0.02-0.01)*80000=800; 实时指数变也不改
self.assertAlmostEqual(v1, 800.0, places=2)
self.assertAlmostEqual(v2, v1, places=6)
conn.close()