From bbcb935bf366045251ffae744d25e3ddeaaf2063 Mon Sep 17 00:00:00 2001 From: jongjae Date: Tue, 26 May 2026 14:55:12 +0900 Subject: [PATCH] =?UTF-8?q?=EA=B3=B5=ED=9C=B4=EC=9D=BC=20=EC=8A=A4?= =?UTF-8?q?=EC=BC=80=EC=A4=84=EB=9F=AC=20=EC=8A=A4=ED=82=B5=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=20=E2=80=94=20holidays=20=EB=9D=BC=EC=9D=B4=EB=B8=8C?= =?UTF-8?q?=EB=9F=AC=EB=A6=AC=20=ED=99=9C=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - scripts/_is_trading_day.py 신규: 한국 공휴일+대체공휴일 판단 (exit 0=거래일, 1=휴장) * holidays.SouthKorea() 기반, 제헌절(7/17) 오탐 제외 처리 - run_morning/midday/evening/bot.ps1: 스크립트 상단에 공휴일 체크 추가 * 휴장일이면 로그 기록 후 즉시 종료 (Claude/봇 미실행) Co-Authored-By: Claude Sonnet 4.6 --- scripts/_is_trading_day.py | 46 ++++++++++++++++++++++++++++++++++++++ scripts/run_bot.ps1 | 9 ++++++++ scripts/run_evening.ps1 | 9 ++++++++ scripts/run_midday.ps1 | 9 ++++++++ scripts/run_morning.ps1 | 9 ++++++++ 5 files changed, 82 insertions(+) create mode 100644 scripts/_is_trading_day.py 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)