If I exit a trade, I do not want to enter again until the MA's cross, what is wrong with this code?

 
//--------------------------------------------------------------- 5 --
   double L_1=iCustom(NULL,0,"CW",4,0);
   double L_5=iCustom(NULL,0,"CW",5,0);
   double MA_c=L_1;
   double MA_p=iCustom(NULL,0,"CW",4,MAReduction);
                                             //reads the previous bar number specified
//-------------------------------------------------------------- 5b --
      //Trade Exit Criteria
   if ((MA_c<MA_p)&&(MA_p>MA_c))             //Criterion for closing a Buy
      {
      Cls_B=true;
      }
   if ((MA_c>MA_p)&&(MA_p<MA_c))             //Criterion for closing a Sell
      {
      Cls_S=true;
      }
//---------------------------------------------------------------5c---
      //Trade Entry Criteria

   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(L_1>L_5)current_direction = 1; //up
    if(L_1<L_5)current_direction = 2; //down

    if(current_direction != last_direction) //changed 
     {
          last_direction = current_direction;
          return (last_direction);
      }
      else
      {
            return (0); //not changed
      }
   if(current_direction == 1)
       {
        Opn_B=true;                               // Criterion for opening Buy                                                  
       }                                
   if(current_direction == 2)
       {
         Opn_S=true;                               // Criterion for opening Sell
       }
Basically, if I exit a Sell I do not want to go back in until the MAs have crossed. Similarly for a Buy. I thought this would accomplish this goal but it is not working and I am struggling to see why. Any thoughts would be greatly appreciated
 

Generally, this is a beginners problem. You do-not want to use static-variables or global-variables for that matter. You already have the logic for this and that should be sufficient. You need Four MA Values and not 2.

double Fast_Ma_Current=iCustom(Symbol(),0,"Whatever_Name",Whatever_Buffer,0);//Where Shift 0=Current.
double Fast_Ma_Previous=iCustom(Symbol(),0,"Whatever_Name",Whatever_Buffer,1);// Shift Bar Before Current.
double Slow_Ma_Current=iCustom(Symbol(),0,"Whatever_Name",Whatever_Buffer,0);//Where Shift 0=Current.
double Slow_Ma_Previous=iCustom(Symbol(),0,"Whatever_Name",Whatever_Buffer,1);//Shift Bar Before Current.


if( Fast_Ma_Previous<Slow_Ma_Previous && Fast_Ma_Current>Slow_Ma_Current) { int Direction= 1; }//Buy_Trigger
if( Fast_Ma_Previous>Slow_Ma_Previous && Fast_Ma_Current<Slow_Ma_Current) { Direction= -1; }//Sell_Trigger

Within the above, the int Direction is a Local-Variable and will Reset to Zero when things don't cross. You could also define the Local variable before the IF.

 
ubzen:

Generally, this is a beginners problem. You do-not want to use static-variables or global-variables for that matter. You already have the logic for this and that should be sufficient. You need Four MA Values and not 2.

double Fast_Ma_Current=iCustom(Symbol(),0,"Whatever_Name",Whatever_Buffer,0);//Where Shift 0=Current.
double Fast_Ma_Previous=iCustom(Symbol(),0,"Whatever_Name",Whatever_Buffer,1);// Shift Bar Before Current.
double Slow_Ma_Current=iCustom(Symbol(),0,"Whatever_Name",Whatever_Buffer,0);//Where Shift 0=Current.
double Slow_Ma_Previous=iCustom(Symbol(),0,"Whatever_Name",Whatever_Buffer,1);//Shift Bar Before Current.


if( Fast_Ma_Previous<Slow_Ma_Previous && Fast_Ma_Current>Slow_Ma_Current) { int Direction= 1; }//Buy_Trigger
if( Fast_Ma_Previous>Slow_Ma_Previous && Fast_Ma_Current<Slow_Ma_Current) { Direction= -1; }//Sell_Trigger

Within the above, the int Direction is a Local-Variable and will Reset to Zero when things don't cross. You could also define the Local variable before the IF.

Thanks for this. I am guilty as charged- a Newbie, but I am learning thanks to you Guys!!

I will give this a go

 
double curDir = Fast_Ma_Current - Slow_Ma_Current,
       preDir = Fast_Ma_Previous - Slow_Ma_Previous;
bool   cross  = curDir * PreDIR < 0;
if (cross){
   if (curDir > 0) // Buy
   :
}
 
WHRoeder:


Now you are just showing off! ;-)
 

WHR's code, which admittedly is pretty good.

double curDir = Fast_Ma_Current - Slow_Ma_Current,
       preDir = Fast_Ma_Previous - Slow_Ma_Previous;
bool   cross  = curDir * PreDIR < 0;
if (cross){
   if (curDir > 0) // Buy
   :
}

And my effort to make a more efficient test (using bools rather than doubles.)

   bool curRise  = Fast_Ma_Current  > Slow_Ma_Current;
   bool prevRise = Fast_Ma_Previous > Slow_Ma_Previous;
   bool cross  = (curRise!=prevRise);
   if( cross ){
      if( curRise ){ // Buy
      }
   }
 
dabbler:
WHR's code, which admittedly is pretty good.
And my effort to make a more efficient test (using bools rather than doubles.)
Dam it dabbler. Here I was basking in my glory
Willforth:
Now you are just showing off! ;-)
and you have to show me up, eliminate a FP multiply and replace 2 FP subtractions with faster comparisons. I guess I'll have to stick with my terse comments.
 
WHRoeder:
Dam it dabbler. Here I was basking in my glory
and you have to show me up, eliminate a FP multiply and replace 2 FP subtractions with faster comparisons. I guess I'll have to stick with my terse comments.
 

Well, if I realised I was going to start a 'Cat Fight' I would have simply said "Thank You! ;-)