Ordersend error 4107

 

Hi guys,

Ive went through every material on error 4107 for EAs. I have this Expert that constantly returns this error, but Ive made all adjustments to prevent this - stopleve, normalize double , but still nothing. im adding the code and hope someone has ideas.

thanks 

       double stoplevel=NormalizeDouble(MarketInfo(Symbol(),MODE_STOPLEVEL)*point,Digits);
        if(!AreThereOrders() && rsi0>rsi_buylevel && stoch0>stoch_buylevel && maFast_0>maSlow_0 && maFast_1<maSlow_1) // Order 1 setup
        {
         for(int i=0;i<OrdersTotal();i++)
           {
            if(OrderSelect(i,SELECT_BY_POS))
              {
               if(OrderMagicNumber()==magic && OrderSymbol()==Symbol() && OrderType()==OP_SELL)
                 {
                  bool close=OrderClose(OrderTicket(),OrderLots(),Ask,slippage,clrNONE);
                 }
              }
           }
          if(CheckMoneyForTrade(Symbol(),LotSize,0)==false) return; 
          SL=NormalizeDouble(Bid-stoploss*point,Digits);
          if(stoploss*point<stoplevel)SL=NormalizeDouble(Bid-stoplevel,Digits);
          TP =NormalizeDouble(Bid+takeprofit*point,Digits);
          if(takeprofit*point<stoplevel)TP=NormalizeDouble(Bid+stoplevel,Digits);
         //---      
         ticket=OrderSend(Symbol(),OP_BUY,LotSize,Ask,slippage*pips2points,SL,TP,NULL,magic,0,clrGreen);
         if(ticket>0)
           {
            Print("Order Opened BUY  !!");
           }
         else
            Print("Error occured BUY  not sent! Error code :",GetLastError());
        }
 
  1. SL and TP can be no closer than MODE_STOPLEVEL. You are placing them at Bid. Use the debugger or print out your variables, and find out why.

  2. Do NOT use NormalizeDouble, EVER. For ANY Reason. It's a kludge, don't use it. It's use is usually wrong, as it is in this case.

  3. In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading,) while you are waiting for the current operation (closing, deleting, modifying) to complete, any number of other operations on other orders could have concurrently happened and changed the position indexing:
    1. For non-FIFO (US brokers,) (or the EA only opens one order per symbol,) you can simply count down in a position loop, and you won't miss orders. Get in the habit of always counting down.
                Loops and Closing or Deleting Orders - MQL4 and MetaTrader 4 - MQL4 programming forum
      For FIFO (US brokers,) and you (potentially) process multiple orders per symbol, you must count up and on a successful operation, reprocess all positions (set index to -1 before continuing.)
    2. and check OrderSelect in case earlier positions were deleted.
                What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
                Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    3. and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use the Predefined Variables (Bid/Ask) or OrderClosePrice() instead, on the next order/server call.

 
Stanislav Ivanov:

Hi guys,

Ive went through every material on error 4107 for EAs. I have this Expert that constantly returns this error, but Ive made all adjustments to prevent this - stopleve, normalize double , but still nothing. im adding the code and hope someone has ideas.

thanks 

Always count down when you close orders.

         for(int i=OrdersTotal()-1;i>=0;i--)

After a trading request, always use RefreshRates() if you want to use predefined variables.

 
Alain Verleyen:

Always count down when you close orders.

After a trading request, always use RefreshRates() if you want to use predefined variables.

Oh, seems to have forgotten this one, thanks
 
whroeder1:
  1. SL and TP can be no closer than MODE_STOPLEVEL. You are placing them at Bid. Use the debugger or print out your variables, and find out why.

  2. Do NOT use NormalizeDouble, EVER. For ANY Reason. It's a kludge, don't use it. It's use is usually wrong, as it is in this case.

Thanks 
Reason: