My EA opens trade on every bar

 

hello guys please i need your help, i developed this EA that uses CCI and moving average filter it was all good but my problem now is that it opens trade on every bar for example if you place it on a 1H chart it will open trade on every 1 hour. please how do i prevent this from happening. Let me explain the EA, if the CCI crosses the zero level upwards and the price is above the EMA it buys vice versa for sell this is the code.


//+------------------------------------------------------------------+

//|                                                     vfx_demo.mq4 |

//|                                                       obj emmanuel |

//|                                                |

//+------------------------------------------------------------------+

#property copyright "obj Emmanuel"



#property version   "1.05"





int _Sell = -1;

int _Buy = 1;



input int   MovingPeriod  =12;

input int   MovingShift   =0;

input int   CciPeriod   =14;



extern int TakeProfit = 250;

extern int StopLoss = 150;





input double Lots = 1;

int nMagic = 369853;

int LastOpenTime = 0;



int OnInit(){

  

   return(INIT_SUCCEEDED);

}



void OnDeinit(const int reason) {

  

}







void OnTick()

 {

  int signal = signal_cci(); 

   

  if (!CheckFilters(signal) && isUseFilters) return;

    

  if (isTradeAllowed()){

         

      if(signal== _Buy) {

         buy(); 

      }

      if(signal== _Sell){

         sell();

      }         

  }

}



int signal_cci() { // ---------------------------------- CCI crossing 100 level - buy; crossing -100 level - sell

   double x1,x2;

   

   x1 = iCCI(Symbol(),_Period,14,PRICE_TYPICAL,0);

   x2 = iCCI(Symbol(),_Period,14,PRICE_TYPICAL,1);

  

  

  if(x1<0) return _Sell;

  if(x2>0) return _Buy;

   return 0;   

}









extern bool isUseFilters = true;

int CheckFilters (int signal) { //true - you can trade

if(signal==_Buy) {

return iMA(NULL,0,MovingPeriod,MovingShift,MODE_EMA,PRICE_TYPICAL,0); }

if(signal==_Sell) {

return iMA(NULL,0,MovingPeriod,MovingShift,MODE_EMA,PRICE_TYPICAL,0); }               

                                              

 return true;

}



 //--------------------------------isTradenAllowed--------------------------------







bool isTradeAllowed() { 

   if (LastOpenTime>=Time[0] - Period()*60) return false; // Do not open order twice on the same bar



   return true;

}





bool sell() {//------------------------------sell----------------------------------

  

   string comment ="";

   

   int ticket = OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+StopLoss*Point,Bid-TakeProfit*Point,comment,nMagic);

   

   if(ticket<0){

     printf("Open SELL order error (%i) | Ask = %g, sl=%g, tp=%g",

            GetLastError(),Bid,3,Bid+StopLoss*Point,Bid-TakeProfit*Point); 

     return false;

   } else {

      LastOpenTime = Time[0];

    

   }

     return true;

}



bool buy( ){ // ---------------------------buy--------------------------------------

   string comment = "";

   int ticket = OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,comment,nMagic);

   

   if(ticket<0) {

      printf("Open BUY order error(%i) | Ask = %i, sl=%g, tp=%g",

            GetLastError(),Ask,Ask-StopLoss*Point,Ask+TakeProfit*Point); 

      return false;

   } else {

      LastOpenTime = Time[0];

   }

     return true;

}



thanks guys

 
Sorry but I can't be bothered to read the code with all those spaces.
 
objemmanuel1997:

hello guys please i need your help, i developed this EA that uses CCI and moving average filter it was all good but my problem now is that it opens trade on every bar for example if you place it on a 1H chart it will open trade on every 1 hour. please how do i prevent this from happening. Let me explain the EA, if the CCI crosses the zero level upwards and the price is above the EMA it buys vice versa for sell this is the code.




thanks guys

It might be better to know when there is a new bar, and if not a new bar, don't do anything. Something like this.

void OnTick(){
static datetime oldbartime=Time[0];
if(oldbartime==Time[0])return;

  // your code //

oldbartime=Time[0];
}

But if the EA accesses other periods or symbols off a "host" chart, then there's other stuff you have to do..

 
  1. Why are you checking bar zero for a buy but bar one for a sell?

  2. Add a new bar check, as suggested.

    For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.

    I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
              New candle - MQL4 programming forum

  3. You are looking at a signal. Act on a change of signal.
              MQL4 (in Strategy Tester) - double testing of entry conditions - Strategy Tester - Expert Advisors and Automated Trading - MQL5 programming forum #1

Reason: