The poster assumed you would know to define the variables.
static datetime oldTime; //define the variable int start() { if(oldTime != Time[0] ) { // first tick of new bar found // do something useful oldTime = Time[0]; } return(0); }
I defined that one outside of the function, figure out why and you win a gold star lol
The poster assumed you would know to define the variables.
I defined that one outside of the function, figure out why and you win a gold star lol
Yes you are right they are static by default, but I didnt make it global for that reason ... I did it because if he declares it locally, when he attaches the EA to a chart for the first time, it will trigger that new bar detection even if the current bar is halfway through completion. He said he wants the first tick of the new bar so if he declares it globaly he can do oldTime = Time[0] in the init() function. Then it will work correctly from the get go.
The poster assumed you would know to define the variables.
I defined that one outside of the function, figure out why and you win a gold star lol
Since oldTime will be initialized to 0 (see here), the code will execute on the next tick regardless if there is a new bar because oldTime will not equal Time[0] just after initialization. In some systems (and for some people), this may present a problem. The following code prevents that problem from occurring:
int start() { static datetime oldTime = 0; if (oldTime == 0) oldTime = Time[0]; else if (oldTime < Time[0]) { Print ("New bar found! (", TimeToStr(TimeCurrent(), TIME_DATE|TIME_MINUTES|TIME_SECONDS), ")"); oldTime = Time[0]; } return(0); }
Yes you are right, but I didnt make it global for that reason ... I did it because if he declares it locally, when he attaches the EA to a chart for the first time, it will trigger that new bar detection even if the current bar is halfway through completion. He said he wants the first tick of the new bar so if he declares it globaly he can do oldTime = Time[0] in the init() function. Then it will work correctly from the get go.
He can do a similar thing in start() . . .
int start() { static datetime oldTime; //define the variable if(oldTime == 0) oldTime = Time[0]; if(oldTime != Time[0] ) { // first tick of new bar found // do something useful oldTime = Time[0]; } return(0); }
using init() is probably more efficient though.
... I did it because if he declares it locally, when he attaches the EA to a chart for the first time, it will trigger that new bar detection even if the current bar is halfway through completion.
See the code in my previous post.
He said he wants the first tick of the new bar so if he declares it globaly he can do oldTime = Time[0] in the init() function to pre-set it. Then it will work correctly from the get go.
By giving oldTime global scope, you allow any function to access and possibly change it.
Functions can't change it by themselves, programmers can fail to keep track of which variable names they used globally, if they do that its their fault. Declaring it globally and using init() for that is just more efficient because it only needs to do it one time, when the EA is first attached to the chart.
Funcyions can't change it by themselves, programmers can fail to remember which variable names they used globally, if they do that its their fault.
lol ok. I was just presenting an option too.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Here's the correct one of phy code in start() function
I'm trying to get and ea to open an order at the start of each bar, and no other time.
I came across the above from an old post, but the editor wants oldTime variable defined...
How do I do that? it does not like int oldTime=Time[0] ; that wants Time to be initialized.
any help?