|
12 | 12 | from canswim import constants
|
13 | 13 | from typing import List
|
14 | 14 | import duckdb
|
| 15 | +from datetime import datetime, timedelta |
| 16 | +import pandas_market_calendars as mcal |
15 | 17 |
|
16 | 18 |
|
17 | 19 | class CanswimForecaster:
|
@@ -233,12 +235,44 @@ def upload_data(self):
|
233 | 235 | self.hfhub.upload_data()
|
234 | 236 |
|
235 | 237 |
|
| 238 | + |
| 239 | +def get_next_open_market_day(): |
| 240 | + # Get calendar for NYSE |
| 241 | + nyse = mcal.get_calendar('NYSE') |
| 242 | + |
| 243 | + # Get today's date |
| 244 | + today = datetime.now().date() |
| 245 | + |
| 246 | + # Look for the next valid trading day within a reasonably big window of 30 days |
| 247 | + valid_days = nyse.valid_days(start_date=today, end_date=today + timedelta(days=30)) |
| 248 | + |
| 249 | + next_trading_day = None |
| 250 | + |
| 251 | + if valid_days is not None and len(valid_days) > 0: |
| 252 | + next_trading_day = valid_days[0] |
| 253 | + |
| 254 | + if next_trading_day is not None: |
| 255 | + logger.info(f"The next open stock market date is: {next_trading_day}") |
| 256 | + else: |
| 257 | + logger.warning("No open market day found within the next 30 days.") |
| 258 | + |
| 259 | + # If we can't find a next valid day within 30 days (which shouldn't happen for NYSE), return None |
| 260 | + return next_trading_day |
| 261 | + |
| 262 | + |
236 | 263 | # main function
|
237 | 264 | def main(forecast_start_date: str = None):
|
238 | 265 | logger.info("Running forecast on stocks and uploading results to HF Hub...")
|
239 | 266 | if forecast_start_date is not None:
|
240 | 267 | logger.info(f"forecast_start_date: {forecast_start_date}")
|
241 | 268 | forecast_start_date = pd.Timestamp(forecast_start_date)
|
| 269 | + else: |
| 270 | + # get next open stock market date |
| 271 | + # Example usage |
| 272 | + forecast_start_date = get_next_open_market_day() |
| 273 | + |
| 274 | + logger.info(f"Forecast start date set to: {forecast_start_date}") |
| 275 | + |
242 | 276 | cf = CanswimForecaster()
|
243 | 277 | cf.download_model()
|
244 | 278 | cf.download_data()
|
|
0 commit comments