Skip to content

Commit bf676e2

Browse files
committed
feat: forecast start date defaults to next open market date
Signed-off-by: ivelin <[email protected]>
1 parent 03d67d1 commit bf676e2

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

forecast.sh

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
#!/usr/bin/bash
2+
# Use this script to run forecast for multiple periods
3+
# with custom forecast start dates
24
echo "Running forecast for multiple periods"
35
set -exv
46

5-
7+
# without setting a specific forecast start date, forecast will start from the next open market date
68
#./canswim.sh forecast
9+
710
#./canswim.sh forecast --forecast_start_date "2023-11-20"
811
#./canswim.sh forecast --forecast_start_date "2023-12-04"
912
#./canswim.sh forecast --forecast_start_date "2023-12-18"
@@ -50,4 +53,5 @@ set -exv
5053
#./canswim.sh forecast --forecast_start_date "2024-11-25"
5154
#./canswim.sh forecast --forecast_start_date "2024-12-02"
5255
#./canswim.sh forecast --forecast_start_date "2024-12-09"
53-
./canswim.sh forecast --forecast_start_date "2024-12-16"
56+
#./canswim.sh forecast --forecast_start_date "2024-12-16"
57+
#./canswim.sh forecast --forecast_start_date "2024-12-23"

setup.cfg

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ python_requires = >=3.10
2424
install_requires =
2525
darts==0.27.2
2626
yfinance[nospam]
27+
pandas_market_calendars
2728
fmpsdk
2829
optuna==3.4.0
2930
huggingface_hub

src/canswim/forecast.py

+34
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
from canswim import constants
1313
from typing import List
1414
import duckdb
15+
from datetime import datetime, timedelta
16+
import pandas_market_calendars as mcal
1517

1618

1719
class CanswimForecaster:
@@ -233,12 +235,44 @@ def upload_data(self):
233235
self.hfhub.upload_data()
234236

235237

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+
236263
# main function
237264
def main(forecast_start_date: str = None):
238265
logger.info("Running forecast on stocks and uploading results to HF Hub...")
239266
if forecast_start_date is not None:
240267
logger.info(f"forecast_start_date: {forecast_start_date}")
241268
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+
242276
cf = CanswimForecaster()
243277
cf.download_model()
244278
cf.download_data()

0 commit comments

Comments
 (0)