eshan6704 commited on
Commit
71e0e89
·
verified ·
1 Parent(s): 9fef9dc

Update app/daily.py

Browse files
Files changed (1) hide show
  1. app/daily.py +20 -20
app/daily.py CHANGED
@@ -6,7 +6,7 @@ import traceback
6
  from . import persist
7
 
8
  # ============================================================
9
- # DAILY DATA FETCH (DO NOT CHANGE)
10
  # ============================================================
11
  def daily(symbol, date_end, date_start):
12
  start = dt.strptime(date_start, "%d-%m-%Y").strftime("%Y-%m-%d")
@@ -14,7 +14,6 @@ def daily(symbol, date_end, date_start):
14
 
15
  df = yf.download(symbol + ".NS", start=start, end=end)
16
 
17
- # Flatten multi-index columns
18
  if isinstance(df.columns, pd.MultiIndex):
19
  df.columns = df.columns.get_level_values(0)
20
 
@@ -24,7 +23,7 @@ def daily(symbol, date_end, date_start):
24
 
25
 
26
  # ============================================================
27
- # DASHBOARD
28
  # ============================================================
29
  def fetch_daily(symbol, date_end, date_start):
30
  key = f"daily_{symbol}"
@@ -37,13 +36,12 @@ def fetch_daily(symbol, date_end, date_start):
37
  try:
38
  df = daily(symbol, date_end, date_start)
39
  if df is None or df.empty:
40
- return "<h1>No data</h1>"
41
 
42
  # -------------------------------
43
- # CLEAN & FORMAT
44
  # -------------------------------
45
  df = df.reset_index()
46
-
47
  df["Date"] = pd.to_datetime(df["Date"], errors="coerce")
48
  df = df.dropna(subset=["Date"])
49
 
@@ -51,19 +49,19 @@ def fetch_daily(symbol, date_end, date_start):
51
  df[c] = pd.to_numeric(df[c], errors="coerce")
52
 
53
  df = df.dropna()
54
- df["DateStr"] = df["Date"].dt.strftime("%d-%b-%Y")
55
 
 
56
  df["MA20"] = df["Close"].rolling(20).mean()
57
  df["MA50"] = df["Close"].rolling(50).mean()
58
 
59
  # -------------------------------
60
- # TABLE HTML
61
  # -------------------------------
62
- table_rows = ""
63
  for i, r in df.iterrows():
64
- color = "#e8f5e9" if i % 2 == 0 else "#f5f5f5"
65
- table_rows += f"""
66
- <tr style="background:{color}">
67
  <td>{r['DateStr']}</td>
68
  <td>{r['Open']:.2f}</td>
69
  <td>{r['High']:.2f}</td>
@@ -82,13 +80,13 @@ def fetch_daily(symbol, date_end, date_start):
82
  <th>Low</th><th>Close</th><th>Volume</th>
83
  </tr>
84
  </thead>
85
- <tbody>{table_rows}</tbody>
86
  </table>
87
  </div>
88
  """
89
 
90
  # -------------------------------
91
- # FULL HTML (IMPORTANT)
92
  # -------------------------------
