Close All Open Positions

Brief

The article teaches how to close all open positions.

Details

How to close all open positions

In this article you will learn how to close all open positions using the orders manager.
Closing all open positions means you will exit the market and liquidate your assets.
You can close all open positions at once, or filter them by instrument or direction.

Before you can close all open positions, you need to:

Step 1: Create an Orders Manager.

ordersManager Hide

typescriptjavascript
let ordersManager = session.getOrdersManager();

Step 2: Create a Request builder.

A request builder is an object that defines the parameters of the closing action.
You can create a request builder using the orders manager's getRequestFactory method and passing it the createCloseAllPositionsRequestBuilder method.

requestBuilder Hide

typescriptjavascript
let requestBuilder: FXConnectLite.CloseAllPositionsRequestBuilder = ordersManager.getRequestFactory().createCloseAllPositionsRequestBuilder(); requestBuilder.setCustomId("CloseOpenPositions#1") .setAcctId(options.account) .setTimeInForce("GTC");

By default, the request builder will close all open positions.
If you want to close only specific positions, you can add filters to the request builder by calling one or both of these methods:
setSymbol: This method takes an instrument symbol as an argument and closes only the positions for that instrument.
setSide: This method takes a direction (buy or sell) as an argument and closes only the positions in that direction.

Below, this code when added to the requestBuilder will close all buy positions for EUR/USD:

Add information to the requestBuilder Hide

typescriptjavascript
requestBuilder.setSide("B"); requestBuilder.setSymbol("EUR/USD");

For example, this code will close all buy positions for EUR/USD.
See also getRequestFactory, createCloseAllPositionsRequestBuilder, closeAllPositionsRequestBuilder.

Step 3: Close all open positions.

Then call the function Hide

typescriptjavascript
ordersManager.closeAllPositions(requestBuilder.build());

See also IOrdersManager, IOpenPositionsManager, closeAllPositions.

Step 4: Create a listener function

A listener function is a class that implements the ICloseAllPositionsCallback interface.
It has two methods: onClosed and onError.

The listener function is called when the closing action is completed.
It takes two parameters:

OpenPositionChangeListener Hide

typescriptjavascript
class OpenPositionChangeListener implements FXConnectLite.IOpenPositionChangeListener { private openPositionsManager: FXConnectLite.IOpenPositionsManager; constructor(openPositionsManager: FXConnectLite.IOpenPositionsManager) { this.openPositionsManager = openPositionsManager; } onChange(openPositionInfo: FXConnectLite.OpenPositionInfo): void { Printer.print(`OpenPosition ${openPositionInfo.getId()} is changed.`) OpenPositionPrinter.print(this.openPositionsManager.getOpenPosition(openPositionInfo.getId())); } onAdd(openPositionInfo: FXConnectLite.OpenPositionInfo): void { Printer.print(`OpenPosition ${openPositionInfo.getId()} is added.`) OpenPositionPrinter.print(this.openPositionsManager.getOpenPosition(openPositionInfo.getId())); } onDelete(openPositionInfo: FXConnectLite.OpenPositionInfo): void { Printer.print(`OpenPosition ${openPositionInfo.getId()} is deleted.`) } onRefresh(): void { Printer.print(`Open positions refreshed`) OpenPositionPrinter.printAll(this.openPositionsManager.getOpenPositionsSnapshot()); } }

Now we can use this class:

Use OpenPositionChangeListener Hide

typescriptjavascript
let openPositionChangeListener = new OpenPositionChangeListener(openPositionsManager);

Conclusion:

You have learned how to close all open positions using the orders manager. Closing all open positions means you will exit the market and liquidate your assets. Before you can close all open positions, you need to create an orders manager, a request builder, and a listener function that handles the success or failure of the closing action. You have also learned how to create a request builder using the orders manager's getRequestFactory method and passing it the createCloseAllPositionsRequestBuilder method. By default, the request builder will close all open positions, but you can add filters to close only specific positions by calling the setSymbol or setSide methods. You can now use this knowledge to manage your open positions on the platform.

Download the sample TypeScript, JavaScript.

back