MT4 Async Functions

easy direct connection to any MT4 and MT5 server

MT4 Async Functions

All trading functions(OrderSend, OrderClose, OrderModify, OrderDelete) and Connect function have asynchronous variants that have Async postfix. Execution of asynchronous function goes in another thread so you need to receive results with using events OnConnect(for ConnectAsync) and OnOrderProgress(for trading functions). Example below shows how to send two orders simultaneously. This also should be useful for trade copiers because after getting order update from master account you can send that update to all slaves simultaneously.

void Run()
{
   try
   {
      MainServer srv = QuoteClient.LoadSrv(@"GerchikCo-Demo.srv");
      QuoteClient qc = new QuoteClient(67611, "wx1yhpn", srv.Host, srv.Port);
      Console.WriteLine("Connecting...");
      qc.Connect();
      OrderClient oc1 = new OrderClient(qc);
      oc1.OnOrderProgress += new OrderProgressEventHandler(oc_OnOrderProgress);
      oc1.Connect();
      OrderClient oc2 = new OrderClient(qc); //could be connected to another broker
      oc2.OnOrderProgress += new OrderProgressEventHandler(oc_OnOrderProgress);
      oc2.Connect();
      Console.WriteLine("Connected to server");
      while (qc.GetQuote("EURUSD") == null)
         Thread.Sleep(10);
      double ask = qc.GetQuote("EURUSD").Ask;
      int id1 = oc1.OrderSendAsync("EURUSD"Op.Buy, 0.1, ask, 0, 0, 0, null, 0, new DateTime());
      Console.WriteLine("Order with RequestID = " + id1 + " sent");
      int id2 = oc2.OrderSendAsync("EURUSD"Op.Buy, 0.1, ask, 0, 0, 0, null, 0, new DateTime());
      Console.WriteLine("Order with RequestID = " + id2 + " sent");
      Console.WriteLine("Press any key...");
      Console.ReadKey();
      qc.Disconnect();
      oc1.Disconnect();
      oc2.Disconnect();
   }
   catch (Exception ex)
   {
      Console.WriteLine(ex.Message);
      Console.WriteLine("Press any key...");
      Console.ReadKey();
   }
 
}
 
void oc_OnOrderProgress(object sender, OrderProgressEventArgs args)
{
   if (args.Exception == null)
   {
      Console.WriteLine("ReuestID = " + args.TempID + ", State = " + args.Type);
      if (args.Type == ProgressType.Opened)
         Console.WriteLine("ReuestID = " + args.TempID + ", Ticket = " + args.Order.Ticket);
   }
   else
      Console.WriteLine(args.Exception.Message);
}

 

Leave a Reply