feat(options): use premium profit RR instead of target index

单独期权与中控改为盈亏比×权利金触发买一平仓,默认2;不达标等到期。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-08-11 10:34:14 +08:00
parent 3b56e15fb1
commit 5c3969674a
13 changed files with 494 additions and 194 deletions
+63 -6
View File
@@ -1,4 +1,4 @@
"""期权目标委托单元测试."""
"""期权目标委托单元测试(盈亏比 + 旧指数兼容)."""
from __future__ import annotations
import sqlite3
@@ -8,6 +8,7 @@ from lib.options.options_target_lib import (
ensure_target_tables,
list_active_targets,
list_closing_targets,
profit_rr_hit,
run_options_target_closes,
target_hit,
upsert_target_monitor,
@@ -21,7 +22,67 @@ class OptionsTargetLibTests(unittest.TestCase):
self.assertTrue(target_hit(opt_type="P", index_px=1800, target_index=1850))
self.assertFalse(target_hit(opt_type="P", index_px=1900, target_index=1850))
def test_upsert_and_trigger_close(self):
def test_profit_rr_hit(self):
# premium=10, rr=2 → need pnl≥20 → recycle≥30 → bid*sheets*ct ≥30
self.assertTrue(
profit_rr_hit(premium=10, bid=30, sheets=1, ct_mult=1, profit_rr=2)
)
self.assertFalse(
profit_rr_hit(premium=10, bid=29.9, sheets=1, ct_mult=1, profit_rr=2)
)
self.assertFalse(
profit_rr_hit(premium=10, bid=None, sheets=1, ct_mult=1, profit_rr=2)
)
def test_upsert_rr_and_trigger_close(self):
conn = sqlite3.connect(":memory:")
conn.row_factory = sqlite3.Row
ensure_target_tables(conn)
out = upsert_target_monitor(
conn,
inst_id="ETH-USD_UM-260717-1900-C",
profit_rr=2,
opt_type="C",
sheets=1,
)
self.assertTrue(out["ok"])
self.assertEqual(out.get("profit_rr"), 2.0)
self.assertEqual(len(list_active_targets(conn)), 1)
closed = []
def close_fn(inst_id: str):
closed.append(inst_id)
return {
"ok": True,
"submitted_sheets": 1,
"premium_received": 30.0,
"close_ord_id": "oid1",
"fully_closed": True,
"remaining_sheets": 0,
}
# bid=30, ct=1 → pnl=20 ≥ 2*10; premium 来自持仓字段
n = run_options_target_closes(
conn,
[
{
"inst_id": "ETH-USD_UM-260717-1900-C",
"idx_px": 1885,
"opt_type": "C",
"pos": 1,
"ct_mult": 1,
"premium_paid": 10,
}
],
close_fn=close_fn,
bid_fn=lambda _i: 30.0,
)
self.assertEqual(n, 1)
self.assertEqual(closed, ["ETH-USD_UM-260717-1900-C"])
self.assertEqual(len(list_active_targets(conn)), 0)
def test_upsert_and_trigger_close_legacy_index(self):
conn = sqlite3.connect(":memory:")
conn.row_factory = sqlite3.Row
ensure_target_tables(conn)
@@ -107,7 +168,6 @@ class OptionsTargetLibTests(unittest.TestCase):
self.assertEqual(len(list_active_targets(conn)), 0)
self.assertEqual(len(list_closing_targets(conn)), 1)
# 模拟后续 sync 异常也不会再推:closing 重试静默
n2 = run_options_target_closes(
conn,
pos,
@@ -120,7 +180,6 @@ class OptionsTargetLibTests(unittest.TestCase):
self.assertEqual(len(list_closing_targets(conn)), 0)
def test_commit_before_wechat_survives_later_rollback(self):
"""状态在推送前已 commit,外层异常回滚不应让委托回到 active."""
conn = sqlite3.connect(":memory:")
conn.row_factory = sqlite3.Row
ensure_target_tables(conn)
@@ -149,12 +208,10 @@ class OptionsTargetLibTests(unittest.TestCase):
close_fn=close_fn,
send_wechat=notices.append,
)
# 模拟 loop 后续 sync 抛错后 close 未再 commit —— 但 status 已提前 commit
conn.rollback()
self.assertEqual(len(notices), 1)
self.assertEqual(len(list_active_targets(conn)), 0)
# 下一轮不应再次触发推送
n2 = run_options_target_closes(
conn,
[{"inst_id": "ETH-USD_UM-260715-1870-P", "idx_px": 1860, "opt_type": "P"}],