static void Main(string[] args) { O2GSession session = null; int iLots = 10; try { LoginParams loginParams = new LoginParams(ConfigurationManager.AppSettings); SampleParams sampleParams = new SampleParams(ConfigurationManager.AppSettings); PrintSampleParams("PatrialFill", 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); session.subscribeResponse(responseListener); O2GAccountRow account = GetAccount(session, sampleParams.AccountID); if (account == null) { if (string.IsNullOrEmpty(sampleParams.AccountID)) { throw new Exception("No valid accounts"); } else { throw new Exception(string.Format("The account '{0}' is not valid", sampleParams.AccountID)); } } else { if(!sampleParams.AccountID.Equals(account.AccountID)) { sampleParams.AccountID = account.AccountID; Console.WriteLine("AccountID='{0}'", sampleParams.AccountID); } } O2GOfferRow offer = GetOffer(session, sampleParams.Instrument); if (offer == null) { throw new Exception(string.Format("The instrument '{0}' is not valid", sampleParams.Instrument)); } O2GLoginRules loginRules = session.getLoginRules(); if (loginRules == null) { throw new Exception("Cannot get login rules"); } O2GTradingSettingsProvider tradingSettingsProvider = loginRules.getTradingSettingsProvider(); int iBaseUnitSize = tradingSettingsProvider.getBaseUnitSize(sampleParams.Instrument, account); int iAmount = iBaseUnitSize * iLots; O2GRequest request = CreateTrueMarketOrderRequest(session, offer.OfferID, sampleParams.AccountID, iAmount, sampleParams.BuySell); if (request == null) { throw new Exception("Cannot create request"); } responseListener.SetRequestID(request.RequestID); session.sendRequest(request); if (responseListener.WaitEvents()) { Thread.Sleep(1000); // Wait for the balance update Console.WriteLine("Done!"); } else { throw new Exception("Response waiting timeout expired"); } 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(); } } }
static void Main(string[] args) { O2GSession session = null; int iLots = 10; try { LoginParams loginParams = new LoginParams(ConfigurationManager.AppSettings); SampleParams sampleParams = new SampleParams(ConfigurationManager.AppSettings); PrintSampleParams("PatrialFill", loginParams, sampleParams); session = O2GTransport.createSession(); session.useTableManager(O2GTableManagerMode.Yes, null); 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 = new ResponseListener(); TableListener tableListener = new TableListener(responseListener); session.subscribeResponse(responseListener); O2GTableManager tableManager = session.getTableManager(); O2GTableManagerStatus managerStatus = tableManager.getStatus(); while (managerStatus == O2GTableManagerStatus.TablesLoading) { Thread.Sleep(50); managerStatus = tableManager.getStatus(); } if (managerStatus == O2GTableManagerStatus.TablesLoadFailed) { throw new Exception("Cannot refresh all tables of table manager"); } O2GAccountRow account = GetAccount(tableManager, sampleParams.AccountID); if (account == null) { if (string.IsNullOrEmpty(sampleParams.AccountID)) { throw new Exception("No valid accounts"); } else { throw new Exception(string.Format("The account '{0}' is not valid", sampleParams.AccountID)); } } else { if (!account.AccountID.Equals(sampleParams.AccountID)) { sampleParams.AccountID = account.AccountID; Console.WriteLine("AccountID='{0}'", sampleParams.AccountID); } } O2GOfferRow offer = GetOffer(tableManager, sampleParams.Instrument); if (offer == null) { throw new Exception(string.Format("The instrument '{0}' is not valid", sampleParams.Instrument)); } O2GLoginRules loginRules = session.getLoginRules(); if (loginRules == null) { throw new Exception("Cannot get login rules"); } O2GTradingSettingsProvider tradingSettingsProvider = loginRules.getTradingSettingsProvider(); int iBaseUnitSize = tradingSettingsProvider.getBaseUnitSize(sampleParams.Instrument, account); int iAmount = iBaseUnitSize * iLots; tableListener.SubscribeEvents(tableManager); O2GRequest request = CreateTrueMarketOrderRequest(session, offer.OfferID, sampleParams.AccountID, iAmount, sampleParams.BuySell); if (request == null) { throw new Exception("Cannot create request"); } responseListener.SetRequestID(request.RequestID); tableListener.SetRequestID(request.RequestID); session.sendRequest(request); if (responseListener.WaitEvents()) { Thread.Sleep(1000); // Wait for the balance update Console.WriteLine("Done!"); } else { throw new Exception("Response waiting timeout expired"); } tableListener.UnsubscribeEvents(tableManager); } } catch (Exception e) { Console.WriteLine("Exception: {0}", e.ToString()); } finally { if (session != null) { if (statusListener.Connected) { if (responseListener != null) { session.unsubscribeResponse(responseListener); } statusListener.Reset(); session.logout(); statusListener.WaitEvents(); } session.unsubscribeSessionStatus(statusListener); session.Dispose(); } } }
static void Main(string[] args) { O2GSession session = null; SessionStatusListener statusListener = null; ResponseListener responseListener = null; int iLots = 10; try { Console.WriteLine("CreateOTO sample\n"); ArgumentParser argParser = new ArgumentParser(args, "CloseAllPositions"); argParser.AddArguments(ParserArgument.Login, ParserArgument.Password, ParserArgument.Url, ParserArgument.Connection, ParserArgument.SessionID, ParserArgument.Pin, ParserArgument.Instrument, ParserArgument.BuySell, ParserArgument.AccountID); argParser.ParseArguments(); if (!argParser.AreArgumentsValid) { argParser.PrintUsage(); return; } argParser.PrintArguments(); LoginParams loginParams = argParser.LoginParams; SampleParams sampleParams = argParser.SampleParams; session = O2GTransport.createSession(); 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 = new ResponseListener(session); session.subscribeResponse(responseListener); O2GAccountRow account = GetAccount(session, sampleParams.AccountID); if (account == null) { if (string.IsNullOrEmpty(sampleParams.AccountID)) { throw new Exception("No valid accounts"); } else { throw new Exception(string.Format("The account '{0}' is not valid", sampleParams.AccountID)); } } else { if (!account.AccountID.Equals(sampleParams.AccountID)) { sampleParams.AccountID = account.AccountID; Console.WriteLine("AccountID='{0}'", sampleParams.AccountID); } } O2GOfferRow offer = GetOffer(session, sampleParams.Instrument); if (offer == null) { throw new Exception(string.Format("The instrument '{0}' is not valid", sampleParams.Instrument)); } O2GLoginRules loginRules = session.getLoginRules(); if (loginRules == null) { throw new Exception("Cannot get login rules"); } O2GTradingSettingsProvider tradingSettingsProvider = loginRules.getTradingSettingsProvider(); int iBaseUnitSize = tradingSettingsProvider.getBaseUnitSize(sampleParams.Instrument, account); int iAmount = iBaseUnitSize * iLots; O2GRequest request = CreateTrueMarketOrderRequest(session, offer.OfferID, sampleParams.AccountID, iAmount, sampleParams.BuySell); if (request == null) { throw new Exception("Cannot create request"); } responseListener.SetRequestID(request.RequestID); session.sendRequest(request); if (responseListener.WaitEvents()) { Thread.Sleep(1000); // Wait for the balance update Console.WriteLine("Done!"); } else { throw new Exception("Response waiting timeout expired"); } } } catch (Exception e) { Console.WriteLine("Exception: {0}", e.ToString()); } finally { if (session != null) { if (statusListener.Connected) { if (responseListener != null) { session.unsubscribeResponse(responseListener); } statusListener.Reset(); session.logout(); statusListener.WaitEvents(); } session.unsubscribeSessionStatus(statusListener); session.Dispose(); } } }