static void Main(string[] args) { O2GSession session = null; try { LoginParams loginParams = new LoginParams(ConfigurationManager.AppSettings); SampleParams sampleParams = new SampleParams(ConfigurationManager.AppSettings); PrintSampleParams("GetOffers", loginParams, sampleParams); session = O2GTransport.createSession(); SessionStatusListener statusListener = new SessionStatusListener(session, loginParams.SessionID, loginParams.Pin); session.subscribeSessionStatus(statusListener); statusListener.Reset(); session.login(loginParams.Login, loginParams.Password, loginParams.URL, loginParams.Connection); if (statusListener.WaitEvents() && statusListener.Connected) { ResponseListener responseListener = new ResponseListener(session); responseListener.SetInstrument(sampleParams.Instrument); session.subscribeResponse(responseListener); O2GLoginRules loginRules = session.getLoginRules(); if (loginRules == null) { throw new Exception("Cannot get login rules"); } O2GResponse response; if (loginRules.isTableLoadedByDefault(O2GTableType.Offers)) { response = loginRules.getTableRefreshResponse(O2GTableType.Offers); if (response != null) { responseListener.PrintOffers(session, response, null); } } else { O2GRequestFactory requestFactory = session.getRequestFactory(); if (requestFactory != null) { O2GRequest offersRequest = requestFactory.createRefreshTableRequest(O2GTableType.Offers); responseListener.SetRequestID(offersRequest.RequestID); session.sendRequest(offersRequest); if (!responseListener.WaitEvents()) { throw new Exception("Response waiting timeout expired"); } response = responseListener.GetResponse(); if (response != null) { responseListener.PrintOffers(session, response, null); } } } // Do nothing 10 seconds, let offers print Thread.Sleep(100000); Console.WriteLine("Done!"); session.unsubscribeResponse(responseListener); statusListener.Reset(); session.logout(); statusListener.WaitEvents(); } session.unsubscribeSessionStatus(statusListener); } catch (Exception e) { Console.WriteLine("Exception: {0}", e.ToString()); } finally { if (session != null) { session.Dispose(); } } }
/// <summary> /// Listener: When Trading session status is changed /// </summary> /// <param name="status"></param> public void onSessionStatusChanged(O2GSessionStatusCode status) { switch (status) { case O2GSessionStatusCode.TradingSessionRequested: // If the trading session requires the session name or pin code... if (OnErrorEvent != null) { OnErrorEvent("Multi-session connectors aren't supported by this example\n"); } mTradingSession.logout(); break; case O2GSessionStatusCode.Connected: // login is completed // now we need collect data about the system properties O2GLoginRules loginRules = mTradingSession.getLoginRules(); mTZConverter = mTradingSession.getTimeConverter(); // get the trading day offset. O2GResponse response; response = loginRules.getSystemPropertiesResponse(); O2GSystemPropertiesReader reader = mTradingSession.getResponseReaderFactory().createSystemPropertiesReader(response); string eod = reader.Properties["END_TRADING_DAY"]; DateTime time = DateTime.ParseExact("01.01.1900_" + eod, "MM.dd.yyyy_HH:mm:ss", CultureInfo.InvariantCulture); // convert Trading day start to EST time because the trading day is always closed by New York time // so to avoid handling different hour depending on daylight saying time - use EST always // for candle calculations time = mTZConverter.convert(time, O2GTimeConverterTimeZone.UTC, O2GTimeConverterTimeZone.EST); // here we have the date when trading day begins, e.g. 17:00:00 // please note that if trading day begins before noon - it begins AFTER calendar date is started, // so the offset is positive (e.g. 03:00 is +3 offset). // if trading day begins after noon, it begins BEFORE calendar date is istarted, // so the offset is negative (e.g. 17:00 is -7 offset). if (time.Hour <= 12) { mTradingDayOffset = time.Hour; } else { mTradingDayOffset = time.Hour - 24; } // ...and now get the list of the offers to which the user is subscribed if (loginRules.isTableLoadedByDefault(O2GTableType.Offers)) { // if it is already loaded - just handle them response = loginRules.getTableRefreshResponse(O2GTableType.Offers); onRequestCompleted(null, response); } else { // otherwise create the request to get offers from the server O2GRequestFactory factory = mTradingSession.getRequestFactory(); O2GRequest offerRequest = factory.createRefreshTableRequest(O2GTableType.Offers); mTradingSession.sendRequest(offerRequest); } break; default: if (OnStateChange != null) { OnStateChange(false); } break; } }
void IO2GSessionStatus.onSessionStatusChanged(O2GSessionStatusCode status) { bool connected; string name; switch (status) { case O2GSessionStatusCode.Connected: connected = true; mTimeConverter = mSession.getTimeConverter(); O2GLoginRules rules = mSession.getLoginRules(); if (rules.isTableLoadedByDefault(O2GTableType.Offers)) { O2GResponse offers = rules.getTableRefreshResponse(O2GTableType.Offers); ReadOffers(offers); mReady = true; } else { O2GRequestFactory reqFactory = mSession.getRequestFactory(); O2GRequest request = reqFactory.createRefreshTableRequest(O2GTableType.Offers); mSession.sendRequest(request); mReady = false; } name = "connected"; break; case O2GSessionStatusCode.Connecting: connected = false; name = "connecting"; break; case O2GSessionStatusCode.Disconnected: connected = false; name = "disconnected"; break; case O2GSessionStatusCode.Disconnecting: connected = false; name = "disconnecting"; break; case O2GSessionStatusCode.PriceSessionReconnecting: connected = false; name = "price channel reconnecting"; break; case O2GSessionStatusCode.SessionLost: connected = false; name = "session has been lost"; break; default: connected = false; name = "unknown"; break; } if (!connected) { mReady = false; mTimeConverter = null; } if (OnSessionStatusChanged != null) { SessionStatusEventArgs args = new SessionStatusEventArgs(connected, name); OnSessionStatusChanged(this, args); } }
// **AFTER LOG IN** // Background worker for table listeners private void priceBW_DoWork(object sender, DoWorkEventArgs e) { Console.WriteLine("Entered BW"); mSession.useTableManager(O2GTableManagerMode.Yes, null); tableManager = mSession.getTableManager(); managerStatus = tableManager.getStatus(); while (managerStatus == O2GTableManagerStatus.TablesLoading) { Thread.Sleep(50); managerStatus = tableManager.getStatus(); } if (managerStatus == O2GTableManagerStatus.TablesLoadFailed) { this.Invoke(new MethodInvoker(delegate { actiBox.AppendText("WARNING: LOADING TABLES FAILED!" + Environment.NewLine); })); return; } // Check Accounts Table and Grab Information try { O2GLoginRules loginRules = mSession.getLoginRules(); Console.WriteLine("Tables are loaded!"); // Check if Accounts table is loaded automatically if (loginRules != null && loginRules.isTableLoadedByDefault(O2GTableType.Accounts)) { // If table is loaded, use getTableRefreshResponse method O2GResponse accountsResponse = loginRules.getTableRefreshResponse(O2GTableType.Accounts); O2GResponseReaderFactory responseFactory = mSession.getResponseReaderFactory(); if (responseFactory != null) { O2GAccountsTableResponseReader accountsReader = responseFactory.createAccountsTableReader(accountsResponse); for (int i = 0; i < accountsReader.Count; i++) { account = accountsReader.getRow(i); accountValue = account.Balance; this.Invoke(new MethodInvoker(delegate { accountValueBox.Text = "$" + Convert.ToString(accountValue); })); acctEq = account.Balance; this.Invoke(new MethodInvoker(delegate { accountLevBox.Text = "$" + Convert.ToString(acctEq); })); sAccountID = account.AccountID.ToString(); amountLimit = account.AmountLimit; baseSize = account.BaseUnitSize; if (account.MaintenanceType == "Y") { this.Invoke(new MethodInvoker(delegate { hedgingBox.Text = "Yes"; })); hedgingLong = true; Settings.Default.hedgeLong = true; hedgingShort = true; Settings.Default.hedgeShort = true; this.Invoke(new MethodInvoker(delegate { longCheckBox.Checked = true; })); this.Invoke(new MethodInvoker(delegate { shortCheckBox.Checked = true; })); } else { this.Invoke(new MethodInvoker(delegate { hedgingBox.Text = "No"; })); if (hedgingShort == false) { hedgingLong = true; this.Invoke(new MethodInvoker(delegate { longCheckBox.Checked = true; })); this.Invoke(new MethodInvoker(delegate { shortCheckBox.Checked = false; })); } else { hedgingShort = true; this.Invoke(new MethodInvoker(delegate { longCheckBox.Checked = false; })); this.Invoke(new MethodInvoker(delegate { shortCheckBox.Checked = true; })); } } } } } else { // If table is not loaded, use createRefreshTableRequest method O2GRequestFactory requestFactory = mSession.getRequestFactory(); if (requestFactory != null) { O2GRequest request = requestFactory.createRefreshTableRequest(O2GTableType.Accounts); mSession.sendRequest(request); Thread.Sleep(1000); } } } catch (Exception acctErr) { Console.WriteLine(acctErr); } // Check if all 20 pairs needed are subscribed to on the account. try { O2GLoginRules loginRules = mSession.getLoginRules(); if (loginRules != null && loginRules.isTableLoadedByDefault(O2GTableType.Offers)) { O2GResponse offersResponse = loginRules.getTableRefreshResponse(O2GTableType.Offers); O2GResponseReaderFactory responseFactory = mSession.getResponseReaderFactory(); if (responseFactory != null) { O2GOffersTableResponseReader offersReader = responseFactory.createOffersTableReader(offersResponse); for (int i = 0; i < offersReader.Count; i++) { O2GOfferRow offers = offersReader.getRow(i); string checkOffer = offers.OfferID; } } } } catch (Exception mmrErr) { Console.WriteLine(mmrErr); } Console.WriteLine("Initializing needed table events."); // Initiate Table Getters O2GOffersTable offersTable = (O2GOffersTable)tableManager.getTable(O2GTableType.Offers); accountsTable = (O2GAccountsTable)tableManager.getTable(O2GTableType.Accounts); O2GSummaryTable summaryTable = (O2GSummaryTable)tableManager.getTable(O2GTableType.Summary); // Trigger Table Events for Subscription offersTable.RowChanged += new EventHandler <RowEventArgs>(offersTable_RowChanged); accountsTable.RowChanged += new EventHandler <RowEventArgs>(accountsTable_RowChanged); summaryTable.RowChanged += new EventHandler <RowEventArgs>(summaryTable_RowChanged); // Check pair subscription status, and add if needed. this.Invoke(new MethodInvoker(delegate { actiBox.AppendText("Connection Established.... Monitoring Pairs..." + Environment.NewLine); })); pastTimer.Start(); }