fc0d76d174
Co-authored-by: Cursor <cursoragent@cursor.com>
233 lines
10 KiB
Python
233 lines
10 KiB
Python
"""复盘真盈亏=期权历史币盈亏×平仓价."""
|
|
from __future__ import annotations
|
|
|
|
import sqlite3
|
|
import unittest
|
|
|
|
|
|
class ReviewLocalSyncFreezeTests(unittest.TestCase):
|
|
def test_refresh_recomputes_u_keeps_frozen_qty_px(self):
|
|
"""刷新按币盈亏×平仓价重算U;币盈亏/平仓价不变."""
|
|
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', -51.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()
|
|
# 0.01*80000=800;旧错账 -51 被刷新纠正
|
|
self.assertAlmostEqual(float(trade["realized_pnl_usdt"]), 800.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)
|
|
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"]), 800.0, places=2)
|
|
conn.close()
|
|
|
|
def test_refresh_prefers_bridge_sell_vwap_over_wrong_index(self):
|
|
"""有卖回 VWAP 时优先用它折U."""
|
|
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 (
|
|
BRIDGE_CLOSED,
|
|
add_bridge_buy_fill,
|
|
add_bridge_close_sell_fill,
|
|
ensure_bridge_table,
|
|
insert_bridge,
|
|
update_bridge,
|
|
)
|
|
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)
|
|
bid = insert_bridge(conn, underlying="BTC", status="holding", budget_usdt=990.0)
|
|
add_bridge_buy_fill(conn, bid, usdt_spent=840.10, coin_bought=0.01)
|
|
add_bridge_close_sell_fill(conn, bid, usdt_recovered=814.18, coin_sold=0.01)
|
|
update_bridge(conn, bid, status=BRIDGE_CLOSED, close=True)
|
|
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, bridge_id,
|
|
created_at, closed_at
|
|
) VALUES (
|
|
'BTC-USD-261015-84000-P', 'BTC', 'P', 1, 0.01,
|
|
0.01, 0.02, 0.01, 'BTC',
|
|
0, 0, 'closed', 1544.0,
|
|
88086.0, 85400.0, 1, ?,
|
|
'2026-10-12 01:46:50', '2026-10-13 08:07:35'
|
|
)
|
|
""",
|
|
(bid,),
|
|
)
|
|
conn.commit()
|
|
sync_options_from_local_trades(conn, None)
|
|
trade = conn.execute(
|
|
"SELECT realized_pnl_usdt, close_index_px FROM options_trades WHERE id=1"
|
|
).fetchone()
|
|
self.assertAlmostEqual(float(trade["close_index_px"]), 81418.0, places=2)
|
|
# 0.01*81418
|
|
self.assertAlmostEqual(float(trade["realized_pnl_usdt"]), 814.18, 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
|
|
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,
|
|
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',
|
|
0, 0, 'closed', NULL,
|
|
90000.0, 80000.0,
|
|
1000.0, 2506.0, 0,
|
|
'2026-10-05 05:11:30', '2026-10-07 10:03:53'
|
|
)
|
|
"""
|
|
)
|
|
conn.commit()
|
|
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.assertAlmostEqual(float(row["realized_pnl_total"]), 800.0, places=2)
|
|
healed = conn.execute(
|
|
"SELECT realized_pnl_usdt FROM options_trades WHERE id=1"
|
|
).fetchone()
|
|
self.assertAlmostEqual(float(healed["realized_pnl_usdt"]), 800.0, places=2)
|
|
conn.close()
|
|
|
|
def test_uncomputable_old_trade_marked(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, 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', NULL, 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()
|
|
self.assertEqual(str(row["status_raw"]), "缺价未核")
|
|
self.assertIsNone(row["realized_pnl_total"])
|
|
trade = conn.execute(
|
|
"SELECT realized_pnl_usdt, pnl_settled FROM options_trades WHERE id=1"
|
|
).fetchone()
|
|
self.assertIsNone(trade["realized_pnl_usdt"])
|
|
self.assertEqual(int(trade["pnl_settled"] or 0), 0)
|
|
conn.close()
|
|
|
|
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.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, 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',
|
|
0, 0, 'closed', 85000.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 FROM options_review_trades WHERE history_key=?",
|
|
("local_opt:1",),
|
|
).fetchone()
|
|
self.assertAlmostEqual(float(row["realized_pnl_total"]), 850.0, places=2)
|
|
conn.close()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|