diff --git a/scripts/_is_trading_day.py b/scripts/_is_trading_day.py new file mode 100644 index 0000000..2fc8c77 --- /dev/null +++ b/scripts/_is_trading_day.py @@ -0,0 +1,46 @@ +""" +한국 주식 시장 거래일 여부 확인 +exit code 0 = 거래일, 1 = 휴장일 + +사용: + python scripts/_is_trading_day.py + if ($LASTEXITCODE -ne 0) { exit } +""" +import sys +from datetime import date +import holidays + +# 라이브러리가 포함하지만 KRX는 개장하는 날 (2008년 이후 제헌절) +_KRX_OPEN_OVERRIDES = { + (7, 17), # 제헌절 — 2008년부터 공휴일 제외, 장 열림 +} + +def is_trading_day(d: date = None) -> tuple[bool, str]: + if d is None: + d = date.today() + + # 주말 + if d.weekday() >= 5: + return False, "주말" + + kr = holidays.SouthKorea(years=d.year) + + if d in kr: + name = kr[d] + # KRX 개장 예외 날짜는 무시 + if (d.month, d.day) in _KRX_OPEN_OVERRIDES: + return True, f"개장 (라이브러리 오류 무시: {name})" + return False, name + + return True, "거래일" + + +if __name__ == "__main__": + today = date.today() + trading, reason = is_trading_day(today) + if trading: + print(f"[거래일] {today} — {reason}") + sys.exit(0) + else: + print(f"[휴장일] {today} — {reason}") + sys.exit(1) diff --git a/scripts/run_bot.ps1 b/scripts/run_bot.ps1 index 6c6a5e4..c3a5ccf 100644 --- a/scripts/run_bot.ps1 +++ b/scripts/run_bot.ps1 @@ -8,10 +8,19 @@ $env:PYTHONIOENCODING = "utf-8" $PROJECT = Split-Path -Parent $PSScriptRoot $LOG = "$PROJECT\logs\bot_start.log" $CLAUDE = "C:\Users\whdwo\AppData\Roaming\npm\claude.cmd" +$PYTHON = "C:\Users\whdwo\.pyenv\pyenv-win\versions\3.11.9\python.exe" $utf8 = New-Object System.Text.UTF8Encoding $false Set-Location $PROJECT +# 공휴일 체크 — 휴장일이면 스킵 +$holiday_result = & $PYTHON "scripts\_is_trading_day.py" 2>&1 +if ($LASTEXITCODE -ne 0) { + $ts = Get-Date -Format "yyyy-MM-dd HH:mm:ss" + [System.IO.File]::AppendAllText($LOG, "[$ts] 휴장일 — 스킵 ($holiday_result)`n", $utf8) + exit +} + $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss" [System.IO.File]::AppendAllText($LOG, "[$timestamp] /start-bot 실행`n", $utf8) diff --git a/scripts/run_evening.ps1 b/scripts/run_evening.ps1 index cc1b5de..7aabdaf 100644 --- a/scripts/run_evening.ps1 +++ b/scripts/run_evening.ps1 @@ -10,10 +10,19 @@ $env:PYTHONIOENCODING = "utf-8" $PROJECT = Split-Path -Parent $PSScriptRoot $LOG = "$PROJECT\logs\evening.log" $CLAUDE = "C:\Users\whdwo\AppData\Roaming\npm\claude.cmd" +$PYTHON = "C:\Users\whdwo\.pyenv\pyenv-win\versions\3.11.9\python.exe" $utf8 = New-Object System.Text.UTF8Encoding $false Set-Location $PROJECT +# 공휴일 체크 — 휴장일이면 스킵 +$holiday_result = & $PYTHON "scripts\_is_trading_day.py" 2>&1 +if ($LASTEXITCODE -ne 0) { + $ts = Get-Date -Format "yyyy-MM-dd HH:mm:ss" + [System.IO.File]::AppendAllText($LOG, "[$ts] 휴장일 — 스킵 ($holiday_result)`n", $utf8) + exit +} + $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss" [System.IO.File]::AppendAllText($LOG, "[$timestamp] claude_evening 시작`n", $utf8) diff --git a/scripts/run_midday.ps1 b/scripts/run_midday.ps1 index 3ead57b..a93ce14 100644 --- a/scripts/run_midday.ps1 +++ b/scripts/run_midday.ps1 @@ -10,10 +10,19 @@ $env:PYTHONIOENCODING = "utf-8" $PROJECT = Split-Path -Parent $PSScriptRoot $LOG = "$PROJECT\logs\midday.log" $CLAUDE = "C:\Users\whdwo\AppData\Roaming\npm\claude.cmd" +$PYTHON = "C:\Users\whdwo\.pyenv\pyenv-win\versions\3.11.9\python.exe" $utf8 = New-Object System.Text.UTF8Encoding $false Set-Location $PROJECT +# 공휴일 체크 — 휴장일이면 스킵 +$holiday_result = & $PYTHON "scripts\_is_trading_day.py" 2>&1 +if ($LASTEXITCODE -ne 0) { + $ts = Get-Date -Format "yyyy-MM-dd HH:mm:ss" + [System.IO.File]::AppendAllText($LOG, "[$ts] 휴장일 — 스킵 ($holiday_result)`n", $utf8) + exit +} + $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss" [System.IO.File]::AppendAllText($LOG, "[$timestamp] claude_midday 시작`n", $utf8) diff --git a/scripts/run_morning.ps1 b/scripts/run_morning.ps1 index eec24b6..e7878cd 100644 --- a/scripts/run_morning.ps1 +++ b/scripts/run_morning.ps1 @@ -10,10 +10,19 @@ $env:PYTHONIOENCODING = "utf-8" $PROJECT = Split-Path -Parent $PSScriptRoot $LOG = "$PROJECT\logs\morning.log" $CLAUDE = "C:\Users\whdwo\AppData\Roaming\npm\claude.cmd" +$PYTHON = "C:\Users\whdwo\.pyenv\pyenv-win\versions\3.11.9\python.exe" $utf8 = New-Object System.Text.UTF8Encoding $false Set-Location $PROJECT +# 공휴일 체크 — 휴장일이면 스킵 +$holiday_result = & $PYTHON "scripts\_is_trading_day.py" 2>&1 +if ($LASTEXITCODE -ne 0) { + $ts = Get-Date -Format "yyyy-MM-dd HH:mm:ss" + [System.IO.File]::AppendAllText($LOG, "[$ts] 휴장일 — 스킵 ($holiday_result)`n", $utf8) + exit +} + $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss" [System.IO.File]::AppendAllText($LOG, "[$timestamp] claude_morning 시작`n", $utf8)