fix真盈亏只认卖光收回减买币,禁止权利金指数冒充。
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
"""复盘导入须使用冻结 U,不得用实时指数改写."""
|
||||
"""复盘真盈亏=卖光收回−买币;禁止权利金×指数冒充."""
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlite3
|
||||
@@ -6,7 +6,103 @@ import unittest
|
||||
|
||||
|
||||
class ReviewLocalSyncFreezeTests(unittest.TestCase):
|
||||
def test_prefers_realized_pnl_usdt_over_live_index(self):
|
||||
def test_account_after_minus_before_is_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,
|
||||
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,
|
||||
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())
|
||||
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)
|
||||
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)
|
||||
conn.close()
|
||||
|
||||
def test_bridge_cash_is_true_pnl_when_no_after(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,
|
||||
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,
|
||||
'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 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
|
||||
@@ -33,75 +129,13 @@ class ReviewLocalSyncFreezeTests(unittest.TestCase):
|
||||
"""
|
||||
)
|
||||
conn.commit()
|
||||
|
||||
class _Ex:
|
||||
def public_get_market_ticker(self, params):
|
||||
return {"data": [{"last": "999999"}]}
|
||||
|
||||
out = sync_options_from_local_trades(conn, _Ex())
|
||||
self.assertTrue(out.get("ok"))
|
||||
sync_options_from_local_trades(conn, None)
|
||||
row = conn.execute(
|
||||
"SELECT realized_pnl_total FROM options_review_trades WHERE history_key=?",
|
||||
"SELECT realized_pnl_total, status_raw FROM options_review_trades WHERE history_key=?",
|
||||
("local_opt:1",),
|
||||
).fetchone()
|
||||
self.assertIsNotNone(row)
|
||||
# (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):
|
||||
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', 800.0, 80000.0,
|
||||
'2026-10-05 05:11:30', '2026-10-07 10:03:53'
|
||||
)
|
||||
"""
|
||||
)
|
||||
conn.commit()
|
||||
|
||||
class _Ex1:
|
||||
def public_get_market_ticker(self, params):
|
||||
return {"data": [{"last": "70000"}]}
|
||||
|
||||
class _Ex2:
|
||||
def public_get_market_ticker(self, params):
|
||||
return {"data": [{"last": "120000"}]}
|
||||
|
||||
sync_options_from_local_trades(conn, _Ex1())
|
||||
v1 = float(
|
||||
conn.execute(
|
||||
"SELECT realized_pnl_total FROM options_review_trades WHERE history_key=?",
|
||||
("local_opt:1",),
|
||||
).fetchone()["realized_pnl_total"]
|
||||
)
|
||||
sync_options_from_local_trades(conn, _Ex2())
|
||||
v2 = float(
|
||||
conn.execute(
|
||||
"SELECT realized_pnl_total FROM options_review_trades WHERE history_key=?",
|
||||
("local_opt:1",),
|
||||
).fetchone()["realized_pnl_total"]
|
||||
)
|
||||
# (0.02-0.01)*80000=800; 实时指数变也不改
|
||||
self.assertAlmostEqual(v1, 800.0, places=2)
|
||||
self.assertAlmostEqual(v2, v1, places=6)
|
||||
# 无账户/桥快照:不得用指数折 U 冒充真盈亏
|
||||
self.assertIsNone(row["realized_pnl_total"])
|
||||
conn.close()
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user