Order based On Second Condition is Not Executed

 

Hi, I am trying to learn MQL4. 

I am curious as why my simple EA cannot execute properly.

When compiled, there is no error and warning.

Would anybody help?

Thank you so much

//+------------------------------------------------------------------+
//|                                                    Learn2022.mq4 |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//--- input parameters
input int      TP=20;
input int      SL=20;
input double   LotSize=1.0;
input int      Slippage=3;
input int      MagicNumber=7777;


int ticket1;
int ticket2;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   //Sell Rule
   if(IsNewBar()==true && Close[1]<Open[1]&& Close[2]<Open[2]&& Close[3]<Open[3])
   {
   ticket1=OrderSend(Symbol(),OP_SELL,LotSize,Bid,Slippage,Bid+SL*_Point,Bid-TP*_Point,"BUY",MagicNumber);//Open Sell Order
   }
   if(ticket1<0)  
  {
   Print("Order Failed",GetLastError());
   }
   else
   {
  Print("Order Success");
   }
  
  
   //Buy Rule
   if(IsNewBar()==true && Close[1]>Open[1]&& Close[2]>Open[2]&& Close[3]>Open[3])
   {
   ticket2=OrderSend(Symbol(),OP_BUY,LotSize,Ask,Slippage,Ask-SL*_Point,Ask+TP*_Point,"BUY",MagicNumber);//Open Buy Order
   }
   if(ticket2<0)  
   {
   Print("Order Failed",GetLastError());
   }
   else
   {
   Print("Order Success");
   }
  }
//+------------------------------------------------------------------+
// Check if there is a new bar
bool IsNewBar()   
{        
      static datetime RegBarTime=0;
      datetime ThisBarTime = Time[0];
      
      if (ThisBarTime == RegBarTime)
      {
         return(false);
      }
      else
      {
         RegBarTime = ThisBarTime;
         return(true);
      }
}   
 

The 2nd condition will never be checked because you call the new bar function twice.

The second time it is called, it cannot return true.