Crossover problem...

 
I am modifying the alligator indicator.
I want it to send me a screen alert when the green line crfosses the red line but it does this constantly, not just once and then again on the next cross
Any ideas:
[quote]
for(int i=0; i<limit; i++)
{
//---- ma_shift set to 0 because SetIndexShift called abowe
ExtBlueBuffer[i]=iMA(NULL,0,JawsPeriod,0,MODE_SMMA,PRICE_MEDIAN,i);
ExtRedBuffer[i]=iMA(NULL,0,TeethPeriod,0,MODE_SMMA,PRICE_MEDIAN,i);
//Print(iMA(NULL,0,TeethPeriod,0,MODE_SMMA,PRICE_MEDIAN,i));
ExtLimeBuffer[i]=iMA(NULL,0,LipsPeriod,0,MODE_SMMA,PRICE_MEDIAN,i);
int isCrossed = Crossed (iMA(NULL,0,TeethPeriod,0,MODE_SMMA,PRICE_MEDIAN,i),iMA(NULL,0,LipsPeriod,0,MODE_SMMA,PRICE_MEDIAN,i));
//if (isCrossed==1) {Alert("Teeth have crossed lips");}
}
[/quote]

Crossed function:
[quote]
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);
}
}
[/quote]
 

SanMiguel wrote >>
I am modifying the alligator indicator.
I want it to send me a screen alert when the green line crfosses the red line but it does this constantly, not just once and then again on the next cross
Any ideas:
[quote]
for(int i=0; i<limit; i++)
{
//---- ma_shift set to 0 because SetIndexShift called abowe
ExtBlueBuffer[i]=iMA(NULL,0,JawsPeriod,0,MODE_SMMA,PRICE_MEDIAN,i);
ExtRedBuffer[i]=iMA(NULL,0,TeethPeriod,0,MODE_SMMA,PRICE_MEDIAN,i);
//Print(iMA(NULL,0,TeethPeriod,0,MODE_SMMA,PRICE_MEDIAN,i));
ExtLimeBuffer[i]=iMA(NULL,0,LipsPeriod,0,MODE_SMMA,PRICE_MEDIAN,i);
int isCrossed = Crossed (iMA(NULL,0,TeethPeriod,0,MODE_SMMA,PRICE_MEDIAN,i),iMA(NULL,0,LipsPeriod,0,MODE_SMMA,PRICE_MEDIAN,i));

int flag;
//if (isCrossed==1&& flag != 1) {Alert("Teeth have crossed lips");
int flag =1}
}
[/quote]

Crossed function:
[quote]
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);
}
}
[/quote]

You can add a "flag" as I have done in RED in your code above, note elsewehere you will have to set the "flag" back to 0, presumably at the next reverse cross.

HTH

Keith

 
kminler wrote >>

You can add a "flag" as I have done in RED in your code above, note elsewehere you will have to set the "flag" back to 0, presumably at the next reverse cross.

HTH

Keith

When it crosses back over again, it won't pick it up if the flag is there or will it?


Isn't that what this part does?


if(line1>line2)current_direction = 1; //up
if(line1<line2)current_direction = 2; //down
if(current_direction != last_direction) //changed


Should this bit be outside of the function?

static int last_direction = 0;

 
SanMiguel wrote >>

When it crosses back over again, it won't pick it up if the flag is there or will it?

I took some more time to work through your logic and if you add:

if(current_direction != last_direction) //changed
{
last_direction = current_direction;flag =0;
return (last_direction);

this should work and if I had some more time and a clearer head, i'd look a little deeper you likely could have done this as well

just using current direction !=last direction

Keith

 
kminler:

I took some more time to work through your logic and if you add:

if(current_direction != last_direction) //changed
{
last_direction = current_direction;flag =0;
return (last_direction);

this should work and if I had some more time and a clearer head, i'd look a little deeper you likely could have done this as well

just using current direction !=last direction

Keith

Ah, I know why. It's because it's in the for loop with something to do with bars.

Where else could I put the code so it is refreshed on each pirce change?

 
SanMiguel wrote >>

Ah, I know why. It's because it's in the for loop with something to do with bars.

Where else could I put the code so it is refreshed on each pirce change?

Not too sure, I don't think that you need to use static variables for last/current direction, just declare them as integer.

As you have it now, every time you call the function both directions are set to zero, when you want to do is check the previous value.

I'm not clear but I think that if you use static variables and don't initialise them then they will automatically take the value zero, not what you want.

We likely have a case of the blind leading the blind here, perhaps a more proficient coder might like to jump in and clarify static variables?

Keith

 
kminler wrote >>

Not too sure, I don't think that you need to use static variables for last/current direction, just declare them as integer.

As you have it now, every time you call the function both directions are set to zero, when you want to do is check the previous value.

I'm not clear but I think that if you use static variables and don't initialise them then they will automatically take the value zero, not what you want.

We likely have a case of the blind leading the blind here, perhaps a more proficient coder might like to jump in and clarify static variables?

Keith

I could declare the last_direction outside of the function but it seems to work.

However, I can't get the indicator to remember when I close down MetaTrader. It starts again when I open it with the alerts.

Any ideas?

 
SanMiguel wrote >>

I could declare the last_direction outside of the function but it seems to work.

However, I can't get the indicator to remember when I close down MetaTrader. It starts again when I open it with the alerts.

Any ideas?

Not too clear on what needs to be remembered, when you restart the indicator it re-calcs everything and should just carry on with the the correct values.

I must be missing something, maybe post the latest version and I or someone else can puzzle it out.

Keith

 
kminler:

Not too clear on what needs to be remembered, when you restart the indicator it re-calcs everything and should just carry on with the the correct values.

I must be missing something, maybe post the latest version and I or someone else can puzzle it out.

Keith

I think maybe I have to set one of those global variables that is remembered for 4 weeks?

Reason: