feat期权盈亏按交易账户快照结算:一次一仓、持仓禁划转、卖回后冻结。
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
"""交易账户快照盈亏结算."""
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlite3
|
||||
import unittest
|
||||
|
||||
|
||||
class AccountPnlSettleTests(unittest.TestCase):
|
||||
def test_settle_after_minus_before(self):
|
||||
from lib.options.options_account_pnl_lib import (
|
||||
settle_closed_trades_from_trading_account,
|
||||
)
|
||||
from lib.options.options_db import init_options_tables
|
||||
|
||||
conn = sqlite3.connect(":memory:")
|
||||
conn.row_factory = sqlite3.Row
|
||||
init_options_tables(conn)
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO options_trades(
|
||||
inst_id, underlying, opt_type, sheets, eth_amount,
|
||||
premium_paid, status, bridge_id, trading_usdt_before, pnl_settled,
|
||||
created_at, closed_at
|
||||
) VALUES (
|
||||
'BTC-USD-261008-86500-P', 'BTC', 'P', 1, 0.01,
|
||||
0.01, 'closed', 7, 1000.0, 0,
|
||||
'2026-10-05 05:11:30', '2026-10-07 10:03:53'
|
||||
)
|
||||
"""
|
||||
)
|
||||
conn.commit()
|
||||
n = settle_closed_trades_from_trading_account(
|
||||
conn, trading_usdt_after=1947.05, bridge_id=7
|
||||
)
|
||||
self.assertEqual(n, 1)
|
||||
row = conn.execute(
|
||||
"SELECT trading_usdt_after, realized_pnl_usdt, pnl_settled FROM options_trades WHERE id=1"
|
||||
).fetchone()
|
||||
self.assertAlmostEqual(float(row["trading_usdt_after"]), 1947.05, places=2)
|
||||
self.assertAlmostEqual(float(row["realized_pnl_usdt"]), 947.05, places=2)
|
||||
self.assertEqual(int(row["pnl_settled"]), 1)
|
||||
|
||||
# 已冻结不再改
|
||||
n2 = settle_closed_trades_from_trading_account(
|
||||
conn, trading_usdt_after=9999.0, bridge_id=7
|
||||
)
|
||||
self.assertEqual(n2, 0)
|
||||
row2 = conn.execute(
|
||||
"SELECT realized_pnl_usdt FROM options_trades WHERE id=1"
|
||||
).fetchone()
|
||||
self.assertAlmostEqual(float(row2["realized_pnl_usdt"]), 947.05, places=2)
|
||||
conn.close()
|
||||
|
||||
def test_stats_only_count_settled_account_trades(self):
|
||||
from lib.options.options_db import init_options_tables
|
||||
from lib.options.options_stats_lib import (
|
||||
compute_options_stats_from_history,
|
||||
history_pnl_to_usdt,
|
||||
load_local_options_stats_history,
|
||||
)
|
||||
|
||||
conn = sqlite3.connect(":memory:")
|
||||
conn.row_factory = sqlite3.Row
|
||||
init_options_tables(conn)
|
||||
# 待结算:不进统计
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO options_trades(
|
||||
inst_id, underlying, opt_type, sheets, eth_amount,
|
||||
premium_paid, premium_received, realized_pnl, premium_ccy,
|
||||
status, trading_usdt_before, pnl_settled, created_at, closed_at
|
||||
) VALUES (
|
||||
'ETH-USD-261009-2500-P', 'ETH', 'P', 1, 0.01,
|
||||
0.01, 0.02, 0.01, 'ETH',
|
||||
'closed', 500.0, 0, '2026-10-06 10:00:00', '2026-10-08 14:00:00'
|
||||
)
|
||||
"""
|
||||
)
|
||||
# 已结算
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO options_trades(
|
||||
inst_id, underlying, opt_type, sheets, eth_amount,
|
||||
premium_paid, premium_received, realized_pnl, premium_ccy,
|
||||
status, trading_usdt_before, trading_usdt_after,
|
||||
realized_pnl_usdt, pnl_settled, created_at, closed_at
|
||||
) VALUES (
|
||||
'ETH-USD-261009-2500-P', 'ETH', 'P', 1, 0.01,
|
||||
0.01, 0.02, 0.01, 'ETH',
|
||||
'closed', 1000.0, 1952.96,
|
||||
952.96, 1, '2026-10-01 10:00:00', '2026-10-02 14:00:00'
|
||||
)
|
||||
"""
|
||||
)
|
||||
conn.commit()
|
||||
hist = load_local_options_stats_history(conn, None)
|
||||
stats = compute_options_stats_from_history(history_pnl_to_usdt(hist, None))
|
||||
self.assertEqual(int(stats["total_closed"]), 1)
|
||||
self.assertAlmostEqual(float(stats["net_realized_pnl"]), 952.96, places=2)
|
||||
conn.close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -104,83 +104,38 @@ class SettleClosedPnlTests(unittest.TestCase):
|
||||
|
||||
|
||||
class SimPremiumPnlStatsTests(unittest.TestCase):
|
||||
def test_sim_uses_premium_net_not_bridge_cash(self):
|
||||
"""卖回−预算会把权利金本金算进盈利;必须用(收回−已付)×平仓指数."""
|
||||
def test_sync_sim_no_longer_overwrites_account_settled(self):
|
||||
"""账户结算后 sync 兼容入口不得改写."""
|
||||
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
|
||||
|
||||
conn = sqlite3.connect(":memory:")
|
||||
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(
|
||||
id, underlying, status, budget_usdt, usdt_recovered
|
||||
) VALUES (1, 'ETH', 'closed', 1000.0, 1952.96)
|
||||
"""
|
||||
)
|
||||
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, close_index_px,
|
||||
created_at, closed_at
|
||||
status, trading_usdt_before, trading_usdt_after,
|
||||
realized_pnl_usdt, pnl_settled, 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, 1947.05, 2500.0,
|
||||
'2026-10-06 10:00:00', '2026-10-08 14:00:00'
|
||||
'closed', 1000.0, 1952.96,
|
||||
952.96, 1, '2026-10-06 10:00:00', '2026-10-08 14:00:00'
|
||||
)
|
||||
"""
|
||||
)
|
||||
conn.commit()
|
||||
n = sync_sim_cash_realized_pnl_usdt(conn, None)
|
||||
self.assertEqual(n, 1)
|
||||
row = conn.execute(
|
||||
"SELECT realized_pnl_usdt, realized_pnl FROM options_trades WHERE id=1"
|
||||
).fetchone()
|
||||
# (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_deducts_fees_from_premium_pnl(self):
|
||||
import sqlite3
|
||||
|
||||
from lib.options.options_db import init_options_tables
|
||||
from lib.options.options_stats_lib import sync_sim_cash_realized_pnl_usdt
|
||||
|
||||
conn = sqlite3.connect(":memory:")
|
||||
conn.row_factory = sqlite3.Row
|
||||
init_options_tables(conn)
|
||||
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, 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.0001, 0.0001, 'closed', 1947.05, 80000.0,
|
||||
'2026-10-05 05:11:30', '2026-10-07 10:03:53'
|
||||
)
|
||||
"""
|
||||
)
|
||||
conn.commit()
|
||||
sync_sim_cash_realized_pnl_usdt(conn, None)
|
||||
self.assertEqual(n, 0)
|
||||
row = conn.execute(
|
||||
"SELECT realized_pnl_usdt FROM options_trades WHERE id=1"
|
||||
).fetchone()
|
||||
# (0.02-0.01-0.0002)*80000 = 784
|
||||
self.assertAlmostEqual(float(row["realized_pnl_usdt"]), 784.0, places=2)
|
||||
self.assertAlmostEqual(float(row["realized_pnl_usdt"]), 952.96, places=2)
|
||||
conn.close()
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user