a5f4ad8e97
Parse signed upl/unrealizedPnl from CCXT positions and fall back to calc_pnl when exchange metrics are missing. Co-authored-by: Cursor <cursoragent@cursor.com>
39 lines
1.1 KiB
Python
39 lines
1.1 KiB
Python
"""OKX 持仓指标解析:未实现盈亏须支持负数。"""
|
|
from __future__ import annotations
|
|
|
|
import unittest
|
|
|
|
|
|
class TestOkxPositionMetrics(unittest.TestCase):
|
|
def test_parse_unrealized_pnl_negative(self):
|
|
from crypto_monitor_okx.app import parse_ccxt_position_metrics
|
|
|
|
pos = {
|
|
"side": "long",
|
|
"contracts": 10,
|
|
"markPrice": 0.43,
|
|
"unrealizedPnl": -1.25,
|
|
"info": {"upl": "-1.25", "markPx": "0.43"},
|
|
}
|
|
out = parse_ccxt_position_metrics(pos, order_leverage=5)
|
|
self.assertIsNotNone(out)
|
|
self.assertAlmostEqual(out["unrealized_pnl"], -1.25)
|
|
self.assertAlmostEqual(out["mark_price"], 0.43)
|
|
|
|
def test_parse_unrealized_pnl_zero(self):
|
|
from crypto_monitor_okx.app import parse_ccxt_position_metrics
|
|
|
|
pos = {
|
|
"side": "long",
|
|
"contracts": 1,
|
|
"unrealizedPnl": 0,
|
|
"info": {"upl": "0"},
|
|
}
|
|
out = parse_ccxt_position_metrics(pos)
|
|
self.assertIsNotNone(out)
|
|
self.assertEqual(out["unrealized_pnl"], 0.0)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|