修复 OKX 期权翻倍出场与币本位浮盈:中控/看板按指数折 U,避免 BTC 币数量当美金。
翻倍出场按全仓买一判定且对冲表异常不再整段跳过;面板按当前倍数展示需回收公式。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -79,12 +79,52 @@ class HubOptionsFundsLibTests(TestCase):
|
||||
"premium_ccy": "ETH",
|
||||
"upl": 0.0016,
|
||||
"idx_px": 2280.0,
|
||||
"close_preview": {"bid_invalid": True},
|
||||
}
|
||||
],
|
||||
}
|
||||
out = options_float_pnl_usdt(snap)
|
||||
self.assertAlmostEqual(out, round(0.0016 * 2280.0, 4), places=4)
|
||||
|
||||
def test_options_float_pnl_usdt_btc_coin_times_btc_index(self):
|
||||
from lib.hub.hub_options_funds_lib import options_float_pnl_usdt
|
||||
|
||||
snap = {
|
||||
"ok": True,
|
||||
"enabled": True,
|
||||
"options_margin_mode": "coin",
|
||||
# 默认指数若误用 ETH 也不应覆盖逐仓 BTC idx
|
||||
"options_index_px": 3000.0,
|
||||
"upl_total_usdc": -100.0,
|
||||
"positions": [
|
||||
{
|
||||
"inst_id": "BTC-USD-260327-100000-P",
|
||||
"margin_mode": "coin",
|
||||
"premium_ccy": "BTC",
|
||||
"upl": -0.001,
|
||||
"idx_px": 100000.0,
|
||||
"close_preview": {"bid_invalid": True},
|
||||
}
|
||||
],
|
||||
}
|
||||
out = options_float_pnl_usdt(snap)
|
||||
self.assertAlmostEqual(out, -100.0, places=4)
|
||||
|
||||
def test_options_float_pnl_usdt_fallback_already_usdt_no_double_fx(self):
|
||||
"""实例 hub 合计已折 U 时,回退不可再 × 指数."""
|
||||
from lib.hub.hub_options_funds_lib import options_float_pnl_usdt
|
||||
|
||||
snap = {
|
||||
"ok": True,
|
||||
"enabled": True,
|
||||
"options_margin_mode": "coin",
|
||||
"options_index_px": 100000.0,
|
||||
"upl_total_usdc": -100.0,
|
||||
"positions": [],
|
||||
}
|
||||
out = options_float_pnl_usdt(snap)
|
||||
self.assertAlmostEqual(out, -100.0, places=4)
|
||||
|
||||
def test_repair_double_counted_fund_entry(self):
|
||||
raw = {
|
||||
"funding_usdt": 586.82,
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
"""币本位期权浮盈折 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},
|
||||
}
|
||||
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()
|
||||
@@ -0,0 +1,39 @@
|
||||
"""翻倍出场:买一价×全仓张数判定达标(不受买一深度截断)."""
|
||||
from __future__ import annotations
|
||||
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
from lib.options.options_profit_exit_lib import _estimate_recycle, profit_exit_hit
|
||||
|
||||
|
||||
class ProfitExitHitTests(unittest.TestCase):
|
||||
def test_hit_at_0_3_mult(self):
|
||||
self.assertTrue(profit_exit_hit(premium_paid=1.0, recycle_usdc=1.3, mult=0.3))
|
||||
self.assertFalse(profit_exit_hit(premium_paid=1.0, recycle_usdc=1.29, mult=0.3))
|
||||
|
||||
def test_estimate_uses_full_sheets_not_depth(self):
|
||||
pos = {"inst_id": "ETH-USD-260927-2700-P", "pos": 100, "ct_mult": 0.01}
|
||||
preview = {
|
||||
"bid_invalid": False,
|
||||
"levels": [{"px": 0.2, "sheets": 10}],
|
||||
"total_received": 0.02,
|
||||
}
|
||||
|
||||
def fake_attach(cfg, ex, row, premium_paid=None):
|
||||
row["close_preview"] = preview
|
||||
row["ct_mult"] = 0.01
|
||||
row["pos"] = 100
|
||||
return row
|
||||
|
||||
with patch(
|
||||
"lib.options.options_positions_lib.attach_close_preview",
|
||||
side_effect=fake_attach,
|
||||
):
|
||||
recv = _estimate_recycle({}, object(), pos, 1.0)
|
||||
self.assertAlmostEqual(float(recv), 0.2, places=6)
|
||||
self.assertTrue(profit_exit_hit(premium_paid=0.15, recycle_usdc=float(recv), mult=0.3))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user