/// <summary> /// Bardziej zaawansowany przykład użycia "NolClient": zalogowanie dopiero na żądanie, /// bez uruchamiania wątku odbierającego komunikaty asynchroniczne (można go obsłużyć samemu). /// Samodzielne przygotowanie, wysyłka i odbiór przykładowego message'a. /// </summary> public void Execute() { using (var nol = new NolClient(false, false)) { // zalogowanie użytkownika Console.WriteLine("\nPress any key... to log in [Esc - cancel]\n"); if (Console.ReadKey(true).Key == ConsoleKey.Escape) { return; } nol.Login(); // otwarcie kanału asynchronicznego // (na razie nic tu z niego nie odbieramy, bo do tego przydałby się oddzielny wątek) Console.WriteLine("\nPress any key... to open async socket [Esc - skip]\n"); Socket asyncSocket = null; if (Console.ReadKey(true).Key != ConsoleKey.Escape) { asyncSocket = NolClient.GetAsyncSocket(); } // wysyłka przykładowego komunikatu // (można skorzystać z gotowych klas zdefiniowanych w pjank.BossaAPI.Fixml, // ale można też spreparować coś zupełnie własnego w oparciu o klasę CustomMsg) Console.WriteLine("\nPress any key... to send a custom message [Esc - cancel]\n"); if (Console.ReadKey(true).Key == ConsoleKey.Escape) { return; } var tmp = FixmlMsg.DebugOriginalXml.Enabled; try { FixmlMsg.DebugOriginalXml.Enabled = true; // otwarcie nowego połączenia (kanał synchroniczny za każdym razem nowy!) using (var syncSocket = NolClient.GetSyncSocket()) { // przygotowanie komunikatu var request = new UserRequestMsg() { Username = "******", Type = UserRequestType.GetStatus, }; // wysyłka komunikatu request.Send(syncSocket); // odbiór odpowiedzi Console.WriteLine("\nPress any key... to read the response\n"); Console.ReadKey(true); var response = new FixmlMsg(syncSocket); Trace.WriteLine("\nResponse XML:\n" + response.Xml.FormattedXml() + "\n"); // dokładniejsza analiza odpowiedzi (w klasie konkretnego rodzaju komunikatu) Console.WriteLine("Press any key... to parse the response message\n"); Console.ReadKey(true); UserResponseMsg parsedResponse = new UserResponseMsg(response); Trace.WriteLine(String.Format("\nResponse parsed:\n Status = {0}, StatusText = '{1}'\n", parsedResponse.Status, parsedResponse.StatusText)); } Console.WriteLine("\nPress any key... to send another custom message [Esc - cancel]\n"); if (Console.ReadKey(true).Key == ConsoleKey.Escape) { return; } // otwarcie nowego połączenia (kanał synchroniczny za każdym razem nowy!) using (var syncSocket = NolClient.GetSyncSocket()) { // tak można spreparować dowolny komunikat, również taki jeszcze nieistniejący ;-> var request = new CustomMsg("MyCustomRequest"); var xmlElement = request.AddElement("Test"); xmlElement.SetAttribute("attr1", "1"); xmlElement.SetAttribute("attr2", "2"); // wysyłka tak samodzielnie spreparowanego komunikatu request.Send(syncSocket); // odbiór odpowiedzi - tutaj powinniśmy otrzymać błąd... "BizMessageRejectException" // niestety aktualna wersja NOL3 zwraca nieprawidłowy XML, którego nie da się parsować Console.WriteLine("\nPress any key... to read the response\n"); Console.ReadKey(true); var response = new FixmlMsg(syncSocket); } } catch (Exception e) { MyUtil.PrintError(e); } FixmlMsg.DebugOriginalXml.Enabled = tmp; Console.ReadKey(true); if (asyncSocket != null) { asyncSocket.Close(); } } // tu następuje automatyczne wylogowanie }
/// <summary> /// Przykład wysyłki zleceń, korzystając bezpośrednio z klas NewOrderSingleMsg i spółki... /// Test ten wrzuca na giełdę zlecenie kupna 1 x FW20 po 1000zł (*raczej* nie ma szans się zrealizować :)), /// następnie je modyfikuje ustawiając limit ceny oczko wyżej... aż ostatecznie całe zlecenie anuluje. /// </summary> public void Execute() { var accountNumber = "00-22-..."; // <- wpisz tu swój numer, żeby program nie musiał o niego pytać if (accountNumber.EndsWith("...")) { Console.Write("Podaj numer rachunku (końcówkę z " + accountNumber + "): "); var str = Console.ReadLine(); accountNumber = accountNumber.Replace("...", str); Trace.WriteLine("Wybrany rachunek: " + accountNumber); } // nawiązanie połączenia z NOL3 i zalogowanie użytkownika using (var nol = new NolClient()) { Thread.Sleep(2000); var tmp = FixmlMsg.DebugFormattedXml.Enabled; try { ExecutionReportMsg execReport; // --- wysyłka nowego zlecenia --- Console.WriteLine("\nPress any key... to send NEW order request [Esc - exit]\n"); if (Console.ReadKey(true).Key == ConsoleKey.Escape) { return; } var newRequest = new NewOrderSingleMsg(); newRequest.Account = accountNumber; newRequest.Side = OrderSide.Buy; newRequest.Instrument = FixmlInstrument.FindBySym("FW20H12"); newRequest.Quantity = 1; newRequest.Price = 1000; using (var socket = NolClient.GetSyncSocket()) { FixmlMsg.DebugFormattedXml.Enabled = true; // <- wyświetli nam dokładną treść komunikatów newRequest.Send(socket); execReport = new ExecutionReportMsg(socket); FixmlMsg.DebugFormattedXml.Enabled = tmp; } Thread.Sleep(3000); // --- modyfikacja tego zlecenia --- Console.WriteLine("\nPress any key... to MODIFY this order request [Esc - exit]\n"); if (Console.ReadKey(true).Key == ConsoleKey.Escape) { return; } var replaceRequest = new OrderReplaceRequestMsg(); replaceRequest.BrokerOrderId2 = execReport.BrokerOrderId2; replaceRequest.Account = accountNumber; replaceRequest.Side = OrderSide.Buy; replaceRequest.Instrument = newRequest.Instrument; replaceRequest.Quantity = 1; replaceRequest.Price = 1001; using (var socket = NolClient.GetSyncSocket()) { FixmlMsg.DebugFormattedXml.Enabled = true; replaceRequest.Send(socket); execReport = new ExecutionReportMsg(socket); FixmlMsg.DebugFormattedXml.Enabled = tmp; } Thread.Sleep(3000); // --- anulowanie tego zlecenia --- Console.WriteLine("\nPress any key... to CANCEL this order request [Esc - exit]\n"); if (Console.ReadKey(true).Key == ConsoleKey.Escape) { return; } var cancelRequest = new OrderCancelRequestMsg(); cancelRequest.BrokerOrderId2 = replaceRequest.BrokerOrderId2; cancelRequest.Account = accountNumber; cancelRequest.Side = newRequest.Side; cancelRequest.Instrument = newRequest.Instrument; cancelRequest.Quantity = newRequest.Quantity; using (var socket = NolClient.GetSyncSocket()) { FixmlMsg.DebugFormattedXml.Enabled = true; cancelRequest.Send(socket); execReport = new ExecutionReportMsg(socket); FixmlMsg.DebugFormattedXml.Enabled = false; } Thread.Sleep(3000); Console.WriteLine("\nPress any key... to exit\n"); Console.ReadKey(true); Console.WriteLine("\n\nThank you :)\n"); } catch (Exception e) { MyUtil.PrintError(e); } FixmlMsg.DebugFormattedXml.Enabled = tmp; } // tu następuje wylogowanie }