How to use table manager in the ForexConnect API
Brief
The article describes the usage of a table manager in the ForexConnect API.
Details
A table manager creates and maintains the trading tables in the ForexConnect memory. The advantages of the table manager usage are described in the O2GTableManager class documentation.
The table manager implementation details are shown in the ForexConnect API Class Hierarchy section.
The following paragraphs contain complete instructions on the table manager usage.
Starting Table Manager
To ensure the correct usage of a table manager, you must follow these steps:
1. Create an O2GSession object by using the O2GTransport.createSession method. For example,
mSession = O2GTransport.createSession();
2. Before the login, specify that your session uses a table manager by calling the O2GSession.useTableManager method. For example,
mSession.useTableManager(O2GTableManagerMode.YES, null);
3. Log on to the trading server by using the O2GSession.login method with your connection parameters. For example
mSession.login(mUserID, mPassword, mURL, mConnection);
Note: For complete login details, refer to the How to log in section.
4. Obtain an instance of the O2GTableManager class for your session by using the O2GSession.getTableManager method. For example,
O2GTableManager tableManager = mSession.getTableManager();
Note: An instance of the O2GTableManager class can be obtained only after a session gets the CONNECTED status.
If the connection between the ForexConnect API and the trading server brakes, you may need to re-create an instance of the O2GTableManager
class.
In case of the restoration of the connection fails, the session status becomes DISCONNECTED and you must log on again and create a new instance of the O2GTableManager
class.
If the ForexConnect API restores the connection successfully, you can use the existing instance of the O2GTableManager
class.
For a detailed explanation of the session statuses, refer to the Session Statuses section.
Getting Data from the Trading Tables
To get data from a trading table, you must follow these steps:
1. Check the table manager status.
An O2GTableManager
object must have the O2GTableManagerStatus.TABLES_LOADED status.
For details on checking the table manager status, refer to the IO2GTableManagerListener documentation.
Note: You can also monitor the changes of the load status of every table in your implementation
of the IO2GTableListener.onStatusChanged method.
The table load status can be obtained at any time by calling the O2GTable.getStatus method.
A table can be used only if it has the REFRESHED status.
2. Use the O2GTableManager.getTable method
to get an instance of one of the O2GTable
subclasses and cast the return value to the corresponding subclass. The syntax examples
for all trading tables are shown below:
Trading table |
Returned O2GTable subclass |
Syntax example |
Offers
|
O2GOffersTable
|
O2GOffersTable offersTable = (O2GOffersTable)tableManager.getTable(O2GTableType.OFFERS);
|
Accounts
|
O2GAccountsTable
|
O2GAccountsTable accountsTable = (O2GAccountsTable)tableManager.getTable(O2GTableType.ACCOUNTS);
|
Orders
|
O2GOrdersTable
|
O2GOrdersTable ordersTable = (O2GOrdersTable)tableManager.getTable(O2GTableType.ORDERS);
|
Trades
|
O2GTradesTable
|
O2GTradesTable tradesTable = (O2GTradesTable)tableManager.getTable(O2GTableType.TRADES);
|
Closed Trades
|
O2GClosedTradesTable
|
O2GClosedTradesTable closedTradesTable = (O2GClosedTradesTable)tableManager.getTable(O2GTableType.CLOSED_TRADES);
|
Messages
|
O2GMessagesTable
|
O2GMessagesTable messagesTable = (O2GMessagesTable)tableManager.getTable(O2GTableType.MESSAGES);
|
Summary
|
O2GSummaryTable
|
O2GSummaryTable summaryTable = (O2GSummaryTable)tableManager.getTable(O2GTableType.SUMMARY);
|
3. Use one of the methods of the O2GTable
subclass to get row level information.
The methods are: findRow
, getRow
, getNextRow
and getNextRowByColumnValue
.
For example, if you want to get ask and bid prices from the Offers table, write the following lines:
Get bid and ask prices [hide]
for (int i = 0; i < offersTable.size(); i++) {
O2GOfferRow offer = offersTable.getRow(i);
System.out.println("Instrument: " + offer.getInstrument() +
" Bid = " + offer.getBid() +
" Ask = " + offer.getAsk());
}
For the methods definitions and code examples, refer to the documentation of the O2GTable
subclasses shown in the table earlier.
Processing Notifications about the Trading Tables Updates
The trading tables are updated automatically.
To process notifications about the trading tables updates, you must follow these steps:
1. Create a table listener class that implements the IO2GTableListener interface. For example,
public class TableListener implements IO2GTableListener {...}
2. Implement the methods of a table listener class:
- to process notifications about row additions to a table, implement the onAdded method;
- to process notifications about row changes of a table, implement the onChanged method;
- to process notifications about row deletions from a table,implement the onDeleted method.
3. Create an instance of a table listener class. For example,
TableListener tableListener = new TableListener();
4. Subscribe an instance of a table listener class to each update type individually by using the
O2GTable
.subscribeUpdate
method.
5. Process notifications about table updates in the methods described in step 2 of this paragraph.
6. Before the logout, unsubscribe the listener from every subscribed update type separately by using
the O2GTable
.unsubscribeUpdate
method.
For example, the table below shows the subscription/unsubsription syntax for the O2GTradesTable class
and corresponding methods of a class implementing the IO2GTableListener
interface.
Update type |
IO2GTableListener method
|
Subscription syntax |
Unsubscription syntax |
INSERT |
onAdded |
tradesTable.subscribeUpdate(O2GTableUpdateType.INSERT, tableListener);
|
tradesTable.unsubscribeUpdate(O2GTableUpdateType.INSERT, tableListener);
|
UPDATE |
onChanged |
tradesTable.subscribeUpdate(O2GTableUpdateType.UPDATE, tableListener);
|
tradesTable.unsubscribeUpdate(O2GTableUpdateType.UPDATE, tableListener);
|
DELETE |
onDeleted |
tradesTable.subscribeUpdate(O2GTableUpdateType.DELETE, tableListener);
|
tradesTable.unsubscribeUpdate(O2GTableUpdateType.DELETE, tableListener);
|
Using Table Manager Example
If you want to use a table manager to maintain information about opened positions, your program may look similar to the classes shown later:
Main.java [hide]
package gettrades;
import com.fxcore2.O2GSession;
import com.fxcore2.O2GTransport;
import com.fxcore2.O2GTableManagerMode;
import com.fxcore2.O2GTableManagerStatus;
import com.fxcore2.O2GTableManager;
import com.fxcore2.O2GTableType;
import com.fxcore2.O2GTableUpdateType;
import com.fxcore2.O2GTradesTable;
import com.fxcore2.O2GTradeTableRow;
public class Main {
public static void main(String[] args) {
// Connection and session variables
String mUserID = "";
String mPassword = "";
String mURL = "";
String mConnection = "";
String mDBName = "";
String mPin = "";
O2GSession mSession = null;
// Check for correct number of arguments
if (args.length < 4) {
System.out.println("Not Enough Parameters!");
System.out.println("USAGE: [user ID] [password] [URL] [connection] [session ID (if needed)] [pin (if needed)]");
System.exit(1);
}
// Get command line arguments
mUserID = args[0];
mPassword = args[1];
mURL = args[2];
mConnection = args[3];
if (args.length > 4) {
mDBName = args[4];
}
if (args.length > 5) {
mPin = args[5];
}
// Create a session, subscribe to session listener, login, get open positions information, logout
try {
// Create a session
mSession = O2GTransport.createSession();
// Specify that your session uses a table manager
mSession.useTableManager(O2GTableManagerMode.YES, null);
SessionStatusListener statusListener = new SessionStatusListener(mSession, mDBName, mPin);
mSession.subscribeSessionStatus(statusListener);
// Login into the trading server
mSession.login(mUserID, mPassword, mURL, mConnection);
while (!statusListener.isConnected() && !statusListener.hasError()) {
Thread.sleep(50);
}
if (!statusListener.hasError()) {
// Get an instance of table manager
O2GTableManager tableManager = mSession.getTableManager();
// Check the table manager status
while (tableManager.getStatus() != O2GTableManagerStatus.TABLES_LOADED &&
tableManager.getStatus() != O2GTableManagerStatus.TABLES_LOAD_FAILED) {
Thread.sleep(50);
}
if (tableManager.getStatus() == O2GTableManagerStatus.TABLES_LOADED) {
// Get an instance of the O2GTradesTable
O2GTradesTable tradeTable = (O2GTradesTable)tableManager.getTable(O2GTableType.TRADES);
// Get row level information
for (int i = 0; i < tradeTable.size(); i++) {
O2GTradeTableRow trade = tradeTable.getRow(i);
System.out.println( "TradeID: " + trade.getTradeID() +
" Close = " + trade.getClose() +
" GrossPL= " + trade.getGrossPL());
}
// Create an instance of a table listener class
TableListener tableListener = new TableListener();
// Subscribe table listener to table updates
tradeTable.subscribeUpdate(O2GTableUpdateType.UPDATE, tableListener);
tradeTable.subscribeUpdate(O2GTableUpdateType.INSERT, tableListener);
// Process updates (see TableListener.java)
Thread.sleep(10000);
// Unsubscribe table listener
tradeTable.unsubscribeUpdate(O2GTableUpdateType.UPDATE, tableListener);
tradeTable.unsubscribeUpdate(O2GTableUpdateType.INSERT, tableListener);
mSession.logout();
while (!statusListener.isDisconnected()) {
Thread.sleep(50);
}
}
}
mSession.unsubscribeSessionStatus(statusListener);
mSession.dispose();
System.exit(1);
} catch (Exception e) {
System.out.println ("Exception: " + e.getMessage());
System.exit(1);
}
}
}
SessionStatusListener.java [show]
SessionStatusListener.java [hide]
package gettrades;
import com.fxcore2.IO2GSessionStatus;
import com.fxcore2.O2GSession;
import com.fxcore2.O2GSessionStatusCode;
import com.fxcore2.O2GSessionDescriptorCollection;
import com.fxcore2.O2GSessionDescriptor;
public class SessionStatusListener implements IO2GSessionStatus {
// Connection , session and status variables
private boolean mConnected = false;
private boolean mDisconnected = false;
private boolean mError = false;
private String mDBName = "";
private String mPin = "";
private O2GSession mSession = null;
private O2GSessionStatusCode mStatus = null;
// Constructor
public SessionStatusListener(O2GSession session, String dbName, String pin) {
mSession = session;
mDBName = dbName;
mPin = pin;
}
//Shows if session is connected
public final boolean isConnected() {
return mConnected;
}
//Shows if session is disconnected
public final boolean isDisconnected() {
return mDisconnected;
}
// Shows if there was an error during the logn process
public final boolean hasError() {
return mError;
}
// Returns current session status
public final O2GSessionStatusCode getStatus() {
return mStatus;
}
// Implementation of IO2GSessionStatus interface public method onSessionStatusChanged
public final void onSessionStatusChanged(O2GSessionStatusCode status) {
mStatus = status;
System.out.println("Status: " + mStatus.toString());
if (mStatus == O2GSessionStatusCode.CONNECTED) {
mConnected = true;
}
else {
mConnected = false;
}
if (status == O2GSessionStatusCode.DISCONNECTED) {
mDisconnected = true;
}
else {
mDisconnected = false;
}
if (mStatus == O2GSessionStatusCode.TRADING_SESSION_REQUESTED) {
O2GSessionDescriptorCollection descs = mSession.getTradingSessionDescriptors();
System.out.println("\nSession descriptors");
System.out.println("id, name, description, requires pin");
for (O2GSessionDescriptor desc : descs) {
System.out.println(desc.getId() + " " + desc.getName() + " " + desc.getDescription() + " " + desc.isPinRequired());
}
if (mDBName.equals("")) {
System.out.println("Argument for trading session ID is missing");
}
else {
mSession.setTradingSession(mDBName, mPin);
}
}
}
// Implementation of IO2GSessionStatus interface public method onLoginFailed
public final void onLoginFailed(String error) {
System.out.println("Login error: " + error + "\nPrevious Status: " + mStatus);
mError = true;
}
}
TableListener.java [hide]
package gettrades;
import com.fxcore2.IO2GTableListener;
import com.fxcore2.O2GRow;
import com.fxcore2.O2GTableStatus;
import com.fxcore2.O2GTradeTableRow;
public class TableListener implements IO2GTableListener {
// Implementation of IO2GTableListener interface public method onAdded
public void onAdded(String rowID, O2GRow rowData) {
O2GTradeTableRow trade = (O2GTradeTableRow)(rowData);
System.out.println("Trade information added " + rowID);
System.out.println("TradeID: " + trade.getTradeID() +
" Close = " + trade.getClose());
}
// Implementation of IO2GTableListener interface public method onChanged
public void onChanged(String rowID, O2GRow rowData) {
O2GTradeTableRow trade = (O2GTradeTableRow)(rowData);
System.out.println("Trade information changed " + rowID);
System.out.println("TradeID: " + trade.getTradeID() +
" Close = " + trade.getClose());
}
// Implementation of IO2GTableListener interface public method onDeleted
public void onDeleted(String rowID, O2GRow rowData) {
O2GTradeTableRow trade = (O2GTradeTableRow)(rowData);
System.out.println("Trade information deleted " + rowID);
System.out.println("TradeID: " + trade.getTradeID() +
" Close = " + trade.getClose());
}
// Implementation of IO2GTableListener interface public method onStatus
public void onStatusChanged(O2GTableStatus status) {
}
}
back