error 4107 - invalid price parameter

 
hi, I ´m working with orders at stop an many orders don´t execute because of error 4107. can anybody explain it for me?. i `m ataching the EA
//|                                                     Pablo 10.mq4 |
//|                                          Copyright © 2008, CLAM. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008,CLAM."
#property link      "http://www.metaquotes.net"
 
 
//---- input parameters
extern double       TrueRangePer=0.7;
extern double      vol_mul=4;
extern double       Deltahi=5;
extern double       Deltalo=0.12;
extern int       emaperiod=15;
extern double       Lots=0.1;
static datetime prevtime=0;
 
int start()
  {
  
 
 if (Time[0] == prevtime) return(0);
   prevtime = Time[0];
   
   if (! IsTradeAllowed()) {
      again();
      return(0);
   }
  
 
     int cnt, ticket, total;
     
     
   int expire= TimeCurrent()+3600;
 //Print ("exp  =",expire);
  //Print ("time  =",TimeCurrent());
  
  
 //Set Up trades 
double Trigger=TrueRangePer*iATR(NULL,0,1,1);
 
//calculate Trailing stops from volatility
 
double vol_stop, EmaAtr;
//EmaAtr=iMA(NULL,0,emaperiod,0,MODE_EMA,iATR(NULL,0,1,0),0);
vol_stop=EmaAtr*vol_mul;
 
double DeltaEMA = iCustom( NULL,0, "DeltaEMA",15,0,1); 
 
total =OrdersTotal();
if (total<1)
{
 // if bool Uptrend=true
  if (Deltahi>DeltaEMA && DeltaEMA>Deltalo) 
 
  {
   Print("Trigger: ", iClose(NULL,0,1)+Trigger);
  
    while(true)
      {
         
 ticket= OrderSend(Symbol(),OP_BUYSTOP,Lots,iClose(NULL,0,1)+Trigger,3,Bid-100*Point,Ask+100*Point,"OpenLong",1,expire,Blue);
 // double fTStop=iClose(NULL,0,0)-vol_stop;
  if(ticket<=0)
        {
           Print("Error opening BUY order : ",GetLastError()); 
           Sleep(10000);
         //---- refresh price data
         RefreshRates();
         break;
        }
      else
         {
            OrderSelect(ticket,SELECT_BY_TICKET); 
            Print("BUY order opened : ",OrderOpenPrice());
            break;
             }           
           }
        }
                   
    
 // if DownTrend=true
  if (-Deltahi<DeltaEMA && DeltaEMA<-Deltalo)
 
 
   { 
     while(true)
      {
    ticket= OrderSend(Symbol(),OP_SELLSTOP,Lots,iClose(NULL,0,1)-Trigger,3,Ask+100*Point,Bid-100*Point,"OpenShort",1,expire,Pink);
  // fTStop=iClose(NULL,0,0)+vol_stop;
   if(ticket<=0)
           {
            Print("Error opening SELL order : ",GetLastError()); 
              Sleep(10000);
         //---- refresh price data
         RefreshRates();
         break;
        }
      else
         {
          OrderSelect(ticket,SELECT_BY_TICKET); 
          Print("SELL order opened : ",OrderOpenPrice());
          break;  
                }
               }
              }       
 
         return(0);
     }
 return(0);
  }
//+------------------------------------------------------------------+
void again() {
   prevtime = Time[1];
   Sleep(30000);
   }
 
juanmartin:
hi, I ´m working with orders at stop an many orders don´t execute because of error 4107. can anybody explain it for me?. i `m ataching the EA
check Ask+(100*point) and Bid-(100*point) hope it works
 
Try using NormalizeDouble() to set correct number of Digits.
,NormalizeDouble(iClose(NULL,0,1)+Trigger,Digits),

 

Try To Ude RefreshRates() Before ticket= OrderSend(.....)

 
ticket= OrderSend(Symbol(), OP_SELLSTOP, Lots, iClose(NULL,0,1)-Trigger, 3,
                  Ask+100*Point, Bid-100*Point, "OpenShort", 1, expire, Pink);
  1. Don't need a RefreshRates unless you have a sleep or another server call before. You can drop the while(true), sleep, and breaks. Simplify.
  2. Can't open a pending order closer than MarketInfo(Symbol(), MODE_STOPLEVEL)*Point from current market. (On IBFX, stoplevel=30, 3 pips.) Last bar close and current bar open are likely +/- one pip. Depending on the timeframe, even adding ATR might be too close.
  3. You don't need to normalize values. As long as you're farther then stoplevel away. I have no normalize in my code. I compare doubles >= never equality. SLnew=mathmin(x,Bid-stoplevel). Etc.

  4. On some brokers you must open the order first and then set the TP/SL. The same applies to pending order I believe. I also found that mt4 was checking the stops against the current price instead of where the price would be. Drop the stops and set them when the order opens.
    start(){
        for(int pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
            OrderSelect(pos, SELECT_BY_POS)     // Only my orders w/
        &&  OrderMagicNumber() == Magic         // my magic number
        &&  OrderType()        <= OP_SELL       // Open orders only
        &&  OrderSymbol()      == Symbol() ){   // and symbol
            if (OrderStopLoss() == 0) {
               if(!OrderModify( OrderTicket(), OrderOpenPrice, oo.SL, oo.TP, 0)){
                  Alert(...
        }
        ...
    


  5. You must adjust TP, SL, and slippage for 5 digit brokers
    //++++ These are adjusted for 5 digit brokers.
    double  pips2points,    // slippage  3 pips    3=points    30=points
            pips2dbl;       // Stoploss 15 pips    0.0015      0.00150
    int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
    int init(){
        if (Digits == 5 || Digits == 3){    // Adjust for five (5) digit brokers.
                    pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
        } else {    pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; }
        // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
    

 
WHRoeder:
  1. Don't need a RefreshRates unless you have a sleep or another server call before.

I do not know, maybe maybe not, but I had a similar problem in the past 12 hours and everything worked out after putting RefrashRates()

 
qjol:

I do not know, maybe maybe not, but I had a similar problem in the past 12 hours and everything worked out after putting RefrashRates()

ordersend followed by ordermodify? Two ordersends? Very long computation?

Doesn't hurt but won't fix other problems.