My EA opens trade on every bar and does not obey exit condition

I developed an EA using CCI and moving average now the EA buys when the CCI crosses level 0 to the upside and the price is above moving average vice versa for sell now it exits only when CCI crosses back below the zero line. I finished it and it was working but I had two issue number one it opens a trade on every candle in any timeframe you attach it to its supposed to be one trade per time and secondly it does not obey the exit condition it’s supposed to close the trade once CCI crosses back below zero line for a buy trade even if price is still above EMA and when CCI crosses back above and price is still above EMA it takes another buy. I will drop the code here if anyone is willing to help and I will appreciate any form of help thanks

Hello johnsamy,
Better structure for an EA is first to have close condition then open conditions. You should have variable (flag) for opened position.

//±-----------------------------------------------------------------+

//| 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+StopLossPoint,Bid-TakeProfitPoint,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-StopLossPoint,Ask+TakeProfitPoint,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;

}

This is the code thanks