8a62cfbb48
币本位 BTC/ETH 净盈亏须 × 指数后再标 U;并兜底从合约推断 premium_ccy,避免缺失时漏折算。 Co-authored-by: Cursor <cursoragent@cursor.com>
46 lines
1.5 KiB
Python
46 lines
1.5 KiB
Python
"""币本位期权浮盈折 U(BTC/ETH 不得把币数量直接当 U)."""
|
||
from __future__ import annotations
|
||
|
||
import unittest
|
||
|
||
from lib.options.options_positions_lib import display_pnl_usdt_from_option_row
|
||
|
||
|
||
class DisplayPnlUsdtTests(unittest.TestCase):
|
||
def test_btc_coin_pnl_times_index(self):
|
||
row = {
|
||
"inst_id": "BTC-USD-260327-100000-P",
|
||
"premium_ccy": "BTC",
|
||
"margin_mode": "coin",
|
||
"idx_px": 100000.0,
|
||
"upl": -0.001,
|
||
"close_preview": {"bid_invalid": True},
|
||
}
|
||
# 残档回退 upl: -0.001 BTC × 100000 = -100 U
|
||
self.assertAlmostEqual(display_pnl_usdt_from_option_row(row), -100.0, places=4)
|
||
|
||
def test_btc_infers_ccy_when_premium_ccy_missing(self):
|
||
row = {
|
||
"inst_id": "BTC-USD-260327-100000-C",
|
||
"margin_mode": "coin",
|
||
"underlying": "BTC",
|
||
"idx_px": 95000.0,
|
||
"upl": 0.002,
|
||
"close_preview": {"bid_invalid": True},
|
||
}
|
||
self.assertAlmostEqual(display_pnl_usdt_from_option_row(row), 190.0, places=4)
|
||
|
||
def test_usdc_unchanged(self):
|
||
row = {
|
||
"inst_id": "BTC-USD_UM-260327-100000-C",
|
||
"premium_ccy": "USDC",
|
||
"margin_mode": "usdc",
|
||
"upl": -12.5,
|
||
"close_preview": {"bid_invalid": True},
|
||
}
|
||
self.assertAlmostEqual(display_pnl_usdt_from_option_row(row), -12.5, places=4)
|
||
|
||
|
||
if __name__ == "__main__":
|
||
unittest.main()
|