Add fixed-direction switch for perp/option open side.

When enabled, lock perp long→buy Put or short→buy Call with ITM/ATM only; off keeps ATM/ask rules.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-07-29 19:18:12 +08:00
parent 55109e82d3
commit 0ee1d8be5d
16 changed files with 427 additions and 46 deletions
+37
View File
@@ -27,6 +27,43 @@ def test_pick_atm_strike() -> None:
assert pick_atm_strike([3400, 3500, 3600], 3510) == 3500
def test_pick_itm_or_atm_strike() -> None:
from app.strategy.selection import is_itm_or_atm, pick_itm_or_atm_strike
strikes = [3400, 3500, 3600]
# CallK≤S,现价 3510 → 3500(平值侧最近)
assert pick_itm_or_atm_strike(strikes, 3510, option_side="call") == 3500
# PutK≥S,现价 3510 → 3600(实值最近;无 3510 档)
assert pick_itm_or_atm_strike(strikes, 3510, option_side="put") == 3600
# Put 现价正好 3500 → 平值 3500
assert pick_itm_or_atm_strike(strikes, 3500, option_side="put") == 3500
assert is_itm_or_atm(option_side="call", strike=3500, mark_px=3510)
assert not is_itm_or_atm(option_side="call", strike=3600, mark_px=3510)
assert is_itm_or_atm(option_side="put", strike=3600, mark_px=3510)
assert not is_itm_or_atm(option_side="put", strike=3400, mark_px=3510)
def test_select_option_pair_itm_put() -> None:
rows = [
{"instId": "ETH-USD_UM-260725-3490-C", "state": "live"},
{"instId": "ETH-USD_UM-260725-3490-P", "state": "live"},
{"instId": "ETH-USD_UM-260725-3500-C", "state": "live"},
{"instId": "ETH-USD_UM-260725-3500-P", "state": "live"},
{"instId": "ETH-USD_UM-260725-3510-C", "state": "live"},
{"instId": "ETH-USD_UM-260725-3510-P", "state": "live"},
]
# 标的 3502Put 实/平 → 3510(≥3502 最近)
pair = select_option_pair(rows, mark_px=3502, expiry_ymd="260725", option_side="put")
assert pair is not None
assert pair.strike == 3510
# Call 实/平 → 3500(≤3502 最近)
pair_c = select_option_pair(
rows, mark_px=3502, expiry_ymd="260725", option_side="call"
)
assert pair_c is not None
assert pair_c.strike == 3500
def test_next_session_expiry_before_open() -> None:
now = datetime(2026, 7, 24, 15, 0, tzinfo=_SH)
assert next_session_expiry_ymd(now) == "260724"
+20
View File
@@ -28,6 +28,26 @@ def test_signal_equal() -> None:
assert decide(10.0, 10.0) is None
def test_decide_fixed_long_put() -> None:
from app.strategy.signal import decide_fixed
s = decide_fixed(20.0, 15.0, perp_side="long")
assert s is not None
assert s.option_side == "put"
assert s.perp_side == "long"
assert s.bias == "fixed_long_put"
def test_decide_fixed_short_call() -> None:
from app.strategy.signal import decide_fixed
s = decide_fixed(20.0, 15.0, perp_side="short")
assert s is not None
assert s.option_side == "call"
assert s.perp_side == "short"
assert s.bias == "fixed_short_call"
def test_signal_strike_below_spot_call_short() -> None:
# 现价 1859、ATM 1850:即使 Put 卖一更高,也走 Call+空
s = decide(10.0, 20.0, strike=1850, mark_px=1859)