open order at new bar only

 

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);
   }

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?

     

    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

     
    SDC:

    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

    Globally declared variables are static by default.  You could make it local and static and it would work.  
     

    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.

     
    SDC:

    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 or, declare the oldTime variable globaly and do oldTime = Time[0] in the init() function first. (The reason for making the variable global) lol

     
    SDC:

    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.  

     
    SDC:

     ... 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.

    SDC:

    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.

     
    SDC:

    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.

    For me, it is part of encapsulation.  It is also part readability--why clutter my global variable definitions section when I don't have too.  Every programmer has his/her style...I was just presenting options. :)
     

    lol ok. I was just presenting an option too.

    Reason: