Spaces:
Sleeping
Sleeping
Update app/daily.py
Browse files- app/daily.py +4 -65
app/daily.py
CHANGED
|
@@ -28,46 +28,6 @@ def daily(symbol, date_end, date_start):
|
|
| 28 |
|
| 29 |
return df
|
| 30 |
|
| 31 |
-
# ===========================================================
|
| 32 |
-
# PATTERN DETECTION (scalar-safe)
|
| 33 |
-
# ===========================================================
|
| 34 |
-
def detect_patterns(df):
|
| 35 |
-
patterns = []
|
| 36 |
-
|
| 37 |
-
for i in range(1, len(df)):
|
| 38 |
-
open_today = df.iat[i, df.columns.get_loc("Open")]
|
| 39 |
-
close_today = df.iat[i, df.columns.get_loc("Close")]
|
| 40 |
-
open_prev = df.iat[i-1, df.columns.get_loc("Open")]
|
| 41 |
-
close_prev = df.iat[i-1, df.columns.get_loc("Close")]
|
| 42 |
-
high = df.iat[i, df.columns.get_loc("High")]
|
| 43 |
-
low = df.iat[i, df.columns.get_loc("Low")]
|
| 44 |
-
|
| 45 |
-
# Bullish Engulfing
|
| 46 |
-
if close_prev < open_prev and close_today > open_today and close_today > open_prev and open_today < close_prev:
|
| 47 |
-
patterns.append({"Date": df.iat[i, df.columns.get_loc("Date")], "Pattern": "Bullish Engulfing"})
|
| 48 |
-
# Bearish Engulfing
|
| 49 |
-
elif close_prev > open_prev and close_today < open_today and open_today > close_prev and close_today < open_prev:
|
| 50 |
-
patterns.append({"Date": df.iat[i, df.columns.get_loc("Date")], "Pattern": "Bearish Engulfing"})
|
| 51 |
-
# Doji
|
| 52 |
-
elif abs(close_today - open_today) / (high - low + 1e-6) < 0.1:
|
| 53 |
-
patterns.append({"Date": df.iat[i, df.columns.get_loc("Date")], "Pattern": "Doji"})
|
| 54 |
-
# Hammer / Hanging Man
|
| 55 |
-
elif (high - max(open_today, close_today)) > 2*(max(open_today, close_today)-min(open_today, close_today)) and \
|
| 56 |
-
(min(open_today, close_today) - low) < 0.1*(high-low):
|
| 57 |
-
if close_today > open_today:
|
| 58 |
-
patterns.append({"Date": df.iat[i, df.columns.get_loc("Date")], "Pattern": "Hammer"})
|
| 59 |
-
else:
|
| 60 |
-
patterns.append({"Date": df.iat[i, df.columns.get_loc("Date")], "Pattern": "Hanging Man"})
|
| 61 |
-
# Gap Up / Gap Down
|
| 62 |
-
if open_today > close_prev * 1.01:
|
| 63 |
-
patterns.append({"Date": df.iat[i, df.columns.get_loc("Date")], "Pattern": "Gap Up"})
|
| 64 |
-
elif open_today < close_prev * 0.99:
|
| 65 |
-
patterns.append({"Date": df.iat[i, df.columns.get_loc("Date")], "Pattern": "Gap Down"})
|
| 66 |
-
|
| 67 |
-
if patterns:
|
| 68 |
-
return pd.DataFrame(patterns)
|
| 69 |
-
return pd.DataFrame(columns=["Date", "Pattern"])
|
| 70 |
-
|
| 71 |
# ===========================================================
|
| 72 |
# DASHBOARD BUILDER
|
| 73 |
# ===========================================================
|
|
@@ -82,7 +42,7 @@ def fetch_daily(symbol, date_end, date_start, b2_save=False):
|
|
| 82 |
try:
|
| 83 |
df = daily(symbol, date_end, date_start)
|
| 84 |
if df is None or df.empty:
|
| 85 |
-
return wrap_html('<div id="daily_wrapper"><h1>No daily data for {}</h1></div>'
|
| 86 |
|
| 87 |
# Reset index if not simple RangeIndex
|
| 88 |
if not isinstance(df.index, pd.RangeIndex):
|
|
@@ -130,15 +90,13 @@ def fetch_daily(symbol, date_end, date_start, b2_save=False):
|
|
| 130 |
]
|
| 131 |
})
|
| 132 |
|
| 133 |
-
# Detect patterns
|
| 134 |
-
patterns_df = detect_patterns(df)
|
| 135 |
-
|
| 136 |
# Plotly dashboard
|
| 137 |
fig = make_subplots(rows=4, cols=1, shared_xaxes=True,
|
| 138 |
vertical_spacing=0.05,
|
| 139 |
row_heights=[0.4,0.2,0.2,0.2],
|
| 140 |
specs=[[{}],[{}],[{}],[{}]])
|
| 141 |
|
|
|
|
| 142 |
fig.add_trace(go.Candlestick(x=df["Date"], open=df["Open"], high=df["High"], low=df["Low"], close=df["Close"], name="OHLC"), row=1, col=1)
|
| 143 |
fig.add_trace(go.Scatter(x=df["Date"], y=df["SMA20"], mode="lines", name="SMA20"), row=1, col=1)
|
| 144 |
fig.add_trace(go.Scatter(x=df["Date"], y=df["SMA50"], mode="lines", name="SMA50"), row=1, col=1)
|
|
@@ -147,22 +105,6 @@ def fetch_daily(symbol, date_end, date_start, b2_save=False):
|
|
| 147 |
fig.add_trace(go.Scatter(x=df["Date"], y=df["UpperBB"], mode="lines", name="UpperBB", line=dict(dash="dot")), row=1, col=1)
|
| 148 |
fig.add_trace(go.Scatter(x=df["Date"], y=df["LowerBB"], mode="lines", name="LowerBB", line=dict(dash="dot")), row=1, col=1)
|
| 149 |
|
| 150 |
-
# Highlight patterns safely
|
| 151 |
-
if not patterns_df.empty:
|
| 152 |
-
for _, row in patterns_df.iterrows():
|
| 153 |
-
pattern_date = row["Date"]
|
| 154 |
-
matching = df[df["Date"]==pattern_date]
|
| 155 |
-
if not matching.empty:
|
| 156 |
-
high_value = matching["High"].values[0]
|
| 157 |
-
fig.add_trace(go.Scatter(
|
| 158 |
-
x=[pattern_date], y=[high_value*1.01],
|
| 159 |
-
mode="markers+text",
|
| 160 |
-
marker=dict(color="red", size=10, symbol="triangle-up"),
|
| 161 |
-
text=[row["Pattern"]],
|
| 162 |
-
textposition="top center",
|
| 163 |
-
showlegend=False
|
| 164 |
-
), row=1, col=1)
|
| 165 |
-
|
| 166 |
# Volume
|
| 167 |
fig.add_trace(go.Bar(x=df["Date"], y=df["Volume"], name="Volume"), row=2, col=1)
|
| 168 |
# Daily Return %
|
|
@@ -178,14 +120,11 @@ def fetch_daily(symbol, date_end, date_start, b2_save=False):
|
|
| 178 |
except Exception as e:
|
| 179 |
chart_html = f'<div id="chart_dashboard"><h2>Chart generation failed: {e}</h2></div>'
|
| 180 |
|
| 181 |
-
# Tables wrapped in divs
|
| 182 |
table_html = f'<div id="summary_stats"><h2>Summary Stats</h2>{summary.to_html(index=False, escape=False)}</div>'
|
| 183 |
data_table_html = f'<div id="ohlc_table"><h2>OHLC Table</h2>{df.to_html(index=False, escape=False)}</div>'
|
| 184 |
-
patterns_html = f'<div id="patterns_table"><h2>Detected Patterns</h2>'
|
| 185 |
-
patterns_html += patterns_df.to_html(index=False, escape=False) if not patterns_df.empty else "<p>No patterns detected.</p>"
|
| 186 |
-
patterns_html += "</div>"
|
| 187 |
|
| 188 |
-
full_html = chart_html + table_html +
|
| 189 |
|
| 190 |
persist.save(key, full_html, "html")
|
| 191 |
return full_html
|
|
|
|
| 28 |
|
| 29 |
return df
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
# ===========================================================
|
| 32 |
# DASHBOARD BUILDER
|
| 33 |
# ===========================================================
|
|
|
|
| 42 |
try:
|
| 43 |
df = daily(symbol, date_end, date_start)
|
| 44 |
if df is None or df.empty:
|
| 45 |
+
return wrap_html(f'<div id="daily_wrapper"><h1>No daily data for {symbol}</h1></div>')
|
| 46 |
|
| 47 |
# Reset index if not simple RangeIndex
|
| 48 |
if not isinstance(df.index, pd.RangeIndex):
|
|
|
|
| 90 |
]
|
| 91 |
})
|
| 92 |
|
|
|
|
|
|
|
|
|
|
| 93 |
# Plotly dashboard
|
| 94 |
fig = make_subplots(rows=4, cols=1, shared_xaxes=True,
|
| 95 |
vertical_spacing=0.05,
|
| 96 |
row_heights=[0.4,0.2,0.2,0.2],
|
| 97 |
specs=[[{}],[{}],[{}],[{}]])
|
| 98 |
|
| 99 |
+
# Candlestick + moving averages + Bollinger
|
| 100 |
fig.add_trace(go.Candlestick(x=df["Date"], open=df["Open"], high=df["High"], low=df["Low"], close=df["Close"], name="OHLC"), row=1, col=1)
|
| 101 |
fig.add_trace(go.Scatter(x=df["Date"], y=df["SMA20"], mode="lines", name="SMA20"), row=1, col=1)
|
| 102 |
fig.add_trace(go.Scatter(x=df["Date"], y=df["SMA50"], mode="lines", name="SMA50"), row=1, col=1)
|
|
|
|
| 105 |
fig.add_trace(go.Scatter(x=df["Date"], y=df["UpperBB"], mode="lines", name="UpperBB", line=dict(dash="dot")), row=1, col=1)
|
| 106 |
fig.add_trace(go.Scatter(x=df["Date"], y=df["LowerBB"], mode="lines", name="LowerBB", line=dict(dash="dot")), row=1, col=1)
|
| 107 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
# Volume
|
| 109 |
fig.add_trace(go.Bar(x=df["Date"], y=df["Volume"], name="Volume"), row=2, col=1)
|
| 110 |
# Daily Return %
|
|
|
|
| 120 |
except Exception as e:
|
| 121 |
chart_html = f'<div id="chart_dashboard"><h2>Chart generation failed: {e}</h2></div>'
|
| 122 |
|
| 123 |
+
# Tables wrapped in divs
|
| 124 |
table_html = f'<div id="summary_stats"><h2>Summary Stats</h2>{summary.to_html(index=False, escape=False)}</div>'
|
| 125 |
data_table_html = f'<div id="ohlc_table"><h2>OHLC Table</h2>{df.to_html(index=False, escape=False)}</div>'
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
+
full_html = chart_html + table_html + data_table_html
|
| 128 |
|
| 129 |
persist.save(key, full_html, "html")
|
| 130 |
return full_html
|