The article teaches how to get a list of all accounts.
How to get a list of all accounts
In this article you will learn how to use the API to get a list of accounts.
You will learn how to:
Listen for account changes
Subscribe to account changes
Get and print accounts
Print account properties
Step 1: Listen for account changes.
The first step is to create an account change listener.
This is a function that will be called whenever an account is added, removed, or updated.
You can use the AccountInfo interface to access the account details.
Below, you can see some example code to create an account change listener:
The account change listener Hide
typescriptjavascriptclass AccountChangeListener implements FXConnectLite.IAccountChangeListener { onChange(accountInfo: FXConnectLite.AccountInfo): void { } onAdd(accountInfo: FXConnectLite.AccountInfo): void { } onRefresh(): void { } onDelete(accountInfo: FXConnectLite.AccountInfo): void { } }
See also AccountInfo
Step 2: Subscribe to account changes.
The next step is to get an instance of the account manager and subscribe to account changes.
The account manager is an object that provides methods for managing accounts.
You can use the IAccountsManager interface to access its methods.
Below, you can see some example code to get an instance of the account manager and subscribe to account changes:
Then get account manager and subscribe to account changes Hide
typescriptjavascriptlet accountChangeListener = new AccountChangeListener(); // create an account change listener. session.getAccountsManager().subscribeAccountChange(accountChangeListener); // subscribe
See also IAccountsManager
Step 3: Get and print accounts.
The third step is to create a function that will get and print the list of accounts.
You can use the getAccountsSnapshot method of the account manager to get a snapshot of the current accounts.
This method returns a promise that resolves with an array of AccountInfo objects.
Below, you can see some example code to create a function that will get and print the list of accounts:
The function to get and print account Hide
typescriptjavascriptprivate getAndPrintAccounts = async (session, application) => { return new Promise<void>((resolve, reject) => { application.resolveGetAccounts = resolve; let accountsManager = session.getAccountsManager(); application.printAccountsSnapshot(accountsManager); }) }
See also getAccountsSnapshot
And the code of callback for providing information about all accounts:
The code of callback Hide
typescriptjavascriptprivate getAccountsSnapshotCallback = new class implements FXConnectLite.IGetAccountsSnapshotCallback { onAccountsReceived(accounts: FXConnectLite.Account[]): void { accounts.forEach(account => { Printer.print(`Current snapshot:`); Application.printAccount(account); }); application.resolveGetAccounts(); } }();
See also onAccountReceived
Finally, add the method to print account properties:
The method to print account properties Hide
typescriptjavascriptpublic static formatDate(date: any): string { return date.getFullYear() + '-' + ('0' + (date.getMonth() + 1)).slice(-2) + '-' + ('0' + date.getDate()).slice(-2); } public static printAccount(account: FXConnectLite.Account): void { Printer.print(``) Printer.print(`AccountId = ${account.getAccountId()}`) Printer.print(`AccountName = ${account.getAccountName()}`) Printer.print(`AccountKind = ${account.getAccountKind()}`) Printer.print(`Base currency = ${account.getBaseCurrency()}`) Printer.print(`Base currency precision = ${account.getBaseCurrencyPrecision()}`) Printer.print(`ATPId = ${account.getATPId()}`) Printer.print(`Balance = ${account.getBalance()}`) Printer.print(`BaseUnitSize = ${account.getBaseUnitSize()}`) Printer.print(`DayPL = ${account.getDayPL()}`) Printer.print(`Equity = ${account.getEquity()}`) Printer.print(`GrossPL = ${account.getGrossPL()}`) Printer.print(`LastMarginCallDate = ${Application.formatDate(account.getLastMarginCallDate())}`) Printer.print(`LeverageProfileId = ${account.getLeverageProfileId()}`) Printer.print(`M2MEquity = ${account.getM2MEquity()}`) Printer.print(`MaintenanceFlag = ${account.getMaintenanceFlag()}`) Printer.print(`MaintenanceType = ${account.getMaintenanceType()}`) Printer.print(`ManagerAccountId = ${account.getManagerAccountId()}`) Printer.print(`MarginCallFlag = ${account.getMarginCallFlag()}`) Printer.print(`NonTradeEquity = ${account.getNonTradeEquity()}`) Printer.print(`OrderAmountLimit = ${account.getOrderAmountLimit()}`) Printer.print(`UsableMaintenanceMargin = ${account.getUsableMaintenanceMargin()}`) Printer.print(`UsableMaintenanceMarginPercentage = ${account.getUsableMaintenanceMarginPercentage()}`) Printer.print(`UsableMargin = ${account.getUsableMargin()}`) Printer.print(`UsableMarginPercentage = ${account.getUsableMarginPercentage()}`) Printer.print(`UsedMaintenanceMargin = ${account.getUsedMaintenanceMargin()}`) Printer.print(`UsedMargin = ${account.getUsedMargin()}`) Printer.print(``) }
See also Account
If you require basic information about all accounts belonging to a particular user, the method getAccountsInfo can assist you.
Conclusion:
You have learned how to use the API to get a list of accounts. You have created an account change listener, subscribed to account changes, and learned how to get and print accounts. You have also learned about the AccountInfo interface, the IAccountsManager interface, and how to use them to manage accounts. You can now use this knowledge to build your own applications that work with accounts in FCLite.
Download the sample TypeScript, JavaScript.