eshan6704 commited on
Commit
8b6696b
·
verified ·
1 Parent(s): 5662f4a

Update stock.py

Browse files
Files changed (1) hide show
  1. stock.py +102 -53
stock.py CHANGED
@@ -8,6 +8,7 @@ from persist import *
8
 
9
  #file_name = f"bhav/bhav_{date_str.replace('-', '_')}.csv"
10
  #upload_file("eshanhf",file_name,df)
 
11
  # ================================================================
12
  # BASIC YFINANCE FETCHERS
13
  # ================================================================
@@ -39,11 +40,11 @@ import yfinance as yf
39
 
40
  def intraday(symbol):
41
  print(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] yf called for {symbol}")
42
- return yf.download(symbol + ".NS", period="1y", interval="1d").round(2)
43
 
44
 
45
  def daily(symbol):
46
- print("yf called")
47
  return yf.download(symbol + ".NS", period="1y", interval="1d").round(2)
48
 
49
 
@@ -135,100 +136,136 @@ def fetch_daily(symbol):
135
 
136
 
137
  # -------------------------- QUARTERLY ------------------------------
138
-
139
  def fetch_qresult(symbol):
 
 
 
 
 
 
 
140
  try:
141
  df = qresult(symbol)
142
  if df.empty:
143
  return wrap_html(f"<h1>No quarterly results for {symbol}</h1>")
144
- file_name = f"qresult/{symbol}.csv"
145
- upload_file("eshanhf",file_name,df)
 
 
 
146
  df_fmt = df.copy()
147
  for col in df_fmt.columns:
148
- df_fmt[col] = df_fmt[col].apply(
149
- lambda x: format_large_number(x) if isinstance(x, (int, float)) else x
150
- )
151
  df_fmt.index = [str(i.date()) if hasattr(i, "date") else str(i) for i in df_fmt.index]
152
 
153
- return wrap_html(make_table(df_fmt),
154
- title=f"{symbol} Quarterly Results")
155
 
 
156
  except Exception as e:
 
157
  return wrap_html(html_error(f"Quarterly Error: {e}"))
158
 
159
 
160
  # -------------------------- ANNUAL ------------------------------
161
-
162
  def fetch_result(symbol):
 
 
 
 
 
 
 
163
  try:
164
  df = result(symbol)
165
  if df.empty:
166
  return wrap_html(f"<h1>No annual results for {symbol}</h1>")
167
- file_name = f"result/{symbol}.csv"
168
- upload_file("eshanhf",file_name,df)
 
169
  df_fmt = df.copy()
170
  for col in df_fmt.columns:
171
- df_fmt[col] = df_fmt[col].apply(
172
- lambda x: format_large_number(x) if isinstance(x, (int, float)) else x
173
- )
174
  df_fmt.index = [str(i.date()) if hasattr(i, "date") else str(i) for i in df_fmt.index]
175
 
176
- return wrap_html(make_table(df_fmt),
177
- title=f"{symbol} Annual Results")
178
 
 
179
  except Exception as e:
 
180
  return wrap_html(html_error(f"Annual Error: {e}"))
181
 
182
 
183
  # -------------------------- BALANCE ------------------------------
184
-
185
  def fetch_balance(symbol):
 
 
 
 
 
 
 
186
  try:
187
  df = balance(symbol)
188
  if df.empty:
189
  return wrap_html(f"<h1>No balance sheet for {symbol}</h1>")
190
- file_name = f"balance/{symbol}.csv"
191
- upload_file("eshanhf",file_name,df)
 
192
  df_fmt = df.copy()
193
  for col in df_fmt.columns:
194
- df_fmt[col] = df_fmt[col].apply(
195
- lambda x: format_large_number(x) if isinstance(x, (int, float)) else x
196
- )
197
  df_fmt.index = [str(i.date()) if hasattr(i, "date") else str(i) for i in df_fmt.index]
198
 
199
- return wrap_html(make_table(df_fmt),
200
- title=f"{symbol} Balance Sheet")
201
 
 
202
  except Exception as e:
 
203
  return wrap_html(html_error(f"Balance Error: {e}"))
204
 
205
 
206
  # -------------------------- CASHFLOW ------------------------------
207
-
208
  def fetch_cashflow(symbol):
 
 
 
 
 
 
 
