fix复盘真盈亏按权利金USDT波动公式计算,忽略现货多买。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-10-13 10:16:44 +08:00
parent c2c69e580c
commit 3416996ae5
10 changed files with 249 additions and 280 deletions
+18 -66
View File
@@ -1,4 +1,4 @@
"""复盘真盈亏=卖光收回−买币;禁止权利金×指数冒充."""
"""复盘真盈亏=币盈亏×卖回价 − 权利金×(开仓价−卖回价)."""
from __future__ import annotations
import sqlite3
@@ -6,7 +6,7 @@ import unittest
class ReviewLocalSyncFreezeTests(unittest.TestCase):
def test_account_after_minus_before_is_true_pnl(self):
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
from lib.sim.mode_lib import MODE_SIM, TRADING_MODE_KEY
@@ -17,77 +17,65 @@ class ReviewLocalSyncFreezeTests(unittest.TestCase):
init_options_tables(conn)
ensure_runtime_settings_table(conn)
runtime_set(conn, TRADING_MODE_KEY, MODE_SIM)
# paid=0.01, recv=0.02, coin_pnl=0.01; open=90000, close=80000
# A = 0.01*80000 - (0.01*90000 - 0.01*80000) = 800 - 100 = 700
# 亦= recv*close - paid*open = 1600 - 900 = 700
conn.execute(
"""
INSERT INTO options_trades(
inst_id, underlying, opt_type, sheets, eth_amount,
premium_paid, premium_received, realized_pnl, premium_ccy,
status, realized_pnl_usdt, close_index_px,
open_fee, close_fee, status, realized_pnl_usdt,
open_index_px, close_index_px,
trading_usdt_before, trading_usdt_after, pnl_settled,
created_at, closed_at
) VALUES (
'BTC-USD-261008-86500-P', 'BTC', 'P', 1, 0.01,
0.01, 0.02, 0.01, 'BTC',
'closed', -51.0, 85000.0,
0, 0, 'closed', -51.0,
90000.0, 80000.0,
1000.0, 2506.0, 1,
'2026-10-05 05:11:30', '2026-10-07 10:03:53'
)
"""
)
conn.commit()
class _Ex:
def public_get_market_ticker(self, params):
return {"data": [{"last": "999999"}]}
out = sync_options_from_local_trades(conn, _Ex())
out = sync_options_from_local_trades(conn, None)
self.assertTrue(out.get("ok"))
row = conn.execute(
"SELECT realized_pnl_total FROM options_review_trades WHERE history_key=?",
("local_opt:1",),
).fetchone()
self.assertIsNotNone(row)
# 真盈亏=after−before=1506,不是错误冻结 -51,也不是指数折算
self.assertAlmostEqual(float(row["realized_pnl_total"]), 1506.0, places=2)
self.assertAlmostEqual(float(row["realized_pnl_total"]), 700.0, places=2)
# 纠偏写回,不用账户差分 1506
healed = conn.execute(
"SELECT realized_pnl_usdt FROM options_trades WHERE id=1"
).fetchone()
self.assertAlmostEqual(float(healed["realized_pnl_usdt"]), 1506.0, places=2)
self.assertAlmostEqual(float(healed["realized_pnl_usdt"]), 700.0, places=2)
conn.close()
def test_bridge_cash_is_true_pnl_when_no_after(self):
def test_same_open_close_px_equals_coin_times_close(self):
from lib.options.options_db import init_options_tables
from lib.options.options_review_lib import sync_options_from_local_trades
from lib.options.options_spot_bridge_lib import ensure_bridge_table
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_bridge_table(conn)
ensure_runtime_settings_table(conn)
runtime_set(conn, TRADING_MODE_KEY, MODE_SIM)
conn.execute(
"""
INSERT INTO options_spot_bridge(
id, underlying, status, budget_usdt, usdt_recovered, message
) VALUES (7, 'BTC', 'closed', 2200.0, 3706.0, 'done')
"""
)
conn.execute(
"""
INSERT INTO options_trades(
inst_id, underlying, opt_type, sheets, eth_amount,
premium_paid, premium_received, realized_pnl, premium_ccy,
status, realized_pnl_usdt, close_index_px,
trading_usdt_before, bridge_id, pnl_settled,
open_fee, close_fee, status, open_index_px, close_index_px,
created_at, closed_at
) VALUES (
'BTC-USD-261008-86500-P', 'BTC', 'P', 1, 0.01,
0.01, 0.02, 0.01, 'BTC',
'closed', 850.0, 85000.0,
1000.0, 7, 0,
0, 0, 'closed', 85000.0, 85000.0,
'2026-10-05 05:11:30', '2026-10-07 10:03:53'
)
"""
@@ -98,44 +86,8 @@ class ReviewLocalSyncFreezeTests(unittest.TestCase):
"SELECT realized_pnl_total FROM options_review_trades WHERE history_key=?",
("local_opt:1",),
).fetchone()
# 3706 − 2200 = 1506;不是 0.01*85000
self.assertAlmostEqual(float(row["realized_pnl_total"]), 1506.0, places=2)
conn.close()
def test_index_formula_not_shown_as_true_pnl(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,
status, realized_pnl_usdt, close_index_px,
created_at, closed_at
) VALUES (
'BTC-USD-261008-86500-P', 'BTC', 'P', 1, 0.01,
0.01, 0.02, 0.01, 'BTC',
'closed', 850.0, 85000.0,
'2026-10-05 05:11:30', '2026-10-07 10:03:53'
)
"""
)
conn.commit()
sync_options_from_local_trades(conn, None)
row = conn.execute(
"SELECT realized_pnl_total, status_raw FROM options_review_trades WHERE history_key=?",
("local_opt:1",),
).fetchone()
# 无账户/桥快照:不得用指数折 U 冒充真盈亏
self.assertIsNone(row["realized_pnl_total"])
# 开平价相同:波动项为0 → 等于币×卖回价=850
self.assertAlmostEqual(float(row["realized_pnl_total"]), 850.0, places=2)
conn.close()