static void Main(string[] args) { O2GSession session = null; SessionStatusListener statusListener = null; ResponseListener responseListener = null; try { Console.WriteLine("OpenPositionNetting sample\n"); ArgumentParser argParser = new ArgumentParser(args, "OpenPositionNetting"); argParser.AddArguments(ParserArgument.Login, ParserArgument.Password, ParserArgument.Url, ParserArgument.Connection, ParserArgument.SessionID, ParserArgument.Pin, ParserArgument.Instrument, ParserArgument.BuySell, ParserArgument.Lots, ParserArgument.AccountID); argParser.ParseArguments(); if (!argParser.AreArgumentsValid) { argParser.PrintUsage(); return; } argParser.PrintArguments(); LoginParams loginParams = argParser.LoginParams; SampleParams sampleParams = argParser.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)); } } sampleParams.AccountID = account.AccountID; O2GOfferRow offer = GetOffer(tableManager, sampleParams.Instrument); if (offer == null) { throw new Exception(string.Format("The instrument '{0}' is not valid", sampleParams.Instrument)); } O2GTradesTable tradesTable = GetTradesTable(tableManager); if (tradesTable == null) { throw new Exception("Cannot get trades table"); } tableListener.SetTradesTable(tradesTable); 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 * sampleParams.Lots; 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()) { System.Threading.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) { statusListener.Reset(); session.logout(); statusListener.WaitEvents(); if (responseListener != null) { session.unsubscribeResponse(responseListener); } session.unsubscribeSessionStatus(statusListener); } session.Dispose(); } } }
static void Main(string[] args) { O2GSession session = null; try { LoginParams loginParams = new LoginParams(ConfigurationManager.AppSettings); SampleParams sampleParams = new SampleParams(ConfigurationManager.AppSettings); PrintSampleParams("CreateOrderBySymbol", loginParams, 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)); } } sampleParams.AccountID = account.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 * sampleParams.Lots; int iCondDistEntryLimit = tradingSettingsProvider.getCondDistEntryLimit(sampleParams.Instrument); int iCondDistEntryStop = tradingSettingsProvider.getCondDistEntryStop(sampleParams.Instrument); string sOrderType = GetEntryOrderType(offer.Bid, offer.Ask, sampleParams.Rate, sampleParams.BuySell, offer.PointSize, iCondDistEntryLimit, iCondDistEntryStop); O2GRequest request = CreateEntryOrderRequest(session, offer.Instrument, sampleParams.AccountID, iAmount, sampleParams.Rate, sampleParams.BuySell, sOrderType); if (request == null) { throw new Exception("Cannot create request"); } responseListener.SetRequestID(request.RequestID); session.sendRequest(request); if (responseListener.WaitEvents()) { 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(); } } }
// Print trading settings of the first account private static void PrintTradingSettings(O2GSession session) { O2GLoginRules loginRules = session.getLoginRules(); if (loginRules == null) { throw new Exception("Cannot get login rules"); } O2GResponse accountsResponse = loginRules.getTableRefreshResponse(O2GTableType.Accounts); if (accountsResponse == null) { throw new Exception("Cannot get response"); } O2GResponse offersResponse = loginRules.getTableRefreshResponse(O2GTableType.Offers); if (offersResponse == null) { throw new Exception("Cannot get response"); } O2GTradingSettingsProvider tradingSettingsProvider = loginRules.getTradingSettingsProvider(); O2GResponseReaderFactory factory = session.getResponseReaderFactory(); if (factory == null) { throw new Exception("Cannot create response reader factory"); } O2GAccountsTableResponseReader accountsReader = factory.createAccountsTableReader(accountsResponse); O2GOffersTableResponseReader instrumentsReader = factory.createOffersTableReader(offersResponse); O2GAccountRow account = accountsReader.getRow(0); for (int i = 0; i < instrumentsReader.Count; i++) { O2GOfferRow instrumentRow = instrumentsReader.getRow(i); string instrument = instrumentRow.Instrument; int condDistStopForTrade = tradingSettingsProvider.getCondDistStopForTrade(instrument); int condDistLimitForTrade = tradingSettingsProvider.getCondDistLimitForTrade(instrument); int condDistEntryStop = tradingSettingsProvider.getCondDistEntryStop(instrument); int condDistEntryLimit = tradingSettingsProvider.getCondDistEntryLimit(instrument); int minQuantity = tradingSettingsProvider.getMinQuantity(instrument, account); int maxQuantity = tradingSettingsProvider.getMaxQuantity(instrument, account); int baseUnitSize = tradingSettingsProvider.getBaseUnitSize(instrument, account); O2GMarketStatus marketStatus = tradingSettingsProvider.getMarketStatus(instrument); int minTrailingStep = tradingSettingsProvider.getMinTrailingStep(); int maxTrailingStep = tradingSettingsProvider.getMaxTrailingStep(); double mmr = tradingSettingsProvider.getMMR(instrument, account); double mmr2 = 0, emr = 0, lmr = 0; bool threeLevelMargin = tradingSettingsProvider.getMargins(instrument, account, ref mmr2, ref emr, ref lmr); string sMarketStatus = "unknown"; switch (marketStatus) { case O2GMarketStatus.MarketStatusOpen: sMarketStatus = "Market Open"; break; case O2GMarketStatus.MarketStatusClosed: sMarketStatus = "Market Close"; break; } Console.WriteLine("Instrument: {0}, Status: {1}", instrument, sMarketStatus); Console.WriteLine("Cond.Dist: ST={0}; LT={1}", condDistStopForTrade, condDistLimitForTrade); Console.WriteLine("Cond.Dist entry stop={0}; entry limit={1}", condDistEntryStop, condDistEntryLimit); Console.WriteLine("Quantity: Min={0}; Max={1}. Base unit size={2}; MMR={3}", minQuantity, maxQuantity, baseUnitSize, mmr); if (threeLevelMargin) { Console.WriteLine("Three level margin: MMR={0}; EMR={1}; LMR={2}", mmr2, emr, lmr); } else { Console.WriteLine("Single level margin: MMR={0}; EMR={1}; LMR={2}", mmr2, emr, lmr); } Console.WriteLine("Trailing step: {0}-{1}", minTrailingStep, maxTrailingStep); } }
static void Main(string[] args) { O2GSession session = null; try { LoginParams loginParams = new LoginParams(ConfigurationManager.AppSettings); SampleParams sampleParams = new SampleParams(ConfigurationManager.AppSettings); PrintSampleParams("CreateELS", loginParams, 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.WaitEvent() && 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)); } } sampleParams.AccountID = account.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 * sampleParams.Lots; double dRate; double dRateStop; double dRateLimit; double dBid = offer.Bid; double dAsk = offer.Ask; double dPointSize = offer.PointSize; // For the purpose of this example we will place entry order 8 pips from the current market price // and attach stop and limit orders 10 pips from an entry order price if (sampleParams.OrderType.Equals(Constants.Orders.LimitEntry)) { if (sampleParams.BuySell.Equals(Constants.Buy)) { dRate = dAsk - 8 * dPointSize; dRateLimit = dRate + 10 * dPointSize; dRateStop = dRate - 10 * dPointSize; } else { dRate = dBid + 8 * dPointSize; dRateLimit = dRate - 10 * dPointSize; dRateStop = dRate + 10 * dPointSize; } } else { if (sampleParams.BuySell.Equals(Constants.Buy)) { dRate = dAsk + 8 * dPointSize; dRateLimit = dRate + 10 * dPointSize; dRateStop = dRate - 10 * dPointSize; } else { dRate = dBid - 8 * dPointSize; dRateLimit = dRate - 10 * dPointSize; dRateStop = dRate + 10 * dPointSize; } } O2GRequest request = CreateELSRequest(session, offer.OfferID, sampleParams.AccountID, iAmount, dRate, dRateLimit, dRateStop, sampleParams.BuySell, sampleParams.OrderType); if (request == null) { throw new Exception("Cannot create request; probably some arguments are missing or incorrect"); } responseListener.SetRequestID(request.RequestID); session.sendRequest(request); if (responseListener.WaitEvents()) { 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.WaitEvent(); } session.unsubscribeSessionStatus(statusListener); 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; try { LoginParams loginParams = new LoginParams(ConfigurationManager.AppSettings); SampleParams sampleParams = new SampleParams(ConfigurationManager.AppSettings); PrintSampleParams("CreateIf-thenELS", loginParams, sampleParams); session = O2GTransport.createSession(); SessionStatusListener statusListener = new SessionStatusListener(session, loginParams.SessionID, loginParams.Pin); session.subscribeSessionStatus(statusListener); 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)); } } sampleParams.AccountID = account.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 * sampleParams.Lots; double otoRate = offer.Bid + 2.0 * offer.PointSize; double dRateELS = offer.Bid - 30.0 * offer.PointSize; double dRateStop = offer.Bid - 50.0 * offer.PointSize; double dRateLimit = offer.Bid + 50.0 * offer.PointSize; Console.WriteLine("Bid={0}, Ask={1}, OTO={2}, ELS={3}, Stop={4}, Limit={5}", offer.Bid, offer.Ask, otoRate, dRateELS, dRateStop, dRateLimit); O2GRequest request = CreateOTO_ELSRequest(session, offer.OfferID, account.AccountID, iAmount, otoRate, dRateELS, dRateStop, dRateLimit); if (request == null) { throw new Exception("Cannot create request"); } List <string> requestIDList = new List <string>(); FillRequestIDs(requestIDList, request); responseListener.SetRequestIDs(requestIDList); session.sendRequest(request); if (responseListener.WaitEvents()) { Console.WriteLine("Done!"); } else { throw new Exception("Response waiting timeout expired"); } statusListener.Reset(); session.logout(); statusListener.WaitEvents(); session.unsubscribeResponse(responseListener); } 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; SessionStatusListener statusListener = null; ResponseListener responseListener = null; string sInstrument = "EUR/USD"; string sBuySell = Constants.Buy; 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.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)); } } sampleParams.AccountID = account.AccountID; O2GOfferRow offer = GetOffer(session, sInstrument); if (offer == null) { throw new Exception(string.Format("The instrument '{0}' is not valid", sInstrument)); } O2GLoginRules loginRules = session.getLoginRules(); if (loginRules == null) { throw new Exception("Cannot get login rules"); } O2GTradingSettingsProvider tradingSettingsProvider = loginRules.getTradingSettingsProvider(); int iBaseUnitSize = tradingSettingsProvider.getBaseUnitSize(sInstrument, account); int iAmount = iBaseUnitSize * 1; double dRate = offer.Ask - (offer.PointSize * 10); O2GRequest request; request = CreateEntryOrderRequest(session, offer.OfferID, account.AccountID, iAmount, dRate, sBuySell, Constants.Orders.LimitEntry); if (request == null) { throw new Exception("Cannot create request"); } responseListener.SetRequestID(request.RequestID); session.sendRequest(request); if (!responseListener.WaitEvents()) { throw new Exception("Response waiting timeout expired"); } string sOrderID = responseListener.GetOrderID(); if (!string.IsNullOrEmpty(sOrderID)) { request = RemoveOrderRequest(session, account.AccountID, sOrderID); if (request == null) { throw new Exception("Cannot create request"); } responseListener.SetRequestID(request.RequestID); session.sendRequest(request); if (responseListener.WaitEvents()) { 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) { statusListener.Reset(); session.logout(); statusListener.WaitEvents(); if (responseListener != null) { session.unsubscribeResponse(responseListener); } } session.unsubscribeSessionStatus(statusListener); session.Dispose(); } } }
static void Main(string[] args) { O2GSession session = null; SessionStatusListener statusListener = null; ResponseListener responseListener = null; try { Console.WriteLine("CreateOCO sample\n"); ArgumentParser argParser = new ArgumentParser(args, "CreateOCO"); argParser.AddArguments(ParserArgument.Login, ParserArgument.Password, ParserArgument.Url, ParserArgument.Connection, ParserArgument.SessionID, ParserArgument.Pin, ParserArgument.Instrument, ParserArgument.Lots, 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); 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)); } } sampleParams.AccountID = account.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 * sampleParams.Lots; // For the purpose of this example we will place entry orders 30 pips from the current market price double dRateUp = offer.Ask + 30.0 * offer.PointSize; double dRateDown = offer.Bid - 30.0 * offer.PointSize; O2GRequest request = CreateOCORequest(session, offer.OfferID, account.AccountID, iAmount, dRateUp, dRateDown); if (request == null) { throw new Exception("Cannot create request"); } List <string> requestIDList = new List <string>(); for (int i = 0; i < request.ChildrenCount; i++) { requestIDList.Add(request.getChildRequest(i).RequestID); } responseListener.SetRequestIDs(requestIDList); session.sendRequest(request); if (responseListener.WaitEvents()) { 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) { statusListener.Reset(); session.logout(); statusListener.WaitEvents(); if (responseListener != null) { session.unsubscribeResponse(responseListener); } session.unsubscribeSessionStatus(statusListener); } session.Dispose(); } } }
static void Main(string[] args) { O2GSession session = null; try { LoginParams loginParams = new LoginParams(ConfigurationManager.AppSettings); SampleParams sampleParams = new SampleParams(ConfigurationManager.AppSettings); PrintSampleParams("CreateOTO", 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)); } } sampleParams.AccountID = account.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 * sampleParams.Lots; // For the purpose of this example we will place primary order 30 pips below the current market price // and our secondary order 15 pips below the current market price double dRatePrimary = offer.Ask - 30.0 * offer.PointSize; double dRateSecondary = offer.Ask - 15.0 * offer.PointSize; O2GRequest request = CreateOTORequest(session, offer.OfferID, account.AccountID, iAmount, dRatePrimary, dRateSecondary); if (request == null) { throw new Exception("Cannot create request"); } tableListener.SubscribeEvents(tableManager); List <string> requestIDList = new List <string>(); for (int i = 0; i < request.ChildrenCount; i++) { requestIDList.Add(request.getChildRequest(i).RequestID); } responseListener.SetRequestIDs(requestIDList); tableListener.SetRequestIDs(requestIDList); session.sendRequest(request); if (responseListener.WaitEvents()) { 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; try { LoginParams loginParams = new LoginParams(ConfigurationManager.AppSettings); SampleParams sampleParams = new SampleParams(ConfigurationManager.AppSettings); PrintSampleParams("OpenPositionNetting", 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)); } } sampleParams.AccountID = account.AccountID; O2GOfferRow offer = GetOffer(session, sampleParams.Instrument); if (offer == null) { throw new Exception(string.Format("The instrument '{0}' is not valid", sampleParams.Instrument)); } O2GTradesTableResponseReader tradesTable = GetTradesTable(session, sampleParams.AccountID, responseListener); if (tradesTable == null) { throw new Exception("Cannot get trades table"); } responseListener.SetTradesTable(tradesTable); 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 * sampleParams.Lots; 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; try { LoginParams loginParams = new LoginParams(ConfigurationManager.AppSettings); SampleParams sampleParams = new SampleParams(ConfigurationManager.AppSettings); PrintSampleParams("GetLastOrderUpdate", loginParams, 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.WaitEvent() && 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)); } } sampleParams.AccountID = account.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 * sampleParams.Lots; O2GRequest request; request = CreateTrueMarketOrderRequest(session, offer.OfferID, account.AccountID, iAmount, sampleParams.BuySell); if (request == null) { throw new Exception("Cannot create request; probably some arguments are missing or incorrect"); } responseListener.SetRequestID(request.RequestID); session.sendRequest(request); if (!responseListener.WaitEvents()) { throw new Exception("Response waiting timeout expired"); } string sOrderID = responseListener.GetOrderID(); if (!string.IsNullOrEmpty(sOrderID)) { Console.WriteLine("You have successfully created a true market order."); Console.WriteLine("Your order ID is {0}", sOrderID); request = GetLastOrderUpdateRequest(session, sOrderID, account.AccountName); if (request == null) { throw new Exception("Cannot create request; probably some arguments are missing or incorrect"); } responseListener.SetRequestID(request.RequestID); session.sendRequest(request); if (!responseListener.WaitEvents()) { throw new Exception("Response waiting timeout expired"); } O2GResponse response = responseListener.GetResponse(); if (response != null && response.Type == O2GResponseType.GetLastOrderUpdate) { O2GResponseReaderFactory readerFactory = session.getResponseReaderFactory(); if (readerFactory != null) { O2GLastOrderUpdateResponseReader reader = readerFactory.createLastOrderUpdateResponseReader(response); Console.WriteLine("Last order update: UpdateType={0}, OrderID={1}, Status={2}, StatusTime={3}", reader.UpdateType.ToString(), reader.Order.OrderID, reader.Order.Status.ToString(), reader.Order.StatusTime.ToString("yyyy-MM-dd HH:mm:ss")); } } Console.WriteLine("Done!"); } } } 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.WaitEvent(); } session.unsubscribeSessionStatus(statusListener); session.Dispose(); } } }
static void Main(string[] args) { O2GSession session = null; string sOrderType = Constants.Orders.LimitEntry; try { LoginParams loginParams = new LoginParams(ConfigurationManager.AppSettings); SampleParams sampleParams = new SampleParams(ConfigurationManager.AppSettings); PrintSampleParams("FindRowByColumnValue", loginParams, sampleParams); session = O2GTransport.createSession(); session.useTableManager(O2GTableManagerMode.Yes, null); 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(); 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 (!sampleParams.AccountID.Equals(account.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 * sampleParams.Lots; double dRate; double dRateStop; double dRateLimit; double dBid = offer.Bid; double dAsk = offer.Ask; double dPointSize = offer.PointSize; // For the purpose of this example we will place entry order 8 pips from the current market price // and attach stop and limit orders 10 pips from an entry order price if (sOrderType.Equals(Constants.Orders.LimitEntry)) { if (sampleParams.BuySell.Equals(Constants.Buy)) { dRate = dAsk - 8 * dPointSize; dRateLimit = dRate + 10 * dPointSize; dRateStop = dRate - 10 * dPointSize; } else { dRate = dBid + 8 * dPointSize; dRateLimit = dRate - 10 * dPointSize; dRateStop = dRate + 10 * dPointSize; } } else { if (sampleParams.BuySell.Equals(Constants.Buy)) { dRate = dAsk + 8 * dPointSize; dRateLimit = dRate + 10 * dPointSize; dRateStop = dRate - 10 * dPointSize; } else { dRate = dBid - 8 * dPointSize; dRateLimit = dRate - 10 * dPointSize; dRateStop = dRate + 10 * dPointSize; } } tableListener.SubscribeEvents(tableManager); O2GRequest request = CreateELSRequest(session, offer.OfferID, sampleParams.AccountID, iAmount, dRate, dRateLimit, dRateStop, sampleParams.BuySell, sOrderType); if (request == null) { throw new Exception("Cannot create request"); } string sRequestID = request.RequestID; responseListener.SetRequestID(sRequestID); tableListener.SetRequestID(sRequestID); session.sendRequest(request); if (!responseListener.WaitEvents()) { throw new Exception("Response waiting timeout expired"); } Console.WriteLine("Search by RequestID:{0}", sRequestID); FindOrders(tableManager, sRequestID); Console.WriteLine("Search by Type:{0} and BuySell:{1}", sOrderType, sampleParams.BuySell); FindOrdersByTypeAndDirection(tableManager, sOrderType, sampleParams.BuySell); Console.WriteLine("Search conditional orders"); FindConditionalOrders(tableManager); Console.WriteLine("Done!"); tableListener.UnsubscribeEvents(tableManager); statusListener.Reset(); session.logout(); statusListener.WaitEvents(); session.unsubscribeResponse(responseListener); } 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; try { LoginParams loginParams = new LoginParams(ConfigurationManager.AppSettings); SampleParams sampleParams = new SampleParams(ConfigurationManager.AppSettings); PrintSampleParams("CreateAndFindEntry", 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)); } } sampleParams.AccountID = account.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 * sampleParams.Lots; double dRate = CalculateRate(sampleParams.OrderType, sampleParams.BuySell, offer.Ask, offer.Bid, offer.PointSize); O2GRequest request = CreateEntryOrderRequest(session, offer.OfferID, account.AccountID, iAmount, dRate, sampleParams.BuySell, sampleParams.OrderType); if (request == null) { throw new Exception("Cannot create request; probably some arguments are missing or incorrect"); } responseListener.SetRequestID(request.RequestID); session.sendRequest(request); if (responseListener.WaitEvents()) { string sOrderID = responseListener.GetOrderID(); if (!string.IsNullOrEmpty(sOrderID)) { Console.WriteLine("You have successfully created an entry order for instrument {0}", sampleParams.Instrument); Console.WriteLine("Your order ID is {0}", sOrderID); FindOrder(session, sOrderID, sampleParams.AccountID, responseListener); Console.WriteLine("Done!"); } } else { throw new Exception("Response waiting timeout expired"); } statusListener.Reset(); session.logout(); statusListener.WaitEvents(); session.unsubscribeResponse(responseListener); } 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; SessionStatusListener statusListener = null; ResponseListener responseListener = null; try { Console.WriteLine("CreateELS sample\n"); ArgumentParser argParser = new ArgumentParser(args, "CreateELS"); argParser.AddArguments(ParserArgument.Login, ParserArgument.Password, ParserArgument.Url, ParserArgument.Connection, ParserArgument.SessionID, ParserArgument.Pin, ParserArgument.Instrument, ParserArgument.AccountID, ParserArgument.BuySell, ParserArgument.OrderType, ParserArgument.Lots, ParserArgument.AccountID ); argParser.ParseArguments(); if (!argParser.AreArgumentsValid) { argParser.PrintUsage(); return; } argParser.PrintArguments(); LoginParams loginParams = argParser.LoginParams; SampleParams sampleParams = argParser.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)); } } sampleParams.AccountID = account.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 * sampleParams.Lots; double dRate; double dRateStop; double dRateLimit; double dBid = offer.Bid; double dAsk = offer.Ask; double dPointSize = offer.PointSize; // For the purpose of this example we will place entry order 8 pips from the current market price // and attach stop and limit orders 10 pips from an entry order price if (sampleParams.OrderType.Equals(Constants.Orders.LimitEntry)) { if (sampleParams.BuySell.Equals(Constants.Buy)) { dRate = dAsk - 8 * dPointSize; dRateLimit = dRate + 10 * dPointSize; dRateStop = dRate - 10 * dPointSize; } else { dRate = dBid + 8 * dPointSize; dRateLimit = dRate - 10 * dPointSize; dRateStop = dRate + 10 * dPointSize; } } else { if (sampleParams.BuySell.Equals(Constants.Buy)) { dRate = dAsk + 8 * dPointSize; dRateLimit = dRate + 10 * dPointSize; dRateStop = dRate - 10 * dPointSize; } else { dRate = dBid - 8 * dPointSize; dRateLimit = dRate - 10 * dPointSize; dRateStop = dRate + 10 * dPointSize; } } tableListener.SubscribeEvents(tableManager); O2GRequest request = CreateELSRequest(session, offer.OfferID, sampleParams.AccountID, iAmount, dRate, dRateLimit, dRateStop, sampleParams.BuySell, sampleParams.OrderType); if (request == null) { throw new Exception("Cannot create request"); } responseListener.SetRequestID(request.RequestID); tableListener.SetRequestID(request.RequestID); session.sendRequest(request); if (responseListener.WaitEvents()) { 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) { statusListener.Reset(); session.logout(); statusListener.WaitEvents(); if (responseListener != null) { session.unsubscribeResponse(responseListener); } session.unsubscribeSessionStatus(statusListener); } session.Dispose(); } } }
static void Main(string[] args) { O2GSession session = null; string sInstrument = "EUR/USD"; string sBuySell = Constants.Buy; try { LoginParams loginParams = new LoginParams(ConfigurationManager.AppSettings); SampleParams sampleParams = new SampleParams(ConfigurationManager.AppSettings); PrintSampleParams("RemoveOrder", loginParams, sampleParams); session = O2GTransport.createSession(); session.useTableManager(O2GTableManagerMode.Yes, null); 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(); 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)); } } sampleParams.AccountID = account.AccountID; O2GOfferRow offer = GetOffer(tableManager, sInstrument); if (offer == null) { throw new Exception(string.Format("The instrument '{0}' is not valid", sInstrument)); } O2GLoginRules loginRules = session.getLoginRules(); if (loginRules == null) { throw new Exception("Cannot get login rules"); } O2GTradingSettingsProvider tradingSettingsProvider = loginRules.getTradingSettingsProvider(); int iBaseUnitSize = tradingSettingsProvider.getBaseUnitSize(sInstrument, account); int iAmount = iBaseUnitSize * 1; double dRate = offer.Ask - (offer.PointSize * 10); tableListener.SubscribeEvents(tableManager); O2GRequest request = CreateEntryOrderRequest(session, offer.OfferID, account.AccountID, iAmount, dRate, sBuySell, Constants.Orders.LimitEntry); if (request == null) { throw new Exception("Cannot create request"); } responseListener.SetRequestID(request.RequestID); tableListener.SetRequestID(request.RequestID); session.sendRequest(request); if (!responseListener.WaitEvents()) { throw new Exception("Response waiting timeout expired"); } string sOrderID = tableListener.GetOrderID(); if (!string.IsNullOrEmpty(sOrderID)) { request = RemoveOrderRequest(session, account.AccountID, sOrderID); if (request == null) { throw new Exception("Cannot create request"); } responseListener.SetRequestID(request.RequestID); tableListener.SetRequestID(request.RequestID); session.sendRequest(request); if (responseListener.WaitEvents()) { Console.WriteLine("Done!"); } else { throw new Exception("Response waiting timeout expired"); } } tableListener.UnsubscribeEvents(tableManager); statusListener.Reset(); session.logout(); statusListener.WaitEvents(); session.unsubscribeResponse(responseListener); } 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; try { LoginParams loginParams = new LoginParams(ConfigurationManager.AppSettings); SampleParams sampleParams = new SampleParams(ConfigurationManager.AppSettings); PrintSampleParams("CalculateTradingCommissions", loginParams, sampleParams); session = O2GTransport.createSession(); session.useTableManager(O2GTableManagerMode.Yes, null); statusListener = new SessionStatusListener(session); session.subscribeSessionStatus(statusListener); statusListener.Reset(); session.login(loginParams.Login, loginParams.Password, loginParams.URL, loginParams.Connection); if (statusListener.WaitEvents() && statusListener.Connected) { 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"); } O2GAccountTableRow account = GetAccount(tableManager, sampleParams.AccountID); if (account == null) { throw new Exception(string.IsNullOrEmpty(sampleParams.AccountID) ? "No valid accounts" : string.Format("The account '{0}' is not valid", sampleParams.AccountID)); } O2GOfferTableRow 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 * sampleParams.Lots; printEstimatedTradingCommissions(session, offer, account, iAmount, sampleParams.BuySell); Console.WriteLine("Done!"); } } catch (Exception e) { Console.WriteLine("Exception: {0}", e.ToString()); } finally { if (session != null) { if (statusListener.Connected) { statusListener.Reset(); session.logout(); statusListener.WaitEvents(); } session.unsubscribeSessionStatus(statusListener); session.Dispose(); } } }
public void Run() { try { string sLogin; string sPassword; string sSessionID; string sPin; if (mIsFirstAccount) { sLogin = mLoginParams.Login; sPassword = mLoginParams.Password; sSessionID = mLoginParams.SessionID; sPin = mLoginParams.Pin; } else { sLogin = mLoginParams.Login2; sPassword = mLoginParams.Password2; sSessionID = mLoginParams.SessionID2; sPin = mLoginParams.Pin2; } statusListener = new SessionStatusListener(mSession, sSessionID, sPin); mSession.subscribeSessionStatus(statusListener); statusListener.Reset(); mSession.login(sLogin, sPassword, mLoginParams.URL, mLoginParams.Connection); if (statusListener.WaitEvents() && statusListener.Connected) { if (!mIsFirstAccount) // Disable receiving price updates for the second account { mSession.setPriceUpdateMode(O2GPriceUpdateMode.NoPrice); } responseListener = new ResponseListener(mSession); mSession.subscribeResponse(responseListener); O2GAccountRow account = null; if (mIsFirstAccount) { bool bIsAccountEmpty = String.IsNullOrEmpty(mSampleParams.AccountID); account = GetAccount(mSession, mSampleParams.AccountID); if (account != null) { if (bIsAccountEmpty) { mSampleParams.AccountID = account.AccountID; Console.WriteLine("Account: " + mSampleParams.AccountID); } } else { throw new Exception(string.Format("The account '{0}' is not valid", mSampleParams.AccountID)); } } else { bool bIsAccountEmpty = String.IsNullOrEmpty(mSampleParams.AccountID2); account = GetAccount(mSession, mSampleParams.AccountID2); if (account != null) { if (bIsAccountEmpty) { mSampleParams.AccountID2 = account.AccountID; Console.WriteLine("Account2: " + mSampleParams.AccountID2); } } else { throw new Exception(string.Format("The account2 '{0}' is not valid", mSampleParams.AccountID2)); } } O2GOfferRow offer = GetOffer(mSession, mSampleParams.Instrument); if (offer == null) { throw new Exception(string.Format("The instrument '{0}' is not valid", mSampleParams.Instrument)); } O2GLoginRules loginRules = mSession.getLoginRules(); if (loginRules == null) { throw new Exception("Cannot get login rules"); } O2GTradingSettingsProvider tradingSettingsProvider = loginRules.getTradingSettingsProvider(); int iBaseUnitSize = tradingSettingsProvider.getBaseUnitSize(mSampleParams.Instrument, account); int iAmount = iBaseUnitSize * mSampleParams.Lots; O2GRequest request; request = CreateTrueMarketOrderRequest(mSession, offer.OfferID, account.AccountID, iAmount, mSampleParams.BuySell); if (request == null) { throw new Exception("Cannot create request"); } responseListener.SetRequestID(request.RequestID); mSession.sendRequest(request); if (!responseListener.WaitEvents()) { throw new Exception("Response waiting timeout expired"); } Console.WriteLine("Done!"); } } catch (Exception e) { Console.WriteLine("Exception: {0}", e.ToString()); } finally { if (statusListener.Connected) { statusListener.Reset(); mSession.logout(); statusListener.WaitEvents(); mSession.unsubscribeResponse(responseListener); } mSession.subscribeSessionStatus(statusListener); mSession.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(); } } }