1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

MQL code to record order execution duration and slippage

Discussion in 'General Forex Talk' started by hyper, Feb 22, 2012.

  1. hyper

    hyper Corporal

    Joined:
    May 29, 2009
    Messages:
    93
    Likes Received:
    0
    Here is some MQL code for sending an order that records execution duration and slippage to use in MetaTrader expert advisors or scripts.

    You can modify it or copy and paste bits of it to put into your own code.

    Code:
    int SendOrder(string symbol, int orderType, double volumeInLot, int openSlipAllowedInPoints, int magicNumber, string orderComment) {
       if (orderType == OP_BUY) double requestedPrice = MarketInfo(symbol, MODE_ASK);
       else requestedPrice = MarketInfo(symbol, MODE_BID);
    
       int orderStartPointInTime = GetTickCount();
    
       int ticket = OrderSend(symbol, orderType, volumeInLot, requestedPrice, OpenSlipAllowedInPoints, 0, 0, orderComment, magicNumber, 0);
       
       int orderExecutionDurationInMs = GetTickCount() - orderStartPointInTime;
    
       if (ticket == -1) { //execution failed
          int errorCode = GetLastError();
          
          Print(StringConcatenate("Error sending order: " + errorCode + " (" + ErrorDescription(errorCode) + "). Execution duration = ", orderExecutionDurationInMs, "ms"));
    
          //"Time (Local)", "Symbol", "Open or close", "Ticket", "Execution duration (ms)", "Slippage (QCU)", "Error code", "Error description"
          FileWrite(diskFileHandle, TimeToStr(TimeLocal(), TIME_DATE|TIME_MINUTES|TIME_SECONDS), symbol, "open", ticket, orderExecutionDurationInMs, "", errorCode, ErrorDescription(errorCode));
          FileFlush(diskFileHandle);
       } else {
          if (OrderSelect(ticket, SELECT_BY_TICKET)) {
             if (orderType == OP_BUY) double slippageIncurred = OrderOpenPrice() - requestedPrice;
             else slippageIncurred = requestedPrice - OrderOpenPrice();
             
             Print(StringConcatenate("Execution duration = ", orderExecutionDurationInMs, "ms,Slippage = ", DoubleToStr(slippageIncurred * 10000, 1), "pips"));
             
             FileWrite(diskFileHandle, TimeToStr(TimeLocal(), TIME_DATE|TIME_MINUTES|TIME_SECONDS), symbol, "open", ticket, orderExecutionDurationInMs, DoubleToStr(slippageIncurred, Digits));
             FileFlush(diskFileHandle);
          }
       }   
       return (ticket);
    }
    
    
    Please report back if you find any brokers whose order executions take more than 1s and/or incur frequent slippage of more than 1 pip.
     
    #1 hyper, Feb 22, 2012
    Last edited: Feb 28, 2012
  2. hyper

    hyper Corporal

    Joined:
    May 29, 2009
    Messages:
    93
    Likes Received:
    0
    Here is MQL code to close an order by ticket that also records execution duration and slippage.

    Code:
    bool CloseOpenOrder(string symbol, int ticketToClose, int closeSlipAllowedInPoints, int magicNumber, bool checkMagicNumber) {
       int orderStartPointInTime = GetTickCount();
    
          if (OrderSelect(ticketToClose, SELECT_BY_TICKET, MODE_TRADES)) {
             if (CheckMagicNumber)
                if (OrderMagicNumber() != magicNumber) return(false);
             if (OrderSymbol() != symbol) return(false);
    
             if (OrderType() == OP_BUY) double requestedPrice = MarketInfo(symbol, MODE_BID);
             else requestedPrice = MarketInfo(symbol, MODE_ASK);
             
                   
                bool orderCloseSuccess = OrderClose(ticketToClose, OrderLots(), requestedPrice, closeSlipAllowedInPoints);
                   
             if (!orderCloseSuccess) {
                int errorCode = GetLastError();
    
                int orderExecutionDurationInMs = GetTickCount() - orderStartPointInTime;
                if (errorCode != 0) Print(StringConcatenate(GetTickCount(), "Error closing order: " + errorCode + " (" + ErrorDescription(errorCode) + "). Execution duration = ", orderExecutionDurationInMs, "ms"));
                //OrderPrint(); //ticket number; open time; trade operation;  amount of lots; symbol; open price; StopLoss;  TakeProfit; close time; close price;  commission; swap; profit;  comment; magic number; pendingorder expiration date.
    
                //"Time (Local)", "Symbol", "Open or close", "Ticket", "Execution duration (ms)", "Slippage (QCU)", "Error code", "Error description"
                FileWrite(diskFileHandle, TimeToStr(TimeLocal(), TIME_DATE|TIME_MINUTES|TIME_SECONDS), symbol, "close", ticketToClose, orderExecutionDurationInMs, "", errorCode, ErrorDescription(errorCode));
                FileFlush(diskFileHandle);
    
                return(false);
             } else {
                if (OrderSelect(ticketToClose, SELECT_BY_TICKET, MODE_HISTORY)) {
    
                   if (OrderType() == OP_BUY) double slippageIncurred = requestedPrice - OrderClosePrice();
                   else slippageIncurred = OrderClosePrice() - requestedPrice;
    
                   orderExecutionDurationInMs = GetTickCount() - orderStartPointInTime;
                   Print(StringConcatenate("Execution duration = ", orderExecutionDurationInMs, "ms,Slippage = ", DoubleToStr(slippageIncurred * 10000, 1), "pips"));
    
                   FileWrite(diskFileHandle, TimeToStr(TimeLocal(), TIME_DATE|TIME_MINUTES|TIME_SECONDS), symbol, "close", ticketToClose, orderExecutionDurationInMs, DoubleToStr(slippageIncurred, Digits));
                   FileFlush(diskFileHandle);
                }
                return (orderCloseSuccess);
             }
          } else {
             errorCode = GetLastError();
             Print(StringConcatenate(GetTickCount(), ": Error selecting order. (" + errorCode + ": " + ErrorDescription(errorCode) + ")"));
          }
       return (orderCloseSuccess);
    }
    
    
    Please report back if you find any brokers whose order executions take more than 1s and/or incur frequent slippage of more than 1 pip.
     
    #2 hyper, Feb 22, 2012
    Last edited: Feb 28, 2012
  3. hyper

    hyper Corporal

    Joined:
    May 29, 2009
    Messages:
    93
    Likes Received:
    0
    Here is the MQL code to initially open the file to which the statistics would be recorded:
    Code:
    int init()
    {
       string fileName = "Execution Statistics_" + AccountCompany() + "_" + AccountServer() + ".csv";
       diskFileHandle = FileOpen(fileName, FILE_CSV|FILE_READ|FILE_WRITE, ',');
       if(diskFileHandle <= 0) return(0);
       if(!FileIsEnding(diskFileHandle)) FileSeek(diskFileHandle, 0, SEEK_END); //append
       if (FileTell(diskFileHandle) == 0) FileWrite(diskFileHandle, "Time (Local)", "Symbol", "Open or close", "Ticket", "Execution duration (ms)", "Slippage (QCU)", "Error code", "Error description");
    
       return(0);
    }
     
    #3 hyper, Feb 22, 2012
    Last edited: Feb 28, 2012
  4. hyper

    hyper Corporal

    Joined:
    May 29, 2009
    Messages:
    93
    Likes Received:
    0
    I've attached an MQL script using the code above that allows you to manually open a trade. It then prints the execution duration and slippage to the Experts tab and log, as well as to a custom CSV log file under the experts\files directory.

    This is just one example of how my code can be used.
     

    Attached Files:

    #4 hyper, Feb 28, 2012
    Last edited: Feb 28, 2012

Share This Page