Multiple if statement, else code is not executed

 

Hello experts,

I have a code that includes multiple IF statements (if inside if) however I have an issue when one of the conditions is not met, the related else statement is not executed...no errors received while compiling the EA.

Please advise about the correct syntax & if anything is missing in the code.

I have already tried with placing {} with each IF & else statements but no difference.

Looking forward for your support.

USE_INDICATOR-X_FOR_ENTRY = false;

void OnTick()
{
//---
   //Check if it is a new candle, need the code to run every new candle instead of OnTick
   if (LastActionTime != Time[0])
      
      //Confirm no active orders for this pair, if no active orders then will check for signals
      if(orderCount == 0)
      {
         //start checking for signal
         Signal(); //check for signals, updates gosell and gobuy to 1 if signal is found
         
         if(gobuy == 1) //if a Buy signal was found
         {
            openPrice = Open[0];
            indicatorX_down = iCustom(_Symbol,PERIOD_CURRENT,"indicator-x",2,0);
            Print("open price: ", openPrice, " - Indicator-X down: ", indicatorX_down);
            if(SIGNAL_TYPE == 0 || SIGNAL_TYPE == 2) //means BUY signals are allowed by the variable SIGNAL_TYPE
               if(USE_INDICATOR-X_FOR_ENTRY)  //if a specific indicator "the flag USE_INDICATOR-X_FOR_ENTRY" is used to confirm entry in position
                  if(openPrice < indicatorX_down)
                  {
                     datetime time = iTime(NULL,0,1);
                     Print("Indicator-X Buy Entry condition met");
                     if(PushAlert)SendNotification(Symbol()+" BUY @  Hour "+string(Hour())+"  Minute "+string(Minute()));
                     if(EmailOn)SendMail("PC BUY SIGNAL - "+ChartSymbol(0)+" "+string(_Period),string(time)+" - Buy - "+ChartSymbol(0)+"\r\n");
                     orderCount +=1;
                     inBuy=true;
                     Print("In buy: ", IntegerToString(inBuy));
                  }
                  else{
                     Print("Indicator-X Buy Entry condition not met");
                  }
               else
               {
                  Print("Indicator-x condition not used");  //////THIS PART OF CODE IS NOT EXECUTED ALTHOUGH FLAG USE_INDICATOR-X_FOR ENTRY IS FALSE///////
                  datetime time = iTime(NULL,0,1);
                  if(PushAlert)SendNotification(Symbol()+" BUY @  Hour "+string(Hour())+"  Minute "+string(Minute()));
                  if(EmailOn)SendMail("PC BUY SIGNAL - "+ChartSymbol(0)+" "+string(_Period),string(time)+" - Buy - "+ChartSymbol(0)+"\r\n");
                  orderCount +=1;
                  inBuy=true;
                  Print("In buy: ", IntegerToString(inBuy));
               }
         }
         
         if(gosell == 1) //if a sell signal was found
         {
           openPrice = Open[0];
           indicatorX_up = iCustom(_Symbol,PERIOD_CURRENT,"indicator-x",1,0);
           Print("open price: ", openPrice, " - Indicator-x up: ",  indicatorX_up);
           if(SIGNAL_TYPE == 1 || SIGNAL_TYPE == 2) //means that SELL signals are allowed by the variable SIGNAL_TYPE
              if(USE_INDICATOR-X_FOR_ENTRY)
                  if(openPrice > indicatorX_up)
                  {
                     datetime time = iTime(NULL,0,1);
                     Print("Indicator-x Sell Entry condition met");
                     if(PushAlert)Alert(Symbol()," SELL @  Hour ",string(Hour()),"  Minute ",string(Minute()));
                     if(EmailOn)SendMail("PC SELL SIGNAL - "+ChartSymbol(0)+" "+string(_Period),string(time)+" - Sell - "+ChartSymbol(0)+"\r\n");
                     orderCount +=1;
                     inSell=true;
                     Print("In Sell: ", IntegerToString(inSell));
                  }
                  else
                     Print("Indicator-x sell Entry condition not met");
               else
               {
                  Print("Indicator-x flag not used");
                  datetime time = iTime(NULL,0,1);
                  if(PushAlert)Alert(Symbol()," SELL @  Hour ",string(Hour()),"  Minute ",string(Minute()));
                  if(EmailOn)SendMail("PC SELL SIGNAL - "+ChartSymbol(0)+" "+string(_Period),string(time)+" - Sell - "+ChartSymbol(0)+"\r\n");
                  orderCount +=1;
                  inSell=true;
                  Print("In Sell: ", IntegerToString(inSell));
               }
         }
         Print("In buy after signal: ", IntegerToString(inBuy)); //All print statements are placed for debugging purposes
         Print("In Sell after signal: ", IntegerToString(inSell));
      }
      else orderCloseCheck(); //if there is active order i.e. order count !=0 then check if closure criteria is met
      LastActionTime = Time[0];
      
}
 