209
  try:
210
  df = cashflow(symbol)
211
  if df.empty:
212
  return wrap_html(f"<h1>No cashflow for {symbol}</h1>")
213
- file_name = f"cashflow/{symbol}.csv"
214
- upload_file("eshanhf",file_name,df)
 
215
  df_fmt = df.copy()
216
  for col in df_fmt.columns:
217
- df_fmt[col] = df_fmt[col].apply(
218
- lambda x: format_large_number(x) if isinstance(x, (int, float)) else x
219
- )
220
  df_fmt.index = [str(i.date()) if hasattr(i, "date") else str(i) for i in df_fmt.index]
221
 
222
- return wrap_html(make_table(df_fmt),
223
- title=f"{symbol} Cash Flow")
224
 
 
225
  except Exception as e:
 
226
  return wrap_html(html_error(f"Cash Flow Error: {e}"))
227
 
228
 
229
  # -------------------------- DIVIDEND ------------------------------
230
-
231
  def fetch_dividend(symbol):
 
 
 
 
 
 
 
232
  try:
233
  df = dividend(symbol)
234
  if df.empty:
@@ -236,21 +273,27 @@ def fetch_dividend(symbol):
236
 
237
  df_fmt = df.copy()
238
  for col in df_fmt.columns:
239
- df_fmt[col] = df_fmt[col].apply(
240
- lambda x: format_large_number(x) if isinstance(x, (int, float)) else x
241
- )
242
  df_fmt.index = [str(i.date()) if hasattr(i, "date") else str(i) for i in df_fmt.index]
243
 
244
- return wrap_html(make_table(df_fmt),
245
- title=f"{symbol} Dividends")
246
 
 
247
  except Exception as e:
 
248
  return wrap_html(html_error(f"Dividend Error: {e}"))
249
 
250
 
251
  # -------------------------- SPLIT ------------------------------
252
-
253
  def fetch_split(symbol):
 
 
 
 
 
 
 
254
  try:
255
  df = split(symbol)
256
  if df.empty:
@@ -258,36 +301,42 @@ def fetch_split(symbol):
258
 
259
  df_fmt = df.copy()
260
  for col in df_fmt.columns:
261
- df_fmt[col] = df_fmt[col].apply(
262
- lambda x: format_large_number(x) if isinstance(x, (int, float)) else x
263
- )
264
  df_fmt.index = [str(i.date()) if hasattr(i, "date") else str(i) for i in df_fmt.index]
265
 
266
- return wrap_html(make_table(df_fmt),
267
- title=f"{symbol} Splits")
268
 
 
269
  except Exception as e:
 
270
  return wrap_html(html_error(f"Split Error: {e}"))
271
 
272
 
273
  # -------------------------- OTHER (EARNINGS) ------------------------------
274
-
275
  def fetch_other(symbol):
 
 
 
 
 
 
 
276
  try:
277
  ticker = yf.Ticker(symbol + ".NS")
278
  df = ticker.earnings
279
-
280
  if df.empty:
281
  return wrap_html(f"<h1>No earnings data for {symbol}</h1>")
282
 
283
  df_fmt = df.copy()
284
  for col in df_fmt.columns:
285
- df_fmt[col] = df_fmt[col].apply(
286
- lambda x: format_large_number(x) if isinstance(x, (int, float)) else x
287
- )
288
 
289
- return wrap_html(make_table(df_fmt),
290
- title=f"{symbol} Earnings")
291
 
 
292
  except Exception as e:
 
293
  return wrap_html(html_error(f"Earnings Error: {e}"))
 
 
8
 
9
  #file_name = f"bhav/bhav_{date_str.replace('-', '_')}.csv"
10
  #upload_file("eshanhf",file_name,df)
11
+
12
  # ================================================================
13
  # BASIC YFINANCE FETCHERS
14
  # ================================================================
 
40
 
41
  def intraday(symbol):
42
  print(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] yf called for {symbol}")
43
+ return yf.download(symbol + ".NS", period="1d", interval="5m").round(2)
44
 
45
 
46
  def daily(symbol):
47
+ print(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] yf called for {symbol}")
48
  return yf.download(symbol + ".NS", period="1y", interval="1d").round(2)
49
 
50
 
 
136
 
137
 
138
  # -------------------------- QUARTERLY ------------------------------
 
