static void Main(string[] args) { O2GSession session = null; SessionStatusListener statusListener = null; try { Console.WriteLine("TradingSettings sample\n"); ArgumentParser argParser = new ArgumentParser(args, "TradingSettings"); argParser.AddArguments(ParserArgument.Login, ParserArgument.Password, ParserArgument.Url, ParserArgument.Connection, ParserArgument.SessionID, ParserArgument.Pin); argParser.ParseArguments(); if (!argParser.AreArgumentsValid) { argParser.PrintUsage(); return; } argParser.PrintArguments(); LoginParams loginParams = argParser.LoginParams; 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) { PrintTradingSettings(session); 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(); } } }
static void Main(string[] args) { O2GSession session1 = null; O2GSession session2 = null; try { Console.WriteLine("TwoConnections sample\n"); ArgumentParser argParser = new ArgumentParser(args, "TwoConnections"); argParser.AddArguments(ParserArgument.Login, ParserArgument.Password, ParserArgument.Url, ParserArgument.Connection, ParserArgument.SessionID, ParserArgument.Pin, ParserArgument.AccountID, ParserArgument.Login2, ParserArgument.Password2, ParserArgument.SessionID2, ParserArgument.Pin2, ParserArgument.AccountID2, ParserArgument.Instrument, ParserArgument.BuySell); argParser.ParseArguments(); if (!argParser.AreArgumentsValid) { argParser.PrintUsage(); return; } argParser.PrintArguments(); LoginParams loginParams = argParser.LoginParams; SampleParams sampleParams = argParser.SampleParams; session1 = O2GTransport.createSession(); session2 = O2GTransport.createSession(); Connection connection1 = new Connection(session1, loginParams, sampleParams, true); Connection connection2 = new Connection(session2, loginParams, sampleParams, false); Thread thread1 = new Thread(connection1.Run); Thread thread2 = new Thread(connection2.Run); thread1.Start(); thread2.Start(); thread1.Join(); thread2.Join(); } catch (Exception e) { Console.WriteLine("Exception: {0}", e.ToString()); } }
static void Main(string[] args) { O2GSession session = null; SessionStatusListener statusListener = null; ResponseListener responseListener = null; try { Console.WriteLine("GetHistPrices sample\n"); ArgumentParser argParser = new ArgumentParser(args, "GetHistPrices"); argParser.AddArguments(ParserArgument.Login, ParserArgument.Password, ParserArgument.Url, ParserArgument.Connection, ParserArgument.SessionID, ParserArgument.Pin, ParserArgument.Instrument, ParserArgument.TimeFrame, ParserArgument.DateFrom, ParserArgument.DateTo); 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); GetHistoryPrices(session, sampleParams.Instrument, sampleParams.Timeframe, sampleParams.DateFrom, sampleParams.DateTo, responseListener); 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(); 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("GetOffers sample\n"); ArgumentParser argParser = new ArgumentParser(args, "GetOffers"); argParser.AddArguments(ParserArgument.Login, ParserArgument.Password, ParserArgument.Url, ParserArgument.Connection, ParserArgument.SessionID, ParserArgument.Pin, ParserArgument.Instrument); 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); 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(10000); 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.WaitEvents(); } session.unsubscribeSessionStatus(statusListener); session.Dispose(); } } }
static void Main(string[] args) { O2GSession session = null; SessionStatusListener statusListener = null; ResponseListener responseListener = null; try { Console.WriteLine("SubscriptionStatus sample\n"); ArgumentParser argParser = new ArgumentParser(args, "SubscriptionStatus"); argParser.AddArguments(ParserArgument.Login, ParserArgument.Password, ParserArgument.Url, ParserArgument.Connection, ParserArgument.SessionID, ParserArgument.Pin, ParserArgument.Instrument, ParserArgument.Status, 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.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)); } O2GRequest request = CreateSetSubscriptionStatusRequest(session, offer.OfferID, sampleParams.Status, responseListener); 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"); } O2GResponse response = responseListener.GetResponse(); if (response != null && response.Type == O2GResponseType.CommandResponse) { Console.WriteLine("Subscription status for '{0}' is set to '{1}'", sampleParams.Instrument, sampleParams.Status); } PrintMargins(session, account, offer); UpdateMargins(session, responseListener); PrintMargins(session, account, offer); 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(); 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("SearchInTable sample\n"); ArgumentParser argParser = new ArgumentParser(args, "SearchInTable"); argParser.AddArguments(ParserArgument.Login, ParserArgument.Password, ParserArgument.Url, ParserArgument.Connection, ParserArgument.SessionID, ParserArgument.Pin, ParserArgument.OrderID, 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; FindOrder(session, sampleParams.AccountID, sampleParams.OrderID, responseListener); 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(); 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; int iContingencyGroupType = 1; // OCO group try { Console.WriteLine("JoinNewGroup sample\n"); ArgumentParser argParser = new ArgumentParser(args, "JoinNewGroup"); argParser.AddArguments(ParserArgument.Login, ParserArgument.Password, ParserArgument.Url, ParserArgument.Connection, ParserArgument.SessionID, ParserArgument.Pin, ParserArgument.PrimaryID, ParserArgument.SecondaryID, 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; O2GRequest request = JoinToNewGroupRequest(session, sampleParams.AccountID, sampleParams.PrimaryID, sampleParams.SecondaryID, iContingencyGroupType); if (request == null) { throw new Exception("Cannot create request"); } List <string> orderIDList = new List <string>(); orderIDList.Add(sampleParams.PrimaryID); orderIDList.Add(sampleParams.SecondaryID); foreach (string sOrderID in orderIDList) { if (!IsOrderExists(session, sampleParams.AccountID, sOrderID, responseListener)) { throw new Exception(string.Format("Order '{0}' does not exist", sOrderID)); } } responseListener.SetOrderIDs(orderIDList); 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; SessionStatusListener statusListener = null; ResponseListener responseListener = null; try { Console.WriteLine("PrintTable sample\n"); ArgumentParser argParser = new ArgumentParser(args, "PrintTable"); argParser.AddArguments(ParserArgument.Login, ParserArgument.Password, ParserArgument.Url, ParserArgument.Connection, ParserArgument.SessionID, ParserArgument.Pin, ParserArgument.TableType); 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) { O2GResponseType responseType = string.Equals(sampleParams.TableType, SampleParams.OrdersTable) == true ? O2GResponseType.GetOrders : O2GResponseType.GetTrades; responseListener = new ResponseListener(responseType); session.subscribeResponse(responseListener); O2GAccountRow account = GetAccount(session); if (account != null) { if (responseType == O2GResponseType.GetOrders) { PrintOrders(session, account.AccountID, responseListener); } else { PrintTrades(session, account.AccountID, responseListener); } Console.WriteLine("Done!"); } else { throw new Exception("No valid accounts"); } } } 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("CloseAllPositions 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(); 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(session); 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; Dictionary <string, CloseOrdersData> closeOrdersData = GetCloseOrdersData(tableManager, sampleParams.AccountID); if (closeOrdersData.Values.Count == 0) { throw new Exception("There are no opened positions"); } tableListener.SubscribeEvents(tableManager); O2GRequest request = CreateCloseAllRequest(session, closeOrdersData); if (request == null) { throw new Exception("Cannot create request"); } List <string> requestIDs = new List <string>(); for (int i = 0; i < request.ChildrenCount; i++) { requestIDs.Add(request.getChildRequest(i).RequestID); } responseListener.SetRequestIDs(requestIDs); tableListener.SetRequestIDs(requestIDs); 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; IPriceHistoryCommunicator communicator = null; SessionStatusListener statusListener = null; try { Console.WriteLine("GetHistPrices sample\n"); ArgumentParser argParser = new ArgumentParser(args, "GetHistPrices"); argParser.AddArguments(ParserArgument.Login, ParserArgument.Password, ParserArgument.Url, ParserArgument.Connection, ParserArgument.SessionID, ParserArgument.Pin, ParserArgument.Instrument, ParserArgument.TimeFrame, ParserArgument.DateFrom, ParserArgument.DateTo, ParserArgument.QuotesCount, ParserArgument.OpenPriceCandlesMode); argParser.ParseArguments(); if (!argParser.AreArgumentsValid) { argParser.PrintUsage(); return; } argParser.PrintArguments(); LoginParams loginParams = argParser.LoginParams; SampleParams sampleParams = argParser.SampleParams; // use the application module path as a base path for quotes storage string storagePath = System.IO.Path.Combine(AppContext.BaseDirectory, "History"); // create the ForexConnect trading session session = O2GTransport.createSession(); statusListener = new SessionStatusListener(session, loginParams.SessionID, loginParams.Pin); // subscribe IO2GSessionStatus interface implementation for the status events session.subscribeSessionStatus(statusListener); statusListener.Reset(); // create an instance of IPriceHistoryCommunicator communicator = PriceHistoryCommunicatorFactory.createCommunicator(session, storagePath); // log in to ForexConnect session.login(loginParams.Login, loginParams.Password, loginParams.URL, loginParams.Connection); if (statusListener.WaitEvents() && statusListener.Connected) { CommunicatorStatusListener communicatorStatusListener = new CommunicatorStatusListener(); communicator.addStatusListener(communicatorStatusListener); // wait until the communicator signals that it is ready if (communicator.isReady() || (communicatorStatusListener.WaitEvents() && communicatorStatusListener.Ready)) { // set open price candles mode, it must be called after login QuotesManager quotesManager = communicator.getQuotesManager(); quotesManager.openPriceCandlesMode = sampleParams.OpenPriceCandlesMode; // attach the instance of the class that implements the IPriceHistoryCommunicatorListener // interface to the communicator ResponseListener responseListener = new ResponseListener(); communicator.addListener(responseListener); GetHistoryPrices(communicator, sampleParams.Instrument, sampleParams.Timeframe, sampleParams.DateFrom, sampleParams.DateTo, sampleParams.QuotesCount, responseListener); Console.WriteLine("Done!"); communicator.removeListener(responseListener); } communicator.removeStatusListener(communicatorStatusListener); } } catch (Exception e) { Console.WriteLine("Exception: {0}", e.ToString()); } finally { if (communicator != null) { communicator.Dispose(); } if (session != null) { try { statusListener.Reset(); session.logout(); statusListener.WaitEvents(); } catch (Exception) { } session.unsubscribeSessionStatus(statusListener); session.Dispose(); } } }
static void Main(string[] args) { O2GSession session = null; SessionStatusListener statusListener = null; try { Console.WriteLine("GetOffers sample\n"); ArgumentParser argParser = new ArgumentParser(args, "GetOffers"); argParser.AddArguments(ParserArgument.Login, ParserArgument.Password, ParserArgument.Url, ParserArgument.Connection, ParserArgument.SessionID, ParserArgument.Pin, ParserArgument.Instrument); 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) { TableListener tableListener = new TableListener(); 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"); } tableListener.SetInstrument(sampleParams.Instrument); tableListener.SubscribeEvents(tableManager); O2GOffersTable offers = null; offers = (O2GOffersTable)tableManager.getTable(O2GTableType.Offers); tableListener.PrintOffers(offers, null); // Do nothing 10 seconds, let offers print Thread.Sleep(10000); Console.WriteLine("Done!"); 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(); session.unsubscribeSessionStatus(statusListener); } session.Dispose(); } } }
static void Main(string[] args) { O2GSession session = null; SessionStatusListener statusListener = null; ResponseListener responseListener = null; try { Console.WriteLine("CloseAllPositionsByInstrument sample\n"); ArgumentParser argParser = new ArgumentParser(args, "CloseAllPositionsByInstrument"); argParser.AddArguments(ParserArgument.Login, ParserArgument.Password, ParserArgument.Url, ParserArgument.Connection, ParserArgument.SessionID, ParserArgument.Pin, ParserArgument.Instrument, 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, sampleParams.Instrument); if (offer == null) { throw new Exception(string.Format("The instrument '{0}' is not valid", sampleParams.Instrument)); } CloseOrdersData closeOrdersData; if (!GetCloseOrdersData(session, sampleParams.AccountID, offer.OfferID, responseListener, out closeOrdersData)) { throw new Exception("There are no opened positions"); } O2GRequest request = CreateCloseMarketNettingOrderRequest(session, closeOrdersData); if (request == null) { throw new Exception("Cannot create request; probably some arguments are missing or incorrect"); } List <string> requestIDs = new List <string>(); for (int i = 0; i < request.ChildrenCount; i++) { requestIDs.Add(request.getChildRequest(i).RequestID); } responseListener.SetRequestIDs(requestIDs); 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(); } } }
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(); } } }
static void Main(string[] args) { O2GSession session = null; SessionStatusListener statusListener = null; try { ArgumentParser argParser = new ArgumentParser(args, "PrintTableListener"); argParser.AddArguments(ParserArgument.Login, ParserArgument.Password, ParserArgument.Url, ParserArgument.Connection, ParserArgument.SessionID, ParserArgument.Pin); argParser.ParseArguments(); if (!argParser.AreArgumentsValid) { argParser.PrintUsage(); return; } argParser.PrintArguments(); LoginParams loginParams = argParser.LoginParams; session = O2GTransport.createSession(); TableManagerListener managerListener = new TableManagerListener(); session.useTableManager(O2GTableManagerMode.Yes, managerListener); 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) { O2GTableManager tableManager = session.getTableManager(); if (managerListener.WaitEvents() && managerListener.IsLoaded) { Console.WriteLine("All tables are loaded"); } else { throw new Exception("Cannot refresh all tables of table manager"); } O2GAccountRow account = GetAccount(tableManager); if (account == null) { throw new Exception("No valid accounts"); } managerListener.Reset(); O2GTableManager tableManagerByAccount = session.getTableManagerByAccount(account.AccountID); if (managerListener.WaitEvents() && managerListener.IsLoaded) { Console.WriteLine(string.Format("All tables are loaded (account {0})", account.AccountID)); } else { throw new Exception(string.Format("Cannot refresh all tables of table manager (account {0})", account.AccountID)); } PrintOrders(tableManagerByAccount, account.AccountID); Console.WriteLine("Done!"); } } catch (Exception e) { Console.WriteLine(string.Format("Exception: {0}", e.ToString())); } finally { if (session != null) { if (statusListener.Connected) { 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; try { Console.WriteLine("IfThen sample\n"); ArgumentParser argParser = new ArgumentParser(args, "IfThen"); 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(); 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 IF-THEN order 30 pips below the current market price // and our stop entry order 15 pips below the current market price double dRateIf = offer.Ask - 30.0 * offer.PointSize; double dRateStop = offer.Ask - 15.0 * offer.PointSize; tableListener.SubscribeEvents(tableManager); O2GRequest request = CreateIfThenRequest(session, offer.OfferID, account.AccountID, iAmount, dRateIf, dRateStop); 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); 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) { 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("GetLastOrderUpdate sample\n"); ArgumentParser argParser = new ArgumentParser(args, "GetLastOrderUpdate"); 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(); 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) { statusListener.Reset(); session.logout(); statusListener.WaitEvent(); if (responseListener != null) { session.unsubscribeResponse(responseListener); } } session.unsubscribeSessionStatus(statusListener); session.Dispose(); } } }
static void Main(string[] args) { O2GSession session = null; SessionStatusListener statusListener = null; try { Console.WriteLine("PrintRollover sample\n"); ArgumentParser argParser = new ArgumentParser(args, "PrintRollover"); argParser.AddArguments(ParserArgument.Login, ParserArgument.Password, ParserArgument.Url, ParserArgument.Connection, ParserArgument.SessionID, ParserArgument.Instrument, ParserArgument.AccountID, ParserArgument.Pin); 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); O2GRolloverProvider rolloverProvider = session.getRolloverProvider(); AutoResetEvent autoEvent = new AutoResetEvent(false); RolloverProviderListener rolloverProviderListener = new RolloverProviderListener(autoEvent); rolloverProvider.subscribe(rolloverProviderListener); 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"); } 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)); } if (autoEvent.WaitOne(10000)) //wait 10s { PrintRollover(rolloverProvider, account, offer); } else { Console.WriteLine("Waiting time expired: Rollover is not avaliavle"); } rolloverProvider.unsubscribe(rolloverProviderListener); } } 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(); } } }
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.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(); 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) { statusListener.Reset(); session.logout(); statusListener.WaitEvent(); 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("NetStopLimit sample\n"); ArgumentParser argParser = new ArgumentParser(args, "NetStopLimit"); argParser.AddArguments(ParserArgument.Login, ParserArgument.Password, ParserArgument.Url, ParserArgument.Connection, ParserArgument.SessionID, ParserArgument.Pin, ParserArgument.Instrument, ParserArgument.RateStop, ParserArgument.RateLimit, 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, sampleParams.Instrument); if (offer == null) { throw new Exception(string.Format("The instrument '{0}' is not valid", sampleParams.Instrument)); } O2GTradeRow trade = GetTrade(session, sampleParams.AccountID, offer.OfferID, responseListener); if (trade == null) { throw new Exception(string.Format("There are no opened positions for instrument '{0}'", sampleParams.Instrument)); } O2GRequest request; string sBuySell = trade.BuySell.Equals(Constants.Buy) ? Constants.Sell : Constants.Buy; request = CreateNetEntryOrderRequest(session, offer.OfferID, sampleParams.AccountID, sampleParams.RateStop, sBuySell, Constants.Orders.StopEntry); 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"); } request = CreateNetEntryOrderRequest(session, offer.OfferID, sampleParams.AccountID, sampleParams.RateLimit, 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"); } 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(); if (responseListener != null) { session.unsubscribeResponse(responseListener); } } session.unsubscribeSessionStatus(statusListener); session.Dispose(); } } }
static void Main(string[] args) { O2GSession session = null; SessionStatusListener statusListener = null; try { Console.WriteLine("PrintTable sample\n"); ArgumentParser argParser = new ArgumentParser(args, "PrintTable"); argParser.AddArguments(ParserArgument.Login, ParserArgument.Password, ParserArgument.Url, ParserArgument.Connection, ParserArgument.SessionID, ParserArgument.Pin, ParserArgument.TableType); 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) { 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); if (account == null) { throw new Exception("No valid accounts"); } O2GResponseType responseType = string.Equals(sampleParams.TableType, SampleParams.OrdersTable) == true ? O2GResponseType.GetOrders : O2GResponseType.GetTrades; if (responseType == O2GResponseType.GetOrders) { PrintOrders(tableManager, account.AccountID); } else { PrintTrades(tableManager, account.AccountID); } 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(); } } }
static void Main(string[] args) { O2GSession session = null; SessionStatusListener statusListener = null; ResponseListener responseListener = null; string sOrderType = Constants.Orders.LimitEntry; try { Console.WriteLine("FindRowByColumnValue sample\n"); ArgumentParser argParser = new ArgumentParser(args, "FindRowByColumnValue"); 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)); } } 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 * 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); } } 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; 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; try { Console.WriteLine("CalculateTradingCommissions sample\n"); ArgumentParser argParser = new ArgumentParser(args, "CalculateTradingCommissions"); argParser.AddArguments(ParserArgument.Login, ParserArgument.Password, ParserArgument.Url, ParserArgument.Connection, ParserArgument.SessionID, ParserArgument.Pin, 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(); session.useTableManager(O2GTableManagerMode.Yes, null); statusListener = new SessionStatusListener(session, loginParams); 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"); } O2GAccountRow 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)); } 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; 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(); } } }
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; SessionStatusListener statusListener = null; ResponseListener responseListener = null; try { Console.WriteLine("CreateAndFindEntry sample\n"); ArgumentParser argParser = new ArgumentParser(args, "CreateAndFindEntry"); argParser.AddArguments(ParserArgument.Login, ParserArgument.Password, ParserArgument.Url, ParserArgument.Connection, ParserArgument.SessionID, ParserArgument.Pin, ParserArgument.Instrument, 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(); 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; 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"); } } } 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(); } } }