93
  html = f"""
94
  <!DOCTYPE html>
@@ -102,8 +100,8 @@ def fetch_daily(symbol, date_end, date_start):
102
  <style>
103
  body {{
104
  font-family: Arial, sans-serif;
105
- margin: 10px;
106
  background: #f4f6f9;
 
107
  }}
108
 
109
  .table-wrap {{
@@ -113,8 +111,8 @@ body {{
113
  }}
114
 
115
  table {{
116
- border-collapse: collapse;
117
  width: 100%;
 
118
  background: white;
119
  }}
120
 
@@ -134,6 +132,7 @@ td {{
134
  td:first-child {{
135
  text-align: left;
136
  }}
 
137
  .chart {{
138
  margin-bottom: 30px;
139
  }}
@@ -142,7 +141,7 @@ td:first-child {{
142
 
143
  <body>
144
 
145
- <h2>{symbol} – Daily Price Table</h2>
146
  {table_html}
147
 
148
  <h2>Candlestick & Volume</h2>
@@ -158,8 +157,9 @@ const highp = {df["High"].round(2).tolist()};
158
  const lowp = {df["Low"].round(2).tolist()};
159
  const closep= {df["Close"].round(2).tolist()};
160
  const volume= {df["Volume"].astype(int).tolist()};
161
- const ma20 = {df["MA20"].round(2).fillna(None).tolist()};
162
- const ma50 = {df["MA50"].round(2).fillna(None).tolist()};
 
163
 
164
  Plotly.newPlot("candle", [
165
  {{
@@ -199,5 +199,5 @@ Plotly.newPlot("ma", [
199
  persist.save(key, html, "html")
200
  return html
201
 
202
- except Exception as e:
203
  return f"<pre>{traceback.format_exc()}</pre>"
 
6
  from . import persist
7
 
8
  # ============================================================
9
+ # DAILY DATA FETCH (FINALIZED)
10
  # ============================================================
11
  def daily(symbol, date_end, date_start):
12
  start = dt.strptime(date_start, "%d-%m-%Y").strftime("%Y-%m-%d")
 
14
 
15
  df = yf.download(symbol + ".NS", start=start, end=end)
16
 
 
17
  if isinstance(df.columns, pd.MultiIndex):
18
  df.columns = df.columns.get_level_values(0)
19
 
 
23
 
24
 
25
  # ============================================================
26
+ # DAILY DASHBOARD (FULL HTML)
27
  # ============================================================
28
  def fetch_daily(symbol, date_end, date_start):
29
  key = f"daily_{symbol}"
 
36
  try:
37
  df = daily(symbol, date_end, date_start)
38
  if df is None or df.empty:
39
+ return "<h1>No daily data</h1>"
40
 
41
  # -------------------------------
42
+ # CLEAN DATA
43
  # -------------------------------
44
  df = df.reset_index()
 
45
  df["Date"] = pd.to_datetime(df["Date"], errors="coerce")
46
  df = df.dropna(subset=["Date"])
47
 
 
49
  df[c] = pd.to_numeric(df[c], errors="coerce")
50
 
51
  df = df.dropna()
 
52
 
53
+ df["DateStr"] = df["Date"].dt.strftime("%d-%b-%Y")
54
  df["MA20"] = df["Close"].rolling(20).mean()
55
  df["MA50"] = df["Close"].rolling(50).mean()
56
 
57
  # -------------------------------
58
+ # HTML TABLE
59
  # -------------------------------
60
+ rows = ""
61
  for i, r in df.iterrows():
62
+ bg = "#eef6ff" if i % 2 == 0 else "#ffffff"
63
+ rows += f"""
64
+ <tr style="background:{bg}">
65
  <td>{r['DateStr']}</td>
66
  <td>{r['Open']:.2f}</td>
67
  <td>{r['High']:.2f}</td>
 
80
  <th>Low</th><th>Close</th><th>Volume</th>
81
  </tr>
82
  </thead>
83
+ <tbody>{rows}</tbody>
84
  </table>
85
  </div>
86
  """
87
 
88
  # -------------------------------
89
+ # FULL HTML OUTPUT
90
  # -------------------------------
91
  html = f"""
92
  <!DOCTYPE html>
 
100
  <style>
101
  body {{
102
  font-family: Arial, sans-serif;
 
103
  background: #f4f6f9;
104
+ margin: 10px;
105
  }}
106
 
107
  .table-wrap {{
 
111
  }}
112
 
113
  table {{
 
114
  width: 100%;
115
+ border-collapse: collapse;
116
  background: white;
117
  }}
118
 
 
132
  td:first-child {{
133
  text-align: left;
134
  }}
135
+
136
  .chart {{
137
  margin-bottom: 30px;
138
  }}
 
141
 
142
  <body>
143
 
144
+ <h2>{symbol} – Daily Data</h2>
145
  {table_html}
146
 
147
  <h2>Candlestick & Volume</h2>
 
157
  const lowp = {df["Low"].round(2).tolist()};
158
  const closep= {df["Close"].round(2).tolist()};
159
  const volume= {df["Volume"].astype(int).tolist()};
160
+
161
+ const ma20 = {df["MA20"].round(2).where(pd.notna(df["MA20"]), None).tolist()};
162
+ const ma50 = {df["MA50"].round(2).where(pd.notna(df["MA50"]), None).tolist()};
163
 
164
  Plotly.newPlot("candle", [
165
  {{
 
199
  persist.save(key, html, "html")
200
  return html
201
 
202
+ except Exception:
203
  return f"<pre>{traceback.format_exc()}</pre>"