Files
hyper/clp_auto_hedger/cleanup_hedger.ps1

195 lines
6.3 KiB
PowerShell

#!/usr/bin/env pwsh
<#
.SYNOPSIS
Cleanup script for CLP Auto Hedger processes and configurations
.DESCRIPTION
Kills Python processes related to the hedger, removes configurations,
and prepares the system for a fresh start.
.AUTHOR
System Administrator
.DATE
December 19, 2025
#>
# Set strict mode for safety
Set-StrictMode -Version Latest
# Color output functions
function Write-Info {
param([string]$Message)
Write-Host "[INFO] $Message" -ForegroundColor Cyan
}
function Write-Success {
param([string]$Message)
Write-Host "[SUCCESS] $Message" -ForegroundColor Green
}
function Write-Warning {
param([string]$Message)
Write-Host "[WARNING] $Message" -ForegroundColor Yellow
}
function Write-Error {
param([string]$Message)
Write-Host "[ERROR] $Message" -ForegroundColor Red
}
try {
Write-Info "Starting CLP Auto Hedger cleanup process..."
# Get current directory
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
Set-Location $ScriptDir
# Kill Python processes related to hedger
Write-Info "Searching for Python processes related to hedger..."
# Find Python processes with hedger-related keywords
$PythonProcesses = Get-Process -Name "python" -ErrorAction SilentlyContinue | Where-Object {
try {
$MainWindowTitle = $_.MainWindowTitle
if ($MainWindowTitle -and ($MainWindowTitle -match "hedger|clp|scalper" -or $MainWindowTitle -match "clp_auto_hedger")) {
return $true
}
# Check command line arguments if possible
$ProcessId = $_.Id
$CommandLine = (Get-WmiObject Win32_Process -Filter "ProcessId=$ProcessId").CommandLine
if ($CommandLine -and ($CommandLine -match "hedger|clp|scalper|clp_auto_hedger")) {
return $true
}
return $false
}
catch {
return $false
}
}
if ($PythonProcesses) {
Write-Info "Found $($PythonProcesses.Count) Python hedger processes. Terminating..."
foreach ($Process in $PythonProcesses) {
try {
Write-Info "Terminating process PID: $($Process.Id)"
$Process.Kill()
$Process.WaitForExit(5000) # Wait up to 5 seconds
Write-Success "Successfully terminated PID: $($Process.Id)"
}
catch {
Write-Warning "Failed to terminate PID: $($Process.Id) - $($_.Exception.Message)"
}
}
}
else {
Write-Info "No Python hedger processes found"
}
# Also look for pythonw processes (Windows GUI Python)
$PythonWProcesses = Get-Process -Name "pythonw" -ErrorAction SilentlyContinue | Where-Object {
try {
$ProcessId = $_.Id
$CommandLine = (Get-WmiObject Win32_Process -Filter "ProcessId=$ProcessId").CommandLine
return $CommandLine -and ($CommandLine -match "hedger|clp|scalper|clp_auto_hedger")
}
catch {
return $false
}
}
if ($PythonWProcesses) {
Write-Info "Found $($PythonWProcesses.Count) pythonw hedger processes. Terminating..."
foreach ($Process in $PythonWProcesses) {
try {
Write-Info "Terminating pythonw process PID: $($Process.Id)"
$Process.Kill()
$Process.WaitForExit(5000)
Write-Success "Successfully terminated pythonw PID: $($Process.Id)"
}
catch {
Write-Warning "Failed to terminate pythonw PID: $($Process.Id) - $($_.Exception.Message)"
}
}
}
# Clean up configuration files
Write-Info "Cleaning up configuration files..."
$ConfigFiles = @(
"hedge_status.json",
"range_config.py",
"trade_state.json"
)
foreach ($ConfigFile in $ConfigFiles) {
$FilePath = Join-Path $ScriptDir $ConfigFile
if (Test-Path $FilePath) {
try {
Write-Info "Removing configuration file: $ConfigFile"
Remove-Item $FilePath -Force
Write-Success "Removed: $ConfigFile"
}
catch {
Write-Warning "Failed to remove $ConfigFile - $($_.Exception.Message)"
}
}
else {
Write-Info "Configuration file not found: $ConfigFile (this is OK)"
}
}
# Clean up log files if requested
$CleanLogs = Read-Host "Do you want to clean up log files? (y/N)"
if ($CleanLogs -match '^y|Y|yes|YES$') {
Write-Info "Cleaning up log files..."
$LogFiles = Get-ChildItem -Path "logs\*.log" -ErrorAction SilentlyContinue
foreach ($LogFile in $LogFiles) {
try {
Write-Info "Removing log file: $($LogFile.Name)"
Remove-Item $LogFile.FullName -Force
Write-Success "Removed log file: $($LogFile.Name)"
}
catch {
Write-Warning "Failed to remove log file $($LogFile.Name) - $($_.Exception.Message)"
}
}
}
# Check for any remaining Python processes
Write-Info "Checking for any remaining Python processes..."
$RemainingPython = Get-Process -Name "python", "pythonw" -ErrorAction SilentlyContinue
if ($RemainingPython) {
Write-Warning "Found $($RemainingPython.Count) Python processes still running:"
$RemainingPython | ForEach-Object {
Write-Warning " PID: $($_.Id), Name: $($_.ProcessName)"
}
}
else {
Write-Success "No Python processes found"
}
Write-Success "Cleanup completed successfully!"
Write-Info "System is ready for a fresh start of the CLP Auto Hedger"
}
catch {
Write-Error "Cleanup failed: $($_.Exception.Message)"
Write-Error "Stack trace: $($_.ScriptStackTrace)"
exit 1
}
# Optional: Ask if user wants to start fresh
$StartFresh = Read-Host "Do you want to run the hedger with a clean slate now? (y/N)"
if ($StartFresh -match '^y|Y|yes|YES$') {
Write-Info "Starting CLP Auto Hedger with clean configuration..."
try {
python clp_scalper_hedger.py
}
catch {
Write-Error "Failed to start hedger: $($_.Exception.Message)"
}
}