Retrieve Binance Balance API PowerShell

Because this is something useful and I’ve been asked, in this article, I introduce you to my Get-Balance function which retrieves the holdings of all the cryptos you have in your Binance spot wallet using the API and PowerShell.

This post is one of a series about how to query the Binance API. Thus if you don’t completely understand how it works, please see the Interact with Binance using API PowerShell.

This function corresponds to my needs, you can modify it according to yours. In order to determine if the amount is greater than the -MinAmount parameter, I also use the Get-Price function. However, to considerably improve the performance, you may want to remove this part.

function Get-Balance{
    <#
		.SYNOPSIS
		Get all your asset

        .PARAMETER MinUSDTAmount
        Specifies the minimum amount in USDT to filter out small amounts
        
        .EXAMPLE
        $Balances = Get-Balance

        .EXAMPLE
        Get-Balance -MinUSDTAmount 100

        .EXAMPLE
        Get-Balance -MinUSDTAmount 100 -Verbose
	#>

    [cmdletbinding()]
    param(
        [Parameter(Mandatory=$false)] 
        [int]$MinUSDTAmount = 0
    )

    BEGIN{
        Get-Command -Name Get-Price -ErrorAction Stop | out-null
        Get-Command -name Get-UnixTimeStamp -ErrorAction Stop | Out-Null
        Get-Command -Name Get-BinanceAPIHeader -ErrorAction Stop | Out-Null
        Get-Command -Name Get-BinanceAPISignature -ErrorAction Stop | Out-Null
        Get-Command -Name Request-API -ErrorAction Stop | Out-Null

        $Balances = @()
        $TimeStamp = Get-UnixTimeStamp
        $QueryString = "recvWindow=5000&timestamp=$TimeStamp"
    }
    
    PROCESS{
        $URI = Get-BinanceAPISignature -QueryString $QueryString -EndPoint "/api/v3/account"
        $Headers = Get-BinanceAPIHeader
        $ObjResults = Request-API -Method GET -URI $URI -Headers $Headers

        $Assets = $ObjResults.balances
        
        foreach($Asset in $Assets){
             # filter void assets
             if($Asset.free -match ".*[1-9].*" -or $Asset.locked -match ".*[1-9].*"){
                
                # filter small amount according to param MinimumAmount
                # if not USDT or add any fiat currency
                if($Asset.asset -eq "USDT"){
                    $CurrentAssetValueFreeInUSDT = $Asset.free
                    $CurrentAssetValueLockedInUSDT = $Asset.locked
                }
                else{
                    $CurrentAssetPrice = Get-Price -Symbol "$($Asset.asset)USDT" -Decimal 7
                    $CurrentAssetValueFreeInUSDT = $CurrentAssetPrice * $Asset.free
                    $CurrentAssetValueLockedInUSDT = $CurrentAssetPrice * $Asset.locked
                }
                                
                Write-Verbose "`n`n`n"
                Write-Verbose "Asset:             $($Asset.asset)"
                write-Verbose "Free:              $($Asset.free)"
                write-Verbose "Locked:            $($Asset.locked)"
                write-Verbose "CurrentAssetPrice: $CurrentAssetPrice"
                write-Verbose "FreeInUSDT:        $CurrentAssetValueFreeInUSDT"
                write-Verbose "LockedInUSDT:      $CurrentAssetValueLockedInUSDT"

                if($CurrentAssetValueFreeInUSDT -lt $MinUSDTAmount -and $CurrentAssetValueLockedInUSDT -lt $MinUSDTAmount){
                    Write-Verbose "Below the minimum value of USDT"
                }
                else{
                    $Balances += $Asset
                    Write-Verbose "Above the minimum value of USDT"
                }
            }
        }
    }

    END{
       return $Balances | sort asset
    }
}

About this function

  • EndPoint /api/v3/account
    Part of the Account Endpoint which requires authentification.

  • Where are the functions Get-BinanceAPISignature, Get-BinanceAPIHeader?
    See post blog number 3

  • [math]::Round($Ticker.Price, $Decimal)
    Round off to a decimal number according to the variable “number”.

Examples

Example the function Get Balance with the API and PowerShell
Binance Balance Powershell API

Serie of post blog Crypto Binance API PowerShell

  1. Retrieve prices from Binance API PowerShell
  2. Get trading info from Binance API PowerShell
  3. Interact with Binance using API PowerShell
  4. New-Order API Binance PowerShell
  5. Retrieve Binance Balance API PowerShell
  6. Open Order Binance PowerShell API

Leave a Comment