Identifying the open and close of a bar in an EA

 

Does anyone have code that can detect the open and close of a bar? It's preferable that either be set up to set true an Open/close bool, or be arranged in if statements, as long as I can figure out how to expand it.


Thanks

Kremmy


EDIT:

I've got the bar open detection from phy @ 'How to trigger order on the next bar open (or on current bar close)', but it doesn't seem to work well for close of the bar detection.

 
int oldTime;
int init(){
   oldTime = Time[0];
   return(0);
}
int start(){
   if(oldTime != Time[0){
   // first tick of new bar found
   // do something useful
   oldTime = Time[0];
   return(0);
}
 

I know this is an old post, but I'm stuck and need some help with this very issue. Here is my code:

#property copyright "Matt Wilson"
#property link      ""
extern int StopLoss=15,
           TrailingStop= 15,
           TakeProfit= 30,
           PauseToReEntryMinutes= 15;
           

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int oldTime;
int init(){
   oldTime = Time[0];
   

//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{
   if(oldTime != Time[0)
   {
   // first tick of new bar found
   // do something useful
   
   
   oldTime = Time[0];

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

 I am receiving the error '/end_of_program - unexpected square bracket' error and I think it's pretty much the same as the code indicated above no?

 Also, what I'm hoping to accomplish here is to close out any existing trades when it recognizes a new bar, then create a new order right away. To do this, would I just enter my "close all open orders" and "buy/sell conditions" in the "do something useful" part ? Thanks a lot

 
mattmoneywilson:

I know this is an old post, but I'm stuck and need some help with this very issue. Here is my code:

 I am receiving the error '/end_of_program - unexpected square bracket' error and I think it's pretty much the same as the code indicated above no?

 Also, what I'm hoping to accomplish here is to close out any existing trades when it recognizes a new bar, then create a new order right away. To do this, would I just enter my "close all open orders" and "buy/sell conditions" in the "do something useful" part ? Thanks a lot

phy - in year 2008 - made some syntax error in his code.

Here's the correct one of phy code in start() function

int start()
  {
   if(oldTime != Time[0] )
      {
      // first tick of new bar found
      // do something useful
      oldTime = Time[0];
      }
   return(0);
   }
 
Worked perfectly. Thank you very much!