When in doubt, it is best to use more brackets or braces even if you believe them to be redundant. Better to be safe than sorry!

Please note that the code is not compilable, so I am not sure if I balanced every single brace or not, but it should help to identify each block more easily.

void OnTick()
{
//---
   //Check if it is a new candle, need the code to run every new candle instead of OnTick
   if(LastActionTime != Time[0])
   {
      //Confirm no active orders for this pair, if no active orders then will check for signals
      if(orderCount == 0)
      {
         //start checking for signal
         Signal(); //check for signals, updates gosell and gobuy to 1 if signal is found
         
         if(gobuy == 1) //if a Buy signal was found
         {
            openPrice = Open[0];
            indicatorX_down = iCustom(_Symbol,PERIOD_CURRENT,"indicator-x",2,0);
            Print("open price: ", openPrice, " - Indicator-X down: ", indicatorX_down);

            if(SIGNAL_TYPE == 0 || SIGNAL_TYPE == 2) //means BUY signals are allowed by the variable SIGNAL_TYPE
            {
               if(USE_INDICATOR-X_FOR_ENTRY)  //if a specific indicator "the flag USE_INDICATOR-X_FOR_ENTRY" is used to confirm entry in position
               {
                  if(openPrice < indicatorX_down)
                  {
                     datetime time = iTime(NULL,0,1);
                     Print("Indicator-X Buy Entry condition met");
                     if(PushAlert)SendNotification(Symbol()+" BUY @  Hour "+string(Hour())+"  Minute "+string(Minute()));
                     if(EmailOn)SendMail("PC BUY SIGNAL - "+ChartSymbol(0)+" "+string(_Period),string(time)+" - Buy - "+ChartSymbol(0)+"\r\n");
                     orderCount +=1;
                     inBuy=true;
                     Print("In buy: ", IntegerToString(inBuy));
                  }
                  else
                     Print("Indicator-X Buy Entry condition not met");
               }  
               else
               {
                  Print("Indicator-x condition not used");  //////THIS PART OF CODE IS NOT EXECUTED ALTHOUGH FLAG USE_INDICATOR-X_FOR ENTRY IS FALSE///////
                  datetime time = iTime(NULL,0,1);
                  if(PushAlert) SendNotification(Symbol()+" BUY @  Hour "+string(Hour())+"  Minute "+string(Minute()));
                  if(EmailOn) SendMail("PC BUY SIGNAL - "+ChartSymbol(0)+" "+string(_Period),string(time)+" - Buy - "+ChartSymbol(0)+"\r\n");
                  orderCount +=1;
                  inBuy=true;
                  Print("In buy: ", IntegerToString(inBuy));
               }
            }
         }
         
         if(gosell == 1) //if a sell signal was found
         {
            openPrice = Open[0];
            indicatorX_up = iCustom(_Symbol,PERIOD_CURRENT,"indicator-x",1,0);
            Print("open price: ", openPrice, " - Indicator-x up: ",  indicatorX_up);
            if(SIGNAL_TYPE == 1 || SIGNAL_TYPE == 2) //means that SELL signals are allowed by the variable SIGNAL_TYPE
            {
               if(USE_INDICATOR-X_FOR_ENTRY)
               {
                  if(openPrice > indicatorX_up)
                  {
                     datetime time = iTime(NULL,0,1);
                     Print("Indicator-x Sell Entry condition met");
                     if(PushAlert)Alert(Symbol()," SELL @  Hour ",string(Hour()),"  Minute ",string(Minute()));
                     if(EmailOn)SendMail("PC SELL SIGNAL - "+ChartSymbol(0)+" "+string(_Period),string(time)+" - Sell - "+ChartSymbol(0)+"\r\n");
                     orderCount +=1;
                     inSell=true;
                     Print("In Sell: ", IntegerToString(inSell));
                  }
                  else
                     Print("Indicator-x sell Entry condition not met");
               }      
               else
               {
                  Print("Indicator-x flag not used");
                  datetime time = iTime(NULL,0,1);
                  if(PushAlert)Alert(Symbol()," SELL @  Hour ",string(Hour()),"  Minute ",string(Minute()));
                  if(EmailOn)SendMail("PC SELL SIGNAL - "+ChartSymbol(0)+" "+string(_Period),string(time)+" - Sell - "+ChartSymbol(0)+"\r\n");
                  orderCount +=1;
                  inSell=true;
                  Print("In Sell: ", IntegerToString(inSell));
               }
            }
         }

         Print("In buy after signal: ", IntegerToString(inBuy)); //All print statements are placed for debugging purposes
         Print("In Sell after signal: ", IntegerToString(inSell));
      }
      else
         orderCloseCheck(); //if there is active order i.e. order count !=0 then check if closure criteria is met

      LastActionTime = Time[0];
   }   
}
 

 the related else statement is not executed...

