Completed Bar

 

Hello.

I'm trying to get my EA to run only when a bar is completed. I've been trying on the Day TimeFrame.

I've tried this code:

if(Time[0] == prevtime) return(0);

I've also tried this:

if (TimeHour(TimeCurrent())!= 0 && TimeMinute(TimeCurrent())!=0)  return(0);

And neither of them work. They should only run when the servertime is at 00:00. But the run at the top of every hour. Now, I have this EA loaded on the Day TimeFrame chart.

Anyone got any ideas on how I can get this to work?

 
nondisclosure:

Hello.

I'm trying to get my EA to run only when a bar is completed. I've been trying on the Day TimeFrame.


datetime PrevTime;
 
int init()
{
    PrevTime = TimeCurrent();
}
 
int start()
{
    if (Hour() != 0) // this can be omitted on Daily timeframe.
        return (0);
        
    if (PrevTime >= Time[0])
        return (0);
    PrevTime = Time[0];
    
    // EA calculations here;    
        
    return (0);
}
 

Wouldn't I want to put the PrevTime = timeCurrent() in start() ? I would want PrevTime to always equal the current time so I can check the if statement. Or am I missing something?

 
That won't make any difference to if (PrevTime >= Time[0]) condition.
 

This code allows coder to select any time point during bar formualtion.

double g;
int m,s,k;
m=Time[0]+Period()*60-TimeCurrent();
g=m/60.0;
s=m%60;
m=(m-m%60)/60;
//   Comment( m + " minutes " + s + " seconds left to bar end");
if(m==Period()) Print ("New Bar Start");
if(m==0) Print ("Bar Close");
Wackena
 
Let's try this, that's more simple.
int start()
{
    if(Volume[0]>1) return;
        
    // EA calculations here;    
        
    return (0);
}
when each new bar start, the volume is 0.
 
alan123:
Let's try this, that's more simple.
int start()
{
    if(Volume[0]>1) return;
        
    // EA calculations here;    
        
    return (0);
}
when each new bar start, the volume is 0.
Not quite. This is simpler but less reliable.
EA can just miss the first tick of the next bar and the calculations will never be executed.
 
In what conditions do you see alan123's code missing the first tick of the new candle?
 
wackena:

This code allows coder to select any time point during bar formualtion.

double g;
int m,s,k;
m=Time[0]+Period()*60-TimeCurrent();
g=m/60.0;
s=m%60;
m=(m-m%60)/60;
//   Comment( m + " minutes " + s + " seconds left to bar end");
if(m==Period()) Print ("New Bar Start");
if(m==0) Print ("Bar Close");
Wackena

Wackena, I take it 60 is one hour.  change for other time frames, ie 15 for 15 minutes, 240 for 4 hours, etc.  Am I correct?
 
nondisclosure:
In what conditions do you see alan123's code missing the first tick of the new candle?
This is not about the code. MT can miss a tick. And Alan123's example will fail to handle new bar if missed tick is the first in the bar.
 
nondisclosure:

Wackena, I take it 60 is one hour. change for other time frames, ie 15 for 15 minutes, 240 for 4 hours, etc. Am I correct?


Yes, buy only if you use sufficient chart timeframe. Example: Max m value on M30 chart is M=30.

Wackena

Reason: