c2c69e580c
Co-authored-by: Cursor <cursoragent@cursor.com>
144 lines
5.8 KiB
Python
144 lines
5.8 KiB
Python
"""复盘真盈亏=卖光收回−买币;禁止权利金×指数冒充."""
|
||
from __future__ import annotations
|
||
|
||
import sqlite3
|
||
import unittest
|
||
|
||
|
||
class ReviewLocalSyncFreezeTests(unittest.TestCase):
|
||
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
|
||
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"])
|
||
conn.close()
|
||
|
||
|
||
if __name__ == "__main__":
|
||
unittest.main()
|