Determine the Nearest Quarter Point

 

Hello All,


I could use some help with determining what I am doing wrong when it comes to getting the nearest quarter point to a relative price on the chart.

Basically I have a fib on the Asia Range, I am looking for a quarter point above or below the Asia Range in the 50-100 Fib retracement. While my code is able to determine a quarter point and some times it will return both quarter points that I am looking for. I will post my code and see if anyone can just point me in the right direction, possibly just add more price points, not sure. Maybe just maybe someone knows a better way to do it and would like to share. 

Per the Photo uploaded, the red line is drawn by the EA, the black line was manually drawn and is the nearest QP to the 100 fib retracement. the EA should draw both lines buts its not, these lines will be used as entry points later in the EA.  


double NearestQuarterPoint(double PriceToRound)
  {


   aMajorSQP = (ceil((iClose(_Symbol, PERIOD_D1, 0) * 100)) / 100);
   bMajorSQP = (floor((iClose(_Symbol, PERIOD_D1, 0) * 100)) / 100);
   oaMajorSQP = aMajorSQP + (250 * Point);
   uaMajorSQP = aMajorSQP - (250 * Point);
   mMajorSQP   =  aMajorSQP - (500 * Point);
   obMajorSQP = bMajorSQP + (250 * Point);
   ubMajorSQP = bMajorSQP - (250 * Point);
   nRound  = (round(PriceToRound * 10000) / 10000);


   /*

   1.3025 = aMajorSQP+125
   if Price < aMajorSQP+125 && Price > aMajorSQP-125 = aMajorSQP
   1.30 = aMajorSQP
   1.29875 = aMajorSQP-125
   if Price < uaMajorSQP+125 && Price > uaMajorSQP-125 = uaMajorSQP
   1.2975 = uaMajorSQP
   1.29625 = uaMajorSQP-125
   if Price < uaMajorSQP-125 && Price > obMajorSQP+125 = mMajorSQP
   1.2950 = mMajorSQP
   1.29375 = obMajorSQP+125
   if Price < obMajorSQP+125 && Price > obMajorSQP-125 = obMajorSQP
   1.2925 = obMajorSQP
   1.29125 = obMajorSQP-125
   if Price < obMajorSQP-125 && Price > bMajorSQP-125 = bMajorSQP
   1.29 = bMajorSQP
   1.28875 = bMajorSQP-125


   */

   if(nRound < oaMajorSQP + (125 * Point) && nRound > aMajorSQP + (125 * Point))
     {
      if(PrintStatements)
        {Print("Nearst SQP is: ", oaMajorSQP);}
      nQP = oaMajorSQP;
     }
   if(nRound < aMajorSQP + (125 * Point) && nRound > aMajorSQP - (125 * Point))
     {
      if(PrintStatements)
        {Print("Nearst SQP is: ", aMajorSQP);}
      nQP = aMajorSQP;
     }

   if(nRound < uaMajorSQP + (125 * Point) && nRound > uaMajorSQP - (125 * Point))
     {
      if(PrintStatements)
         Print("Nearst SQP is: ", uaMajorSQP);
      nQP = uaMajorSQP;
     }

   if(nRound < uaMajorSQP - (125 * Point) &&  nRound > obMajorSQP + (125 * Point))
     {
      if(PrintStatements)
         Print("Nearst SQP is: ", uaMajorSQP - (250 * Point));
      nQP = uaMajorSQP - (250 * Point);
     }

   if(nRound < obMajorSQP + (125 * Point) &&  nRound > obMajorSQP - (125 * Point))
     {
      if(PrintStatements)
         Print("Nearst SQP is: ", obMajorSQP);
      nQP = obMajorSQP;
     }

   if(nRound < obMajorSQP - (125 * Point) &&  nRound > bMajorSQP - (125 * Point))
     {
      if(PrintStatements)
         Print("Nearst SQP is: ", bMajorSQP);
      nQP = bMajorSQP;
     }

   if(nRound < bMajorSQP - (125 * Point) &&  nRound > ubMajorSQP - (125 * Point))
     {
      if(PrintStatements)
         Print("Nearst SQP is: ", ubMajorSQP);
      nQP = ubMajorSQP;
     }


   return nQP;
  }
Files:
SH_EA.png  29 kb
 

