# Closing Positions

```typescript
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);
  });

```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.intentx.io/trading-api/typescript-sdk/examples/closing-positions.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
