eshan6704 commited on
Commit
11f39a0
·
verified ·
1 Parent(s): b706a05

Update daily.py

Browse files
Files changed (1) hide show
  1. daily.py +28 -19
daily.py CHANGED
@@ -3,30 +3,39 @@ 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, wrap_html
7
 
8
  def fetch_daily(symbol):
9
- """
10
- Fetch daily stock data, compute indicators, return full HTML
11
- """
12
  try:
13
- df = yf.download(symbol, period='6mo', interval='1d', auto_adjust=True)
14
- if isinstance(df.columns, pd.MultiIndex):
15
- df.columns = df.columns.get_level_values(0) # flatten multi-level headers
 
16
 
17
- df.reset_index(inplace=True)
18
- df.rename(columns={'Date':'Datetime'}, inplace=True)
19
- df.set_index('Datetime', inplace=True)
20
- if 'Volume' not in df.columns:
21
- df['Volume'] = 1
22
 
23
- # Calculate indicators
24
- indicators = calculate_indicators(df)
25
 
26
- # Build chart
27
- html_chart = build_chart(df, indicators=indicators)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
- return wrap_html(html_card(f"{symbol} Daily Chart", html_chart))
30
  except Exception as e:
31
- import traceback
32
- return wrap_html(html_card("Error", f"{e}<br><pre>{traceback.format_exc()}</pre>"))
 
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")