Adding two additional price points seemed to do resolve my issue.


double NearestQuarterPoint(double PriceToRound)
  {


   aMajorSQP = (ceil((iClose(_Symbol, PERIOD_D1, 0) * 100)) / 100);
   bMajorSQP = (floor((iClose(_Symbol, PERIOD_D1, 0) * 100)) / 100);
   oaMajorSQP = aMajorSQP + (250 * Point);
   uaMajorSQP = aMajorSQP - (250 * Point);

   omMajorSQP   =  aMajorSQP + (500 * Point);//

   mMajorSQP   =  aMajorSQP - (500 * Point);

   umMajorSQP   =  bMajorSQP - (500 * Point); //

   obMajorSQP = bMajorSQP + (250 * Point);
   ubMajorSQP = bMajorSQP - (250 * Point);
   nRound  = (round(PriceToRound * 10000) / 10000);


   /*

   1.3025 = aMajorSQP+125
   if Price < aMajorSQP+125 && Price > aMajorSQP-125 = aMajorSQP
   1.30 = aMajorSQP
   1.29875 = aMajorSQP-125
   if Price < uaMajorSQP+125 && Price > uaMajorSQP-125 = uaMajorSQP
   1.2975 = uaMajorSQP
   1.29625 = uaMajorSQP-125
   if Price < uaMajorSQP-125 && Price > obMajorSQP+125 = mMajorSQP
   1.2950 = mMajorSQP
   1.29375 = obMajorSQP+125
   if Price < obMajorSQP+125 && Price > obMajorSQP-125 = obMajorSQP
   1.2925 = obMajorSQP
   1.29125 = obMajorSQP-125
   if Price < obMajorSQP-125 && Price > bMajorSQP-125 = bMajorSQP
   1.29 = bMajorSQP
   1.28875 = bMajorSQP-125

1.1455 < 1.14625 && 1.1455 > 1.14375 = 1.1455

   */

   if(nRound < umMajorSQP + (125 * Point) && nRound > oaMajorSQP + (125 * Point))
     {
      if(PrintStatements)
        {Print("Nearst SQP is: ", umMajorSQP);}
      nQP = umMajorSQP;
     }
   if(nRound < oaMajorSQP + (125 * Point) && nRound > aMajorSQP + (125 * Point))
     {
      if(PrintStatements)
        {Print("Nearst SQP is: ", oaMajorSQP);}
      nQP = oaMajorSQP;
     }
   if(nRound < aMajorSQP + (125 * Point) && nRound > aMajorSQP - (125 * Point))
     {
      if(PrintStatements)
        {Print("Nearst SQP is: ", aMajorSQP);}
      nQP = aMajorSQP;
     }

   if(nRound < uaMajorSQP + (125 * Point) && nRound > uaMajorSQP - (125 * Point))
     {
      if(PrintStatements)
         Print("Nearst SQP is: ", uaMajorSQP);
      nQP = uaMajorSQP;
     }

   if(nRound < uaMajorSQP - (125 * Point) &&  nRound > obMajorSQP + (125 * Point))
     {
      if(PrintStatements)
         Print("Nearst SQP is: ", uaMajorSQP - (250 * Point));
      nQP = uaMajorSQP - (250 * Point);
     }

   if(nRound < obMajorSQP + (125 * Point) &&  nRound > obMajorSQP - (125 * Point))
     {
      if(PrintStatements)
         Print("Nearst SQP is: ", obMajorSQP);
      nQP = obMajorSQP;
     }

   if(nRound < obMajorSQP - (125 * Point) &&  nRound > bMajorSQP - (125 * Point))
     {
      if(PrintStatements)
         Print("Nearst SQP is: ", bMajorSQP);
      nQP = bMajorSQP;
     }

   if(nRound < bMajorSQP - (125 * Point) &&  nRound > ubMajorSQP - (125 * Point))
     {
      if(PrintStatements)
         Print("Nearst SQP is: ", ubMajorSQP);
      nQP = ubMajorSQP;
     }
   if(nRound < ubMajorSQP - (125 * Point) &&  nRound > umMajorSQP  - (125 * Point))
     {
      if(PrintStatements)
         Print("Nearst SQP is: ", umMajorSQP);
      nQP = umMajorSQP;
     }


   return nQP;
  }
Reason: