Jitendra12421 commited on
Commit
93d68ca
·
verified ·
1 Parent(s): 259978c

Upload 5 files

Browse files
Files changed (2) hide show
  1. app.py +16 -69
  2. requirements.txt +0 -2
app.py CHANGED
@@ -7,18 +7,12 @@ from zoneinfo import ZoneInfo
7
  from data_updater import update_daily_data, is_trading_day
8
  from forecaster_engine import generate_predictions
9
 
10
- from t5_engine import generate_t5_predictions
11
-
12
  IST = ZoneInfo("Asia/Kolkata")
13
  MARKET_CLOSE_BUFFER = time(15, 45) # Update runs after 3:45 PM
14
- T5_RUN_BUFFER = time(9, 21) # T+5 runs after 9:21 AM
15
  PREDICTIONS_FILE = os.path.join(os.path.dirname(__file__), "predictions.json")
16
- PREDICTIONS_FILE_T5 = os.path.join(os.path.dirname(__file__), "predictions_t5.json")
17
 
18
  app = FastAPI(title="HF NIFTY Forecaster Backend")
19
 
20
- import threading
21
-
22
  app.add_middleware(
23
  CORSMiddleware,
24
  allow_origins=["*"],
@@ -26,50 +20,16 @@ app.add_middleware(
26
  allow_headers=["*"],
27
  )
28
 
29
- @app.on_event("startup")
30
- def on_startup():
31
- def startup_check():
32
- now = datetime.now(IST)
33
- today = now.date()
34
- current_time = now.time()
35
-
36
- if not is_trading_day(today):
37
  return
38
 
39
- # If past 09:21 and T+5 predictions are missing, generate them
40
- if current_time >= T5_RUN_BUFFER and not os.path.exists(PREDICTIONS_FILE_T5):
41
- print("Startup: T+5 predictions missing and it's past 09:21. Backfilling...")
42
- try:
43
- generate_t5_predictions()
44
- except Exception as e:
45
- print(f"Startup T+5 generation failed: {e}")
46
-
47
- # If past 15:45 and EOD predictions are missing, generate them
48
- if current_time >= MARKET_CLOSE_BUFFER and not os.path.exists(PREDICTIONS_FILE):
49
- print("Startup: EOD predictions missing and it's past 15:45. Backfilling...")
50
- try:
51
- update_daily_data()
52
- generate_predictions()
53
- except Exception as e:
54
- print(f"Startup EOD generation failed: {e}")
55
-
56
- # Run in a background thread so it doesn't block Uvicorn startup
57
- threading.Thread(target=startup_check, daemon=True).start()
58
-
59
- def run_update_pipeline(is_t5=False):
60
- try:
61
- if is_t5:
62
- # Step 1: Generate T+5 predictions
63
- generate_t5_predictions()
64
- else:
65
- # Step 1: Update daily data
66
- res = update_daily_data()
67
- if res.get("status") == "error":
68
- print(f"Update failed: {res.get('reason')}")
69
- return
70
-
71
- # Step 2: Generate T+1 predictions
72
- generate_predictions()
73
  except Exception as e:
74
  print(f"Pipeline error: {e}")
75
 
@@ -83,16 +43,6 @@ def get_predictions():
83
 
84
  return data
85
 
86
- @app.get("/t5-predictions")
87
- def get_t5_predictions():
88
- if not os.path.exists(PREDICTIONS_FILE_T5):
89
- raise HTTPException(status_code=404, detail="T+5 Predictions not yet generated")
90
-
91
- with open(PREDICTIONS_FILE_T5, "r") as f:
92
- data = json.load(f)
93
-
94
- return data
95
-
96
  @app.post("/cron/update")
97
  def cron_trigger(background_tasks: BackgroundTasks):
98
  now = datetime.now(IST)
@@ -103,17 +53,14 @@ def cron_trigger(background_tasks: BackgroundTasks):
103
  if not is_trading_day(today):
104
  return {"status": "skipped", "reason": f"{today} is a holiday or weekend"}
105
 
106
- # Determine which pipeline to run
107
- if current_time >= MARKET_CLOSE_BUFFER:
108
- # Run standard T+1 end-of-day pipeline
109
- background_tasks.add_task(run_update_pipeline, is_t5=False)
110
- return {"status": "triggered", "message": "End-of-day T+1 update and forecast pipeline started in the background."}
111
- elif current_time >= T5_RUN_BUFFER and current_time < time(10, 0):
112
- # Run T+5 pipeline at 09:21
113
- background_tasks.add_task(run_update_pipeline, is_t5=True)
114
- return {"status": "triggered", "message": "Morning T+5 forecast pipeline started in the background."}
115
- else:
116
- return {"status": "skipped", "reason": f"Current time {current_time} is not in the execution windows (09:21-10:00 for T+5, after 15:45 for T+1)."}
117
 
118
  @app.get("/health")
119
  def health_check():
 
7
  from data_updater import update_daily_data, is_trading_day
8
  from forecaster_engine import generate_predictions
9
 
 
 
10
  IST = ZoneInfo("Asia/Kolkata")
11
  MARKET_CLOSE_BUFFER = time(15, 45) # Update runs after 3:45 PM
 
12
  PREDICTIONS_FILE = os.path.join(os.path.dirname(__file__), "predictions.json")
 
13
 
14
  app = FastAPI(title="HF NIFTY Forecaster Backend")
15
 
 
 
16
  app.add_middleware(
17
  CORSMiddleware,
18
  allow_origins=["*"],
 
20
  allow_headers=["*"],
21
  )
22
 
23
+ def run_update_pipeline():
24
+ try:
25
+ # Step 1: Update data
26
+ res = update_daily_data()
27
+ if res.get("status") == "error":
28
+ print(f"Update failed: {res.get('reason')}")
 
 
29
  return
30
 
31
+ # Step 2: Generate predictions
32
+ generate_predictions()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  except Exception as e:
34
  print(f"Pipeline error: {e}")
35
 
 
43
 
44
  return data
45
 
 
 
 
 
 
 
 
 
 
 
46
  @app.post("/cron/update")
47
  def cron_trigger(background_tasks: BackgroundTasks):
48
  now = datetime.now(IST)
 
53
  if not is_trading_day(today):
54
  return {"status": "skipped", "reason": f"{today} is a holiday or weekend"}
55
 
56
+ # 2. Check if it's past 3:45 PM
57
+ if current_time < MARKET_CLOSE_BUFFER:
58
+ return {"status": "skipped", "reason": "Market is still open or buffer not reached. Runs after 3:45 PM IST."}
59
+
60
+ # Trigger the full pipeline in the background so Netlify doesn't timeout
61
+ background_tasks.add_task(run_update_pipeline)
62
+
63
+ return {"status": "triggered", "message": "Update and forecast pipeline started in the background."}
 
 
 
64
 
65
  @app.get("/health")
66
  def health_check():
requirements.txt CHANGED
@@ -5,5 +5,3 @@ requests
5
  pandas_market_calendars
6
  pyarrow
7
  fastparquet
8
- scikit-learn
9
- joblib
 
5
  pandas_market_calendars
6
  pyarrow
7
  fastparquet