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);
});
Last updated