eshan6704 commited on
Commit
9b86c34
·
verified ·
1 Parent(s): 91633f0

Update daily.py

Browse files
Files changed (1) hide show
  1. daily.py +38 -29
daily.py CHANGED
@@ -3,39 +3,48 @@ import yfinance as yf
3
  import pandas as pd
4
  from indicater import calculate_indicators
5
  from chart_builder import build_chart
6
- from common import html_card, html_section, wrap_html, clean_df, make_table
7
 
8
  def fetch_daily(symbol):
9
  try:
10
- # --- Fetch data from Yahoo Finance ---
11
- data = yf.download(symbol + ".NS", period="6mo", interval="1d", progress=False)
12
- if isinstance(data.columns, pd.MultiIndex):
13
- data.columns = data.columns.get_level_values(0) # flatten multi-level header
14
-
15
- # Ensure OHLCV columns
16
- for col in ['Open', 'High', 'Low', 'Close', 'Volume']:
17
- if col not in data.columns:
18
- data[col] = 0
19
-
20
- data = clean_df(data)
21
-
22
- # --- Calculate all indicators ---
23
- indicators = calculate_indicators(data)
24
-
25
- # --- Build Plotly chart ---
26
- chart_html = build_chart(data, indicators)
27
-
28
- # --- Build data table for the same DataFrame + indicators ---
29
- df_table = data.copy()
30
- for ind_name, ind_series in indicators.items():
31
- df_table[ind_name] = ind_series
32
-
33
- table_html = make_table(df_table)
34
-
35
- # --- Wrap chart + table in card ---
36
- content = html_card(f"{symbol} Daily Chart", chart_html + table_html)
 
 
 
 
 
 
 
 
 
37
 
38
  return wrap_html(content, title=f"{symbol} Daily Data")
39
 
40
  except Exception as e:
41
- return wrap_html(html_section("Error", str(e)), title="Error")
 
3
  import pandas as pd
4
  from indicater import calculate_indicators
5
  from chart_builder import build_chart
6
+ from common import html_card, make_table, wrap_html
7
 
8
  def fetch_daily(symbol):
9
  try:
10
+ # --- Fetch historical data ---
11
+ df = yf.download(symbol + ".NS", period="6mo", interval="1d")
12
+ if df.empty:
13
+ return html_card("Error", f"No daily data found for {symbol}")
14
+ df.reset_index(inplace=True)
15
+
16
+ # --- Calculate indicators ---
17
+ indicators = calculate_indicators(df)
18
+
19
+ # --- Build chart with injected checkbox script ---
20
+ chart_html = build_chart(df, indicators)
21
+
22
+ # --- Format table ---
23
+ table_html = make_table(df)
24
+
25
+ # --- Combine everything in HTML ---
26
+ content = f"""
27
+ <h2>{symbol} - Daily Data</h2>
28
+
29
+ <div>
30
+ <b>Select Indicators to Display:</b><br>
31
+ <input type="checkbox" class="indicator-toggle" data-trace="1" checked> SMA20
32
+ <input type="checkbox" class="indicator-toggle" data-trace="2" checked> SMA50
33
+ <input type="checkbox" class="indicator-toggle" data-trace="3"> EMA20
34
+ <input type="checkbox" class="indicator-toggle" data-trace="4"> EMA50
35
+ <input type="checkbox" class="indicator-toggle" data-trace="5"> MACD
36
+ <input type="checkbox" class="indicator-toggle" data-trace="6"> RSI
37
+ <input type="checkbox" class="indicator-toggle" data-trace="7"> Stochastic
38
+ <button onclick="applyIndicators()">Apply</button>
39
+ </div>
40
+ <br>
41
+
42
+ {chart_html}
43
+ <br>
44
+ {html_card("Data Table", table_html)}
45
+ """
46
 
47
  return wrap_html(content, title=f"{symbol} Daily Data")
48
 
49
  except Exception as e:
50
+ return html_card("Error", str(e))