Spaces:
Sleeping
Sleeping
Update app/daily.py
Browse files- app/daily.py +49 -12
app/daily.py
CHANGED
|
@@ -7,6 +7,7 @@ from . import persist
|
|
| 7 |
from .common import wrap_html
|
| 8 |
import plotly.graph_objects as go
|
| 9 |
import plotly.io as pio
|
|
|
|
| 10 |
|
| 11 |
# ===========================================================
|
| 12 |
# RAW DAILY FETCHER
|
|
@@ -23,7 +24,7 @@ def daily(symbol, date_end, date_start):
|
|
| 23 |
return df
|
| 24 |
|
| 25 |
# ===========================================================
|
| 26 |
-
# PLOTLY CANDLESTICK
|
| 27 |
# ===========================================================
|
| 28 |
def plot_candlestick(df, symbol):
|
| 29 |
fig = go.Figure()
|
|
@@ -40,7 +41,7 @@ def plot_candlestick(df, symbol):
|
|
| 40 |
decreasing_line_color='red'
|
| 41 |
))
|
| 42 |
|
| 43 |
-
# Volume as bar
|
| 44 |
fig.add_trace(go.Bar(
|
| 45 |
x=df['Date'],
|
| 46 |
y=df['Volume'],
|
|
@@ -61,11 +62,36 @@ def plot_candlestick(df, symbol):
|
|
| 61 |
|
| 62 |
return pio.to_html(fig, full_html=False, include_plotlyjs='cdn')
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
# ===========================================================
|
| 65 |
-
# DAILY TABLE
|
| 66 |
# ===========================================================
|
| 67 |
def fetch_daily(symbol, date_end, date_start):
|
| 68 |
-
"""Return HTML table + candlestick
|
| 69 |
key = f"daily_{symbol}"
|
| 70 |
if persist.exists(key, "html"):
|
| 71 |
cached = persist.load(key, "html")
|
|
@@ -93,13 +119,21 @@ def fetch_daily(symbol, date_end, date_start):
|
|
| 93 |
# Daily change %
|
| 94 |
df["Change %"] = ((df["Close"] - df["Open"]) / df["Open"] * 100).round(2)
|
| 95 |
|
| 96 |
-
# Build HTML table
|
| 97 |
html_table = '<div style="max-height:300px; overflow:auto; font-family:Arial,sans-serif; margin-bottom:20px;">'
|
| 98 |
html_table += '<table border="1" style="border-collapse:collapse; width:100%;">'
|
| 99 |
-
html_table += '
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
html_table += '<tr><th>Date</th><th>Open</th><th>High</th><th>Low</th>'
|
| 101 |
-
html_table += '<th>Close</th><th>Volume</th><th>Change %</th></tr>'
|
| 102 |
-
html_table += '</thead><tbody>'
|
| 103 |
|
| 104 |
for idx, r in df.iterrows():
|
| 105 |
row_color = "#e8f5e9" if idx % 2 == 0 else "#f5f5f5"
|
|
@@ -116,11 +150,14 @@ def fetch_daily(symbol, date_end, date_start):
|
|
| 116 |
html_table += '</tr>'
|
| 117 |
html_table += '</tbody></table></div>'
|
| 118 |
|
| 119 |
-
#
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
-
# Combine
|
| 123 |
-
full_html = f'<div id="daily_dashboard">{html_table}{
|
| 124 |
|
| 125 |
persist.save(key, full_html, "html")
|
| 126 |
return full_html
|
|
|
|
| 7 |
from .common import wrap_html
|
| 8 |
import plotly.graph_objects as go
|
| 9 |
import plotly.io as pio
|
| 10 |
+
import plotly.express as px
|
| 11 |
|
| 12 |
# ===========================================================
|
| 13 |
# RAW DAILY FETCHER
|
|
|
|
| 24 |
return df
|
| 25 |
|
| 26 |
# ===========================================================
|
| 27 |
+
# PLOTLY CANDLESTICK + VOLUME
|
| 28 |
# ===========================================================
|
| 29 |
def plot_candlestick(df, symbol):
|
| 30 |
fig = go.Figure()
|
|
|
|
| 41 |
decreasing_line_color='red'
|
| 42 |
))
|
| 43 |
|
| 44 |
+
# Volume as bar
|
| 45 |
fig.add_trace(go.Bar(
|
| 46 |
x=df['Date'],
|
| 47 |
y=df['Volume'],
|
|
|
|
| 62 |
|
| 63 |
return pio.to_html(fig, full_html=False, include_plotlyjs='cdn')
|
| 64 |
|
| 65 |
+
|
| 66 |
+
# ===========================================================
|
| 67 |
+
# ADDITIONAL ANALYSIS CHARTS
|
| 68 |
+
# ===========================================================
|
| 69 |
+
def plot_analysis_charts(df, symbol):
|
| 70 |
+
charts = ""
|
| 71 |
+
|
| 72 |
+
# 1️⃣ OHLC line chart
|
| 73 |
+
fig_line = px.line(df, x='Date', y=['Open','High','Low','Close'], title=f'{symbol} OHLC Line Chart')
|
| 74 |
+
charts += pio.to_html(fig_line, full_html=False, include_plotlyjs=False)
|
| 75 |
+
|
| 76 |
+
# 2️⃣ 20-day and 50-day moving average
|
| 77 |
+
df['MA20'] = df['Close'].rolling(20).mean()
|
| 78 |
+
df['MA50'] = df['Close'].rolling(50).mean()
|
| 79 |
+
fig_ma = px.line(df, x='Date', y=['Close','MA20','MA50'], title=f'{symbol} 20 & 50 Day Moving Avg')
|
| 80 |
+
charts += pio.to_html(fig_ma, full_html=False, include_plotlyjs=False)
|
| 81 |
+
|
| 82 |
+
# 3️⃣ Daily change %
|
| 83 |
+
fig_change = px.bar(df, x='Date', y='Change %', color='Change %', title=f'{symbol} Daily % Change',
|
| 84 |
+
color_continuous_scale=['red','green'])
|
| 85 |
+
charts += pio.to_html(fig_change, full_html=False, include_plotlyjs=False)
|
| 86 |
+
|
| 87 |
+
return charts
|
| 88 |
+
|
| 89 |
+
|
| 90 |
# ===========================================================
|
| 91 |
+
# DAILY TABLE + DASHBOARD
|
| 92 |
# ===========================================================
|
| 93 |
def fetch_daily(symbol, date_end, date_start):
|
| 94 |
+
"""Return HTML table + candlestick + analysis charts."""
|
| 95 |
key = f"daily_{symbol}"
|
| 96 |
if persist.exists(key, "html"):
|
| 97 |
cached = persist.load(key, "html")
|
|
|
|
| 119 |
# Daily change %
|
| 120 |
df["Change %"] = ((df["Close"] - df["Open"]) / df["Open"] * 100).round(2)
|
| 121 |
|
| 122 |
+
# Build HTML table with improved header CSS
|
| 123 |
html_table = '<div style="max-height:300px; overflow:auto; font-family:Arial,sans-serif; margin-bottom:20px;">'
|
| 124 |
html_table += '<table border="1" style="border-collapse:collapse; width:100%;">'
|
| 125 |
+
html_table += '''
|
| 126 |
+
<thead style="
|
| 127 |
+
position:sticky;
|
| 128 |
+
top:0;
|
| 129 |
+
background: linear-gradient(to right,#1a4f8a,#4a7ac7);
|
| 130 |
+
color:white;
|
| 131 |
+
font-weight:bold;
|
| 132 |
+
text-align:center;
|
| 133 |
+
">
|
| 134 |
+
'''
|
| 135 |
html_table += '<tr><th>Date</th><th>Open</th><th>High</th><th>Low</th>'
|
| 136 |
+
html_table += '<th>Close</th><th>Volume</th><th>Change %</th></tr></thead><tbody>'
|
|
|
|
| 137 |
|
| 138 |
for idx, r in df.iterrows():
|
| 139 |
row_color = "#e8f5e9" if idx % 2 == 0 else "#f5f5f5"
|
|
|
|
| 150 |
html_table += '</tr>'
|
| 151 |
html_table += '</tbody></table></div>'
|
| 152 |
|
| 153 |
+
# Candlestick chart
|
| 154 |
+
candlestick_html = plot_candlestick(df, symbol)
|
| 155 |
+
|
| 156 |
+
# Additional analysis charts
|
| 157 |
+
analysis_html = plot_analysis_charts(df, symbol)
|
| 158 |
|
| 159 |
+
# Combine all
|
| 160 |
+
full_html = f'<div id="daily_dashboard">{html_table}{candlestick_html}{analysis_html}</div>'
|
| 161 |
|
| 162 |
persist.save(key, full_html, "html")
|
| 163 |
return full_html
|