139
  def fetch_qresult(symbol):
140
+ key = f"qresult_{symbol}"
141
+ if exists(key, "html"):
142
+ cached = load(key, "html")
143
+ if cached is not False:
144
+ print(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] Using cached quarterly for {symbol}")
145
+ return cached
146
+
147
  try:
148
  df = qresult(symbol)
149
  if df.empty:
150
  return wrap_html(f"<h1>No quarterly results for {symbol}</h1>")
151
+
152
+ # Optional upload
153
+ upload_file("eshanhf", f"qresult/{symbol}.csv", df)
154
+
155
+ # Format
156
  df_fmt = df.copy()
157
  for col in df_fmt.columns:
158
+ df_fmt[col] = df_fmt[col].apply(lambda x: format_large_number(x) if isinstance(x, (int, float)) else x)
 
 
159
  df_fmt.index = [str(i.date()) if hasattr(i, "date") else str(i) for i in df_fmt.index]
160
 
161
+ html = wrap_html(make_table(df_fmt), title=f"{symbol} Quarterly Results")
162
+ save(key, html, "html")
163
 
164
+ return html
165
  except Exception as e:
166
+ print(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] Error fetch_qresult: {e}")
167
  return wrap_html(html_error(f"Quarterly Error: {e}"))
168
 
169
 
170
  # -------------------------- ANNUAL ------------------------------
 
171
  def fetch_result(symbol):
172
+ key = f"result_{symbol}"
173
+ if exists(key, "html"):
174
+ cached = load(key, "html")
175
+ if cached is not False:
176
+ print(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] Using cached annual for {symbol}")
177
+ return cached
178
+
179
  try:
180
  df = result(symbol)
181
  if df.empty:
182
  return wrap_html(f"<h1>No annual results for {symbol}</h1>")
183
+
184
+ upload_file("eshanhf", f"result/{symbol}.csv", df)
185
+
186
  df_fmt = df.copy()
187
  for col in df_fmt.columns:
188
+ df_fmt[col] = df_fmt[col].apply(lambda x: format_large_number(x) if isinstance(x, (int, float)) else x)
 
 
189
  df_fmt.index = [str(i.date()) if hasattr(i, "date") else str(i) for i in df_fmt.index]
190
 
191
+ html = wrap_html(make_table(df_fmt), title=f"{symbol} Annual Results")
192
+ save(key, html, "html")
193
 
194
+ return html
195
  except Exception as e:
196
+ print(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] Error fetch_result: {e}")
197
  return wrap_html(html_error(f"Annual Error: {e}"))
198
 
199
 
200
  # -------------------------- BALANCE ------------------------------
 
201
  def fetch_balance(symbol):
202
+ key = f"balance_{symbol}"
203
+ if exists(key, "html"):
204
+ cached = load(key, "html")
205
+ if cached is not False:
206
+ print(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] Using cached balance for {symbol}")
207
+ return cached
208
+
209
  try:
210
  df = balance(symbol)
211
  if df.empty:
212
  return wrap_html(f"<h1>No balance sheet for {symbol}</h1>")
213
+
214
+ upload_file("eshanhf", f"balance/{symbol}.csv", df)
215
+
216
  df_fmt = df.copy()
217
  for col in df_fmt.columns:
218
+ df_fmt[col] = df_fmt[col].apply(lambda x: format_large_number(x) if isinstance(x, (int, float)) else x)
 
 
219
  df_fmt.index = [str(i.date()) if hasattr(i, "date") else str(i) for i in df_fmt.index]
220
 
221
+ html = wrap_html(make_table(df_fmt), title=f"{symbol} Balance Sheet")
222
+ save(key, html, "html")
223
 
224
+ return html
225
  except Exception as e:
226
+ print(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] Error fetch_balance: {e}")
227
  return wrap_html(html_error(f"Balance Error: {e}"))
228
 
229
 
230
  # -------------------------- CASHFLOW ------------------------------
 
231
  def fetch_cashflow(symbol):
232
+ key = f"cashflow_{symbol}"
233
+ if exists(key, "html"):
234
+ cached = load(key, "html")
235
+ if cached is not False:
236
+ print(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] Using cached cashflow for {symbol}")
237
+ return cached
238
+
239
  try:
240
  df = cashflow(symbol)
