The article discusses how to get offers in Java.
There are two ways to get offers in your Java application.
The first way is to use table listener. In this case you should:
Create a session, a session status listener, subscribe the listener to the session status
(see this article
).
Implement the IO2GTableListener
interface
in a table listener class:
public class TableListener implements IO2GTableListener {...}
Allow the use of table manager (it should be done before login):
mSession.useTableManager(O2GTableManagerMode.YES);
Log in (see this article
)
Get O2GTableManager
:
O2GTableManager tableManager = mSession.getTableManager();
Get "Offers" table:
O2GOffersTable offersTable = (O2GOffersTable)tableManager.getTable(O2GTableType.OFFERS);
The table can be processed to get information about offers:
O2GTableIterator iterator = new O2GTableIterator();
O2GOfferTableRow offerTableRow = offersTable.getNextRow(iterator);
while (offerTableRow!=null)
{
System.out.println("Instrument = " + offerTableRow.getInstrument() + "; Bid = " + offerTableRow.getBid() + "; Ask = " + offerTableRow.getAsk());
offerTableRow = offersTable.getNextRow(iterator);
}
To capture updates of "Offers" table create table listener and subscribe it to "Update" event of "Offers" table:
TableListener tableListener = new TableListener();
offersTable.subscribeUpdate(O2GTableUpdateType.UPDATE, tableListener);
Process table updates in "onChanged" event of IO2GTableListener
// Implementation of IO2GTableListener interface public method onChanged
public void onChanged(String rowID, O2GRow rowData) {
O2GOfferTableRow offerTableRow = (O2GOfferTableRow)(rowData);
if (offerTableRow!=null)
System.out.println("Instrument = " + offerTableRow.getInstrument() + "; Bid = " + offerTableRow.getBid() + "; Ask = " + offerTableRow.getAsk());
}
Unsubscribe from table listener when you are done:
offersTable.unsubscribeUpdate(O2GTableUpdateType.UPDATE, tableListener);
Log out, unsubscribe from the session status listener, and dispose the session:
mSession.logout();
mSession.unsubscribeSessionStatus(statusListener);
mSession.dispose();
Get offers with table manager example: [hide]
package getoffers;
import com.fxcore2.O2GSession;
import com.fxcore2.O2GTransport;
import com.fxcore2.O2GTableType;
import com.fxcore2.O2GOffersTable;
import com.fxcore2.O2GOfferTableRow;
import com.fxcore2.O2GTableIterator;
import com.fxcore2.O2GTableManagerMode;
import com.fxcore2.O2GTableManager;
import com.fxcore2.O2GTableManagerStatus;
import com.fxcore2.O2GTableUpdateType;
import com.fxcore2.IO2GTableListener;
import com.fxcore2.O2GRow;
import com.fxcore2.O2GTableStatus;
import com.fxcore2.IO2GSessionStatus;
import com.fxcore2.O2GSessionStatusCode;
import com.fxcore2.O2GSessionDescriptorCollection;
import com.fxcore2.O2GSessionDescriptor;
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;
String mInstrument = "";
// Check for correct number of arguments
if (args.length < 5) {
System.out.println("Not Enough Parameters!");
System.out.println("USAGE: [instrument] [user ID] [password] [URL] [connection] [session ID (if needed)] [pin (if needed)]");
System.exit(1);
}
// Get command line arguments
mInstrument = args[0];
mUserID = args[1];
mPassword = args[2];
mURL = args[3];
mConnection = args[4];
if (args.length > 5) {
mDBName = args[5];
}
if (args.length > 6) {
mPin = args[6];
}
// Create a session, subscribe to session listener, login, get offers, logout
try {
mSession = O2GTransport.createSession();
SessionStatusListener statusListener = new SessionStatusListener(mSession, mDBName, mPin);
mSession.subscribeSessionStatus(statusListener);
mSession.useTableManager(O2GTableManagerMode.YES, null);
mSession.login(mUserID, mPassword, mURL, mConnection);
while (!statusListener.isConnected() && !statusListener.hasError()) {
Thread.sleep(50);
}
if (!statusListener.hasError()) {
O2GTableManager manager = mSession.getTableManager();
while (manager.getStatus() == O2GTableManagerStatus.TABLES_LOADING) {
Thread.sleep(50);
}
O2GOffersTable offers = null;
OffersListener listener = null;
if (manager.getStatus() == O2GTableManagerStatus.TABLES_LOADED) {
offers = (O2GOffersTable)manager.getTable(O2GTableType.OFFERS);
getOffers(offers);
listener = new OffersListener();
listener.SetInstrumentFilter(mInstrument);
offers.subscribeUpdate(O2GTableUpdateType.UPDATE, listener);
}
System.out.println("Press enter to stop!");
System.in.read();
if (offers != null) {
offers.unsubscribeUpdate(O2GTableUpdateType.UPDATE, listener);
}
mSession.logout();
while (!statusListener.isDisconnected()) {
Thread.sleep(50);
}
}
mSession.unsubscribeSessionStatus(statusListener);
mSession.dispose();
} catch (Exception e) {
System.out.println ("Exception: " + e.getMessage());
System.exit(1);
}
}
// Get offers information
public static void getOffers(O2GOffersTable offers) {
try {
O2GOfferTableRow offer = null;
O2GTableIterator iterator = new O2GTableIterator();
offer = offers.getNextRow(iterator);
while (offer != null) {
System.out.println("Instrument = " + offer.getInstrument() +
" Bid Price = " + offer.getBid() +
" Ask Price = " + offer.getAsk() +
" PipCost = " + offer.getPipCost());
offer = offers.getNextRow(iterator);
}
} catch (Exception e) {
System.out.println("Exception in getOffers().\n\t " + e.getMessage());
}
}
}
class OffersListener implements IO2GTableListener {
private String mInstrument = null;
public void SetInstrumentFilter(String instrument) {
mInstrument = instrument;
}
public void onAdded(String string, O2GRow o2grow) {
}
public void onChanged(String string, O2GRow o2grow) {
O2GOfferTableRow row = (O2GOfferTableRow)o2grow;
if (mInstrument == null || mInstrument.isEmpty() || row.getInstrument().equals(mInstrument)) {
System.out.println("Instrument = " + row.getInstrument() +
" Bid Price = " + row.getBid() +
" Ask Price = " + row.getAsk());
}
}
public void onDeleted(String string, O2GRow o2grow) {
}
public void onStatusChanged(O2GTableStatus ogts) {
}
}
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);
mError = true;
}
}
The second way to get offers is to use response listener. In that case you should:
Create a session, a session status listener, subscribe the listener to the session status
(see this article
).
Implement the IO2GResponseListener
interface
in a response listener class:
public class ResponseListener implements IO2GResponseListener {...}
Subscribe an object of this class to the session response:
ResponseListener responseListener = new ResponseListener(...)
session.subscribeResponse(responseListener);
Log in (see this article
).
Use the listener onSessionStatusChanged
function to capture the CONNECTED
event.
Get O2GLoginRules
:
O2GLoginRules rules = mSession.getLoginRules();
Then you can get O2GResponse
:
O2GResponse response = loginRules.getTableRefreshResponse(O2GTableType.OFFERS);
The response can be processed to extract necessary information.
You should also capture O2GResponse
in the
onTablesUpdates
function of a response listener class and
process the response.
To get information from the "Offers" table from O2GResponse
:
1. Create O2GResponseReaderFactory
:
O2GResponseReaderFactory readerFactory = session.getResponseReaderFactory();
2. Check the readerFactory object for validity:
if (readerFactory == null) {
return;
}
3. Create O2GOffersTableResponseReader
:
O2GOffersTableResponseReader reader = readerFactory.createOffersTableReader(response);
4. Process row by row:
for (i = 0; i < reader.size(); i++)
{
O2GOfferRow row = reader.getRow(i);
//information like row.OfferID, row.Instrument, row.Bid, row.Ask could be extracted now
}
Log out, unsubscribe from the session status listener and response listener, and dispose the session when you are done:
mSession.logout();
mSession.unsubscribeSessionStatus(statusListener);
mSession.unsubscribeResponse(responseListener);
mSession.dispose();
Get offers example: [hide]
package getoffers;
import com.fxcore2.O2GSession;
import com.fxcore2.O2GTransport;
import com.fxcore2.O2GLoginRules;
import com.fxcore2.O2GResponseReaderFactory;
import com.fxcore2.O2GResponse;
import com.fxcore2.O2GOffersTableResponseReader;
import com.fxcore2.O2GTableType;
import com.fxcore2.O2GOfferRow;
import com.fxcore2.O2GRequestFactory;
import com.fxcore2.IO2GResponseListener;
import com.fxcore2.IO2GResponseType;
import com.fxcore2.IO2GSessionStatus;
import com.fxcore2.O2GSessionStatusCode;
import com.fxcore2.O2GSessionDescriptorCollection;
import com.fxcore2.O2GSessionDescriptor;
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;
String mInstrument = "";
// Check for correct number of arguments
if (args.length < 5) {
System.out.println("Not Enough Parameters!");
System.out.println("USAGE: [instrument] [user ID] [password] [URL] [connection] [session ID (if needed)] [pin (if needed)]");
System.exit(1);
}
// Get command line arguments
mInstrument = args[0];
mUserID = args[1];
mPassword = args[2];
mURL = args[3];
mConnection = args[4];
if (args.length > 5) {
mDBName = args[5];
}
if (args.length > 6) {
mPin = args[6];
}
// Create a session, subscribe to session listener, login, get offers, logout
try {
mSession = O2GTransport.createSession();
SessionStatusListener statusListener = new SessionStatusListener(mSession, mDBName, mPin);
mSession.subscribeSessionStatus(statusListener);
ResponseListener responseListener = new ResponseListener(mSession,mInstrument);
mSession.subscribeResponse(responseListener);
mSession.login(mUserID, mPassword, mURL, mConnection);
while (!statusListener.isConnected() && !statusListener.hasError()) {
Thread.sleep(50);
}
if (!statusListener.hasError()) {
getOffers(mSession);
if (!mInstrument.equals("{INSTRUMENT}") && !mInstrument.equals("")) {
O2GRequestFactory requestFactory = mSession.getRequestFactory();
if (requestFactory != null) {
mSession.sendRequest(requestFactory.createRefreshTableRequest(O2GTableType.OFFERS));
Thread.sleep(10000);
}
}
mSession.logout();
while (!statusListener.isDisconnected()) {
Thread.sleep(50);
}
}
mSession.unsubscribeSessionStatus(statusListener);
mSession.unsubscribeResponse(responseListener);
mSession.dispose();
} catch (Exception e) {
System.out.println ("Exception: " + e.getMessage());
System.exit(1);
}
}
// Get offers information
public static void getOffers(O2GSession session) {
try {
O2GLoginRules loginRules = session.getLoginRules();
if (loginRules != null && loginRules.isTableLoadedByDefault(O2GTableType.OFFERS)) {
O2GResponse offersResponse = loginRules.getTableRefreshResponse(O2GTableType.OFFERS);
O2GResponseReaderFactory responseFactory = session.getResponseReaderFactory();
O2GOffersTableResponseReader offersReader = responseFactory.createOffersTableReader(offersResponse);
for (int i = 0; i < offersReader.size(); i++) {
O2GOfferRow offer = offersReader.getRow(i);
System.out.println("Instrument = " + offer.getInstrument() +
" Bid Price = " + offer.getBid() +
" Ask Price = " + offer.getAsk());
}
}
} catch (Exception e) {
System.out.println("Exception in getOffers().\n\t " + e.getMessage());
}
}
}
class ResponseListener implements IO2GResponseListener {
// Session, response and request variables
private O2GSession mSession = null;
private String mInstrument = "";
private O2GResponse mResponse = null;
private String mRequest = "";
// Constructor
public ResponseListener(O2GSession session, String instrument) {
mSession = session;
mInstrument = instrument;
}
// Implementation of IO2GResponseListener interface public method onRequestCompleted
public void onRequestCompleted(String requestID, O2GResponse response) {
if (response.getType() == O2GResponseType.GET_OFFERS) {
mResponse = response;
mRequest = "getoffers";
printOffers(mSession, mResponse, mInstrument);
}
}
// Implementation of IO2GResponseListener interface public method onRequestFailed
public void onRequestFailed(String requestID, String error) {
System.out.println("Request failed. requestID= " + requestID + "; error= " + error);
}
// Implementation of IO2GResponseListener interface public method onTablesUpdates
public void onTablesUpdates(O2GResponse response) {
if (response.getType() == O2GResponseType.TABLES_UPDATES) {
mResponse = response;
mRequest = "tablesupdates";
printOffers(mSession, mResponse, mInstrument);
}
}
// Prints response to a user request and live offer updates
public void printOffers(O2GSession session, O2GResponse response, String instrument) {
O2GResponseReaderFactory readerFactory = session.getResponseReaderFactory();
if (readerFactory != null) {
O2GOffersTableResponseReader reader = readerFactory.createOffersTableReader(response);
for (int i = 0; i < reader.size(); i++) {
O2GOfferRow row = reader.getRow(i);
if (row.isBidValid() && row.isAskValid())
{
if (row.getInstrument().equals(instrument)) {
if ("getoffers".equals(mRequest)) {
System.out.println("This is a response to getOffers request.");
}
if ("tablesupdates".equals(mRequest)) {
System.out.println("This is your live update.");
}
System.out.println("Instrument = " + row.getInstrument() +
" Bid Price = " + row.getBid() +
" Ask Price = " + row.getAsk());
mRequest = "";
break;
}
}
}
}
}
}
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);
mError = true;
}
}