Help, percentile rank.

 
Greetings friends.

I've been through the forum and can't find a percentile rank indicator for metatrader 4 (mql4) and I have no idea how to code it either since I'm new to mql4, I come from tradingview and there I have a rank indicator percentile in pine script, which is the tradingview programming language and it has a function called percentrank, but I can't find any similar function or indicator in metatrader4 (mql4) that shows only the percentile range, I would be very grateful if you help me to code it or give me an idea of how to do it, I attach a tradingview image showing the function I am talking about in the pine script.

Thank you.


 
Andres452: I also have no idea how to code it since I'm new to mql4, … I would be very grateful if you help me to code it or give me an idea of how To do it …
  1. Help you with what? You haven't stated a problem, you stated a want. You have only four choices:
    1. Search for it. Do you expect us to do your research for you?

    2. Beg at:

    3. MT4: Learn to code it.
      MT5: Begin learning to code it.
      If you don't learn MQL4/5, there is no common language for us to communicate. If we tell you what you need, you can't code it. If we give you the code, you don't know how to integrate it into yours.

    4. or pay (Freelance) someone to code it.
                Hiring to write script - General - MQL5 programming forum 2019.08.21

    We're not going to code it for you (although it could happen if you are lucky or the problem is interesting.) We are willing to help you when you post your attempt (using CODE button) and state the nature of your problem.
              No free help 2017.04.21

  2. How grateful?
    Not compiled, not tested, just typed.
    double percentrank(double value, const double& arr[], int length=WHOLE_ARRAY, int iBeg=0){
       if(length == WHOLE_ARRAY) length = ArraySize(arr) - iBeg;
       int iEnd = length + iBeg;
       int count=0;
       while(iBeg < iEnd) if(arr[iBeg++] <= value) ++count;
       return double(count) / length;
    }
    Not compiled, not tested, just typed.
 
William Roeder:
  1. Help you with what? You haven't stated a problem, you stated a want. You have only four choices:
    1. Search for it. Do you expect us to do your research for you?

    2. Beg at:

    3. MT4: Learn to code it.
      MT5: Begin learning to code it.
      If you don't learn MQL4/5, there is no common language for us to communicate. If we tell you what you need, you can't code it. If we give you the code, you don't know how to integrate it into yours.

    4. or pay (Freelance) someone to code it.
                Hiring to write script - General - MQL5 programming forum 2019.08.21

    We're not going to code it for you (although it could happen if you are lucky or the problem is interesting.) We are willing to help you when you post your attempt (using CODE button) and state the nature of your problem.
              No free help 2017.04.21

  2. How grateful?
    Not compiled, not tested, just typed. Not compiled, not tested, just typed.


Thanks for your help, this is my code, the result must be between 0 and 100 but for some reason the result is a negative value, you know why ?


Actually what I'm looking to graph is a line with this formula: PercentRank1(iATR(17),23) where 23 is the number of the historical bars.


//+------------------------------------------------------------------+
//|                                                  CCP PRUEBAS.mq4 |
//|                        Copyright 2020, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots   1
//--- plot ATR
#property indicator_label1  "ATR"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrAqua
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- indicator buffers
double         ATRBuffer[];
double         tmpArray[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+

   double PercentRank1(double r_period, int shift)
   {
      double pcrank, rank;
      //int i;
      
      ArrayCopy(tmpArray, High, 0, shift, r_period);
      ArrayCopy(tmpArray, Low, r_period, shift, r_period);
      ArrayCopy(tmpArray, Close, r_period * 2, shift, r_period);
      ArraySort(tmpArray, r_period * 3);
      rank = ArrayBsearch(tmpArray, Close[shift], r_period * 3);
      pcrank = rank /(r_period * 3 - 1);
      
      return (pcrank);   
   }  
   

int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,ATRBuffer);
   
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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 = rates_total-prev_calculated;    
   
   
   for(int i=0; i<limit; i++)
   {      
      ATRBuffer[i] = PercentRank1(iATR(NULL,0,17,i),23)      
   }
   
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
 
Andres452: the result must be between 0 and 100 but for some reason the result is a negative value, you know why ?
 ATRBuffer[i] = PercentRank1(iATR(NULL,0,17,i),23)      

Prices are 1.2345º. ATR is 0.00xxx. You will never find an ATR in the range of prices.

 
William Roeder:

Los precios son 1.2345º. ATR es 0.00xxx. Nunca encontrará un ATR en el rango de precios.

So, I don't understand why in tradingview pinescript if it works, and the formula is exactly the same, the calculation results are between 0 and 100.
 
Andres452:
So, I don't understand why in tradingview pinescript if it works, and the formula is exactly the same, the calculation results are between 0 and 100.
Can you post a screenshot with the expected result (from tradingview ) ?
 
Alain Verleyen : ¿Puedes publicar una captura de pantalla con el resultado esperado?


Ok


.

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                                                            |...
 
Andres452:


Yes, this is the result of tradingview, I am looking for exactly the same but in mql4


https://i.ibb.co/R2mZngN/j45j45j.png

Post a clickable link or insert the picture please. See the editor toolbar buttons.
 
Alain Verleyen:
Post a clickable link or insert the picture please. See the editor toolbar buttons.
I already corrected my comment, now if you can see the images :)
 
Andres452:
I already corrected my comment, now if you can see the images :)
Yes. It just needs to be coded, perfectly and easily doable with mql4.
 
Alain Verleyen : Si. Solo necesita ser codificado, perfecta y fácilmente factible con mql4.
I think I already have it coded, but for some reason my code throws negative results, do you know what it could be ?
Reason: