eshan6704 commited on
Commit
7856893
·
verified ·
1 Parent(s): 719aaa3

Update app/build_nse_fno.py

Browse files
Files changed (1) hide show
  1. app/build_nse_fno.py +10 -39
app/build_nse_fno.py CHANGED
@@ -1,4 +1,4 @@
1
- # build_nse_fno.py
2
  import os
3
  import subprocess
4
  import zipfile
@@ -6,16 +6,13 @@ import tempfile
6
  import pandas as pd
7
  from datetime import datetime as dt
8
 
9
- from .persist import exists, load, save
10
-
11
-
12
  NSE_FO_BASE = "https://archives.nseindia.com/content/fo"
13
 
14
 
15
  # ============================================================
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
  """
@@ -46,7 +43,6 @@ def _fetch_fo_bhavcopy(fo_date: str) -> pd.DataFrame:
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
 
@@ -54,7 +50,7 @@ def _fetch_fo_bhavcopy(fo_date: str) -> pd.DataFrame:
54
  # ============================================================
55
  # OPTION CHAIN BUILDER
56
  # ============================================================
57
- def _build_option_chain(df: pd.DataFrame) -> pd.DataFrame:
58
  rename = {
59
  "ClsPric": "close",
60
  "PrvsClsgPric": "pre",
@@ -92,40 +88,18 @@ def _build_option_chain(df: pd.DataFrame) -> pd.DataFrame:
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")
103
- cache_html = f"NSE_FNO_HTML_{date_key}_{symbol}"
104
- cache_df = f"NSE_FNO_BHAVCOPY_{date_key}"
105
-
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)
119
- else:
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
 
@@ -143,9 +117,8 @@ def build_nse_fno_html(fo_date: str, symbol: str) -> str:
143
  fut_df = df[df["FinInstrmTp"].isin(["STF", "IDF"])]
144
  opt_df = df[df["FinInstrmTp"].isin(["STO", "IDO"])]
145
 
146
- opt_chain = _build_option_chain(opt_df)
147
 
148
- # ================= HTML =================
149
  html = f"""
150
  <!DOCTYPE html>
151
  <html>
@@ -174,6 +147,4 @@ h2,h3,h4 {{ margin:6px 0; }}
174
  </html>
175
  """
176
 
177
- # ================= SAVE =================
178
- save(cache_html, html, "html")
179
  return html
 
1
+ # build_nse_fno_live.py
2
  import os
3
  import subprocess
4
  import zipfile
 
6
  import pandas as pd
7
  from datetime import datetime as dt
8
 
 
 
 
9
  NSE_FO_BASE = "https://archives.nseindia.com/content/fo"
10
 
11
 
12
  # ============================================================
13
  # FETCH FO BHAVCOPY (RAW DF)
14
  # ============================================================
15
+ def fetch_fo_bhavcopy(fo_date: str) -> pd.DataFrame:
16
  """
17
  fo_date format : DD-MM-YYYY
18
  """
 
43
  with zipfile.ZipFile(zip_path) as z:
44
  if file_name not in z.namelist():
45
  raise RuntimeError("FO bhavcopy csv missing inside zip")
 
46
  with z.open(file_name) as f:
47
  return pd.read_csv(f)
48
 
 
50
  # ============================================================
51
  # OPTION CHAIN BUILDER
52
  # ============================================================
53
+ def build_option_chain(df: pd.DataFrame) -> pd.DataFrame:
54
  rename = {
55
  "ClsPric": "close",
56
  "PrvsClsgPric": "pre",
 
88
 
89
 
90
  # ============================================================
91
+ # MAIN LIVE HTML BUILDER
92
  # ============================================================
93
+ def build_nse_fno_live_html(fo_date: str, symbol: str) -> str:
94
  """
95
+ Returns LIVE HTML for NSE F&O for the given date and symbol.
96
  """
97
 
98
+ fo_df = fetch_fo_bhavcopy(fo_date)
99
+ if fo_df.empty:
100
+ return "<h3>FO Bhavcopy empty</h3>"
 
 
 
 
 
 
 
101
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
  fo = fo_df.copy()
 
103
  exp = pd.to_datetime(fo["FininstrmActlXpryDt"], errors="coerce")
104
  today = pd.Timestamp.today().normalize()
105
 
 
117
  fut_df = df[df["FinInstrmTp"].isin(["STF", "IDF"])]
118
  opt_df = df[df["FinInstrmTp"].isin(["STO", "IDO"])]
119
 
120
+ opt_chain = build_option_chain(opt_df)
121
 
 
122
  html = f"""
123
  <!DOCTYPE html>
124
  <html>
 
147
  </html>
148
  """
149
 
 
 
150
  return html