Which one? 
 
Fernando Carreiro #:

When in doubt, it is best to use more brackets or braces even if you believe them to be redundant. Better to be safe than sorry!

Please note that the code is not compilable, so I am not sure if I balanced every single brace or not, but it should help to identify each block more easily.

Thanks Fernando, this is actually what i was doing but it was not working....i will check yours and see if all will be good.

 
Daniel Cioca #:
Which one? 

The part having this comment 

//////THIS PART OF CODE IS NOT EXECUTED ALTHOUGH FLAG USE_INDICATOR-X_FOR ENTRY IS FALSE///////
 
ahmedelmidany #:

The part having this comment 

Ok… didn’t notice that… 😊… so on the sell I Believe is working…
The issue only on the buy side… maybe that parameter is bigger than openPrice… 

if(openPrice > indicatorX_up)

what I do… I print numbers after each statement… this way I find where the printing stops, the code stops … 
 
Daniel Cioca #:
Ok… didn’t notice that… 😊… so on the sell I Believe is working…
The issue only on the buy side… maybe that parameter is bigger than openPrice… 
what I do… I print numbers after each statement… this way I find where the printing stops, the code stops … 

Thanks Daniel for your response.

In fact it didn't work for both the buy and the sell, i just added the comment in the buy section and forgot to add in the sell section as well.

the point is that the flag USE_INDICATOR-X_FOR_ENTRY is already set to false, so the if statement code should be ignored and the else code should be executed which didn't happen.

it is puzzling but the "print of numbers" is a good idea to be used as check points.

 
ahmedelmidany #:

Thanks Daniel for your response.

In fact it didn't work for both the buy and the sell, i just added the comment in the buy section and forgot to add in the sell section as well.

the point is that the flag USE_INDICATOR-X_FOR_ENTRY is already set to false, so the if statement code should be ignored and the else code should be executed which didn't happen.

it is puzzling but the "print of numbers" is a good idea to be used as check points.

this always executes , i think

LastActionTime = Time[0];

Test case

  bool IO1=false;
  bool IO2=false;
  int io_value=2;
  
  if(IO1)
  if(IO2)
     if(io_value==1)
       {
       Print("IO1 TRUE IO2 TRUE , VALUE 1");
       }
     else
       Print("IO1 TRUE IO2 TRUE , VALUE "+IntegerToString(io_value));
  else{
    Print("IO2 FALSE");
  }
  Print("Theoretically accessible if IO1 true ");
 
Lorentzos Roussos #:

this always executes , i think

Test case

To understand the test case, which else belongs to which if statement? when to use the {} and when you don't use it?

i think it should be as below, is it correct?

if(IO2)
else
        Print("IO1 TRUE IO2 TRUE, VALUE "+IntegerToString(io_value));


//AND
if(IO1)
else {
        Print("IO2 FALSE");


 
ahmedelmidany #:

To understand the test case, which else belongs to which if statement? when to use the {} and when you don't use it?

i think it should be as below, is it correct?


run the code 

Mql does not identify indentation depth as encapsulation like Pinescript

 @Fernando Carreiro 's code should work if all else is okay

This should explain it better : 

datetime LastActionTime=0;
int orderCount=0;

void OnTick()
{
//---
   //Check if it is a new candle, need the code to run every new candle instead of OnTick
   if (LastActionTime != Time[0])
      //Confirm no active orders for this pair, if no active orders then will check for signals
      if(orderCount == 0)
      {
      Print("NEWACTION:Order count 0");
      }
      else Print("NEWACTION:OrderCloseCheck()"); //if there is active order i.e. order count !=0 then check if closure criteria is met
      LastActionTime = Time[0];Print("Updating Last Action Time");
      
} 
 
USE_INDICATOR-X_FOR_ENTRY = false;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
//Check if it is a new candle, need the code to run every new candle instead of OnTick
   if(LastActionTime != Time[0])

      //Confirm no active orders for this pair, if no active orders then will check for signals
      if(orderCount == 0)
        {
         //start checking for signal
         Signal(); //check for signals, updates gosell and gobuy to 1 if signal is found

         if(gobuy == 1) //if a Buy signal was found
           {
            openPrice = Open[0];
            indicatorX_down = iCustom(_Symbol,PERIOD_CURRENT,"indicator-x",2,0);
            Print("open price: ", openPrice, " - Indicator-X down: ", indicatorX_down);
            if(SIGNAL_TYPE == 0 || SIGNAL_TYPE == 2) //means BUY signals are allowed by the variable SIGNAL_TYPE
               if(USE_INDICATOR-X_FOR_ENTRY)   //if a specific indicator "the flag USE_INDICATOR-X_FOR_ENTRY" is used to confirm entry in position
                 {
                  if(openPrice < indicatorX_down)
                    {
                     datetime time = iTime(NULL,0,1);
                     Print("Indicator-X Buy Entry condition met");
                     if(PushAlert)
                        SendNotification(Symbol()+" BUY @  Hour "+string(Hour())+"  Minute "+string(Minute()));
                     if(EmailOn)
                        SendMail("PC BUY SIGNAL - "+ChartSymbol(0)+" "+string(_Period),string(time)+" - Buy - "+ChartSymbol(0)+"\r\n");
                     orderCount +=1;
                     inBuy=true;
                     Print("In buy: ", IntegerToString(inBuy));
                    }
                  else
                    {
                     Print("Indicator-X Buy Entry condition not met");
                    }
                 }
               else
                 {
                  Print("Indicator-x condition not used");  //////THIS PART OF CODE IS NOT EXECUTED ALTHOUGH FLAG USE_INDICATOR-X_FOR ENTRY IS FALSE///////
                  datetime time = iTime(NULL,0,1);
                  if(PushAlert)
                     SendNotification(Symbol()+" BUY @  Hour "+string(Hour())+"  Minute "+string(Minute()));
                  if(EmailOn)
                     SendMail("PC BUY SIGNAL - "+ChartSymbol(0)+" "+string(_Period),string(time)+" - Buy - "+ChartSymbol(0)+"\r\n");
                  orderCount +=1;
                  inBuy=true;
                  Print("In buy: ", IntegerToString(inBuy));
                 }
           }

         if(gosell == 1) //if a sell signal was found
           {
            openPrice = Open[0];
            indicatorX_up = iCustom(_Symbol,PERIOD_CURRENT,"indicator-x",1,0);
            Print("open price: ", openPrice, " - Indicator-x up: ",  indicatorX_up);
            if(SIGNAL_TYPE == 1 || SIGNAL_TYPE == 2) //means that SELL signals are allowed by the variable SIGNAL_TYPE
               if(USE_INDICATOR-X_FOR_ENTRY)
                 {
                  if(openPrice > indicatorX_up)
                    {
                     datetime time = iTime(NULL,0,1);
                     Print("Indicator-x Sell Entry condition met");
                     if(PushAlert)
                        Alert(Symbol()," SELL @  Hour ",string(Hour()),"  Minute ",string(Minute()));
                     if(EmailOn)
                        SendMail("PC SELL SIGNAL - "+ChartSymbol(0)+" "+string(_Period),string(time)+" - Sell - "+ChartSymbol(0)+"\r\n");
                     orderCount +=1;
                     inSell=true;
                     Print("In Sell: ", IntegerToString(inSell));
                    }
                  else
                     Print("Indicator-x sell Entry condition not met");
                 }
               else
                 {
                  Print("Indicator-x flag not used");
                  datetime time = iTime(NULL,0,1);
                  if(PushAlert)
                     Alert(Symbol()," SELL @  Hour ",string(Hour()),"  Minute ",string(Minute()));
                  if(EmailOn)
                     SendMail("PC SELL SIGNAL - "+ChartSymbol(0)+" "+string(_Period),string(time)+" - Sell - "+ChartSymbol(0)+"\r\n");
                  orderCount +=1;
                  inSell=true;
                  Print("In Sell: ", IntegerToString(inSell));
                 }
           }
         Print("In buy after signal: ", IntegerToString(inBuy)); //All print statements are placed for debugging purposes
         Print("In Sell after signal: ", IntegerToString(inSell));
        }
      else
         orderCloseCheck(); //if there is active order i.e. order count !=0 then check if closure criteria is met
   LastActionTime = Time[0];

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

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