/// <summary> /// Find order by ID and print information about it /// </summary> /// <param name="session"></param> /// <param name="sOrderID"></param> /// <param name="sAccountID"></param> /// <param name="responseListener"></param> private static void FindOrder(O2GSession session, string sOrderID, string sAccountID, ResponseListener responseListener) { O2GRequestFactory requestFactory = session.getRequestFactory(); if (requestFactory == null) { throw new Exception("Cannot create request factory"); } O2GRequest request = requestFactory.createRefreshTableRequestByAccount(O2GTableType.Orders, sAccountID); responseListener.SetRequestID(request.RequestID); session.sendRequest(request); if (!responseListener.WaitEvents()) { throw new Exception("Response waiting timeout expired"); } O2GResponse response = responseListener.GetResponse(); if (response != null) { O2GResponseReaderFactory responseFactory = session.getResponseReaderFactory(); O2GOrdersTableResponseReader ordersReader = responseFactory.createOrdersTableReader(response); for (int i = 0; i < ordersReader.Count; i++) { O2GOrderRow order = ordersReader.getRow(i); if (sOrderID.Equals(order.OrderID)) { Console.WriteLine("Information for OrderID = {0}", sOrderID); Console.WriteLine("Account: {0}", order.AccountID); Console.WriteLine("Amount: {0}", order.Amount); Console.WriteLine("Rate: {0}", order.Rate); Console.WriteLine("Type: {0}", order.Type); Console.WriteLine("Buy/Sell: {0}", order.BuySell); Console.WriteLine("Stage: {0}", order.Stage); Console.WriteLine("Status: {0}", order.Status); } } } else { throw new Exception("Cannot get response"); } }
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(); } } }