Update daily.py
Browse files
daily.py
CHANGED
|
@@ -1,40 +1,32 @@
|
|
| 1 |
# daily.py
|
| 2 |
-
import pandas as pd # <-- required
|
| 3 |
import yfinance as yf
|
| 4 |
-
|
| 5 |
from indicater import calculate_indicators
|
| 6 |
from chart_builder import build_chart
|
|
|
|
| 7 |
|
| 8 |
def fetch_daily(symbol):
|
| 9 |
"""
|
| 10 |
-
Fetch daily
|
| 11 |
-
with selectable indicators.
|
| 12 |
"""
|
| 13 |
-
yfsymbol = f"{symbol}.NS"
|
| 14 |
try:
|
| 15 |
-
|
| 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 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
# Calculate indicators
|
| 29 |
indicators = calculate_indicators(df)
|
| 30 |
|
| 31 |
-
# Build
|
| 32 |
-
|
| 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 |
-
|
|
|
|
|
|
| 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>"))
|