This repository was archived by the owner on Jun 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
268 lines (208 loc) · 8.55 KB
/
Copy pathmain.py
File metadata and controls
268 lines (208 loc) · 8.55 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
#!/usr/bin/env python3
"""
OptionChain Example Implementation
This script demonstrates how to use the OptionChain package to analyze
NSE India option chain data. It shows various usage patterns and features
of the library.
Usage:
python main.py
or
uv run python main.py
"""
import logging
from src.optionchain import OptionChainAnalyzer
def setup_logging() -> None:
"""Configure logging for the example."""
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s",
datefmt="%H:%M:%S",
)
def interactive_analysis() -> None:
"""Run interactive analysis with user input."""
print("🔍 OptionChain Interactive Analysis")
print("=" * 40)
# Get user input for security
while True:
security = (
input("\nEnter NSE symbol (e.g., NIFTY, BANKNIFTY): ").strip().upper()
)
if security:
break
print("❌ Please enter a valid symbol.")
# Get OI cutoff preference
try:
oi_cutoff = input("Enter minimum Open Interest cutoff (default: 100): ").strip()
oi_cutoff = int(oi_cutoff) if oi_cutoff else 100
except ValueError:
oi_cutoff = 100
print(f"⚠️ Using default OI cutoff: {oi_cutoff}")
# Initialize analyzer and fetch data
print(f"\n📊 Analyzing {security} with OI cutoff: {oi_cutoff}")
analyzer = OptionChainAnalyzer()
try:
option_chain = analyzer.analyze_symbol(security, oi_cutoff=oi_cutoff)
if not option_chain:
print(f"❌ Failed to fetch data for {security}")
return
# Display summary
display_summary(analyzer)
# Ask user what visualizations they want
show_visualizations(analyzer)
# Optionally show raw data
if input("\nShow first 10 rows of data? (y/N): ").lower().startswith("y"):
print("\n📋 Option Chain Data (First 10 rows):")
print(option_chain.data.head(10).to_string(index=False))
except Exception as e:
print(f"❌ Error during analysis: {e}")
logging.error(f"Analysis failed: {e}")
def display_summary(analyzer: OptionChainAnalyzer) -> None:
"""Display summary statistics."""
summary = analyzer.get_summary()
print("\n" + "=" * 50)
print("📈 OPTION CHAIN SUMMARY")
print("=" * 50)
for key, value in summary.items():
if isinstance(value, int | float) and key.endswith("oi"):
# Format large numbers with commas
print(f"{key.replace('_', ' ').title():.<30} {value:,}")
elif isinstance(value, float):
print(f"{key.replace('_', ' ').title():.<30} {value:.2f}")
else:
print(f"{key.replace('_', ' ').title():.<30} {value}")
print("=" * 50)
def show_visualizations(analyzer: OptionChainAnalyzer) -> None:
"""Show various visualizations based on user preference."""
print("\n🎨 Available Visualizations:")
print("1. Implied Volatility Skew")
print("2. Term Structure Analysis")
print("3. Open Interest Distribution")
print("4. All visualizations")
print("5. Skip visualizations")
choice = input("\nSelect option (1-5): ").strip()
if choice == "1":
print("📊 Generating Volatility Skew...")
analyzer.plot_volatility_skew()
elif choice == "2":
print("📊 Generating Term Structure...")
analyzer.plot_term_structure()
elif choice == "3":
print("📊 Generating Open Interest Analysis...")
analyzer.plot_open_interest_analysis()
elif choice == "4":
print("📊 Generating all visualizations...")
analyzer.plot_volatility_skew()
analyzer.plot_term_structure()
analyzer.plot_open_interest_analysis()
else:
print("⏭️ Skipping visualizations")
def programmatic_example() -> None:
"""Example of programmatic usage without user interaction."""
print("\n🤖 Programmatic Analysis Example")
print("=" * 40)
# Example with NIFTY
analyzer = OptionChainAnalyzer()
print("📊 Fetching NIFTY option chain...")
option_chain = analyzer.analyze_symbol("NIFTY", oi_cutoff=500)
if option_chain:
# Get summary
summary = analyzer.get_summary()
print("✅ Successfully analyzed NIFTY")
print(f" Underlying Price: ₹{summary['underlying_price']:.2f}")
print(f" ATM Strike: ₹{summary['atm_strike']:.2f}")
print(f" Total Call OI: {summary['total_call_oi']:,}")
print(f" Total Put OI: {summary['total_put_oi']:,}")
# Example of custom analysis
print("\n🔍 Custom Analysis:")
# Find strikes with highest OI
data = option_chain.data
max_call_oi_strike = data.loc[data["Call OI"].idxmax(), "Strike"]
max_put_oi_strike = data.loc[data["Put OI"].idxmax(), "Strike"]
print(f" Highest Call OI: ₹{max_call_oi_strike:.0f}")
print(f" Highest Put OI: ₹{max_put_oi_strike:.0f}")
# Calculate Put-Call ratio
total_call_oi = summary["total_call_oi"]
total_put_oi = summary["total_put_oi"]
pcr = total_put_oi / total_call_oi if total_call_oi > 0 else 0
print(f" Put-Call Ratio: {pcr:.2f}")
# Market sentiment based on PCR
if pcr > 1.2:
sentiment = "🐻 Bearish"
elif pcr < 0.8:
sentiment = "🐂 Bullish"
else:
sentiment = "🦆 Neutral"
print(f" Market Sentiment: {sentiment}")
else:
print("❌ Failed to fetch NIFTY data")
def advanced_example() -> None:
"""Advanced usage examples."""
print("\n🚀 Advanced Usage Examples")
print("=" * 40)
from src.optionchain.data_fetcher import NSEOptionFetcher
from src.optionchain.visualization import OptionChainVisualizer
# Custom fetcher with longer timeout
print("🔧 Using custom data fetcher...")
fetcher = NSEOptionFetcher(timeout=60, max_retries=5)
option_chain = fetcher.fetch_option_chain("BANKNIFTY")
if option_chain:
print(f"✅ Fetched BANKNIFTY data: {len(option_chain.data)} records")
# Custom filtering
high_oi_chain = option_chain.filter_by_oi(cutoff=1000)
print(f"🔍 After high OI filter: {len(high_oi_chain.data)} records")
# Group analysis
expiry_groups = option_chain.group_by_expiry(cutoff=500)
print(f"📅 Available expiries: {len(expiry_groups)}")
for i, (expiry_idx, data) in enumerate(expiry_groups.items()):
if i < 3: # Show first 3 expiries
print(f" Expiry {expiry_idx}: {len(data)} strikes")
# Custom visualization
visualizer = OptionChainVisualizer(figsize=(20, 12))
print("🎨 Creating custom visualization (if requested)...")
if input("Generate custom volatility skew? (y/N): ").lower().startswith("y"):
visualizer.plot_volatility_skew(
expiry_data=expiry_groups,
expiry_index=0,
underlying_price=option_chain.underlying_price,
title="BANKNIFTY Custom Volatility Skew",
)
def main() -> None:
"""Main function with example menu."""
setup_logging()
print("🔗 OptionChain Analysis Tool")
print("NSE India Option Chain Data Analysis")
print("=" * 40)
while True:
print("\n📋 Select an example:")
print("1. Interactive Analysis (recommended)")
print("2. Programmatic Example")
print("3. Advanced Usage Examples")
print("4. Exit")
choice = input("\nEnter your choice (1-4): ").strip()
try:
if choice == "1":
interactive_analysis()
elif choice == "2":
programmatic_example()
elif choice == "3":
advanced_example()
elif choice == "4":
print("👋 Thank you for using OptionChain!")
break
else:
print("❌ Invalid choice. Please select 1-4.")
continue
# Ask if user wants to continue
if input("\nRun another example? (Y/n): ").lower().startswith("n"):
print("👋 Thank you for using OptionChain!")
break
except KeyboardInterrupt:
print("\n\n👋 Interrupted by user. Goodbye!")
break
except Exception as e:
print(f"❌ An error occurred: {e}")
logging.error(f"Unexpected error: {e}")
if input("Continue with other examples? (Y/n): ").lower().startswith("n"):
break
if __name__ == "__main__":
main()