Param ( [ValidateScript({ if ($_ -match "^\d{4}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])$") { # If the input matches the regex, return $true (valid) $true } else { # Throw a custom error message if the input is invalid throw "The date format must be 'yyyyMMdd' (e.g., 20240928). You entered '$($_)'." } })] [string]$date = $null ) function Get-TradingDate { # Get the current date and time in Eastern Time (ET) $easternTimeZone = [System.TimeZoneInfo]::FindSystemTimeZoneById("Eastern Standard Time") $currentDateTime = [System.TimeZoneInfo]::ConvertTime([DateTime]::UtcNow, $easternTimeZone) # Get the current time and day of the week $currentTime = $currentDateTime.TimeOfDay $currentDay = $currentDateTime.DayOfWeek # Define 9:30 AM as a TimeSpan object (9 hours, 30 minutes, 0 seconds) $time930am = New-TimeSpan -Hours 9 -Minutes 30 # Adjust for weekends or time before 9:30 AM if ($currentDay -eq 'Saturday') { # If it's Saturday, return the previous Friday $adjustedDate = $currentDateTime.AddDays(-1) } elseif ($currentDay -eq 'Sunday') { # If it's Sunday, return the previous Friday $adjustedDate = $currentDateTime.AddDays(-2) } elseif ($currentDay -eq 'Monday' -and $currentTime -lt $time930am) { # If it's Monday before 9:30 AM, return the previous Friday $adjustedDate = $currentDateTime.AddDays(-3) } elseif ($currentTime -lt $time930am) { # If it's any other weekday before 9:30 AM, return the previous day $adjustedDate = $currentDateTime.AddDays(-1) } else { # Otherwise, return the current date $adjustedDate = $currentDateTime } # Return the adjusted date in yyyymmdd format return $adjustedDate.ToString("yyyyMMdd") } if (-not $date) { $date = Get-TradingDate } Write-Host "The trading date is $date" # Construct the input file path $documentsFolder = [Environment]::GetFolderPath("MyDocuments") $inputFile = Join-Path -Path $documentsFolder -ChildPath "TradeIdeasPro\robotlogs\log.$date.txt" # Output file path $outputFile = Join-Path -Path $documentsFolder -ChildPath "TradeIdeasPro\robotlogs\processed_log.$date.txt" # Check if the input file exists if (-not (Test-Path $inputFile)) { Write-Host "Input file not found: $inputFile" exit } # Process the input file and store results in an array $processedLines = @(Get-Content $inputFile | ForEach-Object { if ($_ -match '(\d{4}-\d{2}-\d{2}) (\d{2}:\d{2}:\d{2}).\d+\tExecution Received\tGot execution: ([A-Z]+),(\d+),(sell|buy),(\d+\.\d+),.*') { $tradedate = $matches[1] $time = $matches[2] $symbol = $matches[3] $quantity = $matches[4] $price = $matches[6] $side = (Get-Culture).TextInfo.ToTitleCase($matches[5]) # Format the output line "$tradedate,$time,$symbol,$quantity,$price,$side,0.00" } }) $processedLines = $processedLines -join "`n" # # The text you want to prepend $header = "Date,Time,Symbol,Quantity,Price,Side,Commission" # Prepend the header to the existing content of $processedLines $processedLines = $header + "`n" + $processedLines # Write to output file $processedLines | Set-Content $outputFile # Copy to clipboard $processedLines | Set-Clipboard Write-Host "Processing complete. Output written to $outputFile and copied to clipboard."