GAP Detection for EA

 

Hello,

I want to add a gap-detection to my EA (MetaTrader 4) and am not sure how to exactly do it. The EA is very simply and compared prices X minutes ago with the current price and then trades. However, after weekend there are of course gaps in the chart, but my EA thinks it´s a breakout and opens a trade. Also, if the server of my broker loses connection for a few minutes, those gaps can also occure and my EA again thinks, when re-connected, a breakout has occured. How can I stop that? Would really like to see some code posted. My idea was something like comparing the current (Server) minute with the previous (Server) minute and if there is a difference of the current (Server) minute and previous (Server) minute +1, return(0). However, when the hour changes, this won´t work (59 to 00). Thanks in advance for any help.

 
geektrader:

Hello,

I want to add a gap-detection to my EA (MetaTrader 4) and am not sure how to exactly do it. The EA is very simply and compared prices X minutes ago with the current price and then trades. However, after weekend there are of course gaps in the chart, but my EA thinks it´s a breakout and opens a trade. Also, if the server of my broker loses connection for a few minutes, those gaps can also occure and my EA again thinks, when re-connected, a breakout has occured. How can I stop that? Would really like to see some code posted. My idea was something like comparing the current (Server) minute with the previous (Server) minute and if there is a difference of the current (Server) minute and previous (Server) minute +1, return(0). However, when the hour changes, this won´t work (59 to 00). Thanks in advance for any help.


datetime current_time=iTime(Symbol(), 1, 0);
datetime past_time=iTime(Symbol(),1,1);

  if((current_time-past_time)>.......){return(0);}
 

Indeed, that´s it already! :) Thanks a lot! I am now using:


if (iTime(Symbol(), 1, 0) - iTime(Symbol(), 1, MathMax(Close_Shift,Close_Shift2)) > MathMax(Close_Shift,Close_Shift2)*60) return(0);


This way I can also determine how far back it should check basing on the input variables from my EA.

 
geektrader:
those gaps can also occure and my EA again thinks, when re-connected, a breakout has occured. How can I stop that?
Detect the missing bars and where the last gap occurred.
extern  int Gap.Min.Pips = 10;
int init(){
    OnInitGapDetect();
    :
}

int start(){
    int iGap = iBarShift(NULL,0, OnTickGapDetect());
    :
}
#define GD_LOOK_BACK 1
int GDbarsCounted;  void OnInitGapDetect(){ GDbarsCounted = GD_LOOK_BACK;   }
datetime OnTickGapDetect(){
    double  gapMin = Gap.Min.Pips * pips2dbl;
    static datetime timeLastGap;
    for(int iBar = Bars - 1 - GDbarsCounted; iBar >= 0; iBar--){
        double  gapSize = MathAbs( Close[iBar+GD_LOOK_BACK] - Open[iBar] );
        if (gapSize >= gapMin){ timeLastGap = Time[iBar]; }
    }
    GDbarsCounted = Bars;   // Looked at all
    return(timeLastGap);
}
Typed, not compiled, not tested.
 
And what do you consider a gap? Bars close and open with 1-10 gaps often.
Reason: