Advertisement
StockJock

Get SOHLCV

Apr 28th, 2020
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #
  2. # Get-SOHLCV.ps1
  3. #
  4. #
  5. #
  6. [CmdletBinding()]
  7. param (
  8.     [string[]]
  9.     $File,
  10.     [switch]
  11.     $ExportAsObject
  12. )
  13. function Convert-CurrencyStringToDecimal ([string]$input)
  14. {
  15.     ((($input -replace '\$') -replace '[)]') -replace '\(', '-') -replace '[^-0-9.]'
  16. }
  17. $global:sohlcvData = New-Object System.Collections.Generic.List[PSCustomObject]
  18. foreach ($f in $File)
  19. {
  20.     if (-not (Test-Path $f))
  21.     {
  22.         throw "Cannot open file '$f'."
  23.     }
  24.     # read csv file
  25.     $content = Get-Content -Path $f
  26.     # find the lines that contain price information
  27.     $csvdata = $content | ? {$_ -match ";.*;"} | ConvertFrom-Csv -Delimiter ';'
  28.     # filter just the lines with (OHLC on them and make into CSV structure
  29.     $data = $csvData | ? {$_ -match "\(SOHLCV"}
  30.     foreach ($item in $data)
  31.     {
  32.         # capture the OHLCV data
  33.         $null = $item.Strategy -match "\(SOHLCV\|(.*)\)"
  34.         $v = $Matches[1] -split '\|'
  35.         $symbol = $v[0]
  36.         $open  = $v[1] | Convert-CurrencyStringToDecimal
  37.         $high  = $v[2] | Convert-CurrencyStringToDecimal
  38.         $low   = $v[3] | Convert-CurrencyStringToDecimal
  39.         $close = $v[4] | Convert-CurrencyStringToDecimal
  40.         $volume = $v[5] | Convert-CurrencyStringToDecimal
  41.         # add to our $sohlcvData array
  42.         $null = $sohlcvData.Add(
  43.             [PSCustomObject]@{
  44.                 'Symbol' = $symbol
  45.                 'DateTime' = ([datetime]::Parse($item.'Date/Time'))
  46.                 'Open' = [decimal]$open
  47.                 'High' = [decimal]$high
  48.                 'Low' = [decimal]$low
  49.                 'Close' = [decimal]$close
  50.                 'Volume' = [decimal]$volume
  51.                 }
  52.             )
  53.     }
  54. }
  55. if ($ExportAsObject)
  56. {
  57.     # helpful message to show caller our output variable
  58.     Write-Output "Out Data $($sohlcvData.Count) items (exported as `$sohlcvData)"
  59. }
  60. else
  61. {
  62.    # don't show any output, and just return the data to the pipeline
  63.    return $sohlcvData
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement