LogoLogo
  • Introduction
    • 🔴Welcome to IntentX
    • 💢The Omnichain DEX
    • ⚡User Experience Focus
    • 🌐The Meta Front-End
  • On-Chain Derivatives Overview
    • ❓What Problem Does IntentX Solve?
    • 📊Current On-Chain Derivatives Landscape
    • 💡IntentX Solution & Architecture Overview
    • ✅Comparison and Advantages of IntentX
  • IntentX Platform
    • 📈Trading on IntentX
      • 📖Trading Basics
      • 🏫Trading Tutorials
        • 🏫Web3 Wallet
        • 🏫Account Abstracted Wallet
      • 🌡️Liquidations, Margin Management (CVA), and Account Health
      • 📊Instant 1-Click Trading
      • 🛑Take Profit and Stop Loss
      • 💵Collateral & Cross-Margin Accounts
      • 💸Understanding Funding Rates
      • 💰Pricing Data and the Role of Oracles
      • ↕️Unrealized Profit and Loss (uPNL)
      • 📏Open Interest (OI) and Market Activity
      • 🔐Withdrawal Process and Security Measures
      • 📈Advanced Charts by TradingView
    • 🔢Pair List
    • 🌀IntentX Solver Network
      • 🌀Example Solver Order Flow
    • 🫂Referral Program
    • 📱Mobile and Progressive Web App
  • Trading API
    • Introduction
    • Rest API V1
    • Typescript SDK
      • Examples
        • Opening Positions
        • Closing Positions
        • Cancel Positions
        • List Subaccounts
        • List Positions
        • Private Key Authentication
        • Enable Instant Actions
  • Tokenomics
    • 🪙INTX Token and xINTX Staking
    • 🔄Trade & Earn xINTX
    • 📅Token Allocation and Release Schedule
  • Technical Docs (WIP)
    • ⚙️Infrastructure Overview
      • 🔩Technical Docs
      • 🌐Sample Solver Docs
  • Additional Information
    • ❓FAQ
    • 🌐Official Links
    • 📃Terms & Conditions
    • 🔐Security & Audits
    • 🌟Brand & Media Assets
    • 💾Contracts
Powered by GitBook
On this page
  1. Trading API
  2. Typescript SDK
  3. Examples

Closing Positions

import { fromWei, OrderType, SupportedChainId, toBN } from "@intentx/core";
import { CloseRequestHooks, TradingSDK } from "@intentx/trading-sdk";
import * as dotenv from "dotenv";

dotenv.config();

async function closePositionExample() {
  const tradingSDK = new TradingSDK({
    apiKey: process.env.API_KEY,
    baseUrl: process.env.API_BASE_URL ?? undefined,
  });

  const positionId = 126568;

  const positionDetails = await tradingSDK.tradeManager.getPositionInfo(
    positionId.toString(),
    SupportedChainId.BASE
  );

  const amountToClose = toBN(fromWei(positionDetails.quantity))
    .minus(positionDetails.closedQuantity)
    .toString();

  const closeRequest = await tradingSDK.tradeManager.closePosition({
    quoteId: positionId,
    chainId: SupportedChainId.BASE,
    quantityToClose: Number(amountToClose),
    slippage: 0.01,
    orderType: OrderType.MARKET,
    useInstantActions: false,
  });

  closeRequest.on(CloseRequestHooks.PRICE_SETTLED, (state, justification) => {
    console.log("Price settled", state, justification);
  });

  closeRequest.on(CloseRequestHooks.SUCCESS, (state, justification) => {
    console.log("Position closed successfully", state, justification);
  });

  closeRequest.on(CloseRequestHooks.FAILED, (state, justification) => {
    console.log("Position closed failed", state, justification);
  });

  await closeRequest.waitForCompletion();
}

closePositionExample()
  .then(() => {
    console.log("Position closed");
  })
  .catch((error) => {
    console.error("Error opening position", error.message, error.code);
  });
PreviousOpening PositionsNextCancel Positions

Last updated 1 month ago