How to detect candle open/close inside an Expert Advisor

 

Hi folks!

I'm start to learn basics to construction an Expert, and it's going well so far. Eevery day some options become clearer and easy to understand, but others, not so much.

Need to know how to detect the exact moment when a candle is closed... and otherwise, also when a new candle starts.

Need this because my strategy works on the closing event of a candle, and open trade on starting next candle!

 

I do a previous search here on the forum and found a lot of information, but not cleared or inserted on much code... and don't understand !

  .

 
wemersonrv:


Need to know how to detect the exact moment when a candle is closed... and otherwise, also when a new candle starts.

Need this because my strategy works on the closing event of a candle, and open trade on starting next candle!


Use this when checking if the candle has opened before you send your orders:

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

Use this when checking if the candle has opened before you send your orders:

 

Great my friend. Thanks.

 

Now i need to know how to detect the end of the candle... or in the detection of new candle check the confluences of my strategy on previous candle.

 
wemersonrv: Need to know how to detect the exact moment when a candle is closed... and otherwise, also when a new candle starts
  1. The candle closes when the new candle starts. A new candle starts when a tick comes in at or beyond the last candle time+period.
  2. DeanDeV posted his IsNewCandle() but I recommend that it not be placed into a function. Calling the function a second time, in the same tick, returns false. I just use
    start(){
       static datetime time0; bool isNewBar = time0 != Time[0]; time0 = Time[0];
    
      if(isNewBar) ...

  3. If both cases, the above codes do not properly handle, first tick or deinit/init cycles. See https://www.mql5.com/en/forum/146192/page3#825991
 
WHRoeder:
  1. The candle closes when the new candle starts. A new candle starts when a tick comes in at or beyond the last candle time+period.
  2. DeanDeV posted his IsNewCandle() but I recommend that it not be placed into a function. Calling the function a second time, in the same tick, returns false. I just use
  3. If both cases, the above codes do not properly handle, first tick or deinit/init cycles. See https://www.mql5.com/en/forum/146192/page3#825991
Ok, understand!
Reason: