Pivots point check only working in one direction...

 

Hi

I have been setting up my first EA with the help of a more experienced coder.

One condition of mt trades is that if the price is X amount from the nearest pivot line then it will invalidate the possible trade.

This is working for long trades, so if the price was too close to a pivot line above then it will invalidate and this is working.

But if there is to be a possible short trade, then the EA doesn't shows in the comments that there next pivot down is not too close but evidentially it is.

Here is some of the coding:

double PivotLine(string nm){
   if(nm==""){double dst=1000000,piv=0;
      for(int i=2;i<8;i++){
         if(MathAbs(Close[0]-pivot[i])<dst){
            dst = MathAbs(Close[0]-pivot[i]);
            piv = pivot[i];
         }
      }
      return(piv);
   }
   if(nm=="PIVOT"){return(pivot[0]);}
   if(nm=="R1"){return(pivot[2]);}
   if(nm=="S1"){return(pivot[5]);}
   if(nm=="R2"){return(pivot[3]);}
   if(nm=="S2"){return(pivot[6]);}
   if(nm=="R3"){return(pivot[4]);}
   if(nm=="S3"){return(pivot[7]);}
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double Pivot(string up_dn){int time = StrToTime("22:00"),shift;
   if(TimeCurrent()<=time){
      shift = iBarShift(Symbol(),Period(),iTime(Symbol(),1440,1)+22*3600,false);
   }else{
      shift = iBarShift(Symbol(),Period(),iTime(Symbol(),1440,0)+22*3600,false);
   }
   int    count    = 1440/Period();
   double PDayHigh = iHigh(Symbol(),Period(),iHighest(Symbol(),Period(),MODE_HIGH,count,shift));
   double PDayLow  = iLow (Symbol(),Period(),iLowest (Symbol(),Period(),MODE_LOW ,count,shift));
   double Pivot    = (PDayHigh + PDayLow + iClose(Symbol(),Period(),shift)) / 3;    // Pivot point
   double Range = PDayHigh - PDayLow;
   pivot[0] = (PDayHigh + PDayLow + iClose(Symbol(),Period(),shift)) / 3;
   pivot[1] = PDayHigh - PDayLow;
   pivot[2] = 2 * pivot[0] - PDayLow;                     // R1
   pivot[3] = pivot[0] + Range;                           // R2
   pivot[4] = pivot[2] + Range;                           // R3
   pivot[5] = 2 * pivot[0] - PDayHigh;                    // S1
   pivot[6] = pivot[0] - Range;                           // S2
   pivot[7] = pivot[5] - Range;                           // S3

   if(DrawPivots){
      SetArrow(4,Red,       pivot[2]," R1 ");
      SetArrow(4,Blue,      pivot[5]," S1 ");
      SetArrow(4,Red,       pivot[3]," R2 ");
      SetArrow(4,Blue,      pivot[6]," S2 ");
      SetArrow(4,Red,       pivot[4]," R3 ");
      SetArrow(4,Blue,      pivot[7]," S3 ");            
      SetArrow(4,Gray  ,    pivot[0]," Pivot ");
   }
   if(up_dn == "lo"){
      return(Pivot - Range);
   }   
   if(up_dn == "hi"){
      return(Pivot + Range);
   }
   return(Pivot); 
}

Thank you

Antony

 

I did have a play about with the up_dn part but that stopped it from picking up any conditions.

Perhaps it this, could it be causng a bias for just pivots higher than the current price:

double PivotLine(string nm){
   if(nm==""){double dst=1000000,piv=0;
      for(int i=2;i<8;i++){
         if(MathAbs(Close[0]-pivot[i])<dst){
            dst = MathAbs(Close[0]-pivot[i]);
            piv = pivot[i];
         }
      }
      return(piv);

Thanks

Antony

Reason: