Files
crypto_monitor/tests/test_hub_agent_mark_price.py
T
2026-06-03 22:33:59 +08:00

45 lines
1.4 KiB
Python

"""子代理持仓:四所标记价字段统一解析。"""
from __future__ import annotations
import sys
import unittest
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "manual_trading_hub"))
from agent import _position_mark_price, _ticker_mark_price # noqa: E402
class TestHubAgentMarkPrice(unittest.TestCase):
def test_binance_mark_price(self):
px = _position_mark_price({"markPrice": 65880.1, "info": {}})
self.assertAlmostEqual(px, 65880.1)
def test_okx_mark_px(self):
px = _position_mark_price({"info": {"markPx": "72.85"}})
self.assertAlmostEqual(px, 72.85)
def test_gate_info_mark(self):
px = _position_mark_price({"info": {"mark_price": "0.2241"}})
self.assertAlmostEqual(px, 0.2241)
def test_missing_returns_none(self):
self.assertIsNone(_position_mark_price({"info": {}}))
def test_infer_from_notional_and_contracts(self):
p = {"notional": 1000, "contracts": 10, "info": {}}
px = _position_mark_price(p)
self.assertAlmostEqual(px, 100.0)
def test_ticker_fallback(self):
class _Ex:
def fetch_ticker(self, sym):
return {"mark": 99.5, "info": {}}
self.assertAlmostEqual(_ticker_mark_price(_Ex(), "BTC/USDT:USDT"), 99.5)
if __name__ == "__main__":
unittest.main()