Function when added to Terminal

 

I have this function to look for line crosses to initiate a position. The problem I have is that when I add the EA to the terminal it immediately takes a position. I'm not sure what to add so that it waits until a subsequent line cross before it initiates a position.

int Crossed (double line1 , double line2)
   {
      static int last_direction = 0;
      static int current_direction = 0;
      
      if(line1>line2)current_direction = 1; //up
      if(line1<line2)current_direction = 2; //down

      
      if(current_direction != last_direction) //changed 
      {
            last_direction = current_direction;
            return (last_direction);
      }
      else
      {
            return (0);
      }
   } 
 
stryker251:
have this function to look for line crosses to initiate a position. The problem I have is that when I add the EA to the terminal it immediately takes a position. I'm not sure what to add so that it waits until a subsequent line cross before it initiates a position.
  1.      static int last_direction = 0;  

    On a DeInit/Init cycle (pair change, TF change...) static's and globals are NOT reset.


  2. int init() { CrossedInit(); ... }
    bool last_direction; void CrossedInit(){ last_direction = 0; }
    int Crossed (double line1 , double line2)
       {
          int current_direction = 0; // Doesn't need to be static      
          if(line1>line2)current_direction = 1; //up
          if(line1<line2)current_direction = 2; //down
          if(current_direction == last_direction) return(0); // No change
          if(last_direction = 0){
             last_direction = current_direction;
             return(0); // Don't flag on start up.
          }
          //changed 
          last_direction = current_direction;
          return (last_direction);
       } 

 
WHRoeder:
  1. On a DeInit/Init cycle (pair change, TF change...) static's and globals are NOT reset.




I didn't even think of using the int init() -- damn noobness! :-)

Could you please explain these two lines a bit more? It sets the intial as zero so as not to flag on start up but I'm not clear on the CrossedInit(); coding itself.

int init() { CrossedInit(); ... }
bool last_direction; void CrossedInit(){ last_direction = 0; }

I'm using this in int start()

double ATRStop_UP=iCustom(NULL,0,"ATRStops_v1.1",0,0);
double ATRStop_DOWN=iCustom(NULL,0,"ATRStops_v1.1",1,0);

int isCrossed=Crossed(ATRStop_DOWN, ATRStop_UP);

Much appreciated!

 
  1. Meant int last_direction.
  2. Can't be static as must be shared between Crossed() and init()
  3. Putting the variable near where used makes more sense to me. Simple substitution pattern
    Type Function(){
       static variable = 0;
       : 
    
    type variable; void FunctionInit(){ variable = 0; }
    Type Function(){
       : 
    
 

Got it working nicely -- much obliged.

Reason: