Files
crypto_okx/tests/test_options_delta_fields.py
2026-10-14 22:20:08 +08:00

63 lines
1.9 KiB
Python

# -*- coding: utf-8 -*-
import unittest
from lib.exchange.okx_options_lib import format_position_row, option_delta_from_summary
class OptionsDeltaFieldsTests(unittest.TestCase):
def test_delta_prefers_summary_delta_bs(self):
self.assertAlmostEqual(
option_delta_from_summary({"deltaBS": "0.55", "delta": "12.3"}),
0.55,
places=4,
)
def test_delta_falls_back_to_ticker(self):
self.assertAlmostEqual(
option_delta_from_summary({}, ticker={"delta": "-0.22"}),
-0.22,
places=4,
)
def test_delta_none_when_missing(self):
self.assertIsNone(option_delta_from_summary({}, ticker={}))
def test_format_position_exposes_delta_bs(self):
row = format_position_row(
{
"instId": "ETH-USD-261015-2500-C",
"pos": "10",
"avgPx": "0.02",
"markPx": "0.025",
"idxPx": "2600",
"optType": "C",
"stk": "2500",
"deltaBS": "0.6123",
"deltaPA": "1.23",
},
ct_mult=0.1,
)
self.assertAlmostEqual(row["delta"], 0.6123, places=4)
self.assertAlmostEqual(row["delta_bs"], 0.6123, places=4)
self.assertAlmostEqual(row["delta_pa"], 1.23, places=4)
def test_format_position_falls_back_delta_field(self):
row = format_position_row(
{
"instId": "BTC-USD-261015-80000-P",
"pos": "1",
"avgPx": "0.01",
"markPx": "0.012",
"idxPx": "82000",
"optType": "P",
"stk": "80000",
"delta": "-0.45",
},
ct_mult=0.01,
)
self.assertAlmostEqual(row["delta"], -0.45, places=4)
if __name__ == "__main__":
unittest.main()