How do we analyse the next bar?

 

Hi,

I want to know how can we tell the program to continuously check for new bar opening?

 

Hi,

Code sample:

////////////////////////////////////////////////////////////////////<         5>
// < 2.1.1. Code : Interface : iNewBar >                          //<          >
int       iNewBar ()         //       - i       4 l       1 o     //<          >
{                                                                 //<          >
static    int       iTime_0 = 0                                 ; //<          >
//                                                                //<          >
if      ( iTime_0 < iTime   ( 0 , 0 , 0 ) )                       //<          >
        { iTime_0 = iTime   ( 0 , 0 , 0 ) ;  return ( TRUE  ) ; } //<          >
else    {                                    return ( EMPTY ) ; } //<          >
}                                                                 //<          >
// </2.1.1. Code : Interface : iNewBar >                          //<          >
     

Explanations:

https://www.mql5.com/en/forum/124521/page5#287213, Answer 2

https://www.mql5.com/en/forum/124521/page8#294769, part 3

Regards,

Airat

 


Thank you for the answer it is very helpful, but i still have a question.

I'm using the NewBar() in the following code. It places the orders in the same bar itself and doesn't scan the bars! Can you please point out the mistakes.

 
if(NewBar())
   {
   checkopen();
   }
 
qjol:

It still places the order in the same bar itself.
 

@Ais & @gjol: Please tell me why it is not checking every bar and placing the orders all at once?

If it can work then it wold be great!!!

 

Please try to show us the renewed code.

To study technique of trade control for single bar, please, look at the Notes in -> https://www.mql5.com/en/forum/124521/page7#287657.

 
Ais:

Please try to show us the renewed code.


I've pasted the mq4 file above. But even then here is the code
extern double MPeriod1 = 5;
extern double MPeriod2 = 17;
bool buycond=true;
bool sellcond=true;
bool buycond1=true;
bool sellcond1=true;
extern double takeprofit = 150;
extern double Stoploss = 400;

//------- Moving Averages
double FMA,SMA,PFMA,PSMA,FFMA,FSMA;
int TimeBar=0,i;


extern int Expert_ID = 100;
int _MagicNumber = 0;

extern double SLB,TPB,SLS,TPS;
int ticket,ticket1,ticket2,ticket3,ticket4,ticket5,ticket6,ticket7,ticket8,ticket9,ticket10,ticket11,ticket12,ticket13,ticket14,ticket15,ticket16,ticket17,ticket18,ticket19,ticket20,ticket21,ticket22,ticket23;





//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----  
     for(int a=1;a<999;a++)
    {
    _MagicNumber = Expert_ID * 10 + a;
    }
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----

      if(NewBar()==true)
         {
            checkopen();          
         }       
        

checkclose();
   
//----
   return(0);
  }
//+------------------------------------------------------------------+


//+------------------------------------------------------------------+
//   check for open condition                                        |
//+------------------------------------------------------------------+
void checkopen()
{
   
   
   
   SLB=NormalizeDouble(Ask-Stoploss*Point,4);
   TPB=NormalizeDouble(Ask+takeprofit*Point,4);

   SLS=NormalizeDouble(Bid+Stoploss*Point,4);
   TPS=NormalizeDouble(Bid-takeprofit*Point,4);

   
   FMA=iMA(NULL,0,MPeriod1,0,MODE_SMA,PRICE_OPEN,0);
   SMA=iMA(NULL,0,MPeriod2,0,MODE_SMA,PRICE_OPEN,0);
   PFMA=iMA(NULL,0,MPeriod1,0,MODE_SMA,PRICE_OPEN,1);
   PSMA=iMA(NULL,0,MPeriod2,0,MODE_SMA,PRICE_OPEN,1);
    
   
   if(FMA<SMA && PFMA>PSMA && buycond==true)
   {
      OrderClose(ticket12,0.1,Bid,3,Snow);     
      OrderClose(ticket11,0.1,Ask,3,Gold);      
   ticket=OrderSend(Symbol(),OP_BUY,0.1,Ask,3,SLB,TPB,"",_MagicNumber,0,Red); // open new BUY Order 
   buycond=false;
   sellcond=true;
   buycond1=false;
   sellcond1=true;
  if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
            Alert("BUY");
           }
         else Print("Error opening BUY order : ",GetLastError());     
         
          if(Open[0]<Open[1] && High[0]<High[1] && sellcond1==true)
         {
         OrderClose(ticket,0.1,Bid,3,Snow);     
         ticket2=OrderSend(Symbol(),OP_SELL,0.1,Bid,3,SLS,TPS,"",_MagicNumber,0,Blue);//open new Sell Order
         sellcond1=false;
         buycond1=true;
         }
         
  }
   
//--------------------------------------------SELL Open---------------------------------------------------------------   
   if(FMA>SMA && PFMA<PSMA && sellcond==true)
   {   
         OrderClose(ticket2,0.1,Ask,3,Gold);     
         OrderClose(ticket,0.1,Bid,3,Snow);     
    ticket11=OrderSend(Symbol(),OP_SELL,0.1,Bid,3,SLS,TPS,"",_MagicNumber,0,Blue);//open new Sell Order
    buycond=true;
    sellcond=false;
    buycond1=true;
   sellcond1=false;
   if(ticket1>0)
           {
            if(OrderSelect(ticket1,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
            Alert("SELL");
           }
         else Print("Error opening SELL order : ",GetLastError());  
         
          if(Open[0]>Open[1] && High[0]>High[1] && buycond1==true)
         {
         OrderClose(ticket11,0.1,Ask,3,Gold);      
         ticket12=OrderSend(Symbol(),OP_BUY,0.1,Ask,3,SLB,TPB,"",_MagicNumber,0,Red); // open new BUY Order 
         buycond1=false;
         sellcond1=true;
         }   
         
         
         
   }//sell if close bracket          
}//function checkopen close bracket

//---------------------------------------------------------+
// condition for closing orders                            |
//---------------------------------------------------------+
void checkclose()
{
for(int i=OrdersTotal()-1;i<0;i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)        break;
      if(OrderMagicNumber()!=_MagicNumber || OrderSymbol()!=Symbol()) continue;
      if(OrderType()==OP_BUY)
      {
       if(FMA>SMA && PFMA<PSMA)
      {
      OrderClose(OrderTicket(),0.1,Ask,3,Gold);
      Print("Buy order closed");
      }
      break;
      }
      if(OrderType()==OP_SELL)
      {
      if(FMA<SMA && PFMA>PSMA)
      {
      OrderClose(OrderTicket(),0.1,Bid,3,Snow);     
      Print("SELL order closed");
      }
      break;
      }

    }//close bracket for for loop
}// close bracket for checkclose

//------------------------------------------------+
//   check for new bar                            |
//------------------------------------------------+
//---- Check Is New Bar

bool NewBar()
{
   static datetime Time.PrevBar;
   bool PrevBar = ( Time.PrevBar < Time[0]);


   if(Time.PrevBar < Time[0])
   {
      Time.PrevBar = Time[0];
      return(true);
   }
   else
   return(false);
  
}
 

change to this function

bool Fun_New_Bar()
   {
   static datetime New_Time = 0;
   bool New_Bar = false;
   if (New_Time!= Time[0])
      {
      New_Time = Time[0];
      New_Bar = true;
      }
   return(New_Bar);
   }

& of course

if(Fun_New_Bar())
   {
   checkopen();          
   }       
        

the function that u using returns always true

 

It is not doing anything new!

It works exactly the same way.... sometimes it executes the condition sometimes doesn't!

 

could be that we don't understand each other, what are you trying to acomplish with that function

Reason: