Scripts/Get-CoinbaseProductTicker.ps1
Function Get-CoinbaseProductTicker { <# .SYNOPSIS Gets snapshot information about the last trade (tick), best bid/ask and 24h volume .DESCRIPTION Gets snapshot information about the last trade (tick), best bid/ask and 24h volume .PARAMETER Product Product Id .PARAMETER ShowChart (optional) Show the online chart for a product (up to 10) .EXAMPLE Get-CoinbaseProductTicker -Product BTC-USD .EXAMPLE Get-CoinbaseProductTicker -Product BTC-USD, ETH-USD -ShowChart .NOTES N/A .LINK N/A #> [CmdletBinding ()] Param ( [Switch]$ShowChart ) DynamicParam { $ParameterNamePair = 'Product' $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute] $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute $ParameterAttribute.Mandatory = $True $ParameterAttribute.HelpMessage = 'Enter currency' $ParameterAttribute.Position = 0 $AttributeCollection.Add($ParameterAttribute) $RuntimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary $ArraySet = Get-Content -Path ("$env:ALLUSERSPROFILE\Coinbase\Pairs.txt" ) $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($ArraySet) $AttributeCollection.Add($ValidateSetAttribute) $RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($ParameterNamePair, [String[]], $AttributeCollection) $RuntimeParameterDictionary.Add($ParameterNamePair, $RuntimeParameter) Return $RuntimeParameterDictionary } BEGIN { $Products = $PSBoundParameters[$ParameterNamePair] Function Show-Output ($Values) { [PSCustomObject]@{ Name = $Coin.ToUpper() Ask = $Values.Ask Bid = $Values.Bid Volume = $Values.Volume TradeId = $Values.Trade_Id Price = $Values.Price Time = Get-Date($Values.Time) RFQVolume = $Values.RFQ_Volume Status = $Status } } } PROCESS { Try { $Headers = New-Object 'System.Collections.Generic.Dictionary[[String],[String]]' $Headers.Add('Content-Type', 'application/json') $Counter = 1 ForEach ($Coin In $Products) { $Response = Invoke-RestMethod "https://api.exchange.coinbase.com/products/$Coin/ticker" -Method 'GET' -Headers $Headers ForEach ($Item In $Response) { $Status = 'Ok' Show-Output ($Item) If ($ShowChart -and ($Counter -le 10)) { Start-Process -FilePath "https://www.coinbase.com/advanced-trade/spot/$Coin" $Counter++ } } Start-Sleep -Milliseconds 100 } } Catch { $Status = $PSItem.Exception.Message Show-Output ('') } } END {} } |