2026-05-27 16:53:52 +09:00
$ErrorActionPreference = " Stop "
$Root = Split-Path -Parent ( Split-Path -Parent $MyInvocation . MyCommand . Path )
Set-Location $Root
. " $Root \scripts\stockbot_env.ps1 "
$Python = Resolve-StockBotPython -Project $Root
$LogDir = Join-Path $Root " logs "
New-Item -ItemType Directory -Force -Path $LogDir | Out-Null
$LogPath = Join-Path $LogDir " training.log "
function Write-Log {
param ( [ string ] $Message )
$Line = " [{0}] {1} " -f ( Get-Date -Format " yyyy-MM-dd HH:mm:ss " ) , $Message
Add-Content -Path $LogPath -Value $Line -Encoding UTF8
Write-Host $Line
}
function Invoke-PythonStep {
param (
[ string ] $Name ,
2026-05-28 20:34:29 +09:00
[ string[] ] $StepArgs ,
2026-05-27 16:53:52 +09:00
[ bool ] $Required = $true
)
Write-Log $Name
2026-05-28 20:34:29 +09:00
$PreviousErrorActionPreference = $ErrorActionPreference
$ErrorActionPreference = " Continue "
$Output = & $Python @StepArgs 2 > & 1
$ErrorActionPreference = $PreviousErrorActionPreference
2026-05-27 16:53:52 +09:00
$Code = $LASTEXITCODE
2026-05-28 20:34:29 +09:00
foreach ( $Line in $Output ) {
Add-Content -Path $LogPath -Value $Line -Encoding UTF8
}
2026-05-27 16:53:52 +09:00
if ( $Code -ne 0 ) {
$Message = " $Name failed with exit code $Code "
if ( $Required ) {
throw $Message
}
Write-Log " warning: $Message "
}
}
2026-06-02 18:36:53 +09:00
try {
Write-Log " training pipeline started "
Write-Log " python resolved: $Python "
2026-05-27 16:53:52 +09:00
2026-06-02 18:36:53 +09:00
$HolidayCheck = & $Python scripts \ _is_trading_day . py 2 > & 1
if ( $LASTEXITCODE -ne 0 ) {
Write-Log " market closed - skipped ( $HolidayCheck ) "
exit 0
}
Write-Log " trading day check passed ( $HolidayCheck ) "
2026-05-27 16:53:52 +09:00
2026-06-02 18:36:53 +09:00
Invoke-PythonStep -Name " collecting daily market features " -StepArgs @ ( " scripts\collect_daily_features.py " ) -Required $false
2026-05-27 16:53:52 +09:00
2026-06-02 18:36:53 +09:00
Invoke-PythonStep -Name " collecting KIS minute data " -StepArgs @ ( " scripts\collect_minute_data.py " , " --top " , " 30 " , " --real-quotes " ) -Required $false
2026-05-27 16:53:52 +09:00
2026-06-02 18:36:53 +09:00
Invoke-PythonStep -Name " exporting bot training dataset " -StepArgs @ ( " scripts\export_training_dataset.py " , " data\training_dataset.csv " ) -Required $true
2026-05-27 16:53:52 +09:00
2026-06-02 18:36:53 +09:00
Invoke-PythonStep -Name " building external training dataset " -StepArgs @ ( " scripts\build_external_training_dataset.py " , " --out " , " data\external_training_dataset.csv " , " --all-minutes " ) -Required $true
2026-05-27 16:53:52 +09:00
2026-06-02 18:36:53 +09:00
Invoke-PythonStep -Name " training model " -StepArgs @ ( " scripts\train_ai_model.py " ) -Required $true
2026-05-27 16:53:52 +09:00
2026-06-02 18:36:53 +09:00
Write-Log " training pipeline finished "
}
catch {
Write-Log " training pipeline failed: $( $_ . Exception . Message ) "
if ( $_ . ScriptStackTrace ) {
Add-Content -Path $LogPath -Value $_ . ScriptStackTrace -Encoding UTF8
}
throw
}