eshan6704 commited on
Commit
6c0386c
·
verified ·
1 Parent(s): 8d22de4

Update daily.py

Browse files
Files changed (1) hide show
  1. daily.py +15 -23
daily.py CHANGED
@@ -1,40 +1,32 @@
1
  # daily.py
2
- import pandas as pd # <-- required
3
  import yfinance as yf
4
- from common import wrap_plotly_html, make_table
5
  from indicater import calculate_indicators
6
  from chart_builder import build_chart
 
7
 
8
  def fetch_daily(symbol):
9
  """
10
- Fetch daily OHLC + volume data for the past 1 year and render as HTML chart + table
11
- with selectable indicators.
12
  """
13
- yfsymbol = f"{symbol}.NS"
14
  try:
15
- # Download daily data
16
- df = yf.download(yfsymbol, period="1y", interval="1d").round(2)
17
-
18
- if df.empty:
19
- return f"<h1>No daily data for {symbol}</h1>"
20
-
21
- # Handle multilevel columns (take 0th level)
22
  if isinstance(df.columns, pd.MultiIndex):
23
- df.columns = df.columns.get_level_values(0)
24
 
25
- # Format table HTML for last 30 rows
26
- table_html = make_table(df.tail(30))
 
 
 
27
 
28
  # Calculate indicators
29
  indicators = calculate_indicators(df)
30
 
31
- # Build Plotly chart with main + volume subplot + subplots for indicators
32
- chart_html = build_chart(df, indicators, symbol)
33
-
34
- # Wrap chart + table together
35
- full_html = wrap_plotly_html(chart_html, table_html)
36
-
37
- return full_html
38
 
 
39
  except Exception as e:
40
- return f"<h1>Error fetching daily data</h1><p>{str(e)}</p>"
 
 
1
  # daily.py
 
2
  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>"))