#property strict

#property indicator_separate_window
#property indicator_minimum    0
#property indicator_maximum    1
#property indicator_buffers    1
#property indicator_color1     DodgerBlue
#property indicator_level1     0.50
#property indicator_levelcolor clrWhite
#property indicator_levelstyle STYLE_DOT
input int InpRPIPeriod=14; 
double ExtRPIBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit(void)
  {
   string short_name;
   IndicatorBuffers(1);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,ExtRPIBuffer);
   short_name="RPI ("+string(InpRPIPeriod)+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,short_name);
   if(InpRPIPeriod<2)
     {
      Print("Incorrect value for input variable InpRSIPeriod = ",InpRPIPeriod);
      return(INIT_FAILED);
     }
   SetIndexDrawBegin(0,InpRPIPeriod);
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Relative Price Index                                          |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
   int i,pos;
//---
   if(Bars<=InpRPIPeriod || InpRPIPeriod<2)
      return(0);
   ArraySetAsSeries(ExtRPIBuffer,false);
//--- the main loop of calculations
   for(i=pos; i<rates_total && !IsStopped(); i++)
     {
      double highestLeft = High[iHighest(NULL,0,MODE_HIGH,InpRPIPeriod,i)];
      
      double lowestLeft = Low[iLowest(NULL,0,MODE_LOW,InpRPIPeriod,i)];
      
      double rpi = (Close[i] - lowestLeft) / (highestLeft - lowestLeft);
      ExtRPIBuffer[i]=rpi;
     }
//---
   return(rates_total);
  }
//+------------------------------------------------------------------+
