Delay in Performing an Order

 

Hi friends,

I have started programming in MQL4 and started with a simple EA as an example to test it out. I am checking the opening values of the bars in the EUR/USD chart, and did a simple 1 BUY order when the OPEN value of a BAR is more than 1.35. I tested it with back testing in MetaTrader and noticed that the buy order delays by 1 timeframe i.e. the order is placed on the next consecutive opening of the bar (at least that is shown on the chart). I changed the timeframe but still does the same, so for example, if I run it with a 1 Minute timeframe the order comes 1 minute after the signal, and if I run it with 1 Day timeframe the order comes 1 day after the signal, which is not good. Is this delay something normal? Have somebody had the same problem?

 
jonaber:

Hi friends,

I have started programming in MQL4 and started with a simple EA as an example to test it out. I am checking the opening values of the bars in the EUR/USD chart, and did a simple 1 BUY order when the OPEN value of a BAR is more than 1.35. I tested it with back testing in MetaTrader and noticed that the buy order delays by 1 timeframe i.e. the order is placed on the next consecutive opening of the bar (at least that is shown on the chart). I changed the timeframe but still does the same, so for example, if I run it with a 1 Minute timeframe the order comes 1 minute after the signal, and if I run it with 1 Day timeframe the order comes 1 day after the signal, which is not good. Is this delay something normal? Have somebody had the same problem?


It is how your program is coded....

It is not my problem to code EA opens at bar i want

 
deVries:


It is how your program is coded....

It is not my problem to code EA opens at bar i want


So in normal cases the order is set immediately after there is a signal? and you can see it visually in the back testing chart?
 
jonaber:

So in normal cases the order is set immediately after there is a signal? and you can see it visually in the back testing chart?
If you want to use a signal on the current bar there is a good chance that that signal will go on and off during the formation of the bar . . . so the most recent closed bar you can use is bar 1, on a M1 chart the signal was set at the start of the current bar not 60 seconds ago . . . on a D1 chart the signal was set at the start of the current bar, not 1 day ago . . .
 
jonaber:

So in normal cases the order is set immediately after there is a signal? and you can see it visually in the back testing chart?

//+------------------------------------------------------------------+
//|                                                   ExampleOne.mq4 |
//|                               Copyright © 2013,  Tjipke de Vries |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2013,  Tjipke de Vries"
#property link      ""

int    MagicNumber = 123;
int    Slippage.Pips = 3;
int    EATrades,BUYTrades,SELLTrades;


//++++ These are adjusted for 5 digit brokers.
int     pips2points;      // slippage  3 pips    3=points    30=points
double  pips2dbl;         // Stoploss 15 pips    0.015      0.0150
int     Digits.pips;      // DoubleToStr(dbl/pips2dbl, Digits.pips)
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   if(Digits % 2 == 1)  // DE30=1/JPY=3/EURUSD=5 forum.mql4.com/43064#515262
     {pips2dbl = Point*10; pips2points = 10;   Digits.pips = 1;}
     else {pips2dbl = Point;    pips2points =  1;   Digits.pips = 0;}
     // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
  Comment(""); 
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
  static datetime opentimelastmarket = 0;  
  
//----
  if(EATrades != OrdersTotal())
    {
    BUYTrades = 0;
    SELLTrades = 0;
    EATrades = OrdersTotal(); //reset counting trades
    for(int i= OrdersTotal()-1; i>=0 ; i--) //count down if you check trades !!!
       { 
        if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)break;
        if(OrderSymbol()!=Symbol() ||OrderMagicNumber()!= MagicNumber ||OrderType()>1) continue;
        if(OrderOpenTime() > opentimelastmarket)opentimelastmarket = OrderOpenTime();
        if(OrderType()==OP_BUY)
          {
          BUYTrades++;         
          }
        if(OrderType()==OP_SELL)
          {
          SELLTrades++;
          }
       }
    }   
//---- end closing check open trades
   Comment("BUYTRADES  ",BUYTrades,"    SELLTRADES  ",SELLTrades);
   if(opentimelastmarket >= Time[0])return(0);

   if(Open[0] > 1.35)OrderSend(Symbol(),OP_BUY,0.1,Ask,Slippage.Pips * pips2points,0,0,"ExampleOneBuy",MagicNumber,0,Blue);
   if(Open[0] < 1.35)OrderSend(Symbol(),OP_SELL,0.1,Bid,Slippage.Pips * pips2points,0,0,"ExampleOneSell",MagicNumber,0,Red);


   
//----
   return(0);
  }
//+------------------------------------------------------------------+
You can test this.....
 
deVries:

You can test this.....

Yes... it actually worked... thanks for your help :)
Reason: