The article teaches how to close all open positions.
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:
Create an orders manager.
Create a request builder.
Close all open positions.
Create a listener function that handles the success or failure of the closing action.
Step 1: Create an Orders Manager.
ordersManager Hide
typescriptjavascriptlet 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
typescriptjavascriptlet 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
typescriptjavascriptrequestBuilder.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
typescriptjavascriptordersManager.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 onClosed method is called when a position is successfully closed.
The onError method is called when there is an error in closing a position.
The listener function is called when the closing action is completed.
It takes two parameters:
list of closed positions.
a list of errors.
If there are no errors, the second parameter is empty.
If there are errors, the function onError is called for each error.
OpenPositionChangeListener Hide
typescriptjavascriptclass 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
typescriptjavascriptlet 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.