New-Order with API Binance and PowerShell

In this article, we are going to see how we can place an order to buy or sell any crypto on the Binance platform with PowerShell and the function New-Order.

If you haven’t read the previous article Interact with your Binance account using API and PowerShell which contains all the necessary functions in order to use New-Order, you should probably read it first.

New-Order

function New-Order{
    <#
		.SYNOPSIS
		Place an order to buy or sell crypto

        .PARAMETER Symbol
        The crypto you want to buy or sell

        .EXAMPLE
        New-Order -Symbol BTCUSDT -Side BUY -Price 34000 -StopPrice 34000 -Quantity 0.000327
        
	#>

    [cmdletbinding()]
    param(
        [Parameter(Mandatory=$true)] 
        [string]$Symbol,

        [Parameter(Mandatory=$true)] 
        [ValidateSet("BUY", "SELL")]
        [string]$Side,

        [Parameter(Mandatory=$true)] 
        [string]$Price,

        [Parameter(Mandatory=$true)] 
        [string]$StopPrice,

        [Parameter(Mandatory=$true)] 
        [string]$Quantity

    )

    BEGIN{
        # Check prerequisit
        try{
            Get-Command -Name Get-UnixTimeStamp -ErrorAction Stop | out-null
            Get-Command -name Get-BinanceAPISignature -ErrorAction Stop | Out-Null
            Get-Command -Name Get-BinanceAPIHeader -ErrorAction Stop | Out-Null
            Get-Command -Name Request-API -ErrorAction Stop | Out-Null
        }
        catch{
            Write-Host "Load Get-UnixTimeStamp, Get-BinanceAPISignature, Get-BinanceAPIHeader, Request-API  first prior to laod the current script" -b red
            Break
        }

        $TimeStamp = Get-UnixTimeStamp

        if($Side -eq "BUY"){
            $QueryString = "symbol=$Symbol&side=$Side&type=TAKE_PROFIT_LIMIT&stopPrice=$StopPrice&price=$Price&timeInForce=GTC&quantity=$Quantity&timestamp=$TimeStamp&recvWindow=5000"
        }
        else{
            #$QueryString = "symbol=$Symbol&side=$Side$&type=TAKE_PROFIT_LIMIT&stopPrice=$StopPrice&price=$Price&timeInForce=GTC&quantity=$Quantity&timestamp=$TimeStamp&recvWindow=5000"
        }  
      
    }
    
    PROCESS{
        $URI = Get-BinanceAPISignature -QueryString $QueryString -EndPoint "/api/v3/order"
        $Headers = Get-BinanceAPIHeader
        $ObjResults = $null # need to do this?
        $ObjResults = Request-API -Method POST -URI $URI -Headers $Headers
    }

    END{
       return $ObjResults
    }
}

Examples

Serie of post blog Crypto Binance API PowerShell

  1. Retrieve prices from Binance using the API and PowerShell
  2. Get trading info from Binance using API and PowerShell
  3. Interact with your Binance account using API and PowerShell
  4. New-Order with API Binance and PowerShell

Leave a Comment

*

code