How to present price crosses over/under the SMA?

 

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)
     {
      // Buy Signal
      if(Close[0] > MAkurz && Close[1] < MAkurz)
        {
         LongSignal = true;
        }
      else
       {
         LongSignal = false;
       }        
      // Short Signal      
      if(Close[0] < MAkurz && Close[1] > 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.

 

What is "NeuePeriodeBegonnen" variable meaning?

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

Close[0] is a current bid price. 

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

 
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[1].

Close[0] is a current bid price. 

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


Sorry, I will attach more codes here:

...
int OnInit()
  {
//---
   PeriodeStartZeit = Time[0];
//---
   return(INIT_SUCCEEDED);
  }
...
...
void OnTick()
  {
//---
   // Überprüfen ob neuer Periodebeginn vorliegt
   if (PeriodeStartZeit != Time[0])   // Time[0] -> aktuelle Zeit
   {
      NeuePeriodeBegonnen = true;
      PeriodeStartZeit = Time[0];
   }
   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)
     {
      // Buy Signal
      if(Close[1] > MAkurz && Close[2] < MAkurz)
        {
         LongSignal = true;
        }
      else
       {
         LongSignal = false;
       }        
      // Short Signal      
      if(Close[1] < MAkurz && Close[2] > MAkurz)
        {
         ShortSignal = true;
        }      
      else
        {
         ShortSignal = false;
        }
     }
...
 

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

 
  1. Your code
    if(Close[1] > MAkurz && Close[2] < MAkurz)
      {
       LongSignal = true;
      }
          else
     {
       LongSignal = false;
     }        
    
    Generic
    if (             true                      ) {return (true);}
    else {return (false);  }
    
    Equivalent
    if (          condition                    ) return (condition);
    
    Simplify your code
    LongSignal = Close[1] > MAkurz && Close[2] < 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[0])   // Time[0] -> aktuelle Zeit
      {
       NeuePeriodeBegonnen = true;
       PeriodeStartZeit = Time[0];
      }
    else
      {
       NeuePeriodeBegonnen = false;
      }
    
    Simplify your code
    // Überprüfen ob neuer Periodebeginn vorliegt
    bool PreviousStart = PeriodeStartZeit; PeriodeStartZeit = Time[0];
    bool NeuePeriodeBegonnen = PeriodeStartZeit != PreviousStart; // Time[0] -> aktuelle Zeit

  4. LongSignal = Close[1] > MAkurz && Close[2] < MAkurz;
    You probably want to compare C[2] < MA[2] && C[1] > MA[1], or the Open[1] < MA[1] && Close[1] > MA[1]
Reason: