Problem with order execution delays on scalping EA

 

Hello everybody,

I have just developed a symple EA which works at tick level. Functionality is very simple. It meassures price differences between a secuence o ticks (between 5 and 20 ticks, but parametrizable). When the price diference is big enough (3 pips, another parameter) the EA opens a positions with a very short SL(0-2 pips) and TP (about 10-15 pips) both parametrizable. Backtesting performs great on normal execution setup but when random delay execution is applied then it start to lose money. The same happens in a real account.

You can find the code here and a set of parameters for EURUSD. 

A question would be if MT5 is suited to develp this kiind of high scalping???? is this high frecuency and can or cannot be implemented with MT5? (Let you know I'm very confortable with MT5, I do really like would like to find the way).  A second question is related to the broker: I¡, working with ActivTrades, they give a ggod service, but again, is this broker suitable for this???? if this broker is good for this or is there any ona else better?????


Any help of suggestion on how to make it work in real if it is possible will be welcome. Of course feel free of using the code at your convenience.

All the best.

Juan  .


//+------------------------------------------------------------------+
//|                                                    botom.r1.mq5 |
//|                                                               jb |
//|                                                          http:// |
//+------------------------------------------------------------------+
#property copyright "jb"
#property link      "http://"
#property version   "1.00"
//---------------------------------------------------------------------
//      PARAMETROS EXTERNOS
//---------------------------------------------------------------------  
input ENUM_TIMEFRAMES period  = PERIOD_M5; 
int input start_h = 0; // -- Hour of start up execution for each day
int input stop_h = 25; // -- Hour of stop execution for each day
int input num_param = 1;  // Max number of positions open
double input input_lotes_1 = 0.01;
double input input_lotes_2 = 0.01;
double input input_lotes_div = 1000;
double input tick_bid_diff = 0.0009; // Price diference in ticks
double input security_sl = 0.01; // A long stop loss to be set in the broker, for security, but not for operation
double input sl = 0.0003;
double input tp = 0.0003;
int input n_t = 10; // Number of ticks to be considered
bool input longshort = true; // The EA can work on long or short positions exclusively, but not simulatenously 
//+------------------------------------------------------------------+
//|FIN PARAMETROS EXTERNOS                                     |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+

double lotes = input_lotes_1;  
string symbol = Symbol();
double entry_price = 0.0;
double sl_price = 0.0;
double sl_w_price = 0.0;
double tp_price = 0.0;

int Slippage = 65;                 
int magicx = 20120819;           
//manejador del chart
long handlex; 
MqlRates rt[10];
MqlTick last_tick, tick, tick_array[50];
// Variables de la operación
MqlTradeRequest mrequest;
MqlTradeResult mresult;
datetime t1;
MqlDateTime str_t1;
int winners = 10000;

struct positions
  {
   bool open; // array position filled = True or empty = False   
   double _tp; // take profit         
   double _sl; // stop loss         
  };
positions po[];
int c = 0;


// ************************************************************************************************
// ************************************************************************************************
// INICIO DEL CODIGO
// ************************************************************************************************
// ************************************************************************************************
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+

void manage_positions()
{
   int i;
    
         // Control de posiciones largas
      if (longshort)
      {
         
         for(i=0; i<num_param; i++) 
         {  
            if (po[i].open)
            {
               if (last_tick.bid <= po[i]._sl) 
               {
                  po[i].open = false; 
                  close_long();                                 
               }
               else 
               if (last_tick.bid > po[i]._tp) 
               {
                  po[i].open = false; 
                  close_long();                
               }
             }
          }                     
      }
      else
      {
         for(i=0; i<num_param; i++) 
         {  
            if (po[i].open)
            {
               if (last_tick.bid >= po[i]._sl) 
               {
                  po[i].open = false; 
                  close_short();                                 
               }
               else 
               if (last_tick.bid < po[i]._tp) 
               {
                  po[i].open = false; 
                  close_short();                
               }
             }
          } 
      }
                     
}




void insert_position()
{
   int i = 0;
   bool found = false;      
   while ((i < num_param) && (found==false))
   {
      if (po[i].open == false)
      {
      
         if (longshort)  open_long();  
         else open_short();
      
         po[i].open = true;
         po[i]._sl = sl_price;
         po[i]._tp = tp_price;
         found = true;                          
      }   
      i++;
   }      
};


void OnTick()
{  
   
          
   SymbolInfoTick(symbol, last_tick);   
   
   manage_positions();
   
   
   for (c=n_t; c>0; c--)   
   {
      tick_array[c] = tick_array[c-1];
      
   };
   tick_array[0] = last_tick;
   
   
   TimeToStruct(TimeCurrent(), str_t1);
   if 
   (
      (str_t1.hour >= start_h) 
      &&
      (str_t1.hour <= stop_h)
   )
   {
      if (longshort)
      {
         if ((last_tick.bid - tick_array[n_t].bid) > tick_bid_diff) 
         {
            
            insert_position();
         }         
      }
      else
      {
         if ((tick_array[n_t].bid - last_tick.bid) > tick_bid_diff) 
         {
            
            insert_position();
         }
      }
   }
};


//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {  
            
   t1 = TimeCurrent();
   // Establecemos velas japonesas
   handlex=ChartID();
   ChartSetInteger(handlex,CHART_MODE,CHART_CANDLES);
   //--- set the display mode for tick volumes
   //Establecemos par y periodo para el grafico
   ChartSetSymbolPeriod(handlex, symbol, period);
   ZeroMemory(mrequest);   
   // Calculamos el soporte y resistencia iniciales
   SymbolInfoTick(symbol, last_tick);
   // Creamos las media moviles y desviacion estandard   
   ChartRedraw(handlex);     
   winners = 100000;      
   ArrayResize(po, num_param);
   t1 = TimeCurrent(); 
   for (c=49; c>0; c--)   
   {
      tick_array[c].bid = 1000.0;
      tick_array[c].ask = -1000.0;      
   };
   return(0);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   ChartRedraw(handlex);   
  }
  
uint open_long()
{
   uint r=-1;   
      
   {
      calc_lot_size();  
      mrequest.sl = 0.0;
      mrequest.tp = 0.0;          
      mrequest.action    = TRADE_ACTION_DEAL;
      mrequest.symbol    = symbol;
      mrequest.volume    = lotes;
      mrequest.magic     = magicx;
      mrequest.deviation = Slippage; 
      mrequest.type      = ORDER_TYPE_BUY;
      mrequest.price     = SymbolInfoDouble(symbol,SYMBOL_ASK);  
      entry_price        = SymbolInfoDouble(symbol,SYMBOL_ASK); 
      sl_price           = SymbolInfoDouble(symbol,SYMBOL_BID) - sl;
      sl_w_price         = sl_price;               
      tp_price           = SymbolInfoDouble(symbol,SYMBOL_BID) + tp;          
      OrderSend(mrequest,mresult);  
      Print("Order Send: ",mresult.comment,"Reply code: ",mresult.retcode);
      r = mresult.retcode;                          
      if (PositionSelect(symbol))
      {         
         mrequest.action   = TRADE_ACTION_SLTP;
         mrequest.sl = SymbolInfoDouble(symbol,SYMBOL_BID) - security_sl;
         OrderSend(mrequest,mresult); 
         Print("OrderSend: ",mresult.comment," reply code ",mresult.retcode);  
      };                                               
    };  
    return(r);  
};

void close_long()
{  
   // Cierre de operacion de compra
   if (PositionSelect(symbol))
   {      
      mrequest.sl = 0.0;
      mrequest.tp = 0.0;      
      mrequest.action   = TRADE_ACTION_DEAL;
      mrequest.price    = SymbolInfoDouble(symbol,SYMBOL_BID);
      mrequest.symbol   = symbol;
      mrequest.volume   = lotes;
      mrequest.magic    = magicx;
      mrequest.type     = ORDER_TYPE_SELL;
      mrequest.deviation   =Slippage;                  
      OrderSend(mrequest,mresult);       
      Print("OrderSend: ",mresult.comment," reply code ",mresult.retcode);                       
   };   
};


void open_short()
{
        
      calc_lot_size(); 
      mrequest.sl = 0.0;
      mrequest.tp = 0.0;     
      mrequest.action    = TRADE_ACTION_DEAL;
      mrequest.symbol    = symbol;
      mrequest.volume    = lotes;
      mrequest.magic     = magicx;
      mrequest.deviation = Slippage; 
      mrequest.type      = ORDER_TYPE_SELL;
      mrequest.price     = SymbolInfoDouble(symbol,SYMBOL_BID); 
      entry_price        = SymbolInfoDouble(symbol,SYMBOL_BID); 
      sl_price           = SymbolInfoDouble(symbol,SYMBOL_ASK) + sl;
      sl_w_price         = sl_price;               
      tp_price           = SymbolInfoDouble(symbol,SYMBOL_ASK) - tp;       
      OrderSend(mrequest,mresult);  
      Print("OrderSend: ",mresult.comment," reply code ",mresult.retcode);
      if (PositionSelect(symbol))
      {         
         mrequest.action   = TRADE_ACTION_SLTP;
         mrequest.sl = SymbolInfoDouble(symbol,SYMBOL_ASK) + security_sl;
         OrderSend(mrequest,mresult); 
         Print("OrderSend: ",mresult.comment," reply code ",mresult.retcode); 
      };                                       
      
};

void close_short()
{   
   // Cierre de operacion de compra
   if (PositionSelect(symbol))
   {
      mrequest.sl = 0.0;
      mrequest.tp = 0.0;
      mrequest.action   = TRADE_ACTION_DEAL;
      mrequest.price    = SymbolInfoDouble(symbol,SYMBOL_ASK);
      mrequest.symbol   = symbol;
      mrequest.magic    = magicx;
      mrequest.type     = ORDER_TYPE_BUY;
      mrequest.deviation   =Slippage;
      OrderSend(mrequest,mresult);
      Print("OrderSend: ",mresult.comment," reply code ",mresult.retcode);
   };
};


/*
void calc_lot_size()
{
    lotes = input_lotes_1;
    
}
*/




double eq = 0;
void calc_lot_size()
{
   int i;
   int p=0;
   for(i=0; i<num_param; i++) 
   {  
      if (po[i].open) {p=p+1;};
   }
   if (p==0)
   {
       eq = AccountInfoDouble(ACCOUNT_EQUITY);
       lotes = input_lotes_1 + MathFloor(eq/input_lotes_div) * input_lotes_2; 
       if (lotes > 10) {lotes = 10;};
       
   }
}
Documentation on MQL5: Standard Constants, Enumerations and Structures / Environment State / Account Properties
Documentation on MQL5: Standard Constants, Enumerations and Structures / Environment State / Account Properties
  • www.mql5.com
Standard Constants, Enumerations and Structures / Environment State / Account Properties - Documentation on MQL5
Files:
 
jbru:

...

There is a similar discussion here:

Any way to reliably test expert advisors?

where a scalper signal is presented but doesn't work on real accounts
 
jlwarrior:

There is a similar discussion here:

Any way to reliably test expert advisors?

where a scalper signal is presented but doesn't work on real accounts

Thanks jlwarrior. This is what I think too...

Juan