Compare two lists and make a report with powershell

Viewed 36

Im trying to make a report for a trading system with Powershell. I have one txt-file (owned.txt) that contains my current tickers:

DNO.OL
EQNR.OL

And another txt-file (signals.txt) that contains buy/sell signals:

DNO.OL,Sell,25.01.2021,7.1580
DSV.CO,Buy,25.01.2021,982.4000

If one of the lines in signals.txt contains a ticker (eks "DNO.OL") with "Sell" that is also in owned.txt. I want to add that line from signals.txt to the report and remove that ticker (eks "DNO.OL") from owned.txt.

If one of the lines in signals.txt contains a ticker (eks "DVS.CO") with "Buy" and that ticker is not in owned.txt. I want that line from signals.txt to be added to the report and the ticker added (eks "DVS.CO") to the owned.txt file.

I have started with this code but it does not work as I want.

$report = "C:\temp\report.txt"

$ToDaySignals = Get-Content "C:\temp\signals.txt"
$OwnedList = "C:\temp\owned.txt"

ForEach ($line in $ToDaySignals)
{
    $part = $line -split ","
    
    
    If ( ((Get-Content $OwnedList) -notcontains $part[0]) -and (($part[1]) -eq "Buy") )
    {
        "Buy: " + $line | Out-file $report -Append
        $part[0] | Out-File $OwnedList -Encoding utf8 -Append
    }
    If ( ((Get-Content $OwnedList) -contains $part[0]) -and (($part[1]) -eq "Sell") )
    {
         "Sell: " + $line | Out-file $report -Append
         Set-Content -Path $OwnedList -Value (get-content -Path $OwnedList | Select-String -Pattern $part[0] -NotMatch)
    }

}
1 Answers

First I want to recommend to get out of the mindset of using text only files. This would be so much easier and cleaner if you utilize objects. Your signals file already appears to be CSV without the headers. To help you accomplish your stated goal, I will stick with the text based output for the report but I urge you to consider alternatives. There are also several repeated, potentially expensive commands such as Get-Content, Set-Content, and Out-File. It would be better to read each one time, build up all the end results then output just once.

$reportfile = "C:\temp\report.txt"

# Capture all of owned.txt in a string array
$owned = Get-Content "C:\temp\owned.txt" -ReadCount 1

# Import TodaySignals as a CSV
$ToDaySignals = Import-Csv "C:\temp\signals.txt" -Header Ticker,Signal,Date,Amount

# Capture every line that is output (only Buy: ... and Sell: ... lines will be output)
$report = foreach($line in $ToDaySignals)
{
    if($line.Signal -eq 'Buy' -and $owned -notcontains $line.ticker)
    {
        "Buy: {0},{1},{2},{3}" -f $line.Ticker,$line.Signal,$line.Date,$line.Amount
        $owned += $line.Ticker
    }
    elseif($line.Signal -eq 'Sell' -and $owned -contains $line.ticker)
    {
        "Sell: {0},{1},{2},{3}" -f $line.Ticker,$line.Signal,$line.Date,$line.Amount
        $owned = $owned | Where-Object {$_ -ne $line.Ticker}
    }
}

Set-Content -Value $report -Path $reportfile

Set-Content -Value $owned -Path "C:\temp\owned.txt"
Related