The article teaches how to get a list of open positions.
How to get a list of open positions
In this article you will learn how to use the API to get a list of open positions.
Before you can get a list of open positions, you need to:
Create an instance of the open positions manager
Create a position change listener
Create a listener for offer changes
Get detailed information about all open positions
Step 1: Create an instance of the open positions manager.
The first step is to create an instance of the open positions manager.
This is an object that provides methods for managing open positions.
You can use the IOpenPositionsManager interface to access its methods and properties.
Below, you can see some example code to create an instance of the open positions manager:
The open positions manager Hide
typescriptjavascriptlet openPositionsManager = session.getOpenPositionsManager();
For more information about the IOpenPositionsManager interface, see the API reference.
Step 2: Create a position change listener.
The next step is to create a position change listener.
This is a function that will be called whenever a position is opened, closed, or updated.
You can use the getOpenPosition and getOpenPositionsSnapshot methods of the open positions manager to get a single position or a snapshot of all positions.
Below, you can see some example code to create a position change listener:
Position change listener 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()); } }
For more information, see also IOpenPositionsManager, getOpenPosition, getOpenPositionsSnapshot
Step 3: Create a listener to offer changes.
The third step is to create a listener to offer changes.
This is a function that will be called whenever an offer's data changes, such as when its price, volume, or spread changes.
You will need this listener if you want to display calculated fields for the positions, such as profit and loss, margin, or pip cost.
Below you can see some example code to create a listener for offer changes:
The listener to offer changes Hide
typescriptjavascriptclass OfferChangeListener implements FXConnectLite.IOfferChangeListener { private openPositionsManager: FXConnectLite.IOpenPositionsManager; constructor(openPositionsManager: FXConnectLite.IOpenPositionsManager) { this.openPositionsManager = openPositionsManager; } onChange(offerInfo: FXConnectLite.OfferInfo): void { this.print(offerInfo, `Offer ${offerInfo.getOfferId()} is changed.`); } onAdd(offerInfo: FXConnectLite.OfferInfo): void { this.print(offerInfo, `Offer ${offerInfo.getOfferId()} is added.`); } print(offerInfo: FXConnectLite.OfferInfo, text: string): void { let was: boolean = false; this.openPositionsManager.getOpenPositionsSnapshot().forEach(openPosition => { if(openPosition.getOfferId() == offerInfo.getOfferId()) { if(!was) { was = true; Printer.print(text) } Printer.print(`Open position:`) OpenPositionPrinter.print(openPosition); } }); } }
See also IOpenPositionsManager, getOpenPosition, getOpenPositionsSnapshot
Step 4: Get detailed information about all open positions.
The final step is to get detailed information about all open positions.
You can use the OpenPosition interface to access the properties of an open position, such as its trade ID, amount, direction, instrument, and stop/limit orders.
Below, you can see some example code to get detailed information about all open positions:
Position change listener Hide
typescriptjavascriptclass OpenPositionPrinter { public static print(openPosition: FXConnectLite.OpenPosition): void { Printer.print(``) Printer.print(`TradeID = ${openPosition.getTradeID()}`) Printer.print(`AccountId = ${openPosition.getAccountId()}`) Printer.print(`AccountName = ${openPosition.getAccountName()}`) Printer.print(`AccountKind = ${openPosition.getAccountKind()}`) Printer.print(`OfferId = ${openPosition.getOfferId()}`) Printer.print(`Amount = ${openPosition.getAmount()}`) Printer.print(`BuySell = ${openPosition.getBuySell()}`) Printer.print(`OpenRate = ${openPosition.getOpenRate()}`) Printer.print(`OpenTime = ${openPosition.getOpenTime()}`) Printer.print(`OpenQuoteId = ${openPosition.getOpenQuoteId()}`) Printer.print(`OpenOrderId = ${openPosition.getOpenOrderId()}`) Printer.print(`OpenOrderReqId = ${openPosition.getOpenOrderReqId()}`) Printer.print(`OrderRequestTXT = ${openPosition.getOpenOrderRequestTXT()}`) Printer.print(`Commission = ${openPosition.getCommission()}`) Printer.print(`RolloverInterest = ${openPosition.getRolloverInterest()}`) Printer.print(`TradeIdOrigin = ${openPosition.getTradeIdOrigin()}`) Printer.print(`ValueDate = ${openPosition.getValueDate()}`) Printer.print(`Parties = ${openPosition.getParties()}`) Printer.print(`PL = ${openPosition.getPL()}`) Printer.print(`PLPips = ${openPosition.getPLPips()}`) Printer.print(`GrossPL = ${openPosition.getGrossPL()}`) Printer.print(`CloseRate = ${openPosition.getCloseRate()}`) Printer.print(`StopRate = ${openPosition.getStopRate()}`) Printer.print(`LimitRate = ${openPosition.getLimitRate()}`) Printer.print(`StopOrderId = ${openPosition.getStopOrderID()}`) Printer.print(`LimitOrderId = ${openPosition.getLimitOrderID()}`) Printer.print(`UsedMargin = ${openPosition.getUsedMargin()}`) Printer.print(`Dividends = ${openPosition.getDividends()}`) Printer.print(``) } public static printAll(openPositions: FXConnectLite.OpenPosition[]): void { Printer.print(`Current open position snapshot:`); Printer.print(`Number of open positions is ${openPositions.length}`); openPositions.forEach(openPosition => { OpenPositionPrinter.print(openPosition); }); } }
See also IOpenPositionsManager, getOpenPosition, getOpenPositionsSnapshot, OpenPosition
Conclusion:
You have learned how to use the API to get a list of open positions. Before you can get a list of open positions, you need to create an instance of the open positions manager, create a position change listener, create a listener for offer changes, and get detailed information about all open positions. You have also learned how to create an instance of the open positions manager using the IOpenPositionsManager interface and how to create a position change listener using the getOpenPosition and getOpenPositionsSnapshot methods of the open positions manager. You can now use this knowledge to manage your open positions on the platform.
Download the sample TypeScript, JavaScript.