Scripts/Get-CoinbaseProductTrade.ps1


Function Get-CoinbaseProductTrade {

    <#
 
    .SYNOPSIS
 
    Gets a list of the latest trades for a product.
 
    .DESCRIPTION
 
    Gets a list of the latest trades for a product.
 
    .PARAMETER Product
 
    Product Id
 
    .PARAMETER Limit
 
    Limit on number of results to return. (optional)
 
    Default = 1000
 
    .PARAMETER Before
 
    Used for pagination. Sets start cursor to before Id.
 
    .PARAMETER After
 
    Used for pagination. Sets end cursor to after Id.
 
    .EXAMPLE
 
    Get-CoinbaseProductTrade -Product BTC-USD
 
    .EXAMPLE
 
    Get-CoinbaseProductTrade -Product BTC-USD -Limit 200
 
    .EXAMPLE
 
    Get-CoinbaseProductTrade -Product BTC-USD -Limit 200 -Before 300 -After 900
 
    .NOTES
 
    .LINK
 
    #>


    [CmdletBinding ()]

    Param (

        [Parameter (Mandatory = $False,
                    ValueFromPipeline = $True,
                    ValueFromPipelineByPropertyName = $True,
                    HelpMessage = 'Enter Limit',
                    Position = 1
                   )
        ]

        [String]$Limit = 1000,

        [Parameter (Mandatory = $False,
                    ValueFromPipeline = $True,
                    ValueFromPipelineByPropertyName = $True,
                    HelpMessage = 'Enter before',
                    Position = 2
                   )
        ]

        [String]$Before,

        [Parameter (Mandatory = $False,
                    ValueFromPipeline = $True,
                    ValueFromPipelineByPropertyName = $True,
                    HelpMessage = 'Enter after',
                    Position = 3
                   )
        ]

        [String]$After

    )

    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()
                TradeId = $Values.Trade_Id
                Side = $Values.Side
                Size = $Values.Size
                Price = $Values.Price
                Time = Get-Date($Values.Time)
                Status = $Status

            }

        }

    }

    PROCESS {

        Try {

            $Headers = New-Object 'System.Collections.Generic.Dictionary[[String],[String]]'
            $Headers.Add('Content-Type', 'application/json')

            ForEach ($Coin In $Products) {

                $Response = Invoke-RestMethod "https://api.exchange.coinbase.com/products/$Coin/trades?limit=$Limit&before=$Before&after=$After" -Method 'GET' -Headers $Headers

                ForEach ($Item In $Response) {

                    $Status = 'Ok'

                    Show-Output ($Item)

                }

                Start-Sleep -Milliseconds 100

            }

        }

        Catch {

            $Status = $PSItem.Exception.Message

            Show-Output ('')

        }

    }

    END {}

}