241
  if df.empty:
242
  return wrap_html(f"<h1>No cashflow for {symbol}</h1>")
243
+
244
+ upload_file("eshanhf", f"cashflow/{symbol}.csv", df)
245
+
246
  df_fmt = df.copy()
247
  for col in df_fmt.columns:
248
+ df_fmt[col] = df_fmt[col].apply(lambda x: format_large_number(x) if isinstance(x, (int, float)) else x)
 
 
249
  df_fmt.index = [str(i.date()) if hasattr(i, "date") else str(i) for i in df_fmt.index]
250
 
251
+ html = wrap_html(make_table(df_fmt), title=f"{symbol} Cash Flow")
252
+ save(key, html, "html")
253
 
254
+ return html
255
  except Exception as e:
256
+ print(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] Error fetch_cashflow: {e}")
257
  return wrap_html(html_error(f"Cash Flow Error: {e}"))
258
 
259
 
260
  # -------------------------- DIVIDEND ------------------------------
 
261
  def fetch_dividend(symbol):
262
+ key = f"dividend_{symbol}"
263
+ if exists(key, "html"):
264
+ cached = load(key, "html")
265
+ if cached is not False:
266
+ print(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] Using cached dividend for {symbol}")
267
+ return cached
268
+
269
  try:
270
  df = dividend(symbol)
271
  if df.empty:
 
273
 
274
  df_fmt = df.copy()
275
  for col in df_fmt.columns:
276
+ df_fmt[col] = df_fmt[col].apply(lambda x: format_large_number(x) if isinstance(x, (int, float)) else x)
 
 
277
  df_fmt.index = [str(i.date()) if hasattr(i, "date") else str(i) for i in df_fmt.index]
278
 
279
+ html = wrap_html(make_table(df_fmt), title=f"{symbol} Dividends")
280
+ save(key, html, "html")
281
 
282
+ return html
283
  except Exception as e:
284
+ print(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] Error fetch_dividend: {e}")
285
  return wrap_html(html_error(f"Dividend Error: {e}"))
286
 
287
 
288
  # -------------------------- SPLIT ------------------------------
 
289
  def fetch_split(symbol):
290
+ key = f"split_{symbol}"
291
+ if exists(key, "html"):
292
+ cached = load(key, "html")
293
+ if cached is not False:
294
+ print(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] Using cached split for {symbol}")
295
+ return cached
296
+
297
  try:
298
  df = split(symbol)
299
  if df.empty:
 
301
 
302
  df_fmt = df.copy()
303
  for col in df_fmt.columns:
304
+ df_fmt[col] = df_fmt[col].apply(lambda x: format_large_number(x) if isinstance(x, (int, float)) else x)
 
 
305
  df_fmt.index = [str(i.date()) if hasattr(i, "date") else str(i) for i in df_fmt.index]
306
 
307
+ html = wrap_html(make_table(df_fmt), title=f"{symbol} Splits")
308
+ save(key, html, "html")
309
 
310
+ return html
311
  except Exception as e:
312
+ print(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] Error fetch_split: {e}")
313
  return wrap_html(html_error(f"Split Error: {e}"))
314
 
315
 
316
  # -------------------------- OTHER (EARNINGS) ------------------------------
 
317
  def fetch_other(symbol):
318
+ key = f"other_{symbol}"
319
+ if exists(key, "html"):
320
+ cached = load(key, "html")
321
+ if cached is not False:
322
+ print(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] Using cached earnings for {symbol}")
323
+ return cached
324
+
325
  try:
326
  ticker = yf.Ticker(symbol + ".NS")
327
  df = ticker.earnings
 
328
  if df.empty:
329
  return wrap_html(f"<h1>No earnings data for {symbol}</h1>")
330
 
331
  df_fmt = df.copy()
332
  for col in df_fmt.columns:
333
+ df_fmt[col] = df_fmt[col].apply(lambda x: format_large_number(x) if isinstance(x, (int, float)) else x)
 
 
334
 
335
+ html = wrap_html(make_table(df_fmt), title=f"{symbol} Earnings")
336
+ save(key, html, "html")
337
 
338
+ return html
339
  except Exception as e:
340
+ print(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] Error fetch_other: {e}")
341
  return wrap_html(html_error(f"Earnings Error: {e}"))
342
+