$ErrorActionPreference = "Stop" [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 $OutputEncoding = [System.Text.Encoding]::UTF8 chcp 65001 | Out-Null $Project = Split-Path -Parent $PSScriptRoot $TaskPath = "\StockBot\" $Weekdays = @( [DayOfWeek]::Monday, [DayOfWeek]::Tuesday, [DayOfWeek]::Wednesday, [DayOfWeek]::Thursday, [DayOfWeek]::Friday ) function Register-StockTask { param( [string]$Name, [string]$Time, [string]$Script, [int]$LimitMinutes ) $Trigger = New-ScheduledTaskTrigger -Weekly -DaysOfWeek $Weekdays -At $Time $Action = New-ScheduledTaskAction ` -Execute "powershell.exe" ` -Argument "-NonInteractive -ExecutionPolicy Bypass -File `"$Project\scripts\$Script`"" ` -WorkingDirectory $Project $Settings = New-ScheduledTaskSettingsSet ` -ExecutionTimeLimit (New-TimeSpan -Minutes $LimitMinutes) ` -StartWhenAvailable ` -DontStopIfGoingOnBatteries ` -RunOnlyIfNetworkAvailable:$false $Settings.DisallowStartIfOnBatteries = $false Register-ScheduledTask ` -TaskName $Name ` -TaskPath $TaskPath ` -Trigger $Trigger ` -Action $Action ` -Settings $Settings ` -RunLevel Limited ` -Force | Out-Null Write-Host "[OK] $Name registered at $Time" -ForegroundColor Green } function Register-WatchdogTask { $TaskName = "\StockBot\StockBot_Watchdog" $ScriptPath = Join-Path $Project "scripts\run_watchdog.ps1" $Command = 'schtasks /Create /TN "\StockBot\StockBot_Watchdog" /TR "\"powershell.exe\" -NonInteractive -ExecutionPolicy Bypass -File \"' + $ScriptPath + '\"" /SC MINUTE /MO 5 /ST 09:00 /ET 15:10 /F' cmd.exe /c $Command | Out-Null if ($LASTEXITCODE -ne 0) { throw "StockBot_Watchdog registration failed" } Write-Host "[OK] StockBot_Watchdog registered at 09:00-15:10 every 5 minutes" -ForegroundColor Green } Register-StockTask "StockBot_Morning" "08:15" "run_morning.ps1" 20 Register-StockTask "StockBot_Midday" "11:20" "run_midday.ps1" 20 Register-StockTask "StockBot_Evening" "15:30" "run_evening.ps1" 30 Register-StockTask "StockBot_Training" "16:00" "run_training_pipeline.ps1" 60 Register-WatchdogTask $BotTask = Get-ScheduledTask -TaskName "StockBot_Bot" -TaskPath $TaskPath -ErrorAction SilentlyContinue if ($BotTask) { Disable-ScheduledTask -TaskName "StockBot_Bot" -TaskPath $TaskPath | Out-Null Write-Host "[OK] StockBot_Bot disabled" -ForegroundColor Yellow } Write-Host "" Write-Host "Registered StockBot tasks:" -ForegroundColor Cyan Get-ScheduledTask -TaskPath $TaskPath | Format-Table TaskName, State