Forex Blog

First-hand Forex trading experience and information about foreign exchange market that will be useful to traders

Archives

OrderSend Error 4107 (Backtesting)

January 24, 2011 (Last updated on September 22, 2011) by

OrderSend Error 4107 is a so-called MQL4 run-time error that is similar to OrderSend Error 129 but, unlike the latter, appears only during backtesting of the MT4 expert advisors, not during their live or demo run. The error is called ERR_INVALID_PRICE_PARAM (Invalid price) in the MT4 documentation; it has no counterpart in MT5. There are two main reasons for this 4107 error to appear:

  • Negative values in price, stoploss or takeprofit parameters of the OrderSend() function. Just make sure you pass the valid price parameters to this function, they surely shouldn’t be negative.
  • Unnormalized doubles in price, stoploss or takeprofit parameters of the OrderSend() function. If your Forex broker uses 4 digits after the dot in quotes and you are trying to use 5 digits in your orders, then you’ll be getting error 4107 during backtesting. The best solution here is to always normalize all the price doubles using the standard MetaTrader function before passing them to OrderSend():
    OpenPrice = NormalizeDouble(OpenPrice, Digits);
    StopLossPrice = NormalizeDouble(StopLossPrice, Digits);
    TakeProfitPrice = NormalizeDouble(TakeProfitPrice, Digits);
    OrderSend(Symbol(), OP_BUY, 1, OpenPrice, StopLossPrice, TakeProfitPrice, ...);


If you have your own ideas for dealing with MT4 Error 4107 or have some questions about it, feel free to reply to this post using the form below.

6 Responses to “OrderSend Error 4107 (Backtesting)”

  1. olly

    please i have this error message in demo trade not back testing what can i do to solve the problem. thanks

    Reply

    admin Reply:

    Demo trading using expert advisor, right? Make sure it uses positive normalized values for open price, stop-loss and take-profit levels in its OrderSend() calls.

    Reply

  2. tzm

    I wrote a very simple script to send an buystop order to the server. Till now, the EA kept reporting error 4107 all the time.What is wrong with my code?

    int start(){
      double openPrice=0.0, SL=0.0;
      int ticket=0;
      double stoplevel;
     
      stoplevel=MarketInfo(sym,MODE_STOPLEVEL);
      if(!IsNewBarbyTime()) return;
      if(Bars!=1==5) return;
      if(Bars0) Alert("ordersend OK!,#", ticket);
      else Alert("ordersend failed: ",GetLastError());
      if(OrderSelect(ticket,SELECT_BY_TICKET)){
        if(!OrderModify( OrderTicket(), OrderOpenPrice(), SL, 0,0, Yellow)) Alert("OrderModify failed: ",GetLastError());
        else Alert("order modify OK!,#", ticket);
      }
      return(0);
    }

    Reply

    admin Reply:

    Do you get error 4107 on OrderModify()? Because this code has no OrderSend() calls.

    Reply

  3. Orlando

    Hello Admin,

    I am in the middle of this problem. Could i have your email?

    Best,

    I am ofcourse gling to post my learnings here but don’t want to bother everybody with the whole string?

    Reply

    Andriy Moraru Reply:

    Upload it to Google Docs or Dropbox and post the link here if there is a lot of text to share.

    Reply

Leave a Reply

required
required (will not be published)
optional