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