eshan6704 commited on
Commit
97848e1
·
verified ·
1 Parent(s): 4833a3e

Update daily.py

Browse files
Files changed (1) hide show
  1. daily.py +43 -31
daily.py CHANGED
@@ -1,4 +1,4 @@
1
- # daily.py
2
  import yfinance as yf
3
  import pandas as pd
4
  from ta_indi_pat import patterns, indicators
@@ -7,50 +7,62 @@ from common import html_card, wrap_html
7
  def fetch_daily(symbol, max_rows=200):
8
  """
9
  Fetch daily OHLCV data, calculate indicators + patterns,
10
- and return 3 separate scrollable HTML tables: OHLCV, indicators, patterns.
11
  """
12
  try:
13
- # --- Fetch historical data ---
14
  df = yf.download(symbol + ".NS", period="1y", interval="1d").round(2)
15
-
16
- #if isinstance(df.columns, pd.MultiIndex):
17
- #df.columns = df.columns.get_level_values(0)
18
  df.columns=["Close", "High", "Low", "Open", "Volume"]
19
  if df.empty:
20
  return html_card("Error", f"No daily data found for {symbol}")
21
- df.reset_index(inplace=True)
22
- df = df.head(max_rows)
23
 
24
- # --- Calculate indicators and patterns ---
25
- indicator_df = indicators(df).head(max_rows)
26
- pattern_df = patterns(df).head(max_rows)
27
 
28
- # --- Handle MultiIndex columns: take only 0th level ---
 
29
 
 
 
 
30
 
31
- # --- Convert each DataFrame to HTML table ---
32
- daily_table_html = f"""
33
- <div style="overflow-x:auto; overflow-y:auto; max-height:400px; border:1px solid #ccc; padding:5px;">
34
- {df.to_html(classes="table table-striped table-bordered", index=False)}
35
- </div>
36
- """
37
- indicator_table_html = f"""
38
- <div style="overflow-x:auto; overflow-y:auto; max-height:400px; border:1px solid #ccc; padding:5px;">
39
- {indicator_df.to_html(classes="table table-striped table-bordered", index=False)}
40
- </div>
41
- """
42
- pattern_table_html = f"""
43
- <div style="overflow-x:auto; overflow-y:auto; max-height:400px; border:1px solid #ccc; padding:5px;">
44
- {pattern_df.to_html(classes="table table-striped table-bordered", index=False)}
45
- </div>
 
 
 
 
 
 
 
 
46
  """
47
 
48
- # --- Wrap each in card ---
49
  content = f"""
50
  <h2>{symbol} - Daily Data</h2>
51
- {html_card("Daily OHLCV Data", daily_table_html)}
52
- {html_card("Indicator Data", indicator_table_html)}
53
- {html_card("Pattern Data", pattern_table_html)}
 
 
 
 
 
 
 
54
  """
55
 
56
  return wrap_html(content, title=f"{symbol} Daily Data")
 
1
+
2
  import yfinance as yf
3
  import pandas as pd
4
  from ta_indi_pat import patterns, indicators
 
7
  def fetch_daily(symbol, max_rows=200):
8
  """
9
  Fetch daily OHLCV data, calculate indicators + patterns,
10
+ return 3 scrollable HTML tables with toggle buttons.
11
  """
12
  try:
13
+ # --- Fetch daily data ---
14
  df = yf.download(symbol + ".NS", period="1y", interval="1d").round(2)
 
 
 
15
  df.columns=["Close", "High", "Low", "Open", "Volume"]
16
  if df.empty:
17
  return html_card("Error", f"No daily data found for {symbol}")
 
 
18
 
19
+ df.reset_index(inplace=True) # make Date a column
 
 
20
 
21
+ # --- Limit rows for display ---
22
+ df_display = df.head(max_rows)
23
 
24
+ # --- Generate indicators and patterns ---
25
+ indicator_df = indicators(df_display)
26
+ pattern_df = patterns(df_display)
27
 
28
+ # --- Convert to HTML tables ---
29
+ df_html = df_display.to_html(classes="table table-striped table-bordered", index=False)
30
+ indicator_html = indicator_df.to_html(classes="table table-striped table-bordered", index=False)
31
+ pattern_html = pattern_df.to_html(classes="table table-striped table-bordered", index=False)
32
+
33
+ # --- Wrap each table in scrollable div ---
34
+ scrollable_template = '<div style="overflow-x:auto; overflow-y:auto; max-height:400px; border:1px solid #ccc; padding:5px; margin-bottom:10px;">{}</div>'
35
+ df_html = scrollable_template.format(df_html)
36
+ indicator_html = scrollable_template.format(indicator_html)
37
+ pattern_html = scrollable_template.format(pattern_html)
38
+
39
+ # --- HTML buttons for toggling ---
40
+ toggle_script = """
41
+ <script>
42
+ function toggleTable(id){
43
+ var tbl = document.getElementById(id);
44
+ if(tbl.style.display === "none"){
45
+ tbl.style.display = "block";
46
+ } else {
47
+ tbl.style.display = "none";
48
+ }
49
+ }
50
+ </script>
51
  """
52
 
53
+ # --- Build buttons and tables ---
54
  content = f"""
55
  <h2>{symbol} - Daily Data</h2>
56
+
57
+ <button onclick="toggleTable('ohlcv_table')">Toggle OHLCV</button>
58
+ <button onclick="toggleTable('indicator_table')">Toggle Indicators</button>
59
+ <button onclick="toggleTable('pattern_table')">Toggle Patterns</button>
60
+
61
+ {toggle_script}
62
+
63
+ <div id="ohlcv_table">{html_card("OHLCV Data", df_html)}</div>
64
+ <div id="indicator_table">{html_card("Indicators", indicator_html)}</div>
65
+ <div id="pattern_table">{html_card("Patterns", pattern_html)}</div>
66
  """
67
 
68
  return wrap_html(content, title=f"{symbol} Daily Data")