iHighest / iLowest isn't working correct (or as expected).

 

I'm creating a function to try and return the PriceRank of the current symbol.  By using the IV Rank calculation, I want want to calculate the Price Rank.

I've used it, but some how, I'm getting higher current Closing prices than the Highest within the last 52 weeks.

Can someone tell me what I'm doing wrong?

Here is my code:

double PriceRank()
{
   int valIdx, valHighest, valLowest;
   double pctHigh, pctCurrent;
   double val;
   
   valIdx = iHighest(NULL, PERIOD_W1, MODE_HIGH, 52, 0);
   
   if(valIdx != -1)
      valHighest = valIdx;
   else
      Print("Error finding highest 52 week value.  Error(", GetLastError(), ")");
      
   valIdx = iLowest(NULL, PERIOD_W1, MODE_LOW, 52, 0);
   if(valIdx != -1)
      valLowest = valIdx;
   else
      Print("Error finding highest 52 week value.  Error(", GetLastError(), ")");
      
   Print("High: ", High[valHighest], "   Low: ", Low[valLowest], "   Current: ", Close[0]);
   
   pctCurrent = 100 * NormalizeDouble(Close[0] / High[valHighest] * 100, 0);
   val = (Close[0] - Low[valLowest]) / (High[valHighest] - Low[valLowest]);
   
   return(val);
}

Screenshot attached of the Journal showing an example.  My thought is that there is no way the closing price can be higher than the 52wk high.  At most it can be equal.

Files:
 
jthornton:

I'm creating a function to try and return the PriceRank of the current symbol.  By using the IV Rank calculation, I want want to calculate the Price Rank.

I've used it, but some how, I'm getting higher current Closing prices than the Highest within the last 52 weeks.

Can someone tell me what I'm doing wrong?

Here is my code:

Screenshot attached of the Journal showing an example.  My thought is that there is no way the closing price can be higher than the 52wk high.  At most it can be equal.

Here is another example of the Lowest actually being higher than the Highest. 

What am I doing wrong?

Files:
 

You are specifying a timeframe (PERIOD_W1) on your iHighest / iLowest calls, but then you use High[] and Low[] which reference the current timeframe.

So unless your current timeframe is PERIOD_W1, you will get the wrong result.

Try using iHigh() and iLow() instead.

 
Print("High: ", High[valHighest], "   Low: ", Low[valLowest], "   Current: ", Close[0]);

use iHigh for the weekly timeframe.

You are getting values for the index on the current chart time-frame.

 
You are mixing apples and oranges.
 valLowest= iLowest(NULL, PERIOD_W1, MODE_LOW, 52, 0);      
   Print("High: ", High[valHighest], "   Low: ", Low[valLowest], "
 
whroeder1:
You are mixing apples and oranges.

I see what you guys are saying.  So, I'm using the INDEX from the W1 timeframe and lets say that the iHighest returns a shift of 8 weeks ago.  My High[valHighest] is going to return the High value from 8 days ago (on the D1 timeframe).

So, I need to extract the datetime from the shift that iHighest is returning and then figure out what the equivalent shift is for D1?

Am I on the right track?

If so, thank you!

 
jthornton: So, I need to extract the datetime from the shift that iHighest is returning and then figure out what the equivalent shift is for D1? Am I on the right track?

Close. It depends on what you are trying to do.

In your case, you have a highest week. You could use iBarShift( weekStart ) and get the starting day of that week.  But that won't get you the high. Which day of the week? You have the shift, just get the price of that bar iHigh.

If you wanted the bar on a lower TF, you'd start with the starting bar iBarShift(weekStart). Then the last bar iBarShift( weekStart + PeriodSeconds(PERIOD_W1) - 1) And then find which bar in that range.

 
whroeder1:

Close. It depends on what you are trying to do.

In your case, you have a highest week. You could use iBarShift( weekStart ) and get the starting day of that week.  But that won't get you the high. Which day of the week? You have the shift, just get the price of that bar iHigh.

If you wanted the bar on a lower TF, you'd start with the starting bar iBarShift(weekStart). Then the last bar iBarShift( weekStart + PeriodSeconds(PERIOD_W1) - 1) And then find which bar in that range.

Cool.  I'll experiment tonight.  Thanks!