The article discusses how to get offers in .Net.
There are different ways to get offers in your .NET application.
Create a session, a session status listener, subscribe the listener to the session status
(see this article
).
Allow the use of table manager:
mSession.useTableManager(O2GTableManagerMode.Yes, null);
It should be done before the login.
Log in (see this article
).
Get O2GTableManager
and wait for TablesLoaded
status:
O2GTableManager tableMgr = mSession.getTableManager();
managerStatus = tableMgr.getStatus();
while (managerStatus == O2GTableManagerStatus.TablesLoading)
{
Thread.Sleep(50);
managerStatus = tableMgr.getStatus();
}
if (managerStatus == O2GTableManagerStatus.TablesLoadFailed)
return;
Get "Offers" table:
O2GOffersTable offersTable = (O2GOffersTable)tableMgr.getTable(O2GTableType.Offers);
Specify the method that will be triggered by the RowChanged
event.
offersTable.RowChanged +=new EventHandler<RowEventArgs>(offersTable_RowChanged);
Get information about a new offer in that method.
static void offersTable_RowChanged(object sender, RowEventArgs e)
{
O2GOfferTableRow offerTableRow = (O2GOfferTableRow)e.RowData;
if (offerTableRow != null)
{
Console.WriteLine("Instrument {0}, Bid={1}, Ask = {2}", offerTableRow.Instrument, offerTableRow.Bid, offerTableRow.Ask);
}
}
Log out, unsubscribe from the session status listener, and dispose the session when you are done:
mSession.logout();
mSession.unsubscribeSessionStatus(statusListener);
mSession.Dispose();
Get offers using table manager example: [hide]
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Runtime.InteropServices;
using fxcore2;
namespace GetOffers
{
class Program
{
static O2GSession mSession;
private static string sSessionID = "";
public static string SessionID
{
get { return sSessionID; }
}
private static string sPin = "";
public static string Pin
{
get { return sPin; }
}
private static string sInstrument = "";
static void Main(string[] args)
{
if (args.Length < 4)
{
Console.WriteLine("Not Enough Parameters!");
Console.WriteLine("USAGE: [application].exe [user ID] [password] [URL] [connection] [session ID (if needed or empty strinig)] [pin (if needed or empty string)] [instrument (optional)]");
Console.WriteLine("\nPress any key to close the program");
Console.ReadKey();
return;
}
string sUserID = args[0];
string sPassword = args[1];
string sURL = args[2];
string sConnection = args[3];
if (args.Length > 4)
sSessionID = args[4];
if (args.Length > 5)
sPin = args[5];
if (args.Length > 6)
sInstrument = args[6];
try
{
mSession = O2GTransport.createSession();
SessionStatusListener statusListener = new SessionStatusListener(mSession);
mSession.subscribeSessionStatus(statusListener);
mSession.useTableManager(O2GTableManagerMode.Yes, null);
mSession.login(sUserID, sPassword, sURL, sConnection);
while (statusListener.Status != O2GSessionStatusCode.Connected && statusListener.Status != O2GSessionStatusCode.Disconnected)
Thread.Sleep(50);
if (statusListener.Status == O2GSessionStatusCode.Connected)
{
O2GTableManager manager = mSession.getTableManager();
while (manager.getStatus() == O2GTableManagerStatus.TablesLoading)
Thread.Sleep(50);
O2GOffersTable offers = null;
if (manager.getStatus() == O2GTableManagerStatus.TablesLoaded)
{
offers = (O2GOffersTable)manager.getTable(O2GTableType.Offers);
printOffers(offers);
offers.RowChanged += new EventHandler<RowEventArgs>(offers_RowChanged);
}
else
{
Console.WriteLine("Tables loading failed!");
}
Console.WriteLine("Press enter to stop!");
Console.ReadLine();
if (offers != null)
{
offers.RowChanged -= new EventHandler<RowEventArgs>(offers_RowChanged);
}
mSession.logout();
}
mSession.unsubscribeSessionStatus(statusListener);
mSession.Dispose();
}
catch (Exception e)
{
Console.WriteLine("Exception: {0}", e.ToString());
}
}
static void offers_RowChanged(object sender, RowEventArgs e)
{
O2GOfferTableRow row = (O2GOfferTableRow)e.RowData;
string sCurrentInstrument = row.Instrument;
if ((sInstrument.Equals("")) || (sInstrument.Equals(sCurrentInstrument)))
PrintOffer(row);
}
public static void printOffers(O2GOffersTable offers)
{
O2GOfferTableRow row = null;
O2GTableIterator iterator = new O2GTableIterator();
while(offers.getNextRow(iterator, out row))
{
string sCurrentInstrument = row.Instrument;
if ((sInstrument.Equals("")) || (sInstrument.Equals(sCurrentInstrument)))
PrintOffer(row);
}
}
public static void PrintOffer(O2GOfferTableRow row)
{
Console.WriteLine("OfferID: {0}, Instrument: {1}, Bid: {2}, Ask: {3}, PipCost: {4}", row.OfferID, row.Instrument, row.Bid, row.Ask, row.PipCost);
}
}
class SessionStatusListener : IO2GSessionStatus
{
private O2GSessionStatusCode mCode = O2GSessionStatusCode.Unknown;
private O2GSession mSession = null;
public O2GSessionStatusCode Status
{
get
{
return mCode;
}
}
public SessionStatusListener(O2GSession session)
{
mSession = session;
}
public void onSessionStatusChanged(O2GSessionStatusCode code)
{
mCode = code;
Console.WriteLine(code.ToString());
if (code == O2GSessionStatusCode.TradingSessionRequested)
{
if (Program.SessionID == "")
Console.WriteLine("Argument for trading session ID is missing");
else
mSession.setTradingSession(Program.SessionID, Program.Pin);
}
}
public void onLoginFailed(string error)
{
Console.WriteLine("Login error " + error);
}
}
}
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:
class TableListener : IO2GTableListener{...}
Allow the use of table manager:
mSession.useTableManager(O2GTableManagerMode.Yes, null);
It should be done before the login.
Log in (see this article
).
Get O2GTableManager
and wait for TablesLoaded
status:
O2GTableManager tableMgr = mSession.getTableManager();
managerStatus = tableMgr.getStatus();
while (managerStatus == O2GTableManagerStatus.TablesLoading)
{
Thread.Sleep(50);
managerStatus = tableMgr.getStatus();
}
if (managerStatus == O2GTableManagerStatus.TablesLoadFailed)
return;
Get "Offers" table:
O2GOffersTable offersTable = (O2GOffersTable)tableMgr.getTable(O2GTableType.Offers);
The table can be processed to get information about offers:
O2GTableIterator iterator = new O2GTableIterator();
O2GOfferTableRow offerTableRow;
while (offersTable.getNextRow(iterator, out offerTableRow) == true)
{
Console.WriteLine("Instrument = {0}, Bid = {1}, Ask = {2}",
offerTableRow.Instrument, offerTableRow.Bid, offerTableRow.Ask);
}
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
public void onChanged(string rowID, O2GRow rowData)
{
O2GOfferTableRow offerTableRow = (O2GOfferTableRow)rowData;
if (offerTableRow != null)
{
Console.WriteLine("Instrument = {0}, Bid = {1}, Ask = {2}",
offerTableRow.Instrument, offerTableRow.Bid, offerTableRow.Ask);
}
}
Unsubscribe from table listener, log out, unsubscribe from the session status listener, and dispose the session when you are done:
offersTable.unsubscribeUpdate(O2GTableUpdateType.Update, tableListener);
mSession.logout();
mSession.unsubscribeSessionStatus(statusListener);
mSession.Dispose();
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 MyResponseListener : IO2GResponseListener{...}
Subscribe an object of this class to the session response:
MyResponseListener responseListener = new MyResponseListener();
mSession.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 = rules.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 respReaderFactory = session.getResponseReaderFactory();
2. Create O2GOffersTableResponseReader
:
O2GOffersTableResponseReader reader = respReaderFactory.createOffersTableReader(response);
3. Process row by row:
for (i = 0; i < reader.Count; 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.unsubscribeResponse(responseListener);
mSession.logout();
mSession.unsubscribeSessionStatus(statusListener);
mSession.Dispose();
Get offers example: [hide]
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Runtime.InteropServices;
using fxcore2;
namespace GetOffers
{
class Program
{
static O2GSession mSession;
private static string sSessionID = "";
public static string SessionID
{
get { return sSessionID; }
}
private static string sPin = "";
public static string Pin
{
get { return sPin; }
}
private static string sInstrument = "";
static void Main(string[] args)
{
if (args.Length < 4)
{
Console.WriteLine("Not Enough Parameters!");
Console.WriteLine("USAGE: [application].exe [user ID] [password] [URL] [connection] [session ID (if needed or empty strinig)] [pin (if needed or empty string)] [instrument (optional)]");
Console.WriteLine("\nPress enter to close the program");
Console.ReadLine();
return;
}
string sUserID = args[0];
string sPassword = args[1];
string sURL = args[2];
string sConnection = args[3];
if (args.Length > 4)
sSessionID = args[4];
if (args.Length > 5)
sPin = args[5];
if (args.Length > 6)
sInstrument = args[6];
try
{
mSession = O2GTransport.createSession();
SessionStatusListener statusListener = new SessionStatusListener(mSession);
ResponseListener responseListener = new ResponseListener();
mSession.subscribeResponse(responseListener);
mSession.subscribeSessionStatus(statusListener);
mSession.login(sUserID, sPassword, sURL, sConnection);
while (statusListener.Status != O2GSessionStatusCode.Connected && statusListener.Status != O2GSessionStatusCode.Disconnected) ;
Thread.Sleep(10000);
mSession.logout();
mSession.unsubscribeSessionStatus(statusListener);
mSession.unsubscribeResponse(responseListener);
mSession.Dispose();
}
catch (Exception e)
{
Console.WriteLine("Exception: {0}", e.ToString());
}
}
public static void printOffers(O2GResponse response)
{
O2GResponseReaderFactory readerFactory = mSession.getResponseReaderFactory();
if (readerFactory != null)
{
O2GOffersTableResponseReader reader = readerFactory.createOffersTableReader(response);
int i;
for (i = 0; i < reader.Count; i++)
{
O2GOfferRow row = reader.getRow(i);
if (row.isBidValid && row.isAskValid)
{
string sCurrentInstrument = row.Instrument;
if ((sInstrument.Equals("")) || (sInstrument.Equals(sCurrentInstrument)))
Console.WriteLine("{0} {1} {2} {3}", row.OfferID, row.Instrument, row.Bid, row.Ask);
}
}
}
}
}
class ResponseListener : IO2GResponseListener
{
public void onRequestCompleted(string requestID, O2GResponse response)
{
if (response.Type == O2GResponseType.GetOffers)
Program.printOffers(response);
}
public void onRequestFailed(string requestID, string error)
{
}
public void onTablesUpdates(O2GResponse response)
{
if (response.Type == O2GResponseType.GetOffers || response.Type == O2GResponseType.TablesUpdates)
Program.printOffers(response);
}
}
class SessionStatusListener : IO2GSessionStatus
{
private O2GSessionStatusCode mCode = O2GSessionStatusCode.Unknown;
private O2GSession mSession = null;
public O2GSessionStatusCode Status
{
get
{
return mCode;
}
}
public SessionStatusListener(O2GSession session)
{
mSession = session;
}
public void onSessionStatusChanged(O2GSessionStatusCode code)
{
mCode = code;
if (code == O2GSessionStatusCode.Connected)
{
O2GLoginRules rules = mSession.getLoginRules();
if (rules.isTableLoadedByDefault(O2GTableType.Offers))
{
Program.printOffers(rules.getTableRefreshResponse(O2GTableType.Offers));
}
else
{
O2GRequestFactory requestFactory = mSession.getRequestFactory();
if (requestFactory != null)
mSession.sendRequest(requestFactory.createRefreshTableRequest(O2GTableType.Offers));
}
}
else if (code == O2GSessionStatusCode.TradingSessionRequested)
{
if (Program.SessionID == "")
Console.WriteLine("Argument for trading session ID is missing");
else
mSession.setTradingSession(Program.SessionID, Program.Pin);
}
}
public void onLoginFailed(string error)
{
Console.WriteLine("Login error " + error);
}
}
}