expert advisor sometimes doesn't apply sl and take profit

Giorgos Cut  

Hey i have been experimenting with mql5 and I noticed during backtesting that sometimes some of my orders enter without stop loss or take profit. In the code i have posted is shown how a request is made, and the "Low" and "High" veriables returned from a method i call that returns a double value. 

I am not sure some of the orders enter without a sl and tp and some do, since the if statements do work.

//create order request
         request.type = ORDER_TYPE_SELL;
         request.action = TRADE_ACTION_DEAL;
         request.deviation = 20;
         request.symbol = _Symbol;
         request.volume = 0.25;
         request.price    =SymbolInfoDouble(Symbol(),SYMBOL_BID);
         request.type_filling = ORDER_FILLING_FOK;
         
         //now to set stop loss and take profit we check current price
         //and compare to latest high and low
         
         if(Low < SymbolInfoDouble(_Symbol, SYMBOL_BID) + SymbolInfoInteger(Symbol(), SYMBOL_SPREAD) + 0.001){
            request.tp = Low;
         }else{
            request.tp = SymbolInfoDouble(_Symbol, SYMBOL_BID) - SymbolInfoInteger(Symbol(), SYMBOL_SPREAD) - 0.0012;
         }
         
         if(High > SymbolInfoDouble(_Symbol, SYMBOL_ASK) - SymbolInfoInteger(Symbol(), SYMBOL_SPREAD) - 0.001){
           request.sl = High;
         }else{
            request.sl = SymbolInfoDouble(_Symbol, SYMBOL_ASK) + SymbolInfoInteger(Symbol(), SYMBOL_SPREAD) + 0.0012;
         }

         
         //place order
         if(!OrderSend(request,result))
            PrintFormat("OrderSend error %d",GetLastError());


Lorentzos Roussos  
Giorgos Cut:

Hey i have been experimenting with mql5 and I noticed during backtesting that sometimes some of my orders enter without stop loss or take profit. In the code i have posted is shown how a request is made, and the "Low" and "High" veriables returned from a method i call that returns a double value. 

I am not sure some of the orders enter without a sl and tp and some do, since the if statements do work.


Hello , are you referring to the history tab of orders where some of them appear without sl or tp ? If yes then these are the closing of previous orders which print as deals in the opposite direction.

Other than that , the spread is in points which means it should be turned to price by multiplication with _Point , and , there is an additional possible limitation with the stop level of each broker.

Also the filling mode must be checked -if i recall correctly and it has not been updated - 

Here is an example that places both sl and tp . 

int tr=0;
datetime barstamp=0;
int OnInit()
  {
//---
  tr=0;
  barstamp=0;
//---
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
//create order request
  
  if(iTime(_Symbol,_Period,0)>barstamp)
    {
    tr++;
    barstamp=iTime(_Symbol,_Period,0);
    }
  if(tr==6){
  double Low=iLow(_Symbol,_Period,3);
  double High=iHigh(_Symbol,_Period,3);
  MqlTradeRequest request={};
  MqlTradeResult result={};
     
        request.type = ORDER_TYPE_SELL;
        request.action = TRADE_ACTION_DEAL;
        request.deviation = 20;
        request.symbol = _Symbol;
        request.volume = 0.25;
        request.price    =SymbolInfoDouble(Symbol(),SYMBOL_BID);
        request.type_filling = GetFillingA(_Symbol);
         
         /*
         you also need to anticipate a stop level which is the minimum "proximity" a limit order
         can have with the current price
         So let's construct the lowest possible sl 
         The broker will return a 0 if this is off or a # in points .
         That number is the minimum so if we anticipate requotes we will need 
              to stretch that "buffer zone" This is what the multiple below does
              Also i think you want the stop to be as big as the spread so 
              we will pick the biggest of the 2 , the stop level or the spread 
         */
         double _enforced_extention=1.4;
         double buffer_zone=_Point*MathMax((((double)SymbolInfoInteger(_Symbol,SYMBOL_TRADE_STOPS_LEVEL))*_enforced_extention),((double)SymbolInfoInteger(_Symbol,SYMBOL_SPREAD)));
         //so the lowest possible SL for a sell is 
         double lowest_possible_SL=SymbolInfoDouble(_Symbol,SYMBOL_ASK)+buffer_zone;
         //and then the highest possible TP , again for sells only
         double highest_possible_TP=SymbolInfoDouble(_Symbol,SYMBOL_BID)-buffer_zone;

         if(Low < highest_possible_TP){
            request.tp = Low;
         }else{
            request.tp = highest_possible_TP;
         }
         
         if(High > lowest_possible_SL){
           request.sl = High;
         }else{
            request.sl = lowest_possible_SL;
         }

         
         //place order
         if(!OrderSend(request,result))
            PrintFormat("OrderSend error %d",GetLastError());   
  tr=0;
  }
  }
ENUM_ORDER_TYPE_FILLING GetFillingA( const string Symb, const uint Type = ORDER_FILLING_FOK )
{
  const ENUM_SYMBOL_TRADE_EXECUTION ExeMode = (ENUM_SYMBOL_TRADE_EXECUTION)::SymbolInfoInteger(Symb, SYMBOL_TRADE_EXEMODE);
  const int FillingMode = (int)::SymbolInfoInteger(Symb, SYMBOL_FILLING_MODE);

  return((FillingMode == 0 || (Type >= ORDER_FILLING_RETURN) || ((FillingMode & (Type + 1)) != Type + 1)) ?
         (((ExeMode == SYMBOL_TRADE_EXECUTION_EXCHANGE) || (ExeMode == SYMBOL_TRADE_EXECUTION_INSTANT)) ?
           ORDER_FILLING_RETURN : ((FillingMode == SYMBOL_FILLING_IOC) ? ORDER_FILLING_IOC : ORDER_FILLING_FOK)) :
          (ENUM_ORDER_TYPE_FILLING)Type);
}

Giorgos Cut  
Lorentzos Roussos #:

Hello , are you referring to the history tab of orders where some of them appear without sl or tp ? If yes then these are the closing of previous orders which print as deals in the opposite direction.

Other than that , the spread is in points which means it should be turned to price by multiplication with _Point , and , there is an additional possible limitation with the stop level of each broker.

Also the filling mode must be checked -if i recall correctly and it has not been updated - 

Here is an example that places both sl and tp . 

Thanks alot for helping! :) 
Reason: