ERR_INVALID_STOPS

 

I have error type 130 during OrderSend function, can you help me to fix it? 

extern string ParametriCandele =   "Inserimento parametri candele di set-up";

extern string CandelePrecedentiBuy =   "Inserimento numero candele precedenti Buy";
extern int    candelePrecedentiBuy =    5;       // candele precedenti da considerare

int index = 0;
int ticket1 = 0;
bool condizione1, condizione2, checkBuy = false;
bool setup1, setup2 = false;
double EntryPointBuy,valTPBuy,valSLBuy;



//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {

   if (IsTesting()) {
   
      Print ("Testing mode!");   
   
   }

   return(INIT_SUCCEEDED);
  }
  
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  
{

 Print("Fine sessione. Ciao, Ciao!");

}

   
  
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
int start(){

    
   if ( condizione1Buy() && !condizione1){
   
       condizione1 = true;
       
       EntryPointBuy = ((High[1] - Low[1]) / 2) + Low[1]; // 50% del range candela set-up 1
       
       valSLBuy = ( Low[1] - ((High[1] - Low[1]) / 2));     // Stop loss 150% sotto la candela 1 
       
       Print("valSL:", valSLBuy);
       
       valTPBuy = (High[1] - Low[1]) + High[1]; 
         
   }
   
   if (condizione1 && !condizione2) {
   
      condizione2Buy();          
   
   }
  
    
   
   
   if (condizione1 && condizione2) {
   
      if(ticket1 > 0) {            // da verificare che la condizione sia solo su ordini pendenti 
      
         if (!OrderDelete(ticket1))   
      
         stampa("Errore in cancellazione ticket #1 ", GetLastError());  
       
       
      }    
       
      esecuzioneStrategiaBuy();
     
      if (ticket1 > 0) {
     
           checkBuy = true;
     
      }
  
   
   }
    
 return(0);   
    
}    
//+------------------------------------------------------------------+


bool condizione1Buy(){      //Verifica se la candela 1 è quella con range più grande delle ultime 5 e se ha chiuso fuori la BB 50 (candela set-up n° 1)

   int candele = candelePrecedentiBuy;
       
   double RangecCandelaMax = 0;
   
   for (int i = 1; i <= candele; i++)
      {
      
      if ( RangecCandelaMax < High[i] - Low[i] )
         {
         
         RangecCandelaMax = High[i] - Low[i];
         
         index = i;
         
         }
      } 
   
    if (RangecCandelaMax == High[1] - Low[1])  {
                  
       if ( Close[1] < iBands(NULL,0,50,2,0,PRICE_CLOSE,MODE_LOWER,1)){
   
         Print(" La candela [1] ha range più grande delle ultime 'n' e ha chiuso fuori la BB 50 ");
   
      return(true);
   
       }
   
   
      else return(false);
      
     } 
      
     else return(false);
     
}   



bool condizione2Buy(){   //Si attende che il prezzo risalendo disegni almeno una candela (candela set-up n° 2) con Low maggiore del 50% della candela di set-up 1


    if ( Low[1] > EntryPointBuy) {
    
    condizione2 = true;
    
    Print(" Il min dell'ultima candela è > Entrypoint ");
    
    return(true);
    
    }
       else return(false);

}


void esecuzioneStrategiaBuy(){

   ticket1 = OrderSend(Symbol(),OP_BUYLIMIT,1,Ask,0,valSLBuy,valTPBuy," Buy ordine #1 ",0,0,clrCrimson);
   
   if (ticket1 < 0) stampa("Errore in apertura BuyLimit ordine #1 ", GetLastError());
   
   condizione1 = condizione2 = false;
   
}




void stampa(string erroreIn,int lastErrIn){

   string comment = erroreIn + IntegerToString(lastErrIn);
   Print(comment);
   
}


Based on backtesting seems that valSLBuy expression is not executed, the issue I think that the code inside the below If is not executed:

 if ( condizione1Buy() && !condizione1){
   
       condizione1 = true;
       
       EntryPointBuy = ((High[1] - Low[1]) / 2) + Low[1]; // 50% del range candela set-up 1
       
       valSLBuy = ( Low[1] - ((High[1] - Low[1]) / 2));     // Stop loss 150% sotto la candela 1 
       
       Print("valSL:", valSLBuy);
       
       valTPBuy = (High[1] - Low[1]) + High[1]; 
         
   }



Thanks a lot

Giovanni

 
   ticket1 = OrderSend(Symbol(),OP_BUYLIMIT,1,Ask,0,valSLBuy,valTPBuy," Buy ordine #1 ",0,0,clrCrimson);
You buy at the Ask and sell at the Bid. You can not place a pending order closer to the market than MODE_STOPLEVEL * _Point. Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial
 
whroeder1:
You buy at the Ask and sell at the Bid. You can not place a pending order closer to the market than MODE_STOPLEVEL * _Point. Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial


I run Mode_Stopelevel and the result is 0.0

So OpenPrice-SL ≥ StopLevel condition is true, no?

valSLBuy = ( Low[1] - ((High[1] - Low[1]) / 2)); 
 
elfunambolo OpenPrice-SL ≥ StopLevel condition is true, no?
  1. Not the SL, the open price. You are trying to open a pending order at the market.
  2. If stop level is zero that means it is floating (not constant.) I substitute twice the spread.
Reason: