EA based on 3 different timeframe conditions (Code executes properly; however, there is a logical error I can't find it.)

 
//+------------------------------------------------------------------+
//|                               EA_Testing(Buy kýsmý tamamdýr).mq4 |
//|                                 Copyright 2014, Baris Yalcinkaya |
//|                                              https://www.mql4.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, Baris Yalcinkaya"
#property link      "https://www.mql4.com"
#property version   "1.00"
#property strict

extern bool BuyRule0 = true;
extern bool BuyRule1 = true;
extern bool BuyRule2 = true;
extern bool SellRule0 = true;
extern bool SellRule1 = true;
extern bool SellRule2 = true;

extern double StopLoss_Pips = 9;
//extern double TakeProfit_Pips = 36;
extern double RiskPercent = 3;
extern double Reward_Ratio = 1.5;
extern int MAX_ORDER_COUNT = 1;
extern double Lot_Size = 0;

// Trailing Stop Part
extern bool UseCandleTrailingStop = false;
extern int  PipAmountsForTrailing = 7;  
extern int  CandlesBack = 5;       

// Targets 
extern bool Target1_CCI50 = false;
extern bool Target3_4xStopLoss = true;
extern bool Target5_CCI8Candle = false;
extern bool Target6_EMAvsMVA = false;

extern int RSI_Period_W = 14;

extern int SMA_Period_D = 200;
extern int MACD_Fast_D  = 26;
extern int MACD_Slow_D  = 12;
extern int MACD_Shift_D = 9;
extern int ADX_Period_D = 14;
extern int CCI_Period_D = 14;
extern int RSI_Period_D = 14;

extern int MACD_Fast_H4  = 26;
extern int MACD_Slow_H4  = 12;
extern int MACD_Shift_H4 = 9;
extern int ADX_Period_H4 = 14;
extern int CCI_Period_H4 = 14;
extern int RSI_Period_H4 = 14;

extern int EMA_Period_H4 = 22;
extern int SMA_Period_H4 = 20;



double pips;

int BuyRule_D_MACD_status= 0, 
     BuyRule_D_RSI_status= 0, 
     BuyRule_D_DI_status= 0, 
     BuyRule_D_CCI_status= 0,
     BuyRule_D_Stoch_status = 0;

int BuyRule_H4_MACD_status = 0;
int BuyRule_H4_RSI_status = 0;
int BuyRule_H4_DI_status = 0;
int BuyRule_H4_CCI_status = 0;

int SellRule_D_MACD_status= 0, 
     SellRule_D_RSI_status= 0, 
     SellRule_D_DI_status= 0, 
     SellRule_D_CCI_status= 0,
     SellRule_D_Stoch_status = 0;

int SellRule_H4_MACD_status = 0;
int SellRule_H4_RSI_status = 0;
int SellRule_H4_DI_status = 0;
int SellRule_H4_CCI_status = 0;

int ruleIndex = 0;
int sellRuleIndex = 0;

 int ticket;
 int MagicNumber;



int init()
  {
   ruleIndex = 0;
   sellRuleIndex = 0;
   MagicNumber = 1234;
   double ticksize = MarketInfo(Symbol(), MODE_TICKSIZE);
      if(ticksize == 0.00001 || ticksize == 0.001)
      pips = ticksize*10;
      else pips = ticksize;

   return(0);
  }


int start()
{
 
 //Rule_0(Weekly) BUY Variables
 double RSI_W;
 
 //Rule_1(Daily) BUY Variables
 double SMA_D;
 
 double DIplus_D,DIminus_D;
 double CCI_Current_D, CCI_Previous_D;
 double RSI_D;
 double MACD_CurMain_D, MACD_CurSignal_D, MACD_PrevMain_D, MACD_PrevSignal_D;
 
 
 double EMA_H4, SMA_H4;
 bool BuyRule_H4_EMAvsSMA;
 bool SellRule_H4_EMAvsSMA;
 
 //Rule_2(4Hours) BUY Variables
 double MACD_CurMain_H4, MACD_CurSignal_H4, MACD_PrevMain_H4, MACD_PrevSignal_H4;
 double DIplus_H4,DIminus_H4;
 double CCI_Current_H4, CCI_Previous_H4;
 double RSI_H4;
 
 bool BuyRule_W_RSI;
 bool BuyRule_D_SMA, BuyRule_D_MACD, BuyRule_D_RSI, BuyRule_D_DI, BuyRule_D_CCI;
 bool BuyRule_H4_MACD, BuyRule_H4_RSI, BuyRule_H4_DI, BuyRule_H4_CCI;
 
 bool BuyRule_D_Stoch,SellRule_D_Stoch;
 double Stoch_CurMain_D, Stoch_CurSignal_D, Stoch_PrevMain_D, Stoch_PrevSignal_D;
 
 bool SellRule_W_RSI;
 bool SellRule_D_SMA, SellRule_D_MACD, SellRule_D_RSI, SellRule_D_DI, SellRule_D_CCI;
 bool SellRule_H4_MACD, SellRule_H4_RSI, SellRule_H4_DI, SellRule_H4_CCI;

 // For loop

 

 
   if(Bars<100)
    {
     Print("Bars less than 100");
     return(0);
    } 
//------------------------------------------------------
   //------RSI Weekly
   RSI_W=iRSI(NULL,PERIOD_W1,RSI_Period_W,PRICE_CLOSE,0);
   
   //------SMA Daily
   SMA_D=iMA(NULL,PERIOD_D1,SMA_Period_D,0,MODE_SMA,PRICE_CLOSE,0);
   //MACD Daily
   MACD_CurMain_D=iMACD(NULL,PERIOD_D1,MACD_Fast_D,MACD_Slow_D,MACD_Shift_D,PRICE_CLOSE,MODE_MAIN,0);
   MACD_CurSignal_D=iMACD(NULL,PERIOD_D1,MACD_Fast_D,MACD_Slow_D,MACD_Shift_D,PRICE_CLOSE,MODE_SIGNAL,0);
   MACD_PrevMain_D=iMACD(NULL,PERIOD_D1,MACD_Fast_D,MACD_Slow_D,MACD_Shift_D,PRICE_CLOSE,MODE_MAIN,1);
   MACD_PrevSignal_D=iMACD(NULL,PERIOD_D1,MACD_Fast_D,MACD_Slow_D,MACD_Shift_D,PRICE_CLOSE,MODE_SIGNAL,1);
   //DI+ and DI- Daily
   DIplus_D=iADX(NULL,PERIOD_D1,ADX_Period_D,PRICE_CLOSE,MODE_PLUSDI,0);
   DIminus_D=iADX(NULL,PERIOD_D1,ADX_Period_D,PRICE_CLOSE,MODE_MINUSDI,0);
   //CCI Daily
   CCI_Current_D=iCCI(NULL,PERIOD_D1,CCI_Period_D,PRICE_CLOSE,0);
   CCI_Previous_D=iCCI(NULL,PERIOD_D1,CCI_Period_D,PRICE_CLOSE,1);
   //RSI Daily
   RSI_D=iRSI(NULL,PERIOD_D1,RSI_Period_D,PRICE_CLOSE,0);
   
   
    //Stochastic Daily
   Stoch_CurMain_D=iStochastic(NULL,PERIOD_D1,5,3,3,MODE_SMA,0,MODE_MAIN,0);
   Stoch_CurSignal_D=iStochastic(NULL,PERIOD_D1,5,3,3,MODE_SMA,0,MODE_SIGNAL,0);
   Stoch_PrevMain_D=iStochastic(NULL,PERIOD_D1,5,3,3,MODE_SMA,0,MODE_MAIN,1);
   Stoch_PrevSignal_D=iStochastic(NULL,PERIOD_D1,5,3,3,MODE_SMA,0,MODE_SIGNAL,1);
   
   //-------MACD H4
   MACD_CurMain_H4=iMACD(NULL,PERIOD_H4,MACD_Fast_H4,MACD_Slow_H4,MACD_Shift_H4,PRICE_CLOSE,MODE_MAIN,0);
   MACD_CurSignal_H4=iMACD(NULL,PERIOD_H4,MACD_Fast_H4,MACD_Slow_H4,MACD_Shift_H4,PRICE_CLOSE,MODE_SIGNAL,0);
   MACD_PrevMain_H4=iMACD(NULL,PERIOD_H4,MACD_Fast_H4,MACD_Slow_H4,MACD_Shift_H4,PRICE_CLOSE,MODE_MAIN,1);
   MACD_PrevSignal_H4=iMACD(NULL,PERIOD_H4,MACD_Fast_H4,MACD_Slow_H4,MACD_Shift_H4,PRICE_CLOSE,MODE_SIGNAL,1);
   //CCI H4
   CCI_Current_H4=iCCI(NULL,PERIOD_H4,CCI_Period_H4,PRICE_CLOSE,0);
   CCI_Previous_H4=iCCI(NULL,PERIOD_H4,CCI_Period_H4,PRICE_CLOSE,1);
   //RSI H4
   RSI_H4=iRSI(NULL,PERIOD_H4,RSI_Period_H4,PRICE_CLOSE,0);
   bool RSI_Previous_H4=iRSI(NULL,PERIOD_H4,RSI_Period_H4,PRICE_CLOSE,1);
   //DI+ and DI- H4
   DIplus_H4=iADX(NULL,PERIOD_H4,ADX_Period_H4,PRICE_CLOSE,MODE_PLUSDI,0);
   DIminus_H4=iADX(NULL,PERIOD_H4,ADX_Period_H4,PRICE_CLOSE,MODE_MINUSDI,0);
   
   //EMA 22
   EMA_H4 = iMA(NULL,PERIOD_H4,EMA_Period_H4,0,MODE_EMA,PRICE_CLOSE,0);
   //SMA 20
   SMA_H4 = iMA(NULL,PERIOD_H4,SMA_Period_H4,0,MODE_SMA,PRICE_CLOSE,0);
     
     
     
   //ORDER OPENING-------------------

//BUY RULES---------------
   //Rules Weekly Options
   BuyRule_W_RSI  = (RSI_W>50);
   
   //Rules Daily Options
   BuyRule_D_SMA  = (SMA_D>200); 

   BuyRule_D_MACD = (MACD_CurMain_D<0  && MACD_CurMain_D>MACD_CurSignal_D && MACD_PrevMain_D<MACD_PrevSignal_D);
   BuyRule_D_RSI  = (RSI_D>50);
   BuyRule_D_DI   = (DIplus_D>DIminus_D);
   BuyRule_D_CCI  = ((CCI_Previous_D<CCI_Current_D)&&(CCI_Current_D<-100));
   BuyRule_D_Stoch =((Stoch_PrevMain_D<Stoch_PrevSignal_D)&&(Stoch_CurMain_D>Stoch_CurSignal_D)&&Stoch_CurMain_D<50);
 
   //Rules H4 Options
   BuyRule_H4_MACD = (MACD_CurMain_H4<0 && MACD_CurMain_H4>MACD_CurSignal_H4 && MACD_PrevMain_H4<MACD_PrevSignal_H4);
   BuyRule_H4_RSI  = (RSI_H4>50);
   bool BuyRule_Previous_H4_RSI  = (RSI_Previous_H4>50);
   
   BuyRule_H4_DI   = (DIplus_H4>DIminus_H4);
   BuyRule_H4_CCI  = ((CCI_Previous_H4<-100)&&(CCI_Current_H4>-100));

   BuyRule_H4_EMAvsSMA =(EMA_H4>SMA_H4);
   SellRule_H4_EMAvsSMA =(EMA_H4<SMA_H4);



//SELL RULES---------------
   //Rules Weekly Options
   SellRule_W_RSI = (RSI_W<50);
   //Rules Daily Options
   SellRule_D_SMA = (SMA_D<200);
   
   SellRule_D_MACD = (MACD_CurMain_D>0 && MACD_CurMain_D<MACD_CurSignal_D && MACD_PrevMain_D>MACD_PrevSignal_D);
   SellRule_D_RSI  = (RSI_D<50);
   SellRule_D_DI   = (DIplus_D<DIminus_D);
   SellRule_D_CCI  = ((CCI_Previous_D>CCI_Current_D)&&(CCI_Current_D>100));
   SellRule_D_Stoch = ((Stoch_PrevMain_D>Stoch_PrevSignal_D)&&(Stoch_CurMain_D<Stoch_CurSignal_D)&&Stoch_CurMain_D>50);
   
   //Rules H4 Options
   SellRule_H4_MACD = (MACD_CurMain_H4>0 && MACD_CurMain_H4<MACD_CurSignal_H4 && MACD_PrevMain_H4>MACD_PrevSignal_H4);
   SellRule_H4_RSI  = (RSI_H4<50);
   bool SellRule_Previous_H4_RSI  = (RSI_Previous_H4<50);
   SellRule_H4_DI   = (DIplus_H4<DIminus_H4);
   SellRule_H4_CCI  = ((CCI_Previous_H4>100)&&(CCI_Current_H4<100));
   
   
 //---------------------------------------------------------------
 
    bool buyConfirmed = false;
    bool sellConfirmed = false;
     
      if(IsNewCandle())
        {
        if(MAX_ORDER_COUNT >OrdersTotal()){
           if(ruleIndex == 0){  
               if(!BuyRule0)
                {
                 ruleIndex = 1;
                } 
               if(BuyRule_W_RSI){
                  ruleIndex = 1;
                  resetHourlyStatus();
                  resetDailyStatus();
                  Comment("Passing Rule0");
               }
               
           }
           else if(ruleIndex == 1){ 
              if(!BuyRule1){
                  ruleIndex = 2;
              }
                 
               if(BuyRule_D_SMA){
               
                  int dailyCount = 0;
                  if(isCrossed(BuyRule_D_MACD_status,BuyRule_D_MACD)){
                     dailyCount++;
                  }
                  if(isCrossed(BuyRule_D_RSI_status,BuyRule_D_RSI)){
                     dailyCount++;
                  }
                  if(isCrossed(BuyRule_D_DI_status,BuyRule_D_DI)){
                     dailyCount++;
                  }
                  if(isCrossed(BuyRule_D_CCI_status,BuyRule_D_CCI)){
                     dailyCount++;
                  }
                  if(isCrossed(BuyRule_D_Stoch_status,BuyRule_D_Stoch)){
                     dailyCount++;
                  }
                     if(dailyCount>=1){
                        Comment("Passing Rule1");
                        ruleIndex = 2;
                     }  
               }
           }
           
           else if(ruleIndex == 2){
               if(!BuyRule2){
                   ruleIndex = 0;
                   buyConfirmed = true;
               }
                     
               if(BuyRule_H4_EMAvsSMA){
                  int fourHourCount = 0;
                  
                  if(isCrossed(BuyRule_H4_MACD_status,BuyRule_H4_MACD)){
                     fourHourCount++;
                     Comment("H4_MACD");
                  }
                  if(isCrossed(BuyRule_H4_RSI_status,BuyRule_H4_RSI)){      
                     fourHourCount++;
                     Comment("H4_RSI");
                  }
                  if(isCrossed(BuyRule_H4_DI_status,BuyRule_H4_DI)){
                     fourHourCount++;
                     Comment("H4_DI");
                  }
                  if(isCrossed(BuyRule_H4_CCI_status,BuyRule_H4_CCI)){
                     fourHourCount++;
                     Comment("H4_CCI");
                  }
                  
                  if(fourHourCount >= 2){
                     ruleIndex = 0;
                     buyConfirmed = true;
                     Comment("Buy Order Opened");
                  }
               }
           }
           
           if(sellRuleIndex == 0){  
               if(!SellRule0)
                {
                 sellRuleIndex = 1;
                } 
               if(SellRule_W_RSI){
                  sellRuleIndex = 1;
                  resetSellHourlyStatus();
                  resetSellDailyStatus();
                  Comment("Passing Rule0");
               }
               
           }
           else if(sellRuleIndex == 1){ 
              if(!SellRule1){
                  sellRuleIndex = 2;
              }
                 
               if(SellRule_D_SMA){
               
                  int dailyCount = 0;
                  if(isCrossed(SellRule_D_MACD_status,SellRule_D_MACD)){
                     dailyCount++;
                  }
                  if(isCrossed(SellRule_D_RSI_status,SellRule_D_RSI)){
                     dailyCount++;
                  }
                  if(isCrossed(SellRule_D_DI_status,SellRule_D_DI)){
                     dailyCount++;
                  }
                  if(isCrossed(SellRule_D_CCI_status,SellRule_D_CCI)){
                     dailyCount++;
                  }
                  if(isCrossed(SellRule_D_Stoch_status,SellRule_D_Stoch)){
                     dailyCount++;
                  }
                     if(dailyCount>=1){
                        Comment("Passing Rule1");
                        sellRuleIndex = 2;
                     }  
               }
           }
           
           else if(sellRuleIndex == 2){
               if(!SellRule2){
                   sellRuleIndex = 0;
                   sellConfirmed = true;
               }
                     
               if(SellRule_H4_EMAvsSMA)
               {
                  int fourHourCount = 0;
                  
                  if(isCrossed(SellRule_H4_MACD_status,SellRule_H4_MACD)){
                     fourHourCount++;
                     Comment("H4_MACD");
                  }
                  if(isCrossed(SellRule_H4_RSI_status,SellRule_H4_RSI)){
                     fourHourCount++;
                     Comment("H4_RSI");
                  }
                  if(isCrossed(SellRule_H4_DI_status,SellRule_H4_DI)){
                     fourHourCount++;
                     Comment("H4_DI");
                  }
                  if(isCrossed(SellRule_H4_CCI_status,SellRule_H4_CCI)){
                     fourHourCount++;
                     Comment("H4_CCI");
                  }
                  
                  if(fourHourCount >= 2){
                     sellRuleIndex = 0;
                     sellConfirmed = true;
                     Comment("Sell Order Opened");
                  }
               }
           }
           
           
        }
        

 
     if(buyConfirmed){
         double LotSize_Buy = 0;
         double Equity_Buy = AccountEquity();
         double RiskedAmount_Buy = Equity_Buy*RiskPercent*0.01;
        
         double SL_Buy = Low[1]-StopLoss_Pips*pips;
         double SL_Buy_Loss = Bid - SL_Buy;
         double TP_Buy = Bid + (SL_Buy_Loss*Reward_Ratio);
         LotSize_Buy = (RiskedAmount_Buy/(SL_Buy_Loss/pips))/10;
         if(Lot_Size>0){
         LotSize_Buy = Lot_Size;
         }
         
         ticket = OrderSend(Symbol(),OP_BUY,LotSize_Buy,Ask,3,SL_Buy,TP_Buy,NULL,MagicNumber,0,Green);
         if(ticket>0)
          {
           if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
           Comment("BUY order opened : ",OrderOpenPrice());
          }
           else Comment("Error opening BUY order : ",GetLastError());
           return(0);
                         
      }
      if(sellConfirmed){
         double LotSize_Sell = 0;
         double Equity_Sell = AccountEquity();
         double RiskedAmount_Sell = Equity_Sell*RiskPercent*0.01;
         
         double SL_Sell = High[1]+StopLoss_Pips*pips;
         double SL_Sell_Loss = SL_Sell-Ask;
         double TP_Sell = Ask-(SL_Sell_Loss*Reward_Ratio);
         
         LotSize_Sell = (RiskedAmount_Sell/(SL_Sell_Loss/pips))/10;
         if(Lot_Size>0){
         LotSize_Sell = Lot_Size;
         }
         ticket = OrderSend(Symbol(),OP_SELL,LotSize_Sell,Bid,3,SL_Sell,TP_Sell,NULL,MagicNumber,0,Green);
         if(ticket>0)
          {
           if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
           Comment("SELL order opened : ",OrderOpenPrice());
          }
           else Comment("Error opening SELL order : ",GetLastError());
           return(0);
      
      
      
      }
      
      
      
      TrailingStop();
      
       for(int b = OrdersTotal()-1; b>=0; b--)
       {
        if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
          if(OrderMagicNumber() == MagicNumber)
            if(OrderSymbol() == Symbol())
              if(OrderType() == OP_BUY){
                  
                  int shift = iBarShift(NULL, PERIOD_H4, OrderOpenTime());
                  double openingCCI = iCCI(NULL,PERIOD_H4,CCI_Period_H4, PRICE_CLOSE, shift);
                  // target 1
                  if(openingCCI > 50  && CCI_Current_H4 < 50){
                     OrderClose(OrderTicket(), OrderLots(), Bid, 3);
                     Comment("Target 1 reached");
                     Alert("Target 1");
                  }
                  else
                     {
                      
                        // target 5 ( last 8 cci > 100 & cci < 100)
                        bool notSatisfied = false;
                        for(int i = 1; i <= 8; i++){
                           double cci = iCCI(NULL,PERIOD_H4,CCI_Period_H4,PRICE_CLOSE,i);
                           if(cci < 100){
                              notSatisfied = true;
                           }
                        }
                        if(!notSatisfied){
                           double cci = iCCI(NULL,PERIOD_H4,CCI_Period_H4,PRICE_CLOSE,0);
                           if(cci < 100){
                              OrderClose(OrderTicket(), OrderLots(), Bid, 3);
                              Comment("Target 5 reached");
                              Alert("Target 5");
                           }         
                           else{
                              notSatisfied = true;
                           }
                        }
                        if(notSatisfied){
                           // target 6 ( rule 2 reversed)
                           if(!BuyRule_H4_EMAvsSMA){
                              int ruleCount = 0;
                              if(SellRule_H4_MACD || SellRule_H4_RSI || SellRule_H4_CCI || SellRule_H4_DI){
                                 OrderClose(OrderTicket(), OrderLots(), Bid, 3);
                                 Comment("Target 6 reached");
                                 Alert("Target 6");
                              }  
                           }
                        }
                     }
                  }
                  else if(OrderType() == OP_SELL){
                      int shift = iBarShift(NULL, PERIOD_H4, OrderOpenTime());
                     double openingCCI = iCCI(NULL,PERIOD_H4,CCI_Period_H4, PRICE_CLOSE, shift);
                     // target 1
                     if(openingCCI < -50  && CCI_Current_H4 > -50){
                        OrderClose(OrderTicket(), OrderLots(), Ask, 3);
                        Comment("Target 1 reached");
                        Alert("Target 1");
                     }
                     else
                        {
                         
                           // target 5 ( last 8 cci > 100 & cci < 100)
                           bool notSatisfied = false;
                           for(int i = 1; i <= 8; i++){
                              double cci = iCCI(NULL,PERIOD_H4,CCI_Period_H4,PRICE_CLOSE,i);
                              if(cci > -100){
                                 notSatisfied = true;
                              }
                           }
                           if(!notSatisfied){
                              double cci = iCCI(NULL,PERIOD_H4,CCI_Period_H4,PRICE_CLOSE,0);
                              if(cci > -100){
                                 OrderClose(OrderTicket(), OrderLots(), Ask, 3);
                                 Comment("Target 5 reached");
                                 Alert("Target 5");
                              }         
                              else{
                                 notSatisfied = true;
                              }
                           }
                           if(notSatisfied){
                              // target 6 ( rule 2 reversed)
                              if(!SellRule_H4_EMAvsSMA){
                                 int ruleCount = 0;
                                 if(BuyRule_H4_MACD || BuyRule_H4_RSI || BuyRule_H4_CCI || BuyRule_H4_DI){
                                    OrderClose(OrderTicket(), OrderLots(), Ask, 3);
                                    Comment("Target 6 reached");
                                    Alert("Target 6");
                                 }  
                              }
                           }
                        }
                    }
              }
              
     }
     

   
    return(0);
}  // Start()
bool isCrossed(int& status, bool condition){
   if(status == 0){
      if(!condition){
         status = 1;
      }
   }
   else if(status == 1){
      if(condition){
         status = 2;
      }
   }
   return condition && status >= 2;
}

void resetDailyStatus(){
    BuyRule_D_MACD_status= 0;
    BuyRule_D_RSI_status= 0;
    BuyRule_D_DI_status= 0;
    BuyRule_D_CCI_status= 0;
    BuyRule_D_Stoch_status = 0;
}

void resetHourlyStatus(){
    BuyRule_H4_MACD_status = 0;
    BuyRule_H4_RSI_status = 0;
    BuyRule_H4_DI_status = 0;
    BuyRule_H4_CCI_status = 0;
}
void resetSellDailyStatus(){
    SellRule_D_MACD_status= 0;
    SellRule_D_RSI_status= 0;
    SellRule_D_DI_status= 0;
    SellRule_D_CCI_status= 0;
    SellRule_D_Stoch_status = 0;
}

void resetSellHourlyStatus(){
    SellRule_H4_MACD_status = 0;
    SellRule_H4_RSI_status = 0;
    SellRule_H4_DI_status = 0;
    SellRule_H4_CCI_status = 0;
}

bool IsNewCandle()
{
 static int  BarsOnChart = 0;
 if(Bars == BarsOnChart)
 return(false);
 BarsOnChart = Bars;
 return(true);
} 


 
void TrailingStop()
{

   if(!UseCandleTrailingStop){
      return;
   }
 int BuyStopCandle  = iLowest(NULL,0,MODE_LOW,CandlesBack,0);
 int SellStopCandle = iHighest(NULL,0,MODE_HIGH,CandlesBack,0);

 for(int b = OrdersTotal()-1; b>=0; b--)
 {
  if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
    if(OrderMagicNumber() == MagicNumber)
      if(OrderSymbol() == Symbol())
        if(OrderType() == OP_BUY)
          
          if(OrderStopLoss()<Low[BuyStopCandle]-PipAmountsForTrailing*pips)
             OrderModify(OrderTicket(),OrderOpenPrice(),
               Low[BuyStopCandle]-PipAmountsForTrailing*pips,OrderTakeProfit(),0,CLR_NONE);
 }
 

 for(int s = OrdersTotal()-1; s>=0; s--)
 {
  if(OrderSelect(s,SELECT_BY_POS,MODE_TRADES))
    if(OrderMagicNumber()== MagicNumber)
      if(OrderSymbol() == Symbol())
        if(OrderType() == OP_SELL)
          
          if(OrderStopLoss()>High[SellStopCandle]+PipAmountsForTrailing*pips)
             OrderModify(OrderTicket(),OrderOpenPrice(),
             High[SellStopCandle]+PipAmountsForTrailing*pips,OrderTakeProfit(),0,CLR_NONE);         
 }          

}  // Trailing Stop Closing        

NOTE: I should divide the code in two pieces.

Hello,

Can anybody help me in this code? I think, indicators don't work properly. I tried to write correctly; however, probably the part related with passing the next rule may be wrong.
I tried to write an EA ruled by conditions attached below.

Rule0: Weekly RSI>50
Rule1: Daily Price >SMA200 and also at least one of them below:
*Daily RSI>50
*Daily DI+>DI-
*MACD crossed upwards
*CCI just crossover -100
*Stochastic crossed upwards and also Stoch D >50
Rule2: 4H EMA22 > 4H SMA20 and also at least two of them should be true:
*4H RSI >50
*4H MACD crossed upwards
*4H DI+>DI-
*4H CCI > -100

*** The main issue in there is for example the second trade cannot open if indicators don't turn to their deault positions. (Like after the first trade, RSI should be down to 50 and then cross upward the 50 level again.)

Target 1: If the position opened above 50, it should be closed when it is below 50.
Target 3: 1.5*StopLoss
Target 5: If 4H CCI is above 100 through 8 candles, it should be closed when it crosses the 100 downward.
Target 6: Opposite Rule 2 conditions (4H EMA22 < 4H SMA 20 and at least two of them give opposite direction signal.

Thank you for your interest,

Cheers,

Baris

Reason: