# 请帮忙看看这些代码有什么问题，判断快慢均线穿越的函数。

43

int Crossed (double line1 , double line2)
{
static int last_direction = 0;
static int current_direction = 0;
//Don't work in the first load, wait for the first cross!
static bool first_time = true;
if(first_time == true)
{
first_time = false;
return (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); //not changed
}
}
----------------------------------------------------

12275

The better solution is that

```int Crossed (double currline1 , double currline2, double prevline1 , double prevline2)
{
int current_direction=0;
//----
if(currline1 > currline2 && prevline1 < prevline2)
current_direction = 1;  //up
if(currline1 < currline2 && prevline1 > prevline2)
current_direction = -1;  //down
//----
return (current_direction);
}```
43

THANKS

12275

Yes, this is good idea and next step in improving this function. For examle:

```int Crossed (double currline1 , double currline2, double prevline1 , double prevline2, int minDifferenceInPoints)
{
int current_direction=0;
//----
if (MathAbs(currline1-currline2)>minDifferenceInPoints*Point)
{
if(currline1 > currline2 && prevline1 < prevline2)
current_direction = 1;  //up
if(currline1 < currline2 && prevline1 > prevline2)
current_direction = -1;  //down
}
//----
return (current_direction);
}```