Find percent between high and low over previous 24 trading hours

 

I am trying to find how the current price of a pair ranks compared to prices over the past 24 trading hours.  

For example, if it is at the current high of the previous 24 hour periods then it should return "1" and if it is at the current low of the previous 24 hour periods then it should return "0" and if it is have way between the low and high of the past 24 trading hours it should return "0.5". 

My code returns strange numbers.  Sometimes it returns negative numbers and sometimes numbers greater than 1.

Alert("GBPCHF: ",  DoubleToStr(RangePercent("GBPCHF"),9));

double RangePercent(string pair)  {
   double high = High[iHighest(pair,PERIOD_H1,MODE_HIGH,24,0)];
   double low =   Low[iLowest (pair,PERIOD_H1,MODE_LOW ,24,0)];
   double current = (MarketInfo(pair, MODE_ASK) + MarketInfo(pair, MODE_BID)) /2;
   double range = high - low;
   double diff = current - low;
   return (diff / range);
}
 
  1. current = (MarketInfo(pair, MODE_ASK) + MarketInfo(pair, MODE_BID)) /2;
    Drop the Ask. MT4 charts are bid charts. Your high/low are bid values.
  2. diff / range is a number between zero and one (inclusive.) No integer conversions required.
    double fraction = diff / range;
    if(fraction > 0.66) return(1);
    if(fraction > 0.33) return(0);
    return(0.5);

 
WHRoeder:
  1. Drop the Ask. MT4 charts are bid charts. Your high/low are bid values.
  2. diff / range is a number between zero and one (inclusive.) No integer conversions required.
Yeah, I missed the fact that he's looking for a Fraction in the title earlier.
 

Thanks for the helpful suggestions, however there is still a problem. My code only works if it is attached to the GBPAUD chart.  If it is attached to a different chart, like BGPJPY I get a bad result:

 High: 7.5495 | Low: 7.5483 | Current: 1.4805 | Range: 0.0012 | Diff: -6.0678 | Percent: -4973.5902

int start() {
 Print("GBPAUD: ",  DoubleToStr(RangePercent("GBPAUD"),9));
}

double RangePercent(string pair)  {
   double high = High[iHighest(pair,PERIOD_H1,MODE_HIGH,24,0)];
   double low =   Low[iLowest (pair,PERIOD_H1,MODE_LOW ,24,0)];
   double current = MarketInfo(pair, MODE_BID);
   double range = high - low;
   double diff = current - low;
   Alert("High: ",high, " | Low: ", low, " | Current: ", current, " | Range: ", range, " | Diff: ", diff, " | Percent: ", (  diff / range  ));
   if(range==0) return (0); 
   else return (diff / range);
}
 

High: 7.5495 | Low: 7.5483 | Current: 1.4805 |

How can current be outside the range of High___Low? 

 
hknight:

Thanks for the helpful suggestions, however there is still a problem. My code only works if it is attached to the GBPAUD chart.  If it is attached to a different chart, like BGPJPY I get a bad result:

 High: 7.5495 | Low: 7.5483 | Current: 1.4805 | Range: 0.0012 | Diff: -6.0678 | Percent: -4973.5902

 

Well use iHigh() and iLow() instead of High[] and Low[]
 
I want to use iHighest NOT iHigh.  Why is it getting using the pair from the chart anyway?
 

Because you're using High[].

double high = High         [iHighest(pair,PERIOD_H1,MODE_HIGH,24,0)];

High[] returns the value of the Attached_Chart. 

https://docs.mql4.com/predefined/variables/high

If you're going to pass Symbol_Names then use iHigh( pair, period, iHighest() );

https://docs.mql4.com/series/iHigh 

 

How can I do this without getting an error? When I change  High[] and Low[] to iHigh() and iLow() I get an error.

 

int start() {
 Print("GBPAUD: ",  DoubleToStr(RangePercent("GBPAUD"),9));
}

double RangePercent(string pair)  {
   double high = iHigh(iHighest(pair,PERIOD_H1,MODE_HIGH,24,0));
   double low =   iLow(iLowest (pair,PERIOD_H1,MODE_LOW ,24,0));
   double current = MarketInfo(pair, MODE_BID);
   double range = high - low;
   double diff = current - low;
   Alert(pair, " | ", "High: ",high, " | Low: ", low, " | Current: ", current, " | Range: ", range, " | Diff: ", diff, " | Percent: ", (  diff / range  ));
   if(range==0) return (0); 
   else return (diff / range);
}
 
 double high = iHigh(iHighest(pair,PERIOD_H1,MODE_HIGH,24,0));
RTFM iHigh takes 3 arguments. The first is NOT an integer.
Reason: