Trying to make the EA make 1 set of calculations per bar. Why wouldn't this work??

 
int barcount;
        bool closebuy = false;
        bool closesell = false;
        
        //Process calculations only once per bar
        barcount = GlobalVariableGet("BAR_"+Symbol());
                if (barcount!=iBarShift(NULL,0,0))
     {
        barcount=iBarShift(NULL,0,0);
        GlobalVariableSet("BAR_"+Symbol(), barcount);  


        
    //// CALCULATING buy / sell signals

Any insights ?

Thank you for your time =]

 
investguy:

Any insights ?

Thank you for your time =]

If barcount == max bars in history then it's value won't change when the next bar comes along. Use time[1] instead.
 
When you use iBarShift it's for the shift in bars from a specified date (your trying to get the shift from a time stamp of 0 or Jan 1 1970). What you want is the pre-defined variable bars. It will increase every time there is a new bar. You could just replace the barcount variable with Bars and it should sort it out.
 
heelflip43:
When you use iBarShift it's for the shift in bars from a specified date (your trying to get the shift from a time stamp of 0 or Jan 1 1970). What you want is the pre-defined variable bars. It will increase every time there is a new bar. You could just replace the barcount variable with Bars and it should sort it out.
bars is not reliable for a similar reason as I stated above. USE time . . .
 

I normally do, I just suggested it because he was using bars anyway. This is the bit of code which I use:

static datetime currTime;
if(currTime == iTime(Symbol(), 0, 0))return;
currTime = iTime(Symbol(), 0, 0));
 
bool NewBar(string Symb,int Perd){
    static datetime LastTime; datetime Time0=iTime(Symb,Perd,0);
    if(LastTime!=Time0){LastTime=Time0; return(true);}
}
Same things I guess. The Poster could just search Google for Once_Per_Bar and find all the millions of ways it's been done before.
Reason: