# How to present price crosses over/under the SMA? 372

Hi,

In my algo I just want to check if the close price crosses over or under the SMA. My code look as follow:

```...
double MAkurz = iMA(NULL,0,MA_kurz,0,MODE_SMA,PRICE_CLOSE,1);
...
...
if(NeuePeriodeBegonnen == true)
{
if(Close > MAkurz && Close < MAkurz)
{
LongSignal = true;
}
else
{
LongSignal = false;
}
// Short Signal
if(Close < MAkurz && Close > MAkurz)
{
ShortSignal = true;
}
else
{
ShortSignal = false;
}
}
...  ```

I think my code must be correct. But by back testing it looks different. There is little buy or sell signals. 3220

What is "NeuePeriodeBegonnen" variable meaning?

If it is true when new a candle is created, then you are comparing current bid price with Close.

Close is a current bid price.

You have to either wait for the new candle to open and then compare Close with Close, or you have to compare Close and Close on every tick, not just at the start of a new candle. 372

Drazen Penic:

What is "NeuePeriodeBegonnen" variable meaning?

If it is true when new a candle is created, then you are comparing current bid price with Close.

Close is a current bid price.

You have to either wait for the new candle to open and then compare Close with Close, or you have to compare Close and Close on every tick, not just at the start of a new candle.

Sorry, I will attach more codes here:

```...
int OnInit()
{
//---
PeriodeStartZeit = Time;
//---
return(INIT_SUCCEEDED);
}
...
...
void OnTick()
{
//---
// Überprüfen ob neuer Periodebeginn vorliegt
if (PeriodeStartZeit != Time)   // Time -> aktuelle Zeit
{
NeuePeriodeBegonnen = true;
PeriodeStartZeit = Time;
}
else
{
NeuePeriodeBegonnen = false;
}

double MAkurz = iMA(NULL,0,MA_kurz,0,MODE_SMA,PRICE_CLOSE,1);

if(NeuePeriodeBegonnen == true)
...```

I think I should change my codes as follow:

```...
if(NeuePeriodeBegonnen == true)
{
if(Close > MAkurz && Close < MAkurz)
{
LongSignal = true;
}
else
{
LongSignal = false;
}
// Short Signal
if(Close < MAkurz && Close > MAkurz)
{
ShortSignal = true;
}
else
{
ShortSignal = false;
}
}
...``` 3220

The second version will create a signal when candle opens, and the previous candle crosses moving average. 22646

1.  Your code ```if(Close > MAkurz && Close < MAkurz)   {    LongSignal = true;   }       else {    LongSignal = false; }         ``` Generic ```if (             true                      ) {return (true);} else {return (false);  } ``` Equivalent ```if (          condition                    ) return (condition); ``` Simplify your code `LongSignal = Close > MAkurz && Close < MAkurz;`

2. `if(NeuePeriodeBegonnen == true)`
You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So Don't write if(bool == true), just use if(bool) or if(!bool).
3.  Your code ```// Überprüfen ob neuer Periodebeginn vorliegt if (PeriodeStartZeit != Time)   // Time -> aktuelle Zeit   {    NeuePeriodeBegonnen = true;    PeriodeStartZeit = Time;   } else   {    NeuePeriodeBegonnen = false;   } ``` Simplify your code ```// Überprüfen ob neuer Periodebeginn vorliegt bool PreviousStart = PeriodeStartZeit; PeriodeStartZeit = Time; bool NeuePeriodeBegonnen = PeriodeStartZeit != PreviousStart; // Time -> aktuelle Zeit```

4. `LongSignal = Close > MAkurz && Close < MAkurz;`
You probably want to compare C < MA && C > MA, or the Open < MA && Close > MA