Update daily.py
Browse files
daily.py
CHANGED
|
@@ -1,37 +1,42 @@
|
|
| 1 |
# daily.py
|
| 2 |
import pandas as pd
|
| 3 |
import yfinance as yf
|
| 4 |
-
from common import
|
| 5 |
from indicater import calculate_indicators
|
| 6 |
from chart_builder import build_chart
|
| 7 |
|
| 8 |
def fetch_daily(symbol):
|
| 9 |
"""
|
| 10 |
-
|
| 11 |
-
|
| 12 |
"""
|
| 13 |
try:
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
df = yf.download(yfsymbol, period="6mo", interval="1d")
|
| 18 |
if df.empty:
|
| 19 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
df = clean_df(df)
|
| 22 |
|
| 23 |
-
#
|
| 24 |
indicators = calculate_indicators(df)
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
|
| 28 |
|
| 29 |
-
#
|
| 30 |
table_html = make_table(df)
|
| 31 |
|
| 32 |
-
#
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
| 35 |
|
| 36 |
except Exception as e:
|
| 37 |
-
return
|
|
|
|
| 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>"
|