Update daily.py
Browse files
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 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
df['Volume'] = 1
|
| 22 |
|
| 23 |
-
|
| 24 |
-
indicators = calculate_indicators(df)
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
return wrap_html(html_card(f"{symbol} Daily Chart", html_chart))
|
| 30 |
except Exception as e:
|
| 31 |
-
|
| 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")
|
|
|