Example: rest-usdm-order-sl.ts

Static snapshot for Binance/Rest/Futures/rest-usdm-order-sl.ts.

Example Path

Binance/Rest/Futures/rest-usdm-order-sl.ts

Source Link

Repository source: https://github.com/sieblyio/crypto-api-examples/blob/master/examples/Binance/Rest/Futures/rest-usdm-order-sl.ts

Code Snapshot

/* eslint-disable @typescript-eslint/no-unused-vars */
import { USDMClient } from 'binance';

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

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

const symbol = 'BTCUSDT';

async function start() {
  try {
    // ### This is for Hedge Mode Only ###
    // assuming you currently have a open position, and you want to modify the SL order.

    /**
     * first we get all long and short positions status
     * the result of this method in hedge mode is array of two objects
     * first index for LONG and second index for SHORT
     */
    const [
      { positionAmt: longAmount, ...long },
      { positionAmt: shortAmount, ...short },
    ]: any = await client.getPositionsV3({ symbol });

    // if longAmount is bigger than 0 means we have open long position and if shortAmount is below 0 means we have open short position
    const hasLong = parseFloat(longAmount) > 0;
    const hasShort = parseFloat(shortAmount) < 0;
    const hasOpen = hasLong || hasShort;

    // if we have any open position then we continue
    if (hasOpen) {
      // we get ourstop loss here
      const orders = await client.getAllOpenOrders({ symbol });
      const stopOrders =
        orders.filter(({ type }) => type === 'STOP_MARKET') ?? [];

      // we want to modify our long position SL here
      if (hasLong) {
        // we get the StopLoss order which is realted to long
        const { orderId }: any = stopOrders.find(
          ({ positionSide: ps }) => ps == 'LONG',
        );

        // if it exists, cancel it.
        if (orderId) {
          await client.cancelOrder({ symbol, orderId });
        }

        const { markPrice }: any = long;

        // creating SL order
        const result = await client.submitNewOrder({
          symbol,
          side: 'SELL', // the action of order, means this order will sell which is sl for long position
          positionSide: 'LONG', // based on the headge mode we either LONG or SHORT, here we are doing it for our long pos
          timeInForce: 'GTC',
          type: 'STOP_MARKET',
          closePosition: 'true', // this is here because we don't have the position quantity value, and it means closee all quantity
          stopPrice: parseFloat((markPrice * 0.99).toFixed(3)), // set sl price 1% below current price
          workingType: 'MARK_PRICE',
        });
        console.log('SL Modifiled sell result: ', result);
      }
    } else {
      console.log('No Open position found');
    }
  } catch (e) {
    console.error('market sell failed: ', e);
  }
}

start();

This is a static, crawlable snapshot. The interactive app loads after JavaScript starts and can refresh live data.