-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_helpers.py
More file actions
165 lines (150 loc) · 4.72 KB
/
plot_helpers.py
File metadata and controls
165 lines (150 loc) · 4.72 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
# This file will contain methods for modular Plotly charts for Greeks/surfaces/overlays - namely, 3D surfaces and scatter plots,
# heatmaps, and time series data.
import plotly.graph_objects as go
import plotly.express as px
import pandas as pd
import numpy as np
def plot_greeks_surface(
x: np.ndarray, y: np.ndarray, z: np.ndarray,
x_label: str, y_label: str, z_label: str
) -> go.Figure:
fig = go.Figure(data=[go.Surface(z=z, x=x, y=y)])
fig.update_layout(
title=f'{z_label} Surface',
scene=dict(
xaxis_title=x_label,
yaxis_title=y_label,
zaxis_title=z_label
),
autosize=True
)
return fig
def plot_strategy_payoff(payoff_df: pd.DataFrame):
import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(go.Scatter(
x=payoff_df["Spot"],
y=payoff_df["Net P&L"],
mode="lines",
fill="tozeroy",
name="Payoff"
))
fig.update_layout(
title="Payoff at Expiration",
xaxis_title="Spot Price at Expiry",
yaxis_title="Net Profit / Loss",
template="plotly_dark",
height=500
)
return fig
def _contract_code(row) -> str:
return f"{row['dte']}d {row['type'][0].upper()} {row['strike']}"
def plot_iv_skew(df: pd.DataFrame) -> go.Figure:
"""
IV vs strike scatter, colour = skew-z. Legend (Call / Put) is moved to
the top-left and the colour-bar is nudged right to avoid overlap.
"""
fig = go.Figure()
for opt_type, colr in [("call", "royalblue"), ("put", "firebrick")]:
sub = df[df["type"] == opt_type]
if sub.empty:
continue
# only *one* trace gets the colour-bar so we don't have two bars
showscale = opt_type == "call"
fig.add_trace(
go.Scatter(
x=sub["strike"],
y=sub["iv"],
mode="markers",
marker=dict(
size=8,
color=sub["skew_z"],
colorscale="RdBu",
showscale=showscale,
colorbar=dict(
title="Skew z",
x=1.05, # push bar to the right
len=0.85,
),
),
name=opt_type.capitalize(),
text=sub.apply(
lambda r: f"{r['dte']}d {r['type'][0].upper()} {r['strike']}", axis=1
),
hovertemplate=(
"Strike: %{x}<br>IV: %{y:.2%}<br>Skew z: %{marker.color:.2f}<br>%{text}"
),
showlegend=True,
)
)
fig.update_layout(
title="IV skew by strike",
xaxis_title="Strike",
yaxis_title="IV",
legend=dict(
orientation="h",
yanchor="bottom",
y=1.08,
xanchor="left",
x=0,
),
margin=dict(r=90), # leave room for colour-bar
)
return fig
def plot_iv_hv_scatter(df: pd.DataFrame, ratio_thresh: float = 1.2) -> go.Figure:
"""
Scatter of IV/HV ratio (x) vs IV (y).
Points to the right of the red line are overpriced ≥ ratio_thresh.
"""
import numpy as np
import plotly.graph_objects as go
if df.empty:
return go.Figure()
# Use a colour map: grey if cheap/fair, orange if rich
colours = np.where(df["iv_hv_ratio"] >= ratio_thresh, "orange", "lightgrey")
fig = go.Figure(
data=go.Scatter(
x=df["iv_hv_ratio"],
y=df["iv"],
mode="markers",
marker=dict(size=8, color=colours),
text=df.apply(
lambda r: f"{r['dte']}d {r['type'][0].upper()} {r['strike']}", axis=1
),
hovertemplate="IV/HV: %{x:.2f}<br>IV: %{y:.2%}<br>%{text}",
)
)
# vertical reference = threshold
fig.add_shape(
type="line",
x0=ratio_thresh,
x1=ratio_thresh,
y0=0,
y1=max(df["iv"]) * 1.05,
line=dict(color="red", dash="dot"),
)
fig.update_layout(
title="IV / HV mispricing",
xaxis_title="IV ÷ HV (ratio)",
yaxis_title="IV",
showlegend=False,
)
return fig
def plot_volume_spikes(df: pd.DataFrame) -> go.Figure:
"""
Bar chart of volume multiples for flagged contracts.
"""
fig = go.Figure(
data=go.Bar(
x=df.apply(_contract_code, axis=1),
y=df["vol_mult"],
marker_color="seagreen",
hovertemplate="Vol mult: %{y:.1f}<br>%{x}",
)
)
fig.update_layout(
title="Volume spike multiples",
xaxis_title="Contract",
yaxis_title="Volume / baseline",
)
return fig