Binance TypeScript SDK example: rest-spot-private-trade.ts

Binance REST Spot REST spot private trade example for the Siebly Binance SDK, with TypeScript source for exchange REST API and WebSocket integration, setup, and production SDK docs.

What This Example Covers

  • Binance REST API example in TypeScript.
  • Uses the Siebly Binance SDK package binance instead of hand-written HTTP request plumbing.
  • Source path: Binance/Rest/Spot/rest-spot-private-trade.ts.
  • Example category: REST Spot.
  • Imports SDK symbols including MainClient.
  • Calls SDK methods such as getBalances(), getSymbolPriceTicker(), testNewOrder(), submitNewOrder().

How To Use This Example

  • Start here for the specific request or stream pattern, then check the matching SDK guide for install, credentials, and operational notes.
  • Open the repository source when you need the latest committed version: GitHub source file.

Example Path

Binance/Rest/Spot/rest-spot-private-trade.ts

Source Link

Repository source: https://github.com/sieblyio/crypto-api-examples/blob/master/examples/Binance/Rest/Spot/rest-spot-private-trade.ts

Related SDK Docs

Example Source

import {
  MainClient,
  NewSpotOrderParams,
  OrderResponseFull,
  SymbolPrice,
} from 'binance';

const key = process.env.API_KEY_COM || 'APIKEY';
const secret = process.env.API_SECRET_COM || 'APISECRET';

const client = new MainClient({
  api_key: key,
  api_secret: secret,
  beautifyResponses: true,
});

const entryAmountPercent = 50; // trigger trade with 50%
const symbol = 'BTCUSDT';
const assetDecimalPlaces = 4; // get this from exchange info, it varies per asset

// method to trim down to decimal.
function trimToDecimalPlaces(number: number, precision: number): number {
  const array: any[] = number.toString().split('.');
  array.push(array.pop().substring(0, precision));
  const trimmedstr = array.join('.');
  return parseFloat(trimmedstr);
}

/**
 * This is a very silly demonstration on placing market orders using various parts of the module.
 * By default it will use 50% of your available USDT balance to buy BTC and sell it again.
 */

(async () => {
  try {
    /**
     * Get available balance
     */
    const balance = await client.getBalances();

    const usdtBal = balance.find((assetBal) => assetBal.coin === 'USDT');
    // console.log('USDT balance object: ', usdtBal);

    const usdtAvailable = usdtBal?.free;

    if (!usdtBal || !usdtAvailable) {
      return console.error('Error: funds to trade from USDT');
    }

    const buyAmountValue = Number(usdtAvailable) * (50 / 100);
    console.log(
      `Executing trade with ${entryAmountPercent}% of ${usdtAvailable} USDT = ${buyAmountValue} USDT value`,
    );

    /**
     * Get last asset price
     */
    const btcTicker = (await client.getSymbolPriceTicker({
      symbol: symbol,
    })) as SymbolPrice;
    const lastPrice = btcTicker?.price;
    if (!lastPrice) {
      return console.error('Error: no price returned');
    }

    /**
     * Calculate and submit buy amount
     */
    const buyAmountBtc = +(buyAmountValue / Number(lastPrice)).toFixed(
      assetDecimalPlaces,
    );
    console.log(
      `Last ${symbol} price: ${lastPrice} => will buy ${buyAmountBtc} ${symbol}`,
    );

    const buyOrderRequest: NewSpotOrderParams = {
      symbol: symbol,
      quantity: buyAmountBtc,
      side: 'BUY',
      type: 'MARKET',
      /**
       * ACK = confirmation of order acceptance (no placement/fill information) -> OrderResponseACK
       * RESULT = fill state -> OrderResponseResult
       * FULL = fill state + detail on fills and other detail -> OrderResponseFull
       */
      newOrderRespType: 'FULL',
    };

    console.log('Submitting buy order: ', buyOrderRequest);
    await client.testNewOrder(buyOrderRequest);
    const buyOrderResult = (await client.submitNewOrder(
      buyOrderRequest,
    )) as OrderResponseFull;
    console.log(
      'Order result: ',
      JSON.stringify(
        { request: buyOrderRequest, response: buyOrderResult },
        null,
        2,
      ),
    );

    /**
     * Process bought fills and submit sell amount
     */
    const assetAmountBought = buyOrderResult.executedQty;
    const assetFillsMinusFees = buyOrderResult.fills.reduce(
      (sum, fill) =>
        sum +
        Number(fill.qty) -
        (fill.commissionAsset !== 'BNB' ? Number(fill.commission) : 0),
      0,
    );
    console.log(
      `Filled buy ${symbol} ${assetAmountBought} : bought minus fees ${assetFillsMinusFees}`,
    );

    const sellOrderRequest: NewSpotOrderParams = {
      symbol: symbol,
      quantity: trimToDecimalPlaces(assetFillsMinusFees, assetDecimalPlaces),
      side: 'SELL',
      type: 'MARKET',
      newOrderRespType: 'FULL',
    };

    console.log('Submitting sell order: ', sellOrderRequest);
    await client.testNewOrder(sellOrderRequest);
    const sellOrderResult = (await client.submitNewOrder(
      sellOrderRequest,
    )) as OrderResponseFull;

    console.log(
      'Order result: ',
      JSON.stringify(
        { request: sellOrderRequest, response: sellOrderResult },
        null,
        2,
      ),
    );

    console.log(`All ${symbol} should have been sold!`);
  } catch (e) {
    console.error('Error: request failed: ', e);
  }

  process.exit(1);
})();