rolling mechanics
Browse files- options_scanner/rolling.py +137 -0
options_scanner/rolling.py
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Position management and rolling mechanics.
|
| 2 |
+
|
| 3 |
+
RollManager.check() takes an open position plus today's chain and returns
|
| 4 |
+
recommended actions with a concrete proposal (close / take-profit / stop /
|
| 5 |
+
roll out / roll down / roll out-and-down / roll out-and-up).
|
| 6 |
+
"""
|
| 7 |
+
from dataclasses import dataclass, field
|
| 8 |
+
from .pricing import bs_delta
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
@dataclass
|
| 12 |
+
class Position:
|
| 13 |
+
symbol: str
|
| 14 |
+
structure: str # e.g. "Bull Put Spread (credit)"
|
| 15 |
+
legs: list # [(kind, strike, expiry, side, qty)]
|
| 16 |
+
entry_credit: float = 0.0 # >0 credit, <0 debit, per share
|
| 17 |
+
entry_date: str = ""
|
| 18 |
+
qty_sets: int = 1
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
@dataclass
|
| 22 |
+
class RollAction:
|
| 23 |
+
action: str # close_profit | close_stop | roll_out | roll_down | roll_out_and_down | roll_out_and_up | hold
|
| 24 |
+
reason: str
|
| 25 |
+
proposed_legs: list = field(default_factory=list) # replacement legs if rolling
|
| 26 |
+
urgency: str = "normal" # normal | high
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def _position_value(position, chain, horizon_expiry=None):
|
| 30 |
+
"""Mark-to-market per share using current chain quotes (mid), back legs
|
| 31 |
+
valued at front expiry via BS when they outlive the horizon."""
|
| 32 |
+
from .pricing import bs_price
|
| 33 |
+
from datetime import date, datetime, timezone
|
| 34 |
+
value = 0.0
|
| 35 |
+
for kind, strike, expiry, side, qty in position.legs:
|
| 36 |
+
q = chain.get(strike, expiry, kind)
|
| 37 |
+
if q and q.mid > 0:
|
| 38 |
+
price = q.mid
|
| 39 |
+
else:
|
| 40 |
+
dte = (date.fromisoformat(expiry) - datetime.now(timezone.utc).date()).days
|
| 41 |
+
price = bs_price(chain.spot, strike, max(dte, 0.5) / 365.0, 0.30, kind)
|
| 42 |
+
value += side * qty * price
|
| 43 |
+
return value
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def _short_deltas(position, chain):
|
| 47 |
+
"""Current |delta| of each short leg."""
|
| 48 |
+
out = []
|
| 49 |
+
for kind, strike, expiry, side, qty in position.legs:
|
| 50 |
+
if side < 0:
|
| 51 |
+
q = chain.get(strike, expiry, kind)
|
| 52 |
+
d = abs(q.delta) if q and q.delta else 0.0
|
| 53 |
+
if not d:
|
| 54 |
+
from .pricing import bs_delta as _bd
|
| 55 |
+
from datetime import date, datetime, timezone
|
| 56 |
+
dte = (date.fromisoformat(expiry) - datetime.now(timezone.utc).date()).days
|
| 57 |
+
d = abs(_bd(chain.spot, strike, max(dte, 0.5) / 365.0, 0.30, kind))
|
| 58 |
+
out.append((kind, strike, expiry, d))
|
| 59 |
+
return out
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def _dte(expiry):
|
| 63 |
+
from datetime import date, datetime, timezone
|
| 64 |
+
return (date.fromisoformat(expiry) - datetime.now(timezone.utc).date()).days
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def check(position, chain, rules):
|
| 68 |
+
"""rules: profit_target_pct, stop_loss_mult, roll_dte, tested_delta."""
|
| 69 |
+
actions = []
|
| 70 |
+
value = _position_value(position, chain)
|
| 71 |
+
# realized/unrealized P&L per share relative to entry
|
| 72 |
+
pnl = (position.entry_credit - value) if position.entry_credit >= 0 else (value - position.entry_credit)
|
| 73 |
+
is_credit = position.entry_credit >= 0
|
| 74 |
+
dte = min(_dte(e) for _, _, e, _, _ in position.legs)
|
| 75 |
+
|
| 76 |
+
# 1. Take profit: credit -> X% of credit captured; debit -> X% of max gain proxy
|
| 77 |
+
if is_credit and pnl >= rules["profit_target_pct"] * position.entry_credit:
|
| 78 |
+
actions.append(RollAction("close_profit",
|
| 79 |
+
f"captured {pnl / position.entry_credit:.0%} of entry credit "
|
| 80 |
+
f"(${pnl * 100:.2f}/set)", urgency="normal"))
|
| 81 |
+
if not is_credit and position.entry_credit < 0 and value > 0 and \
|
| 82 |
+
(value - position.entry_credit) / (-position.entry_credit) >= rules["profit_target_pct"]:
|
| 83 |
+
actions.append(RollAction("close_profit",
|
| 84 |
+
f"debit spread up {pnl / -position.entry_credit:.0%} of entry debit"))
|
| 85 |
+
# 2. Stop loss
|
| 86 |
+
if is_credit and value >= position.entry_credit * rules["stop_loss_mult"]:
|
| 87 |
+
actions.append(RollAction("close_stop",
|
| 88 |
+
f"unrealized loss ${-pnl * 100:.2f}/set exceeds "
|
| 89 |
+
f"{rules['stop_loss_mult']}x credit — cut it",
|
| 90 |
+
urgency="high"))
|
| 91 |
+
# 3. Roll mechanics
|
| 92 |
+
if dte <= rules["roll_dte"]:
|
| 93 |
+
short_strikes = _short_deltas(position, chain)
|
| 94 |
+
tested = [s for s in short_strikes if s[3] >= rules["tested_delta"]]
|
| 95 |
+
if tested:
|
| 96 |
+
kind, strike, expiry, d = tested[0]
|
| 97 |
+
back = chain.expirations(min_dte=25, max_dte=60)
|
| 98 |
+
if not back or back[0] <= expiry:
|
| 99 |
+
actions.append(RollAction("close_stop" if pnl < 0 else "close_profit",
|
| 100 |
+
f"DTE {dte} <= {rules['roll_dte']} and no further expiry to roll to — close"))
|
| 101 |
+
else:
|
| 102 |
+
new_exp = back[0]
|
| 103 |
+
if pnl >= 0:
|
| 104 |
+
actions.append(RollAction("roll_out",
|
| 105 |
+
f"winning short {strike:g}{kind} at delta {d:.2f} — "
|
| 106 |
+
f"roll to {new_exp} to harvest more theta"))
|
| 107 |
+
elif kind == "P":
|
| 108 |
+
new_k = chain.nearest_strike(strike * 0.97, new_exp, kind)
|
| 109 |
+
if new_k:
|
| 110 |
+
actions.append(RollAction("roll_out_and_down",
|
| 111 |
+
f"tested put {strike:g} (delta {d:.2f}) — "
|
| 112 |
+
f"roll out-and-down to {new_exp} {new_k.strike:g}P",
|
| 113 |
+
proposed_legs=[("P", new_k.strike, new_exp, -1, 1)],
|
| 114 |
+
urgency="high"))
|
| 115 |
+
else:
|
| 116 |
+
new_k = chain.nearest_strike(strike * 1.03, new_exp, kind)
|
| 117 |
+
if new_k:
|
| 118 |
+
actions.append(RollAction("roll_out_and_up",
|
| 119 |
+
f"tested call {strike:g} (delta {d:.2f}) — "
|
| 120 |
+
f"roll out-and-up to {new_exp} {new_k.strike:g}C",
|
| 121 |
+
proposed_legs=[("C", new_k.strike, new_exp, -1, 1)],
|
| 122 |
+
urgency="high"))
|
| 123 |
+
else:
|
| 124 |
+
actions.append(RollAction("hold",
|
| 125 |
+
f"DTE {dte}, short deltas below {rules['tested_delta']} — "
|
| 126 |
+
f"let theta work"))
|
| 127 |
+
if not actions:
|
| 128 |
+
actions.append(RollAction("hold", f"DTE {dte}, inside all thresholds"))
|
| 129 |
+
return actions
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
DEFAULT_ROLL_RULES = {
|
| 133 |
+
"profit_target_pct": 0.50, # close at 50% of credit captured
|
| 134 |
+
"stop_loss_mult": 2.0, # stop when loss reaches 2x entry credit
|
| 135 |
+
"roll_dte": 21, # start roll management at 21 DTE
|
| 136 |
+
"tested_delta": 0.40, # short leg delta that counts as tested
|
| 137 |
+
}
|