Help, percentile rank. - page 2

 
Andres452:
I already corrected my comment, now if you can see the images :)

No idea what settings they used, but it should look (and how it compares to ATR) something like this :


#property copyright ""
#property link      ""
#property version   ""
#property strict
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots   1
//
#property indicator_label1  "Rank"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrDodgerBlue
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1

input int inpAtrPeriod =17;
input int inpRankPeriod=23;

double ATRBuffer[],rankBuffer[];

//------------------------------------------------------------------
//
//------------------------------------------------------------------
int OnInit()
{
   IndicatorBuffers(2);
      SetIndexBuffer(0,rankBuffer);
      SetIndexBuffer(1,ATRBuffer);
   return(INIT_SUCCEEDED);
}

//
//
//

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 limit = MathMin(rates_total-prev_calculated,rates_total-1);    
   
   for(int i=limit; i>=0; i--)
   {      
      ATRBuffer[i]  = iATR(NULL,0,inpAtrPeriod,i);
      rankBuffer[i] = PercentileRank(ATRBuffer,i,inpRankPeriod,rates_total);
   }
   return(rates_total);
}

//
//
//

double PercentileRank(double& arr[], int i, int period, int arrSize) 
{ 
    double count   = 0; 
    double percent = 0; 
  
          for (int k=1; k<period && (i+k)<arrSize; k++) 
          { 
            if (arr[i] > arr[i+k]) count++; 
          } 
          percent = (count * 100.0) / (period - 1); 
   return(percent);
} 
 
Mladen Rakic:

No idea what settings they used, but it should look (and how it compares to ATR) something like this :


Thanks for the help, I'm testing your code, do you know why it doesn't give me any results when I change the variable inpRankPeriod for the value 1 ?


Modify line 69, percent = (count * 100.0) / (period);


But it still doesn't give me results, do you know what could happen ?

 
Andres452:

Thanks for the help, I'm testing your code, do you know why it doesn't give me any results when I change the variable inpRankPeriod for the value 1 ?


Modify line 69, percent = (count * 100.0) / (period);


But it still doesn't give me results, do you know what could happen ?

You can not rank just one value (ie: period 1 - one single value can not be smaller or greater than itself)

And don't change that line of code - nothing does not need to be changed there

 
Andres452:

I also do not understand the setting of this indicator in tradingview pinescript, but I enter the value 1 in pinescript and it gives me a result between 100 and 0.


So I asked you in the comment above.


I always use 2 different settings in tradingview and I want to set the same in mql4, percentrank(atr(17),23) and percentrank(atr(100),1).


The percentrank(atr(100),1) setting in tradingview pinescript gives me only 2 results, value 0 or value 100.

You mean it "oscillates" between 0 and 100 when period is 1? Ie: once it is greater than itself and the other time it is smaller than itself?

Tell them to correct their percent rank function


Now forgive me : I rest my case. Nothing to be added 
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
  • www.mql5.com
//| Expert initialization function                                   | //| Expert deinitialization function                                 | //| Expert tick function                                             | //| test1                                                            |...
 
Mladen Rakic:

You mean it "oscillates" between 0 and 100 when period is 1? Ie: once it is greater than itself and the other time it is smaller than itself?

Tell them to correct their percent rank function


Now forgive me : I rest my case. Nothing to be added 

Ok, I'll keep looking for a solution so that the formula percentrank(atr(100),1) returns results in mql4.

 
Andres452 #:

Ok, I'll keep looking for a solution so that the formula percentrank(atr(100),1) returns results in mql4.

Use this

percentrank(atr(1),100) 

Percent rank of truerange over the last 100