模拟盘统计改为现货买币/卖回的真实 USDT 净盈亏,对齐钱包。
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -73,6 +73,103 @@ class SettleClosedPnlTests(unittest.TestCase):
|
||||
self.assertAlmostEqual(float(s["realized_pnl_usdt"]), 27.0, places=4)
|
||||
|
||||
|
||||
class SimCashPnlStatsTests(unittest.TestCase):
|
||||
def test_sim_cash_from_bridge_overrides_index_pnl(self):
|
||||
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)
|
||||
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,
|
||||
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, 884.54,
|
||||
'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 FROM options_trades WHERE id=1"
|
||||
).fetchone()
|
||||
self.assertAlmostEqual(float(row["realized_pnl_usdt"]), 952.96, places=2)
|
||||
conn.close()
|
||||
|
||||
def test_sim_cash_from_ledger_when_bridge_missing_recovered(self):
|
||||
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
|
||||
from lib.sim.db_lib import init_sim_tables
|
||||
|
||||
conn = sqlite3.connect(":memory:")
|
||||
conn.row_factory = sqlite3.Row
|
||||
init_options_tables(conn)
|
||||
ensure_bridge_table(conn)
|
||||
init_sim_tables(conn)
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO options_spot_bridge(
|
||||
id, underlying, status, budget_usdt, usdt_recovered
|
||||
) VALUES (1, 'ETH', 'closed', 1000.0, NULL)
|
||||
"""
|
||||
)
|
||||
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,
|
||||
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, 884.54,
|
||||
'2026-10-06 10:00:00', '2026-10-08 14:00:00'
|
||||
)
|
||||
"""
|
||||
)
|
||||
# 买币 -1000U, 卖回 +1952.96U
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO sim_ledger_entries(kind, amount, ccy, account, note, ts)
|
||||
VALUES
|
||||
('convert', -1000.0, 'USDT', 'trading', 'ETH/USDT mkt 2500', '2026-10-06 10:00:05'),
|
||||
('convert', 1952.96, 'USDT', 'trading', 'from eth', '2026-10-08 14:00:10')
|
||||
"""
|
||||
)
|
||||
conn.commit()
|
||||
n = sync_sim_cash_realized_pnl_usdt(conn, None)
|
||||
self.assertEqual(n, 1)
|
||||
row = conn.execute(
|
||||
"SELECT realized_pnl_usdt FROM options_trades WHERE id=1"
|
||||
).fetchone()
|
||||
self.assertAlmostEqual(float(row["realized_pnl_usdt"]), 952.96, places=2)
|
||||
conn.close()
|
||||
|
||||
|
||||
class ExchangeFreezeStatsTests(unittest.TestCase):
|
||||
def test_freeze_uses_exchange_coin_pnl(self):
|
||||
import sqlite3
|
||||
|
||||
Reference in New Issue
Block a user