Spaces:
Sleeping
Sleeping
Update app/daily.py
Browse files- app/daily.py +26 -24
app/daily.py
CHANGED
|
@@ -4,7 +4,7 @@ import pandas as pd
|
|
| 4 |
from datetime import datetime as dt
|
| 5 |
from plotly import graph_objs as go
|
| 6 |
from plotly.subplots import make_subplots
|
| 7 |
-
import traceback
|
| 8 |
|
| 9 |
from . import persist
|
| 10 |
from . import backblaze as b2
|
|
@@ -46,10 +46,12 @@ def detect_patterns(df):
|
|
| 46 |
elif open_today < close_prev * 0.99:
|
| 47 |
patterns.append({"Date": df.iat[i, df.columns.get_loc("Date")], "Pattern": "Gap Down"})
|
| 48 |
|
| 49 |
-
|
|
|
|
|
|
|
| 50 |
|
| 51 |
# ===========================================================
|
| 52 |
-
#
|
| 53 |
# ===========================================================
|
| 54 |
def fetch_daily(symbol, date_end, date_start, b2_save=False):
|
| 55 |
key = f"daily_{symbol}"
|
|
@@ -60,7 +62,6 @@ def fetch_daily(symbol, date_end, date_start, b2_save=False):
|
|
| 60 |
return cached
|
| 61 |
|
| 62 |
try:
|
| 63 |
-
# Download data
|
| 64 |
start = dt.strptime(date_start, "%d-%m-%Y").strftime("%Y-%m-%d")
|
| 65 |
end = dt.strptime(date_end, "%d-%m-%Y").strftime("%Y-%m-%d")
|
| 66 |
print(f"[{dt.now().strftime('%Y-%m-%d %H:%M:%S')}] Fetching daily for {symbol}")
|
|
@@ -122,17 +123,18 @@ def fetch_daily(symbol, date_end, date_start, b2_save=False):
|
|
| 122 |
fig.add_trace(go.Scatter(x=df["Date"], y=df["LowerBB"], mode="lines", name="LowerBB", line=dict(dash="dot")), row=1, col=1)
|
| 123 |
|
| 124 |
# Highlight patterns on chart
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
|
|
|
| 136 |
|
| 137 |
# Volume
|
| 138 |
fig.add_trace(go.Bar(x=df["Date"], y=df["Volume"], name="Volume"), row=2, col=1)
|
|
@@ -142,20 +144,20 @@ def fetch_daily(symbol, date_end, date_start, b2_save=False):
|
|
| 142 |
fig.add_trace(go.Scatter(x=df["Date"], y=df["ATR"], mode="lines", name="ATR"), row=4, col=1)
|
| 143 |
|
| 144 |
fig.update_layout(height=1000, width=1200, title=f"{symbol} Daily Analysis Dashboard", xaxis_rangeslider_visible=False)
|
| 145 |
-
|
|
|
|
|
|
|
|
|
|
| 146 |
|
| 147 |
# Tables
|
| 148 |
table_html = wrap_html(f"<h2>Summary Stats</h2>{summary.to_html(index=False, escape=False)}")
|
| 149 |
data_table_html = wrap_html(f"<h2>OHLC Table</h2>{df.to_html(index=False, escape=False)}")
|
| 150 |
-
patterns_html = wrap_html(
|
|
|
|
|
|
|
|
|
|
| 151 |
|
| 152 |
-
|
| 153 |
-
csv_buffer = io.StringIO()
|
| 154 |
-
df.to_csv(csv_buffer, index=False)
|
| 155 |
-
csv_base64 = base64.b64encode(csv_buffer.getvalue().encode()).decode()
|
| 156 |
-
download_html = f'<a href="data:text/csv;base64,{csv_base64}" download="{symbol}_daily.csv">Download CSV</a>'
|
| 157 |
-
|
| 158 |
-
full_html = chart_html + table_html + patterns_html + data_table_html + download_html
|
| 159 |
|
| 160 |
# Cache
|
| 161 |
persist.save(key, full_html, "html")
|
|
|
|
| 4 |
from datetime import datetime as dt
|
| 5 |
from plotly import graph_objs as go
|
| 6 |
from plotly.subplots import make_subplots
|
| 7 |
+
import traceback
|
| 8 |
|
| 9 |
from . import persist
|
| 10 |
from . import backblaze as b2
|
|
|
|
| 46 |
elif open_today < close_prev * 0.99:
|
| 47 |
patterns.append({"Date": df.iat[i, df.columns.get_loc("Date")], "Pattern": "Gap Down"})
|
| 48 |
|
| 49 |
+
if patterns:
|
| 50 |
+
return pd.DataFrame(patterns)
|
| 51 |
+
return pd.DataFrame(columns=["Date", "Pattern"])
|
| 52 |
|
| 53 |
# ===========================================================
|
| 54 |
+
# Daily Analysis Dashboard
|
| 55 |
# ===========================================================
|
| 56 |
def fetch_daily(symbol, date_end, date_start, b2_save=False):
|
| 57 |
key = f"daily_{symbol}"
|
|
|
|
| 62 |
return cached
|
| 63 |
|
| 64 |
try:
|
|
|
|
| 65 |
start = dt.strptime(date_start, "%d-%m-%Y").strftime("%Y-%m-%d")
|
| 66 |
end = dt.strptime(date_end, "%d-%m-%Y").strftime("%Y-%m-%d")
|
| 67 |
print(f"[{dt.now().strftime('%Y-%m-%d %H:%M:%S')}] Fetching daily for {symbol}")
|
|
|
|
| 123 |
fig.add_trace(go.Scatter(x=df["Date"], y=df["LowerBB"], mode="lines", name="LowerBB", line=dict(dash="dot")), row=1, col=1)
|
| 124 |
|
| 125 |
# Highlight patterns on chart
|
| 126 |
+
if not patterns_df.empty:
|
| 127 |
+
for _, row in patterns_df.iterrows():
|
| 128 |
+
pattern_date = row["Date"]
|
| 129 |
+
high_value = df.loc[df["Date"]==pattern_date, "High"].values[0]
|
| 130 |
+
fig.add_trace(go.Scatter(
|
| 131 |
+
x=[pattern_date], y=[high_value*1.01],
|
| 132 |
+
mode="markers+text",
|
| 133 |
+
marker=dict(color="red", size=10, symbol="triangle-up"),
|
| 134 |
+
text=[row["Pattern"]],
|
| 135 |
+
textposition="top center",
|
| 136 |
+
showlegend=False
|
| 137 |
+
), row=1, col=1)
|
| 138 |
|
| 139 |
# Volume
|
| 140 |
fig.add_trace(go.Bar(x=df["Date"], y=df["Volume"], name="Volume"), row=2, col=1)
|
|
|
|
| 144 |
fig.add_trace(go.Scatter(x=df["Date"], y=df["ATR"], mode="lines", name="ATR"), row=4, col=1)
|
| 145 |
|
| 146 |
fig.update_layout(height=1000, width=1200, title=f"{symbol} Daily Analysis Dashboard", xaxis_rangeslider_visible=False)
|
| 147 |
+
try:
|
| 148 |
+
chart_html = fig.to_html(full_html=False, include_plotlyjs='cdn')
|
| 149 |
+
except Exception as e:
|
| 150 |
+
chart_html = f"<h2>Chart generation failed: {e}</h2>"
|
| 151 |
|
| 152 |
# Tables
|
| 153 |
table_html = wrap_html(f"<h2>Summary Stats</h2>{summary.to_html(index=False, escape=False)}")
|
| 154 |
data_table_html = wrap_html(f"<h2>OHLC Table</h2>{df.to_html(index=False, escape=False)}")
|
| 155 |
+
patterns_html = wrap_html(
|
| 156 |
+
f"<h2>Detected Patterns</h2>" +
|
| 157 |
+
(patterns_df.to_html(index=False, escape=False) if not patterns_df.empty else "<p>No patterns detected.</p>")
|
| 158 |
+
)
|
| 159 |
|
| 160 |
+
full_html = chart_html + table_html + patterns_html + data_table_html
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
|
| 162 |
# Cache
|
| 163 |
persist.save(key, full_html, "html")
|