How to use table manager in ForexConnect API
Brief
The article describes usage of a table manager in ForexConnect API.
Details
Table manager creates a convenient interface which allows access to trading tables.
Table manager provides the following advantages:
Keeps tables with up-to-date values in memory (especially important for the Offers table, which allows access to current prices).
Provides calculated fields (i.e. PipCost in the Offers table, UsableMargin in the Accounts table, etc.). These fields are updated automatically. If you use response listener, calculated fields are not available.
Provides the Summary table.
Allows separate subscriptions to trading tables events. These events are specified in the IO2GTableListener interface.
The disadvantage of table manager usage compared to the usage of response listener is performance decrease. Performance decreases because the calculated values are computed on each change of the price. If your application does not need calculated values, usage of table manager is not recommended.
For implementation details, please refer to the ForexConnect API Class Hierarchy section.
To ensure correct usage of table manager, please follow these steps:
Create an IO2GSession
object using createSession()
method:
IO2GSession *session = CO2GTransport::createSession();
Before the login, specify that your session will use table manager by calling useTableManager() method:
session->useTableManager(::Yes, NULL);
Login into the ForexConnect API using login() method with your connection parameters:
session->login(user.c_str(), password.c_str(), url.c_str(), connection.c_str());
Note: For complete login details, please refer to the How to log in section.
Obtain table manager
for your session using getTableManager() method:
IO2GTableManager *tableManager = session->getTableManager();
Wait for TablesLoaded
status of IO2GTableManager
.
while (tableManager->getStatus() != TablesLoaded && tableManager->getStatus() != TablesLoadFailed)
uni::Sleep(50);
if (tableManager->getStatus() == TablesLoaded)
{
//...
}
Now trading tables are ready and available for usage.
Please see examples below about getting data from the tables and getting notification about tables updates.
Do not forget to release table manager when you are done:
tableManager->release();
To get data from trading table:
- use getTable() method and cast the return value to the corresponding class;
- get row data using findRow
, getRow
, or getNextRow
.
Please see a table below for a list of trading tables and corresponding subclasses of IO2GTable
Table |
Corresponding class |
For example, if you want to get ask and bid prices from the Offers table, please follow these steps:
Obtain IO2GOffersTable using getTable() method
and casting result to IO2GOffersTable:
IO2GOffersTable *offersTable = (IO2GOffersTable *)tableManager->getTable(::Offers);
Obtain IO2GOfferRow using getNextRow() method,
extract necessary information from it, and release IO2GOfferRow:
IO2GTableIterator tableIterator = {0, 0, NULL};
IO2GOfferTableRow *offerRow = NULL;
while (offersTable->getNextRow(tableIterator, offerRow))
{
//...
offerRow->release();
}
Release IO2GOffersTable
when you are done.
offersTable->release();
To receive notifications about updates of trading tables you should:
- create a table listener class which implements IO2GTableListener interface
- create an object of that class
- subscribe table listener object to a table event
- unsubscribe table listener from that table event when you are done
Please see the table below for subscribing/unsubscribing methods of IO2GTable
and corresponding events of IO2GTableListener
.
Method of |
Update type |
Event of |
Method of |
||||||
N/A |
For example, if you want to get updates for ask and bid prices, your table listener class may look similar to this:
tablelistener.h [show]
tablelistener.cpp [show]
Follow these steps:
Create an instance of your table listener class:
TableListener *tableListener = new TableListener();
Subscribe to onChanged()
event using the subscribeUpdate()
method:
offersTable->subscribeUpdate(Update, tableListener);
Wait for updates:
uni::Sleep(10000);
Unsubscribe from your table listener class using the unsubscribeUpdate()
method:
offersTable->unsubscribeUpdate(Update, tableListener);
Release table, table listener, and table manager:
offersTable->release();
tableListener->release();
tableManager->release();
For complete list of steps required to obtain offers information, please refer to How to get offers section.