Spaces:
Sleeping
Sleeping
Delete nse.py
Browse files
nse.py
DELETED
|
@@ -1,204 +0,0 @@
|
|
| 1 |
-
import requests
|
| 2 |
-
import pandas as pd
|
| 3 |
-
from datetime import datetime
|
| 4 |
-
|
| 5 |
-
# ================================
|
| 6 |
-
# NSE Base Settings
|
| 7 |
-
# ================================
|
| 8 |
-
NSE_HEADERS = {
|
| 9 |
-
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
|
| 10 |
-
"(KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
|
| 11 |
-
"Accept-Language": "en-US,en;q=0.9",
|
| 12 |
-
}
|
| 13 |
-
|
| 14 |
-
# Persistent session
|
| 15 |
-
session = requests.Session()
|
| 16 |
-
session.get("https://www.nseindia.com", headers=NSE_HEADERS, timeout=5)
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
# ================================
|
| 20 |
-
# Helper: Convert DataFrame to HTML
|
| 21 |
-
# ================================
|
| 22 |
-
def _to_html(title, df):
|
| 23 |
-
if df is None or df.empty:
|
| 24 |
-
return f"<h3>{title}</h3><p>No data available</p>"
|
| 25 |
-
return f"<h3>{title}</h3>" + df.to_html(index=False, border=1)
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
# ================================
|
| 29 |
-
# Helper: Perform GET request
|
| 30 |
-
# ================================
|
| 31 |
-
def _get_json(url):
|
| 32 |
-
try:
|
| 33 |
-
r = session.get(url, headers=NSE_HEADERS, timeout=5)
|
| 34 |
-
r.raise_for_status()
|
| 35 |
-
return r.json()
|
| 36 |
-
except Exception as e:
|
| 37 |
-
return {"error": str(e)}
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
# ================================
|
| 41 |
-
# 1. Stock Quote
|
| 42 |
-
# ================================
|
| 43 |
-
def nse_stock(symbol):
|
| 44 |
-
url = f"https://www.nseindia.com/api/quote-equity?symbol={symbol.upper()}"
|
| 45 |
-
data = _get_json(url)
|
| 46 |
-
|
| 47 |
-
if "error" in data:
|
| 48 |
-
return f"<p>Error fetching stock: {data['error']}</p>"
|
| 49 |
-
|
| 50 |
-
info = pd.json_normalize(data.get("info", {}))
|
| 51 |
-
price = pd.json_normalize(data.get("priceInfo", {}))
|
| 52 |
-
meta = pd.json_normalize(data.get("metadata", {}))
|
| 53 |
-
|
| 54 |
-
html = ""
|
| 55 |
-
html += _to_html("Stock Info", info)
|
| 56 |
-
html += _to_html("Price Info", price)
|
| 57 |
-
html += _to_html("Metadata", meta)
|
| 58 |
-
return html
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
# ================================
|
| 62 |
-
# 2. F&O / Option Chain
|
| 63 |
-
# ================================
|
| 64 |
-
def nse_fno(symbol):
|
| 65 |
-
url = f"https://www.nseindia.com/api/option-chain-equities?symbol={symbol.upper()}"
|
| 66 |
-
data = _get_json(url)
|
| 67 |
-
|
| 68 |
-
if "error" in data:
|
| 69 |
-
return f"<p>Error fetching FNO: {data['error']}</p>"
|
| 70 |
-
|
| 71 |
-
records = data.get("records", {})
|
| 72 |
-
all_data = pd.DataFrame(records.get("data", []))
|
| 73 |
-
|
| 74 |
-
ce_list = [x["CE"] for x in records.get("data", []) if "CE" in x]
|
| 75 |
-
pe_list = [x["PE"] for x in records.get("data", []) if "PE" in x]
|
| 76 |
-
|
| 77 |
-
df_ce = pd.DataFrame(ce_list)
|
| 78 |
-
df_pe = pd.DataFrame(pe_list)
|
| 79 |
-
|
| 80 |
-
html = ""
|
| 81 |
-
html += _to_html("F&O Combined", all_data)
|
| 82 |
-
html += _to_html("CALL Options (CE)", df_ce)
|
| 83 |
-
html += _to_html("PUT Options (PE)", df_pe)
|
| 84 |
-
return html
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
# ================================
|
| 88 |
-
# 3. Futures Data
|
| 89 |
-
# ================================
|
| 90 |
-
def nse_future(symbol):
|
| 91 |
-
url = f"https://www.nseindia.com/api/quote-derivative?symbol={symbol.upper()}"
|
| 92 |
-
data = _get_json(url)
|
| 93 |
-
|
| 94 |
-
if "error" in data:
|
| 95 |
-
return f"<p>Error fetching futures: {data['error']}</p>"
|
| 96 |
-
|
| 97 |
-
futures = pd.DataFrame(data.get("stocks", []))
|
| 98 |
-
meta = pd.json_normalize(data.get("info", {}))
|
| 99 |
-
|
| 100 |
-
html = ""
|
| 101 |
-
html += _to_html("Futures Data", futures)
|
| 102 |
-
html += _to_html("Metadata", meta)
|
| 103 |
-
return html
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
# ================================
|
| 107 |
-
# 4. 52-Week High / Low
|
| 108 |
-
# ================================
|
| 109 |
-
def nse_high_low():
|
| 110 |
-
url = "https://www.nseindia.com/api/market-data-52Week"
|
| 111 |
-
data = _get_json(url)
|
| 112 |
-
|
| 113 |
-
if "error" in data:
|
| 114 |
-
return f"<p>Error fetching high-low: {data['error']}</p>"
|
| 115 |
-
|
| 116 |
-
high = pd.DataFrame(data.get("FiftyTwoWeekHigh", []))
|
| 117 |
-
low = pd.DataFrame(data.get("FiftyTwoWeekLow", []))
|
| 118 |
-
|
| 119 |
-
html = ""
|
| 120 |
-
html += _to_html("52 Week High", high)
|
| 121 |
-
html += _to_html("52 Week Low", low)
|
| 122 |
-
return html
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
# ================================
|
| 126 |
-
# 5. Bhavcopy (date format flexible)
|
| 127 |
-
# ================================
|
| 128 |
-
def nse_bhav(date_str):
|
| 129 |
-
"""
|
| 130 |
-
date_str: DD-MM-YYYY, DD/MM/YYYY, or DDMMYYYY
|
| 131 |
-
"""
|
| 132 |
-
try:
|
| 133 |
-
if "-" in date_str:
|
| 134 |
-
dt = datetime.strptime(date_str, "%d-%m-%Y")
|
| 135 |
-
elif "/" in date_str:
|
| 136 |
-
dt = datetime.strptime(date_str, "%d/%m/%Y")
|
| 137 |
-
elif len(date_str) == 8:
|
| 138 |
-
dt = datetime.strptime(date_str, "%d%m%Y")
|
| 139 |
-
else:
|
| 140 |
-
return f"<p>Invalid date format: {date_str}</p>"
|
| 141 |
-
except Exception as e:
|
| 142 |
-
return f"<p>Error parsing date: {e}</p>"
|
| 143 |
-
|
| 144 |
-
date_api = dt.strftime("%d-%m-%Y")
|
| 145 |
-
url = f"https://www.nseindia.com/api/reports?archives=true&date={date_api}&type=equities&mode=single"
|
| 146 |
-
data = _get_json(url)
|
| 147 |
-
|
| 148 |
-
if "error" in data:
|
| 149 |
-
return f"<p>Error fetching bhavcopy: {data['error']}</p>"
|
| 150 |
-
|
| 151 |
-
df = pd.DataFrame(data.get("data", []))
|
| 152 |
-
return _to_html(f"Bhavcopy {date_api}", df)
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
# ================================
|
| 156 |
-
# 6. All NSE Indices
|
| 157 |
-
# ================================
|
| 158 |
-
def nse_indices():
|
| 159 |
-
url = "https://www.nseindia.com/api/allIndices"
|
| 160 |
-
data = _get_json(url)
|
| 161 |
-
|
| 162 |
-
if "error" in data:
|
| 163 |
-
return f"<p>Error fetching indices: {data['error']}</p>"
|
| 164 |
-
|
| 165 |
-
df = pd.DataFrame(data.get("data", []))
|
| 166 |
-
return _to_html("All NSE Indices", df)
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
# ================================
|
| 170 |
-
# 7. NSE Open Market Data
|
| 171 |
-
# ================================
|
| 172 |
-
def nse_open(index_name="NIFTY 50"):
|
| 173 |
-
url = f"https://www.nseindia.com/api/equity-stockIndices?index={index_name.replace(' ', '%20')}"
|
| 174 |
-
data = _get_json(url)
|
| 175 |
-
|
| 176 |
-
if "error" in data:
|
| 177 |
-
return f"<p>Error fetching open data: {data['error']}</p>"
|
| 178 |
-
|
| 179 |
-
meta = pd.json_normalize(data.get("metadata", {}))
|
| 180 |
-
df = pd.DataFrame(data.get("data", []))
|
| 181 |
-
|
| 182 |
-
html = ""
|
| 183 |
-
html += _to_html("Index Metadata", meta)
|
| 184 |
-
html += _to_html("Index Open Data", df)
|
| 185 |
-
return html
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
# ================================
|
| 189 |
-
# 8. NSE Pre-Open Market Data
|
| 190 |
-
# ================================
|
| 191 |
-
def nse_preopen(index_name="NIFTY 50"):
|
| 192 |
-
url = "https://www.nseindia.com/api/market-data-pre-open?key=NIFTY"
|
| 193 |
-
data = _get_json(url)
|
| 194 |
-
|
| 195 |
-
if "error" in data:
|
| 196 |
-
return f"<p>Error fetching preopen: {data['error']}</p>"
|
| 197 |
-
|
| 198 |
-
df = pd.DataFrame(data.get("data", []))
|
| 199 |
-
meta = pd.json_normalize(data.get("metadata", {}))
|
| 200 |
-
|
| 201 |
-
html = ""
|
| 202 |
-
html += _to_html("Pre-Open Metadata", meta)
|
| 203 |
-
html += _to_html("Pre-Open Market Data", df)
|
| 204 |
-
return html
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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