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.
static int last_direction = 0;
On a DeInit/Init cycle (pair change, TF change...) static's and globals are NOT reset.
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); }
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!
- Meant int last_direction.
- Can't be static as must be shared between Crossed() and init()
- 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.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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.