Invertee / CoinbasePro-Powershell

Powershell module for the CoinbasePro API.
MIT License
11 stars 6 forks source link

Pagination (-Before and -After) #8

Open jasonpearce opened 4 years ago

jasonpearce commented 4 years ago

Either I am unable to figure out how to use -Before and -After (I think they make use of the Coinbase Pro API Pagination at https://docs.pro.coinbase.com/#pagination), or it's not working properly.

Objective: Retrieve all of my Fills. The quantity of fills exceeds the default -Limit 100.

Testing -Before: Here's a test. I'm attempting to retrieve the five most recent BTC-USD fills, then the next 5, then the next 5. The result should be the 15 most recent BTC-USD fills.

# Get the five most recent BTC-USD fills (1-5)
Get-CoinbaseProFills -APIKey $APIKeyViewOnly -APISecret $APISecretViewOnly -APIPhrase $APIPhraseViewOnly -ProductID BTC-USD -Limit 5 | Format-Table created_at, trade_id -AutoSize

created_at               trade_id
----------               --------
2020-01-23T12:00:42.78Z  81616182
2020-01-23T02:10:28.494Z 81597680
2020-01-21T19:41:42.511Z 81540634
2020-01-19T11:03:26.573Z 81433806
2020-01-19T11:01:54.112Z 81431376

# Get the next 5 most recent BTC-USD fills (6-10)
Get-CoinbaseProFills -APIKey $APIKeyViewOnly -APISecret $APISecretViewOnly -APIPhrase $APIPhraseViewOnly -ProductID BTC-USD -Limit 5 -Before 1 | Format-Table created_at, trade_id -AutoSize

created_at               trade_id
----------               --------
2017-08-02T13:37:08.414Z 18838454
2017-07-30T05:45:22.854Z 18731934
2017-07-30T05:38:29.166Z 18730012
2017-07-26T06:45:26.241Z 18618952
2017-07-25T19:01:58.041Z 18598131

# Get the next 5 most recent BTC-USD fills (11-15)
Get-CoinbaseProFills -APIKey $APIKeyViewOnly -APISecret $APISecretViewOnly -APIPhrase $APIPhraseViewOnly -ProductID BTC-USD -Limit 5 -Before 2 | Format-Table created_at, trade_id -AutoSize

created_at               trade_id
----------               --------
2017-08-02T13:37:08.414Z 18838454
2017-07-30T05:45:22.854Z 18731934
2017-07-30T05:38:29.166Z 18730012
2017-07-26T06:45:26.241Z 18618952
2017-07-25T19:01:58.041Z 18598131

Unexpected Results: The results above are not what I expected.

For the Get-CoinbaseProFills, -Before is not used. The five most recent Fills are retrieved (fills 1 to 5) that occurred in 2020. This works as expected.

For the next Get-CoinbaseProFills, I add -Before 1. My expectation is that the function retrieves the next five fills (fills 6 to 10). Instead, the function retrieves the first 5 fills I ever made on Coinbase in 2017.

For my third Get-CooinbaseProFills test, I add -Before 2. Now my expectation is that the function would retrieve the five fills that I made in 2017. If so, then I could retrieve all of my fills by working backwards. Instead, the function again retrieves the first 5 fills I ever made on Coinbase in 2017.

Is Pagination working correctly and I'm just not smart enough to figure it out? I've tried several other test usages of -Before and -After. Either pagination is not working correctly, or I'm unable to figure out how to properly use these pagination options.

At best, I'm able to retrieve my 100 most recent fills and my first 100 fills for a total of 200 unique fills. If pagination isn't broken, would you kindly provide me an example of how to retrieve the fills in the middle (before and after the first and last 100 fills)?

Thank you.

jasonpearce commented 4 years ago

To provide you more context on what I'm attempting to accomplish.

I want to first calculate my Weighted Average Price that I've paid over time for all of my fills. Do this per coin and place results in some coin-specific variables.

Once I've done that, I'll compare my Weighted Average Price to the Current Ticker Price for each coin, and determine if I should Buy more (to lower my Weighted Average Price) or Sell some (because I'm profitable).

Here's what I'm working on so far for calculating my Weighted Average Price per coin. But since I cannot figure out how to get Pagination (-Before and -After) to work, I'm really only getting a Weighted Average Price for the 100 most recent Fills. I'm calling this the 100-Fill Weighted Average Price (treating it a bit like a 100-day moving average, but with Fills instead).


# 100-Fill Moving Weighted Average Price
# Calculate Weighted Average Price Per Coin for past 100 trades
# Akin to obtaining a 100-day moving average, but for the 100 most recent fills

# Set your View-Only credentials
$APIKeyViewOnly     = "xxx"
$APISecretViewOnly  = "xxx"
$APIPhraseViewOnly  = "xxx"

# Get-CoinbaseProAccounts 
$CBPAccounts = Get-CoinbaseProAccounts -APIKey $APIKeyViewOnly -APISecret $APISecretViewOnly -APIPhrase $APIPhraseViewOnly | Sort Currency

# Get-CoinbaseProProducts
$CBPProducts = Get-CoinbaseProProducts | Sort-Object Id

# Get-CoinbaseProFills and dynamically create Fills variables (if not Null) 
# and Weighted Average Price Per Coin (buys only)
foreach ($CBPProduct in $CBPProducts) {

    # Clear Some Previous Variables
    $Fills = $Null

    # Variables
    $ProductPair           = $CBPProduct.id.Replace("-","")
    $ProductId             = $CBPProduct.id
    $ProductBaseCurrency   = $CBPProduct.base_currency
    $ProductQuoteCurrency  = $CBPProduct.quote_currency
    $ProductBaseMinSize    = $CBPProduct.base_min_size
    $ProductBaseMaxSize    = $CBPProduct.base_max_size
    $ProductQuoteIncrement = $CBPProduct.quote_increment
    $ProductBaseIncrement  = $CBPProduct.base_increment
    $ProductDisplayName    = $CBPProduct.display_name
    $ProductMinMarketFunds = $CBPProduct.min_market_funds
    $ProductMaxMarketFunds = $CBPProduct.max_market_funds
    $ProductMarginEnabled  = $CBPProduct.margin_enabled
    $ProductPostOnly       = $CBPProduct.post_only
    $ProductLimitOnly      = $CBPProduct.limit_only
    $ProductCancelOnly     = $CBPProduct.cancel_only
    $ProductStatus         = $CBPProduct.status
    $ProductStatusMessage  = $CBPProduct.status_message

    # Get-CoinbaseProFills 100 most recent fills for Product (if any)
    $Fills = Get-CoinbaseProFills -APIKey $APIKeyViewOnly -APISecret $APISecretViewOnly -APIPhrase $APIPhraseViewOnly -ProductID $ProductId

    # Count how many Fills were retrieved
    $FillsMeasured = ($Fills | Measure-Object).Count

    ####################################
    # Pagination isn't working correctly
    # If I wanted to at least also get the first 100 fills for a max total of 200
    # if ($FillsMeasured -gt "95") {$Fills += Get-CoinbaseProFills -APIKey $APIKeyViewOnly -APISecret $APISecretViewOnly -APIPhrase $APIPhraseViewOnly -ProductID $ProductId -Before 1}
    # Without the ability to get all Fills, I'm instead going to treat the 100 most-resent fills that I can retrieve like a 100-day moving average.
    ####################################

    # Create Fill variable if Fills is not Null
    if ($Fills -ne $Null) {

        # Create Fill-Specific Variable
        $Name = "CBP" + $ProductPair + "Fills"
        $Value = $Fills
        New-Variable -Name $Name -Value $Value -Force
        Write-Host "Created new variable: `$$Name"

        # Create Fill-Specific Variable if not Null
        $Name = "CBP" + $ProductPair + "FillsBuy"
        $Value = $Fills | Where-Object {$_.side -eq "buy" -and $_.settled -eq $True}
        if ($Value -ne $Null) {
            New-Variable -Name $Name -Value $Value -Force
            Write-Host "Created new variable: `$$Name"
        }

        # Create Fill-Specific Variable if not Null
        $Name = "CBP" + $ProductPair + "FillsSell"
        $Value = $Fills | Where-Object {$_.side -eq "sell" -and $_.settled -eq $True}
        if ($Value -ne $Null) {
            New-Variable -Name $Name -Value $Value -Force
            Write-Host "Created new variable: `$$Name"
        }

        #####################################################################
        # BEGIN: Calculate Weighted Average Price Per Coin (buys only)
        # Weighted Average Price = ((first price * size) + (second price * size)) / total number of size
        #####################################################################

        # Create some empty variables
        $WeightedAveragePrice = $Null
        $Numerator = $Null
        $Denominator = $Null

        # Calculate Weighted Average Price Per Coin (buys only) 
        foreach ($Fill in ($Fills | Where-Object {$_.side -eq "buy" -and $_.settled -eq $True})) {

            # Variables
            $FillCreatedAt = $Fill.created_at
            $FillTradeId   = $Fill.trade_id
            $FillProductId = $Fill.prodcut_id
            $FillOrderId   = $Fill.order_id
            $FillUserId    = $Fill.user_id
            $FillProfileId = $Fill.profile_id
            $FillLiquidity = $Fill.liquidity
            $FillPrice     = $Fill.price / 1 #converts string to number
            $FillSize      = $Fill.size / 1 #converts string to number
            $FillFee       = $Fill.fee / 1 #converts string to number
            $FillSide      = $Fill.side
            $FillSettled   = $Fill.settled
            $FillUsdVolume = $Fill.usd_volume / 1 #converts string to number

            # Multiply Price * Size
            $PricePerSize = $FillPrice * $FillSize

            # Numerator Total
            $Numerator += $PricePerSize

            # Total Number of Size
            $Denominator += $FillSize

        }

        # Complete the Weighted Average Price calculation
        if ($Denominator -ne $Null) {

            # Divide Numerator by Denominator
            $WeightedAveragePrice = $Numerator / $Denominator

            # Round to eight decimal places
            $RoundedWeightedAveragePrice = [math]::round($WeightedAveragePrice,8)

            # Create Fill-Specific Variable
            $Name = "CBP" + $ProductPair + "WeightedAveragePrice"
            $Value = $RoundedWeightedAveragePrice
            New-Variable -Name $Name -Value $Value -Force
            Write-Host "Created new variable: `$$Name"

        }

        #####################################################################
        # END: Calculate Weighted Average Price Per Coin (buys only)
        #####################################################################

        # Write a line break
        Write-Host "-----------------------------------------------------"

    }

}

# Write New Weighted Average Price Variables to Screen
Get-Variable -Name CBP*WeightedAveragePrice*