eshan6704 commited on
Commit
7b6c581
·
verified ·
1 Parent(s): 961053b

Update app/daily.py

Browse files
Files changed (1) hide show
  1. app/daily.py +27 -13
app/daily.py CHANGED
@@ -10,6 +10,23 @@ from . import persist
10
  from . import backblaze as b2
11
  from .common import wrap_html, format_large_number
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  # ===========================================================
14
  # Candlestick Pattern Detection (scalar-safe)
15
  # ===========================================================
@@ -51,7 +68,7 @@ def detect_patterns(df):
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,18 +79,17 @@ def fetch_daily(symbol, date_end, date_start, b2_save=False):
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}")
68
- df = yf.download(symbol + ".NS", start=start, end=end)
69
- if df.empty:
70
  return wrap_html(f"<h1>No daily data for {symbol}</h1>")
71
 
72
- # Flatten MultiIndex columns if present
73
- if isinstance(df.columns, pd.MultiIndex):
74
- df.columns = df.columns.get_level_values(0)
75
 
76
- df.reset_index(inplace=True)
 
 
77
  df["Date"] = df["Date"].dt.strftime("%d-%b-%Y")
78
 
79
  if b2_save:
@@ -113,7 +129,6 @@ def fetch_daily(symbol, date_end, date_start, b2_save=False):
113
  row_heights=[0.4,0.2,0.2,0.2],
114
  specs=[[{}],[{}],[{}],[{}]])
115
 
116
- # Candlestick + SMA/EMA/Bollinger
117
  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)
118
  fig.add_trace(go.Scatter(x=df["Date"], y=df["SMA20"], mode="lines", name="SMA20"), row=1, col=1)
119
  fig.add_trace(go.Scatter(x=df["Date"], y=df["SMA50"], mode="lines", name="SMA50"), row=1, col=1)
@@ -122,7 +137,7 @@ def fetch_daily(symbol, date_end, date_start, b2_save=False):
122
  fig.add_trace(go.Scatter(x=df["Date"], y=df["UpperBB"], mode="lines", name="UpperBB", line=dict(dash="dot")), row=1, col=1)
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"]
@@ -159,7 +174,6 @@ def fetch_daily(symbol, date_end, date_start, b2_save=False):
159
 
160
  full_html = chart_html + table_html + patterns_html + data_table_html
161
 
162
- # Cache
163
  persist.save(key, full_html, "html")
164
  return full_html
165
 
 
10
  from . import backblaze as b2
11
  from .common import wrap_html, format_large_number
12
 
13
+ # ===========================================================
14
+ # RAW DAILY FETCHER (from your stock.py)
15
+ # ===========================================================
16
+ def daily(symbol, date_end, date_start):
17
+ print(f"[{dt.now().strftime('%Y-%m-%d %H:%M:%S')}] yf called for {symbol}")
18
+
19
+ start = dt.strptime(date_start, "%d-%m-%Y").strftime("%Y-%m-%d")
20
+ end = dt.strptime(date_end, "%d-%m-%Y").strftime("%Y-%m-%d")
21
+
22
+ df = yf.download(symbol + ".NS", start=start, end=end).round(2)
23
+
24
+ # Flatten MultiIndex columns if present
25
+ if isinstance(df.columns, pd.MultiIndex):
26
+ df.columns = df.columns.get_level_values(0)
27
+
28
+ return df
29
+
30
  # ===========================================================
31
  # Candlestick Pattern Detection (scalar-safe)
32
  # ===========================================================
 
68
  return pd.DataFrame(columns=["Date", "Pattern"])
69
 
70
  # ===========================================================
71
+ # DASHBOARD BUILDER
72
  # ===========================================================
73
  def fetch_daily(symbol, date_end, date_start, b2_save=False):
74
  key = f"daily_{symbol}"
 
79
  return cached
80
 
81
  try:
82
+ df = daily(symbol, date_end, date_start)
83
+ if df is None or df.empty:
 
 
 
84
  return wrap_html(f"<h1>No daily data for {symbol}</h1>")
85
 
86
+ # Reset index if necessary
87
+ if not isinstance(df.index, pd.RangeIndex):
88
+ df.reset_index(inplace=True)
89
 
90
+ # Format date
91
+ df["Date"] = pd.to_datetime(df["Date"], errors='coerce')
92
+ df = df.dropna(subset=["Date"])
93
  df["Date"] = df["Date"].dt.strftime("%d-%b-%Y")
94
 
95
  if b2_save:
 
129
  row_heights=[0.4,0.2,0.2,0.2],
130
  specs=[[{}],[{}],[{}],[{}]])
131
 
 
132
  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)
133
  fig.add_trace(go.Scatter(x=df["Date"], y=df["SMA20"], mode="lines", name="SMA20"), row=1, col=1)
134
  fig.add_trace(go.Scatter(x=df["Date"], y=df["SMA50"], mode="lines", name="SMA50"), row=1, col=1)
 
137
  fig.add_trace(go.Scatter(x=df["Date"], y=df["UpperBB"], mode="lines", name="UpperBB", line=dict(dash="dot")), row=1, col=1)
138
  fig.add_trace(go.Scatter(x=df["Date"], y=df["LowerBB"], mode="lines", name="LowerBB", line=dict(dash="dot")), row=1, col=1)
139
 
140
+ # Highlight patterns
141
  if not patterns_df.empty:
142
  for _, row in patterns_df.iterrows():
143
  pattern_date = row["Date"]
 
174
 
175
  full_html = chart_html + table_html + patterns_html + data_table_html
176
 
 
177
  persist.save(key, full_html, "html")
178
  return full_html
179