Get Closed Positions

Brief

The article teaches how to get a list of closed positions.

Details

How to Get a List of Closed Positions

In this article you will learn how to get a list of closed positions using the closed positions manager.
Closed positions are positions that have been exited from the market and have a final profit or loss.
You can get a list of all closed positions, or filter them by instrument or time.

Before you can get a list of closed positions, you need to:

Step 1: Creating a closed position manager and listener.

A closed position listener is a class that implements the IClosedPositionChangeListener interface.
It has one method: onClosedPositionChange.

The method is called when a new position is closed or an existing position is updated.

Below, you can see some example code for creating the Closed positions listener:

Closed positions manager and closed position listener Hide

typescriptjavascript
let closedPositionsManager = session.getClosedPositionsManager(); // get closed position manager let closedPositionChangeListener = new ClosedPositionChangeListener(closedPositionsManager); // create listener closedPositionsManager.subscribeClosedPositionChange(closedPositionChangeListener); // subscribe

To use the closed position listener, you need to subscribe it to the closed positions manager using the subscribeClosedPositionChange method.

See also IClosedPositionsManager, subscribeClosedPositionChange

Step 2: Creating a position change listener.

A position change listener is a class that implements the IPositionChangeListener interface.
It has one method: onPositionChange. The method is called when a position is opened, closed, or modified.

Below, you can see some example code for creating the position change listener:

Position change listener Hide

typescriptjavascript
class ClosedPositionChangeListener implements FXConnectLite.IClosedPositionChangeListener { private closedPositionsManager: FXConnectLite.IClosedPositionsManager; constructor(closedPositionsManager: FXConnectLite.IClosedPositionsManager) { this.closedPositionsManager = closedPositionsManager; } onChange(closedPositionInfo: FXConnectLite.ClosedPositionInfo): void { Printer.print(`Closed position ${closedPositionInfo.getId()} changed`) ClosedPositionPrinter.print(this.closedPositionsManager.getClosedPosition(closedPositionInfo.getId())); } onAdd(closedPositionInfo: FXConnectLite.ClosedPositionInfo): void { Printer.print(`Closed position ${closedPositionInfo.getId()} added`) ClosedPositionPrinter.print(this.closedPositionsManager.getClosedPosition(closedPositionInfo.getId())); } onRefresh(): void { Printer.print(`Closed positions refreshed`) ClosedPositionPrinter.printAll(this.closedPositionsManager.getClosedPositionsSnapshot()); } }

To use the position change listener, you need to register it to the closed positions manager using the registerPositionChangeListener method.

See also IClosedPositionsManager, getClosedPosition, getClosedPositionsSnapshot

Step 3: Getting a List of Closed Positions.

To get a list of closed positions, you can use one of these methods:

You can use the printer function to print the details of each closed position.

Creating a printer function

A printer function is a function that takes a closed position as an argument and prints its properties and logs.

ClosedPositionPrinter Hide

typescriptjavascript
class ClosedPositionPrinter { public static print(closedPosition: FXConnectLite.ClosedPosition): void { Printer.print(``) Printer.print(`TradeID = ${closedPosition.getTradeID()}`) Printer.print(`AccountId = ${closedPosition.getAccountId()}`) Printer.print(`AccountName = ${closedPosition.getAccountName()}`) Printer.print(`AccountKind = ${closedPosition.getAccountKind()}`) Printer.print(`OfferId = ${closedPosition.getOfferId()}`) Printer.print(`Amount = ${closedPosition.getAmount()}`) Printer.print(`BuySell = ${closedPosition.getBuySell()}`) Printer.print(`OpenRate = ${closedPosition.getOpenRate()}`) Printer.print(`OpenTime = ${closedPosition.getOpenTime()}`) Printer.print(`OpenQuoteId = ${closedPosition.getOpenQuoteId()}`) Printer.print(`OpenOrderId = ${closedPosition.getOpenOrderId()}`) Printer.print(`OpenOrderReqId = ${closedPosition.getOpenOrderReqId()}`) Printer.print(`OpenOrderRequestTXT = ${closedPosition.getOpenOrderRequestTXT()}`) Printer.print(`CloseRate = ${closedPosition.getCloseRate()}`) Printer.print(`CloseTime = ${closedPosition.getCloseTime()}`) Printer.print(`CloseQuoteId = ${closedPosition.getCloseQuoteId()}`) Printer.print(`CloseOrderId = ${closedPosition.getCloseOrderId()}`) Printer.print(`CloseOrderReqId = ${closedPosition.getCloseOrderReqId()}`) Printer.print(`CloseOrderRequestTXT = ${closedPosition.getCloseOrderRequestTXT()}`) Printer.print(`Commission = ${closedPosition.getCommission()}`) Printer.print(`RolloverInterest = ${closedPosition.getRolloverInterest()}`) Printer.print(`TradeIdOrigin = ${closedPosition.getTradeIdOrigin()}`) Printer.print(`ValueDate = ${closedPosition.getValueDate()}`) Printer.print(`CloseOrderParties = ${closedPosition.getCloseOrderParties()}`) Printer.print(`PL = ${closedPosition.getPL()}`) Printer.print(`NetPL = ${closedPosition.getNetPL()}`) Printer.print(`PLPips = ${closedPosition.getPLPips()}`) Printer.print(`GrossPL = ${closedPosition.getGrossPL()}`) Printer.print(``) } public static printAll(closedPositions: FXConnectLite.ClosedPosition[]): void { Printer.print(`Current closed positions snapshot:`); Printer.print(`Number of closed positions: ${closedPositions.length}`); closedPositions.forEach(closedPosition => { ClosedPositionPrinter.print(closedPosition); }); } }

See also ClosedPosition

Conclusion:

You have learned how to use the closed positions manager to get a list of closed positions. You have created a closed positions manager, a closed position listener, a position change listener, and a printer function. You have subscribed the closed position listener to the closed positions manager using the subscribeClosedPositionChange method. You can now use this knowledge to build your own applications that work with closed positions.

Download the sample TypeScript , JavaScript.

back