MT5 Async order open/close

easy direct connection to any MT4 and MT5 server

MT5 Async order open/close

Here you can see how to send trade request without waiting of execution.

static void  async()
{
    var api = new MT5API("45.76.194.231:443", 7371, "mt5api");
    api.OnConnectStatus += ConnectionStatus;
    api.OnOrderProgress += Api_OnOrderProgress;
    api.Connect();
    int id = api.GetId();
    string symbol = "EURUSD";
    api.Subscribe(symbol);
    while (api.GetQuote(symbol) == null)
        Thread.Sleep(1);
    api.OrderSendAsync(id, symbol, 100, api.GetQuote(symbol).Ask, OrderType.Buy, 0, 0, 0, null, 0);
    Console.WriteLine("requestID = " + id);
}
 
private static void Api_OnOrderProgress(MT5API api, int requestID, OrderProgress progress, Order order)
{
    Console.WriteLine(requestID + " " + progress + " " + order);
    if(progress == OrderProgress.REQUEST_EXECUTED)
        if (order.ClosePrice > 0 || order.State == OrderState.Canceled)
            Console.WriteLine("Closed with profit " + order.Profit);
        else
        {
            int id = api.GetId();
            Console.WriteLine(requestID + " opened at " + order.OpenPrice + " Ticket = " + order.Ticket + " closeRequestId = " + id);
            api.OrderCloseAsync(id, order.Ticket, order.Symbol, order.OpenPrice, order.Volume, order.OrderType);
        }
}

Leave a Reply