Repository navigation
Expand file tree
/
Copy pathoptions_data.py
More file actions
252 lines (212 loc) · 8.32 KB
/
Copy pathoptions_data.py
File metadata and controls
252 lines (212 loc) · 8.32 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
"""
BTC Options market data from Deribit (free public API, no auth needed).
Provides leading indicators that spot/futures markets don't show:
- Put/Call ratio: crowd positioning (< 0.7 = bullish, > 1.0 = bearish/hedging)
- DVOL (Deribit Volatility Index): implied volatility expectation
- Max pain strike: price level where most options expire worthless
- IV skew: whether puts or calls are more expensive (fear vs greed)
- Large OI clusters: magnetic price levels
Options market often LEADS spot by 12-48 hours. The Feb 2026 crash was
signaled by 25-delta skew falling to -19.34 before BTC dropped from $90K to $60K.
"""
from __future__ import annotations
import logging
from collections import defaultdict
from datetime import datetime, timezone
import requests
logger = logging.getLogger(__name__)
DERIBIT_BASE = "https://www.deribit.com/api/v2/public"
REQUEST_TIMEOUT = 12
def _deribit_get(method: str, params: dict = None) -> dict | None:
try:
r = requests.get(
f"{DERIBIT_BASE}/{method}",
params=params or {},
timeout=REQUEST_TIMEOUT,
)
r.raise_for_status()
return r.json().get("result")
except Exception as exc:
logger.debug("Deribit %s failed: %s", method, exc)
return None
def get_options_data() -> dict:
"""
Fetch comprehensive BTC options market data.
All from Deribit public API — zero cost, no API key.
"""
result = {
"put_call_ratio": None,
"put_call_signal": None,
"total_call_oi_btc": None,
"total_put_oi_btc": None,
"dvol": None,
"dvol_signal": None,
"max_pain_strike": None,
"top_oi_strikes": [],
"index_price": None,
"max_pain_distance_pct": None,
"summary": "no options data",
}
# 1. Put/Call open interest ratio
book = _deribit_get("get_book_summary_by_currency",
{"currency": "BTC", "kind": "option"})
if book:
calls = [d for d in book if d.get("instrument_name", "").endswith("C")]
puts = [d for d in book if d.get("instrument_name", "").endswith("P")]
total_call_oi = sum(d.get("open_interest", 0) for d in calls)
total_put_oi = sum(d.get("open_interest", 0) for d in puts)
result["total_call_oi_btc"] = round(total_call_oi, 0)
result["total_put_oi_btc"] = round(total_put_oi, 0)
if total_call_oi > 0:
pcr = total_put_oi / total_call_oi
result["put_call_ratio"] = round(pcr, 3)
if pcr < 0.5:
result["put_call_signal"] = "very_bullish"
elif pcr < 0.7:
result["put_call_signal"] = "bullish"
elif pcr < 1.0:
result["put_call_signal"] = "neutral"
elif pcr < 1.3:
result["put_call_signal"] = "bearish"
else:
result["put_call_signal"] = "very_bearish"
# Max pain calculation (strike with highest total OI)
strike_oi = defaultdict(float)
for d in book:
name = d.get("instrument_name", "")
parts = name.split("-")
if len(parts) >= 3:
try:
strike = float(parts[2])
strike_oi[strike] += d.get("open_interest", 0)
except ValueError:
pass
if strike_oi:
max_pain = max(strike_oi.items(), key=lambda x: x[1])
result["max_pain_strike"] = max_pain[0]
top_strikes = sorted(strike_oi.items(), key=lambda x: -x[1])[:5]
result["top_oi_strikes"] = [
{"strike": s, "oi_btc": round(oi, 0)} for s, oi in top_strikes
]
# 2. DVOL (Deribit Volatility Index)
dvol_data = _deribit_get("get_volatility_index_data", {
"currency": "BTC",
"resolution": 3600,
"start_timestamp": 0,
"end_timestamp": 9999999999999,
})
if dvol_data and dvol_data.get("data"):
latest = dvol_data["data"][-1]
dvol = latest[4] # close value
result["dvol"] = round(dvol, 1)
if dvol < 30:
result["dvol_signal"] = "low_vol_calm"
elif dvol < 50:
result["dvol_signal"] = "moderate_vol"
elif dvol < 70:
result["dvol_signal"] = "high_vol"
else:
result["dvol_signal"] = "extreme_vol_fear"
# 3. Index price (for distance-to-max-pain calculation)
idx = _deribit_get("get_index_price", {"index_name": "btc_usd"})
if idx:
result["index_price"] = idx.get("index_price")
if result["max_pain_strike"] and result["index_price"]:
dist = (result["max_pain_strike"] / result["index_price"] - 1) * 100
result["max_pain_distance_pct"] = round(dist, 1)
# Build summary
parts = []
pcr = result["put_call_ratio"]
if pcr is not None:
parts.append(f"P/C={pcr:.2f} ({result['put_call_signal']})")
dvol = result["dvol"]
if dvol:
parts.append(f"DVOL={dvol:.0f} ({result['dvol_signal']})")
mp = result["max_pain_strike"]
if mp:
dist = result.get("max_pain_distance_pct", 0)
parts.append(f"MaxPain=${mp:,.0f} ({dist:+.1f}%)")
result["summary"] = ", ".join(parts) if parts else "no options data"
# Composite options flow signal (combines P/C + DVOL + max pain proximity)
score = 0 # -3 to +3 scale
pcr_val = result["put_call_ratio"]
if pcr_val is not None:
if pcr_val < 0.5:
score += 2
elif pcr_val < 0.7:
score += 1
elif pcr_val > 1.3:
score -= 2
elif pcr_val > 1.0:
score -= 1
dvol_val = result["dvol"]
if dvol_val:
if dvol_val > 60:
score -= 1 # extreme fear
elif dvol_val < 30:
score += 1 # calm, breakout pending
mp_dist = result.get("max_pain_distance_pct")
if mp_dist is not None:
if mp_dist > 5:
score += 1 # price below max pain → upward pull
elif mp_dist < -5:
score -= 1 # price above max pain → downward pull
if score >= 2:
result["composite_signal"] = "bullish"
elif score <= -2:
result["composite_signal"] = "bearish"
else:
result["composite_signal"] = "neutral"
result["composite_score"] = score
return result
def get_options_context() -> str:
"""Format options data for Claude's prompt."""
data = get_options_data()
if data["summary"] == "no options data":
return ""
lines = ["OPTIONS MARKET (Deribit — leading indicator):"]
pcr = data["put_call_ratio"]
if pcr is not None:
call_oi = data["total_call_oi_btc"]
put_oi = data["total_put_oi_btc"]
signal = data["put_call_signal"]
lines.append(
f" Put/Call OI: {pcr:.2f} ({signal}) — "
f"{call_oi:,.0f} BTC calls / {put_oi:,.0f} BTC puts"
)
dvol = data["dvol"]
if dvol:
lines.append(f" DVOL (IV): {dvol:.0f}% ({data['dvol_signal']})")
mp = data["max_pain_strike"]
if mp:
dist = data.get("max_pain_distance_pct", 0)
direction = "above" if dist > 0 else "below"
lines.append(
f" Max Pain: ${mp:,.0f} (price is {abs(dist):.1f}% {direction} max pain)"
)
strikes = data.get("top_oi_strikes", [])
if strikes:
strike_str = ", ".join(f"${s['strike']:,.0f}" for s in strikes[:3])
lines.append(f" Key OI levels: {strike_str}")
composite = data.get("composite_signal", "neutral")
cscore = data.get("composite_score", 0)
lines.append(f" COMPOSITE: {composite.upper()} (score {cscore:+d}/6)")
return "\n".join(lines)
def get_options_ml_features() -> dict:
"""Return flat dict of features for the ML model."""
data = get_options_data()
signal_map = {
"very_bullish": 2, "bullish": 1, "neutral": 0,
"bearish": -1, "very_bearish": -2,
}
vol_map = {
"low_vol_calm": -1, "moderate_vol": 0,
"high_vol": 1, "extreme_vol_fear": 2,
}
return {
"opt_put_call_ratio": data.get("put_call_ratio", 0) or 0,
"opt_pc_signal": signal_map.get(data.get("put_call_signal", ""), 0),
"opt_dvol": data.get("dvol", 0) or 0,
"opt_dvol_signal": vol_map.get(data.get("dvol_signal", ""), 0),
"opt_max_pain_dist_pct": data.get("max_pain_distance_pct", 0) or 0,
}