Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #
- # Get-SOHLCV.ps1
- #
- #
- #
- [CmdletBinding()]
- param (
- [string[]]
- $File,
- [switch]
- $ExportAsObject
- )
- function Convert-CurrencyStringToDecimal ([string]$input)
- {
- ((($input -replace '\$') -replace '[)]') -replace '\(', '-') -replace '[^-0-9.]'
- }
- $global:sohlcvData = New-Object System.Collections.Generic.List[PSCustomObject]
- foreach ($f in $File)
- {
- if (-not (Test-Path $f))
- {
- throw "Cannot open file '$f'."
- }
- # read csv file
- $content = Get-Content -Path $f
- # find the lines that contain price information
- $csvdata = $content | ? {$_ -match ";.*;"} | ConvertFrom-Csv -Delimiter ';'
- # filter just the lines with (OHLC on them and make into CSV structure
- $data = $csvData | ? {$_ -match "\(SOHLCV"}
- foreach ($item in $data)
- {
- # capture the OHLCV data
- $null = $item.Strategy -match "\(SOHLCV\|(.*)\)"
- $v = $Matches[1] -split '\|'
- $symbol = $v[0]
- $open = $v[1] | Convert-CurrencyStringToDecimal
- $high = $v[2] | Convert-CurrencyStringToDecimal
- $low = $v[3] | Convert-CurrencyStringToDecimal
- $close = $v[4] | Convert-CurrencyStringToDecimal
- $volume = $v[5] | Convert-CurrencyStringToDecimal
- # add to our $sohlcvData array
- $null = $sohlcvData.Add(
- [PSCustomObject]@{
- 'Symbol' = $symbol
- 'DateTime' = ([datetime]::Parse($item.'Date/Time'))
- 'Open' = [decimal]$open
- 'High' = [decimal]$high
- 'Low' = [decimal]$low
- 'Close' = [decimal]$close
- 'Volume' = [decimal]$volume
- }
- )
- }
- }
- if ($ExportAsObject)
- {
- # helpful message to show caller our output variable
- Write-Output "Out Data $($sohlcvData.Count) items (exported as `$sohlcvData)"
- }
- else
- {
- # don't show any output, and just return the data to the pipeline
- return $sohlcvData
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement