Fix instance dashboard order PnL columns showing empty dashes.

Compute float_pnl and tp_profit from mark/entry/contracts using each exchange contract size during dashboard enrich.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-08-06 14:12:57 +08:00
parent 993189ce13
commit 75a4175522
5 changed files with 104 additions and 17 deletions
+34
View File
@@ -167,6 +167,40 @@ class TestInstanceDashboardLib(unittest.TestCase):
self.assertIn("对冲#2", opt["target_monitor"])
conn.close()
def test_enrich_order_items_fills_float_pnl_and_tp_profit(self):
from lib.instance.instance_dashboard_lib import enrich_order_items_with_marks
items = [
{
"id": 1,
"symbol": "BTC/USDT:USDT",
"price_symbol": "BTC/USDT:USDT",
"direction": "long",
"entry": 64693.6,
"contracts": 132,
"take_profit": 66000.0,
"mark_price": None,
"tp_profit": None,
"float_pnl": None,
}
]
def get_price(sym):
return 64809.5
def get_cs(sym):
return 0.0001
out = enrich_order_items_with_marks(
items, get_price=get_price, get_contract_size=get_cs
)
self.assertEqual(len(out), 1)
self.assertEqual(out[0]["mark_price"], 64809.5)
# (64809.5 - 64693.6) * 132 * 0.0001 ≈ 1.53
self.assertAlmostEqual(out[0]["float_pnl"], 1.53, places=2)
self.assertIsNotNone(out[0]["tp_profit"])
self.assertGreater(out[0]["tp_profit"], 0)
if __name__ == "__main__":
unittest.main()