-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
532 lines (436 loc) · 21.3 KB
/
app.py
File metadata and controls
532 lines (436 loc) · 21.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
# This web app is designed to allow users to explore how the Greeks (i.e., Delta, Gamma, Theta, Vega, and Rho) affect option pricing under the
# Black-Scholes-Merton (BSM) model by adjusting input parameters (ex: strike price, time to maturity, volatility, interest rate, etc.) using sliders.
# The web app was designed using Streamlit and plotly.
import streamlit as st
import numpy as np
from greeks_calculator import price, delta, gamma, vega, theta, rho
from utils import (fetch_chain, calculate_strategy_payoff, get_option_chain,
fetch_hist_prices, calc_hv, compute_skew, tag_signals)
from plot_helpers import (
plot_greeks_surface, plot_strategy_payoff, plot_iv_skew,
plot_iv_hv_scatter, plot_volume_spikes
)
import yfinance as yf
import pandas as pd
import plotly.express as px
import json
st.set_page_config(page_title="Options Risk Diagnostics", layout="wide")
# --- Sidebar: Global Parameters ---
st.sidebar.header("Market & Option Parameters")
ticker = st.sidebar.text_input("Ticker", value="AAPL")
r = st.sidebar.number_input("Risk-free rate (r)", value=0.03)
q = st.sidebar.number_input("Dividend yield (q)", value=0.0)
# --- Tabs ---
tabs = st.tabs([
"Tab 1 - Greeks Explorer",
"Tab 2 - Chain Analyzer",
"Tab 3 - Strategy Simulator",
"Tab 4 - Trade Scanner"
])
# =======================
# Tab 1: Greeks Explorer
# =======================
with tabs[0]:
st.header("Greeks Sensitivity Explorer")
st.subheader("Developed by Riju Datta")
st.markdown("Explore how option Greeks behave across different market conditions.")
col1, col2 = st.columns(2)
with col1:
S = st.slider("Spot Price (S)", 50, 300, 150)
K = st.slider("Strike Price (K)", 50, 300, 150)
sigma = st.slider("Volatility (σ)", 0.05, 1.0, 0.3)
with col2:
T = st.slider("Time to Maturity (T, in years)", 0.01, 2.0, 0.5)
option_type = st.selectbox("Option Type", ["call", "put"])
mesh_choice = st.selectbox("Meshgrid", ["S vs σ", "S vs T", "σ vs T"])
# Meshgrid based on user choice
s_vals = np.linspace(50, 300, 40)
sigma_vals = np.linspace(0.05, 1.0, 40)
T_vals = np.linspace(0.01, 2.0, 40)
# Greek selection
selected_greek = st.radio(
"Select Greek to View:",
["Delta", "Gamma", "Vega", "Theta", "Rho"],
horizontal=True
)
if mesh_choice == "S vs σ":
X, Y = np.meshgrid(s_vals, sigma_vals)
if selected_greek == "Delta":
Z = delta(X, K, T, Y, r, q, option_type)
elif selected_greek == "Gamma":
Z = gamma(X, K, T, Y, r, q)
elif selected_greek == "Vega":
Z = vega(X, K, T, Y, r, q)
elif selected_greek == "Theta":
Z = theta(X, K, T, Y, r, q, option_type)
elif selected_greek == "Rho":
Z = rho(X, K, T, Y, r, q, option_type)
st.plotly_chart(plot_greeks_surface(X, Y, Z, "S", "σ", selected_greek), use_container_width=True)
elif mesh_choice == "S vs T":
X, Y = np.meshgrid(s_vals, T_vals)
if selected_greek == "Delta":
Z = delta(X, K, T, Y, r, q, option_type)
elif selected_greek == "Gamma":
Z = gamma(X, K, T, Y, r, q)
elif selected_greek == "Vega":
Z = vega(X, K, T, Y, r, q)
elif selected_greek == "Theta":
Z = theta(X, K, T, Y, r, q, option_type)
elif selected_greek == "Rho":
Z = rho(X, K, T, Y, r, q, option_type)
st.plotly_chart(plot_greeks_surface(X, Y, Z, "S", "T", selected_greek), use_container_width=True)
elif mesh_choice == "σ vs T":
X, Y = np.meshgrid(sigma_vals, T_vals)
if selected_greek == "Delta":
Z = delta(X, K, T, Y, r, q, option_type)
elif selected_greek == "Gamma":
Z = gamma(X, K, T, Y, r, q)
elif selected_greek == "Vega":
Z = vega(X, K, T, Y, r, q)
elif selected_greek == "Theta":
Z = theta(X, K, T, Y, r, q, option_type)
elif selected_greek == "Rho":
Z = rho(X, K, T, Y, r, q, option_type)
st.plotly_chart(plot_greeks_surface(X, Y, Z, "σ", "T", selected_greek), use_container_width=True)
# Inline interpretation
greek_descriptions = {
"Delta": """
**Delta** measures **how much the option price moves when the underlying asset (S) changes by $1**.
- For calls, Delta is positive (0 to 1); for puts, it’s negative (-1 to 0).
- A Delta of 0.60 means the call option gains 60 cents if the stock rises 1 dollar.
**It increases with:**
- Higher spot price (S): you're more in-the-money, so the option acts more like the stock.
- Shorter time to expiration (T): Delta tends to 1 or 0 as expiry nears.
**It decreases with:**
- Deeper out-of-the-money options or longer-dated options (higher T): less sensitive to small moves in S.
""",
"Gamma": """
**Gamma** measures **how quickly Delta changes as the underlying asset (S) moves** — it’s the "Delta of Delta".
- High Gamma means Delta shifts fast — important for hedging.
- Gamma peaks **when the option is at-the-money**.
**It increases with:**
- Shorter time to maturity (T): gamma spikes near expiration.
- ATM options: Gamma is max when S ≈ K.
**It decreases with:**
- Deep ITM or OTM options: their Deltas are already near 1 or 0.
- Longer expiries: Delta changes more gradually.
**Why it matters:** Traders with Delta-hedged portfolios still face risk from large Gamma — i.e., needing to rebalance Delta frequently as prices move.
""",
"Vega": """
**Vega** measures **how sensitive the option price is to changes in implied volatility (σ)**.
- A Vega of 0.10 means the option price changes by $0.10 for a 1% change in volatility.
**It increases with:**
- Longer time to maturity (T): more time = more exposure to volatility.
- ATM options: they gain most from volatility increases.
**It decreases with:**
- Deep ITM/OTM options: their payouts are less affected by changes in volatility.
- As T → 0: less time to benefit from volatility.
**Why it matters:** When you buy options, you’re long Vega — higher volatility helps. When you sell options, you want volatility to stay low.
""",
"Theta": """
**Theta** measures **how much value an option loses per day as time passes** — known as **time decay**.
- A Theta of -0.05 means the option loses $0.05 in value per day (if everything else stays the same).
**It increases (more negative) with:**
- ATM options: they lose time value fastest.
- Shorter time to expiry (T): decay accelerates near expiration.
**It decreases (less negative or near 0) with:**
- Deep ITM or OTM options: less time value left to decay.
- Longer-dated options: decay is slower initially.
**Why it matters:** Option buyers fight Theta decay every day. Sellers profit from it if nothing moves.
""",
"Rho": """
**Rho** measures **how much the option price changes with a 1% change in interest rates (r)**.
- A Rho of 0.10 means the option price changes by $0.10 for a 1% rise in rates.
**For calls:**
- Rho is positive — rising rates increase call value.
**For puts:**
- Rho is negative — rising rates decrease put value.
**It increases with:**
- Longer time to maturity (T): more exposure to discounting effect.
- Deep ITM options: their intrinsic value dominates.
**It’s small/negligible when:**
- T is short (approaching expiry).
- Option is near the money or low premium.
**Why it matters:** Most relevant during macro shifts (e.g., Fed policy). Often ignored but can affect long-dated options significantly.
"""
}
st.markdown(greek_descriptions[selected_greek])
# =======================
# Tab 2: Chain Analyzer
# =======================
with tabs[1]:
st.header("Real Options Chain Analyzer")
ticker = st.text_input("Enter Stock Ticker", value="AAPL").upper()
if ticker:
try:
ticker_obj = yf.Ticker(ticker)
valid_expiries = ticker_obj.options
except Exception:
st.error("Could not fetch options data for this ticker.")
st.stop()
if not valid_expiries:
st.error("No available option expirations for this ticker.")
st.stop()
expiry = st.selectbox("Select Expiry Date", valid_expiries, index=0)
raw_df = fetch_chain(ticker, expiry)
if raw_df.empty:
st.warning("Fetched option chain is empty.")
st.stop()
df = raw_df.copy()
# Fetch spot price
spot_data = ticker_obj.history(period="1d")
spot = spot_data["Close"].iloc[-1] if not spot_data.empty else None
if spot is None:
st.error("Could not fetch current spot price.")
st.stop()
st.write(f"Current Spot Price: **${spot:.2f}**")
# Process chain
df["mid"] = (df["bid"] + df["ask"]) / 2
df["spread"] = df["ask"] - df["bid"]
df["spread_pct"] = df["spread"] / df["mid"].replace(0, np.nan)
df["ITM"] = ((df["option_type"] == "call") & (df["strike"] < spot)) | \
((df["option_type"] == "put") & (df["strike"] > spot))
df["dist_from_spot(%)"] = 100 * (df["strike"] - spot) / spot
df["break_even"] = np.where(df["option_type"] == "call",
df["strike"] + df["mid"],
df["strike"] - df["mid"])
df["weird"] = (df["mid"] <= 0) | (df["bid"] > df["ask"]) | (df["spread_pct"] > 0.5)
# Section 1: Liquid contracts
st.subheader("Most Liquid Contracts")
st.markdown("""
These are the **top 10 contracts** with high trading **volume and open interest**, and relatively **tight bid-ask spreads**.
These contracts are easier to enter and exit without slippage and are typically favored by active traders.
""")
liquid = df[(df["volume"] > 0) & (df["openInterest"] > 0)].copy()
liquid = liquid.sort_values(by=["volume", "spread_pct"], ascending=[False, True])
st.dataframe(liquid[["contractSymbol", "option_type", "strike", "volume", "openInterest", "bid", "ask", "spread_pct"]].head(10))
# Section 2: IV Skew
st.subheader("Implied Volatility Skew")
st.markdown("""
**Implied Volatility (IV)** reflects how much the market expects the stock to move.
A **steep skew** in IV across strikes often indicates **hedging demand** or **directional bias**.
- **Call IV > Put IV** ➝ bullish demand or call overwriting.
- **Put IV > Call IV** ➝ bearish protection or crash hedging.
""")
if "impliedVolatility" in df.columns and df["impliedVolatility"].notnull().any():
fig_iv = px.line(df, x="strike", y="impliedVolatility", color="option_type",
title="Implied Volatility vs Strike", markers=True)
st.plotly_chart(fig_iv, use_container_width=True)
else:
st.warning("No implied volatility data found for this chain.")
# Section 3: ITM vs OTM Comparison
st.subheader("In-The-Money vs Out-of-The-Money Contracts")
st.markdown("""
**In-the-money (ITM)** options have intrinsic value and behave more like stock, while **out-of-the-money (OTM)** options
are pure premium and are often used for **speculation** or **hedging**.
This section shows how far the break-even point is from the current stock price,
which is critical when evaluating whether a trade makes sense.
""")
itm_df = df[df["ITM"]].sort_values(by="strike")
otm_df = df[~df["ITM"]].sort_values(by="strike")
st.markdown("**ITM Contracts:**")
st.dataframe(itm_df[["contractSymbol", "option_type", "strike", "mid", "break_even", "dist_from_spot(%)"]].head(10))
st.markdown("**OTM Contracts:**")
st.dataframe(otm_df[["contractSymbol", "option_type", "strike", "mid", "break_even", "dist_from_spot(%)"]].head(10))
# Section 4: Weird Contracts
st.subheader("Potentially Mispriced or Illiquid Contracts")
st.markdown("""
This identifies contracts with unusual pricing behavior:
- **Negative or 0 mid-price**
- **Bid > Ask** (data error or stale quote)
- **Very wide bid-ask spread** (> 50%)
These contracts are often **illiquid** or **data anomalies**, and should be **avoided** in serious strategies.
""")
weird_df = df[df["weird"]].sort_values(by="spread_pct", ascending=False)
st.dataframe(weird_df[["contractSymbol", "option_type", "strike", "bid", "ask", "mid", "volume", "spread_pct"]].head(10))
# =======================
# Tab 3: Strategy Simulator
# =======================
with tabs[2]:
st.header("Options Strategy Simulator")
st.markdown(
"Build a custom multi-leg options strategy, add an underlying stock hedge, "
"stress-test implied volatility, or start from a predefined template."
)
# -------------------------- basic inputs --------------------------
ticker_sim = st.text_input("Ticker Symbol", value="AAPL")
try:
ticker_obj_sim = yf.Ticker(ticker_sim)
expiries_sim = ticker_obj_sim.options
except Exception:
st.error("Could not load option expirations.")
st.stop()
expiry_sim = st.selectbox("Select Expiration", expiries_sim)
# --------------------- 1️⃣ Strategy Templates ----------------------
strategy_templates = {
"Bull Call Spread": [
{"type": "call", "action": "buy", "strike": 100, "qty": 1, "price": 5, "expiry": expiry_sim},
{"type": "call", "action": "sell", "strike": 110, "qty": 1, "price": 2, "expiry": expiry_sim},
],
"Long Straddle": [
{"type": "call", "action": "buy", "strike": 100, "qty": 1, "price": 4, "expiry": expiry_sim},
{"type": "put", "action": "buy", "strike": 100, "qty": 1, "price": 5, "expiry": expiry_sim},
],
"Iron Condor": [
{"type": "call", "action": "sell", "strike": 110, "qty": 1, "price": 2, "expiry": expiry_sim},
{"type": "call", "action": "buy", "strike": 115, "qty": 1, "price": 1, "expiry": expiry_sim},
{"type": "put", "action": "sell", "strike": 90, "qty": 1, "price": 2, "expiry": expiry_sim},
{"type": "put", "action": "buy", "strike": 85, "qty": 1, "price": 1, "expiry": expiry_sim},
],
}
template_choice = st.selectbox(
"Choose Strategy Template",
["None"] + list(strategy_templates.keys()),
index=0,
help="Auto-populate common multi-leg strategies."
)
# Initialise session state
if "legs" not in st.session_state:
st.session_state.legs = []
# Apply template
if template_choice != "None" and st.button("Load Template"):
# overwrite current legs with a deep copy of template legs
import copy
st.session_state.legs = copy.deepcopy(strategy_templates[template_choice])
st.subheader("Add / Edit Option Legs")
# -------------------- manual leg entry form ----------------------
with st.form("leg_form", clear_on_submit=True):
col1, col2, col3 = st.columns(3)
with col1:
opt_type = st.selectbox("Option Type", ["Call", "Put"])
with col2:
action = st.selectbox("Action", ["Buy", "Sell"])
with col3:
qty = st.number_input("Quantity", min_value=1, value=1, step=1)
strike = st.number_input("Strike Price", min_value=0.0, value=100.0, step=1.0)
price = st.number_input("Premium (optional)", min_value=0.0, value=0.0, step=0.01)
if st.form_submit_button("Add Leg"):
st.session_state.legs.append(
{
"type": opt_type.lower(),
"action": action.lower(),
"strike": strike,
"qty": qty,
"price": price,
"expiry": expiry_sim,
}
)
# ---------------------------- payoff inputs -----------------------------
if st.session_state.legs:
st.subheader("Current Strategy Legs")
st.dataframe(pd.DataFrame(st.session_state.legs))
spot_price = st.number_input(
"Current Spot Price", min_value=0.0, value=100.0, step=0.5
)
# 2️⃣ Underlying hedge
underlying_qty = st.number_input(
"Shares of Underlying ( + long / – short )",
value=0,
step=1,
help="Add stock to hedge delta or create covered positions.",
)
# 3️⃣ IV stress-test
iv_shift = st.slider(
"Implied Volatility Shift (%)",
min_value=-50,
max_value=50,
value=0,
step=1,
help="Re-price each option leg with shifted IV before payoff calc.",
)
# ------------------- calculate and plot strategy --------------------
payoff_df, metrics = calculate_strategy_payoff(
st.session_state.legs,
spot_price,
r, # risk-free
underlying_qty,
iv_shift
)
fig = plot_strategy_payoff(payoff_df)
st.plotly_chart(fig, use_container_width=True)
st.subheader("Key Metrics")
st.write(metrics)
if st.button("Reset Strategy"):
st.session_state.legs = []
# =======================
# Tab 4: Trade Scanner
# =======================
with tabs[3]:
st.header("Options Signal Scanner")
st.markdown(
"Scan a ticker (or 1–5 tickers) for **IV skew, IV > HV mispricing, "
"and unusual volume / open-interest activity.**"
)
# ---------------- primary controls ----------------
tickers_input = st.text_input(
"Ticker(s) – comma-separated", value="AAPL", help="1–5 symbols max"
)
ticker_list = [t.strip().upper() for t in tickers_input.split(",") if t.strip()][:5]
expiry_window = st.selectbox(
"Expiry window",
("Next weekly", "Next monthly", "All ≤ 45 DTE"),
)
skew_z_thresh = st.number_input("Skew |z| threshold", value=1.5, step=0.1)
iv_hv_thresh = st.slider("IV / HV threshold (%)", 100, 300, 120, step=5)
vol_mult_thresh = st.slider("Volume multiple vs baseline", 1, 10, 3, step=1)
scan_btn = st.button("Scan")
if scan_btn:
all_flags = []
all_contracts = []
with st.spinner("Fetching option chains & price history…"):
for tk in ticker_list:
# 1️⃣ pull option chain
chain_df = get_option_chain(tk, expiry_window)
# 2️⃣ fetch recent prices & realised vol
price_ser = fetch_hist_prices(tk)
hv_20d = calc_hv(price_ser)
# 3️⃣ compute skew z-scores
chain_df = compute_skew(chain_df)
chain_df["iv_hv_ratio"] = chain_df["iv"] / hv_20d if np.isfinite(hv_20d) and hv_20d > 0 else np.nan
all_contracts.append(chain_df.assign(Ticker=tk))
# 4️⃣ tag signals
cfg = {
"skew_z": skew_z_thresh,
"iv_hv": iv_hv_thresh / 100,
"vol_mult": vol_mult_thresh,
"hv": hv_20d,
}
flagged = tag_signals(chain_df, cfg)
if not flagged.empty:
flagged["Ticker"] = tk
all_flags.append(flagged)
if not all_flags:
st.info("No contracts met your criteria. Try relaxing thresholds.")
else:
result_df = pd.concat(all_flags, ignore_index=True)
st.subheader("Flagged contracts")
st.dataframe(result_df, use_container_width=True)
full_df = pd.concat(all_contracts, ignore_index=True) # every contract scanned
full_df = full_df.dropna(subset=["iv", "iv_hv_ratio"])
# -------- plots ----------
with st.expander("Visualisations", expanded=False):
st.plotly_chart(plot_iv_skew(full_df), use_container_width=True)
st.caption(
"• **IV skew by strike** – Implied vol vs strike; colour = z-score "
"relative to its expiry/type mean. Extreme wings (dark red/blue) "
"signal skew opportunities."
)
st.plotly_chart(
plot_iv_hv_scatter(full_df, ratio_thresh=iv_hv_thresh / 100),
use_container_width=True,
)
st.caption(
f"• **IV / HV mispricing** – Each dot is an option. Dots right of the "
f"red line trade at ≥ {iv_hv_thresh}% of realised vol (rich); grey dots "
"are fair or cheap."
)
st.plotly_chart(plot_volume_spikes(result_df), use_container_width=True)
st.caption(
"• **Volume spike multiples** – Bars show today's volume relative to "
"a baseline (≈ OI/10). Tall bars flag unusual activity worth a closer look."
)
# -------- CSV download ----
csv = result_df.to_csv(index=False).encode()
st.download_button(
"Download CSV", csv, "signal_scanner_results.csv", "text/csv"
)