Fix dashboard PnL using spot contract size of 1.

Prefer perpetual symbols and normalize before market.contractSize lookup so Gate BTC float matches ~0.4U not thousands.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-08-06 14:20:43 +08:00
parent 75a4175522
commit d41028b766
5 changed files with 91 additions and 22 deletions
+35
View File
@@ -201,6 +201,41 @@ class TestInstanceDashboardLib(unittest.TestCase):
self.assertIsNotNone(out[0]["tp_profit"])
self.assertGreater(out[0]["tp_profit"], 0)
def test_enrich_prefers_swap_contract_size_over_spot(self):
"""看板 price_symbol 常为 BTC/USDT,现货面会落到 1,必须用永续面值."""
from lib.instance.instance_dashboard_lib import enrich_order_items_with_marks
items = [
{
"id": 1,
"symbol": "BTC/USDT:USDT",
"price_symbol": "BTC/USDT",
"direction": "long",
"entry": 64693.6,
"contracts": 132,
"take_profit": 65135.0,
"mark_price": None,
"tp_profit": None,
"float_pnl": None,
}
]
def get_price(sym):
return 64727.2
def get_cs(sym):
# 模拟未 normalize 的旧行为:现货 1,永续 0.0001
if ":" in (sym or ""):
return 0.0001
return 1.0
out = enrich_order_items_with_marks(
items, get_price=get_price, get_contract_size=get_cs
)
# (64727.2 - 64693.6) * 132 * 0.0001 ≈ 0.44
self.assertAlmostEqual(out[0]["float_pnl"], 0.44, places=2)
self.assertLess(out[0]["tp_profit"], 10)
if __name__ == "__main__":
unittest.main()