eshan6704 commited on
Commit
d01fabf
·
verified ·
1 Parent(s): 5335506

Update daily.py

Browse files
Files changed (1) hide show
  1. daily.py +21 -16
daily.py CHANGED
@@ -1,37 +1,42 @@
1
  # daily.py
2
  import pandas as pd
3
  import yfinance as yf
4
- from common import wrap_html, html_error, clean_df, make_table
5
  from indicater import calculate_indicators
6
  from chart_builder import build_chart
7
 
8
  def fetch_daily(symbol):
9
  """
10
- Fetches daily OHLCV data, calculates indicators, builds chart,
11
- and returns the complete HTML for Gradio UI.
12
  """
13
  try:
14
- yfsymbol = symbol + ".NS"
15
-
16
- # ---------------- Fetch daily data ----------------
17
- df = yf.download(yfsymbol, period="6mo", interval="1d")
18
  if df.empty:
19
- return html_error(f"No daily data found for {symbol}")
 
 
 
 
20
 
21
  df = clean_df(df)
22
 
23
- # ---------------- Calculate indicators ----------------
24
  indicators = calculate_indicators(df)
25
 
26
- # ---------------- Build chart ----------------
27
- html_chart = build_chart(df, indicators)
28
 
29
- # ---------------- Build table ----------------
30
  table_html = make_table(df)
31
 
32
- # ---------------- Wrap into full HTML ----------------
33
- html = wrap_html(f"{html_chart}<br>{table_html}", title=f"{symbol} Daily Chart")
34
- return html
 
 
35
 
36
  except Exception as e:
37
- return html_error(f"Error generating daily chart for {symbol}: {e}")
 
1
  # daily.py
2
  import pandas as pd
3
  import yfinance as yf
4
+ from common import format_number, format_large_number, html_card, make_table, wrap_html, clean_df
5
  from indicater import calculate_indicators
6
  from chart_builder import build_chart
7
 
8
  def fetch_daily(symbol):
9
  """
10
+ Fetch real daily stock data from Yahoo Finance, calculate indicators,
11
+ build chart and table, and return full HTML.
12
  """
13
  try:
14
+ # 1. Fetch daily OHLCV data
15
+ yf_symbol = f"{symbol}.NS" # NSE stocks; adjust for your exchange
16
+ df = yf.download(yf_symbol, period="1y", interval="1d", progress=False)
 
17
  if df.empty:
18
+ return f"<h3>No daily data found for {symbol}</h3>"
19
+
20
+ # 2. Handle multi-level columns (take 0th level)
21
+ if isinstance(df.columns, pd.MultiIndex):
22
+ df.columns = df.columns.get_level_values(0)
23
 
24
  df = clean_df(df)
25
 
26
+ # 3. Calculate indicators
27
  indicators = calculate_indicators(df)
28
 
29
+ # 4. Build Plotly chart with indicators
30
+ chart_html = build_chart(df, indicators, symbol=symbol)
31
 
32
+ # 5. Format table
33
  table_html = make_table(df)
34
 
35
+ # 6. Wrap chart + table in card layout
36
+ content_html = html_card(f"{symbol} Daily Data", chart_html + table_html)
37
+
38
+ # 7. Return full HTML
39
+ return wrap_html(content_html, title=f"{symbol} Daily Data")
40
 
41
  except Exception as e:
42
+ return f"<h3>Error fetching daily data for {symbol}:</h3><pre>{e}</pre>"