Spaces:
Running
Running
Update app/build_nse_fno.py
Browse files- app/build_nse_fno.py +19 -6
app/build_nse_fno.py
CHANGED
|
@@ -16,6 +16,9 @@ NSE_FO_BASE = "https://archives.nseindia.com/content/fo"
|
|
| 16 |
# FETCH FO BHAVCOPY (RAW DF)
|
| 17 |
# ============================================================
|
| 18 |
def _fetch_fo_bhavcopy(fo_date: str) -> pd.DataFrame:
|
|
|
|
|
|
|
|
|
|
| 19 |
date = dt.strptime(fo_date, "%d-%m-%Y").date()
|
| 20 |
ymd = date.strftime("%Y%m%d")
|
| 21 |
|
|
@@ -41,6 +44,9 @@ def _fetch_fo_bhavcopy(fo_date: str) -> pd.DataFrame:
|
|
| 41 |
raise RuntimeError("FO bhavcopy download failed")
|
| 42 |
|
| 43 |
with zipfile.ZipFile(zip_path) as z:
|
|
|
|
|
|
|
|
|
|
| 44 |
with z.open(file_name) as f:
|
| 45 |
return pd.read_csv(f)
|
| 46 |
|
|
@@ -86,13 +92,11 @@ def _build_option_chain(df: pd.DataFrame) -> pd.DataFrame:
|
|
| 86 |
|
| 87 |
|
| 88 |
# ============================================================
|
| 89 |
-
# MAIN HTML BUILDER
|
| 90 |
# ============================================================
|
| 91 |
def build_nse_fno_html(fo_date: str, symbol: str) -> str:
|
| 92 |
"""
|
| 93 |
-
|
| 94 |
-
- Bhavcopy cached per DATE
|
| 95 |
-
- HTML cached per DATE + SYMBOL
|
| 96 |
"""
|
| 97 |
|
| 98 |
date_key = dt.strptime(fo_date, "%d-%m-%Y").strftime("%Y%m%d")
|
|
@@ -102,9 +106,13 @@ def build_nse_fno_html(fo_date: str, symbol: str) -> str:
|
|
| 102 |
# ================= HTML CACHE =================
|
| 103 |
if exists(cache_html, "html"):
|
| 104 |
cached = load(cache_html, "html")
|
|
|
|
| 105 |
if isinstance(cached, str):
|
| 106 |
return cached
|
| 107 |
|
|
|
|
|
|
|
|
|
|
| 108 |
# ================= BHAVCOPY CACHE =================
|
| 109 |
if exists(cache_df):
|
| 110 |
fo_df = load(cache_df)
|
|
@@ -112,8 +120,12 @@ def build_nse_fno_html(fo_date: str, symbol: str) -> str:
|
|
| 112 |
fo_df = _fetch_fo_bhavcopy(fo_date)
|
| 113 |
save(cache_df, fo_df)
|
| 114 |
|
|
|
|
|
|
|
|
|
|
| 115 |
# ================= PROCESS =================
|
| 116 |
fo = fo_df.copy()
|
|
|
|
| 117 |
exp = pd.to_datetime(fo["FininstrmActlXpryDt"], errors="coerce")
|
| 118 |
today = pd.Timestamp.today().normalize()
|
| 119 |
|
|
@@ -144,6 +156,7 @@ body {{ font-family: Arial; margin: 12px; background:#f5f5f5; }}
|
|
| 144 |
table {{ border-collapse: collapse; width: 100%; background:white; }}
|
| 145 |
th, td {{ border:1px solid #bbb; padding:6px; text-align:center; }}
|
| 146 |
th {{ background:#2e7d32; color:white; }}
|
|
|
|
| 147 |
</style>
|
| 148 |
</head>
|
| 149 |
<body>
|
|
@@ -152,10 +165,10 @@ th {{ background:#2e7d32; color:white; }}
|
|
| 152 |
<h4>Expiry: {expiry}</h4>
|
| 153 |
|
| 154 |
<h3>Futures</h3>
|
| 155 |
-
{fut_df.to_html(index=False)}
|
| 156 |
|
| 157 |
<h3>Option Chain</h3>
|
| 158 |
-
{opt_chain.to_html(index=False)}
|
| 159 |
|
| 160 |
</body>
|
| 161 |
</html>
|
|
|
|
| 16 |
# FETCH FO BHAVCOPY (RAW DF)
|
| 17 |
# ============================================================
|
| 18 |
def _fetch_fo_bhavcopy(fo_date: str) -> pd.DataFrame:
|
| 19 |
+
"""
|
| 20 |
+
fo_date format : DD-MM-YYYY
|
| 21 |
+
"""
|
| 22 |
date = dt.strptime(fo_date, "%d-%m-%Y").date()
|
| 23 |
ymd = date.strftime("%Y%m%d")
|
| 24 |
|
|
|
|
| 44 |
raise RuntimeError("FO bhavcopy download failed")
|
| 45 |
|
| 46 |
with zipfile.ZipFile(zip_path) as z:
|
| 47 |
+
if file_name not in z.namelist():
|
| 48 |
+
raise RuntimeError("FO bhavcopy csv missing inside zip")
|
| 49 |
+
|
| 50 |
with z.open(file_name) as f:
|
| 51 |
return pd.read_csv(f)
|
| 52 |
|
|
|
|
| 92 |
|
| 93 |
|
| 94 |
# ============================================================
|
| 95 |
+
# MAIN HTML BUILDER
|
| 96 |
# ============================================================
|
| 97 |
def build_nse_fno_html(fo_date: str, symbol: str) -> str:
|
| 98 |
"""
|
| 99 |
+
Returns HTML ONLY
|
|
|
|
|
|
|
| 100 |
"""
|
| 101 |
|
| 102 |
date_key = dt.strptime(fo_date, "%d-%m-%Y").strftime("%Y%m%d")
|
|
|
|
| 106 |
# ================= HTML CACHE =================
|
| 107 |
if exists(cache_html, "html"):
|
| 108 |
cached = load(cache_html, "html")
|
| 109 |
+
|
| 110 |
if isinstance(cached, str):
|
| 111 |
return cached
|
| 112 |
|
| 113 |
+
if isinstance(cached, pd.DataFrame):
|
| 114 |
+
return cached.to_html(index=False)
|
| 115 |
+
|
| 116 |
# ================= BHAVCOPY CACHE =================
|
| 117 |
if exists(cache_df):
|
| 118 |
fo_df = load(cache_df)
|
|
|
|
| 120 |
fo_df = _fetch_fo_bhavcopy(fo_date)
|
| 121 |
save(cache_df, fo_df)
|
| 122 |
|
| 123 |
+
if not isinstance(fo_df, pd.DataFrame) or fo_df.empty:
|
| 124 |
+
return "<h3>Invalid FO Bhavcopy</h3>"
|
| 125 |
+
|
| 126 |
# ================= PROCESS =================
|
| 127 |
fo = fo_df.copy()
|
| 128 |
+
|
| 129 |
exp = pd.to_datetime(fo["FininstrmActlXpryDt"], errors="coerce")
|
| 130 |
today = pd.Timestamp.today().normalize()
|
| 131 |
|
|
|
|
| 156 |
table {{ border-collapse: collapse; width: 100%; background:white; }}
|
| 157 |
th, td {{ border:1px solid #bbb; padding:6px; text-align:center; }}
|
| 158 |
th {{ background:#2e7d32; color:white; }}
|
| 159 |
+
h2,h3,h4 {{ margin:6px 0; }}
|
| 160 |
</style>
|
| 161 |
</head>
|
| 162 |
<body>
|
|
|
|
| 165 |
<h4>Expiry: {expiry}</h4>
|
| 166 |
|
| 167 |
<h3>Futures</h3>
|
| 168 |
+
{fut_df.to_html(index=False) if not fut_df.empty else "<i>No Futures</i>"}
|
| 169 |
|
| 170 |
<h3>Option Chain</h3>
|
| 171 |
+
{opt_chain.to_html(index=False) if not opt_chain.empty else "<i>No Options</i>"}
|
| 172 |
|
| 173 |
</body>
|
| 174 |
</html>
|