MathMedian() - page 2

 
//+------------------------------------------------------------------+
//|                                           SimpleMovingMedian.mq5 |
//+------------------------------------------------------------------+
#property description    "Simple Moving Median"
#property description    " "
#property description    " "
#property description    "MQL5 Profile: https://www.mql5.com/en/users/tonegarot"
#property description    "My Website: http://www.garot.com/trading/"
#property link           "https://www.mql5.com/en/forum/273812"
#property copyright      "Anthony Garot & Arthur Albano"
#property strict

//--- indicator settings
#property indicator_chart_window

#property indicator_buffers 1
#property indicator_plots   1
#property indicator_type1   DRAW_LINE
#property indicator_color1  LightSeaGreen
#property indicator_width1  1

#include <Math\Stat\Normal.mqh>

//--- input parameters
input int InpLookbackPeriod=12;        // Period
input int InpShift = -6; // Shift
//--- indicator buffers
double    ExtMedianBuffer[];

void OnInit()
{
    //--- indicator buffers mapping
    SetIndexBuffer(0,ExtMedianBuffer,INDICATOR_DATA);
    //--- set accuracy
    IndicatorSetInteger(INDICATOR_DIGITS,2);
    //--- name for DataWindow and indicator subwindow label
    IndicatorSetString(INDICATOR_SHORTNAME,"Med("+string(InpLookbackPeriod)+";"+string(InpShift)+")");
    //--- set plot shift along the horizontal axis by InpShift bars
    PlotIndexSetInteger(0,PLOT_SHIFT,InpShift);
    //--- set plot begin from the bar with number InpLookbackPeriod
    PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,InpLookbackPeriod);
    
    ArraySetAsSeries(ExtMedianBuffer,false);
    //--- initialization done
}

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,limit;

    //--- check for rates count
    if(rates_total<InpLookbackPeriod)
        return(0);

    //--- Checking and calculating the number of countable bars
    limit=rates_total-prev_calculated;
    if(limit>1)
    {
        limit=rates_total-1;
    }

    double ExtPercentile = 0.5;
    
    //--- the main loop of calculations
    for(i=limit; i>=0 && !IsStopped(); i--)
    {
        double medianArray[];
        ArrayCopy(medianArray, close, 0, i-InpLookbackPeriod+1, InpLookbackPeriod);
        ExtMedianBuffer[i]=MathNumericalPercentile(medianArray,ExtPercentile);
    }

    //--- OnCalculate done. Return new prev_calculated.
   return(rates_total);
}
//+------------------------------------------------------------------+
 

Fixed simulation problem.

//+------------------------------------------------------------------+
//|                                           SimpleMovingMedian.mq5 |
//+------------------------------------------------------------------+
#property description    "Simple Moving Median"
#property description    " "
#property description    " "
#property description    "MQL5 Profile: https://www.mql5.com/en/users/tonegarot"
#property description    "My Website: http://www.garot.com/trading/"
#property link           "Created for https://www.mql5.com/en/forum/273812"
#property copyright      "Anthony Garot & Arthur Albano"
#property strict

//--- indicator settings
#property indicator_chart_window

#property indicator_buffers 1
#property indicator_plots   1
#property indicator_type1   DRAW_LINE
#property indicator_color1  LightSeaGreen
#property indicator_width1  1

#include <Math\Stat\Normal.mqh>

//--- input parameters
input int InpLookbackPeriod=12;        // Period
input int InpShift = -6; // Shift
//--- indicator buffers
double    ExtMedianBuffer[];

void OnInit()
{
    //--- indicator buffers mapping
    SetIndexBuffer(0,ExtMedianBuffer,INDICATOR_DATA);
    //--- set accuracy
    IndicatorSetInteger(INDICATOR_DIGITS,2);
    //--- name for DataWindow and indicator subwindow label
    IndicatorSetString(INDICATOR_SHORTNAME,"Med("+string(InpLookbackPeriod)+";"+string(InpShift)+")");
    //--- set plot shift along the horizontal axis by InpShift bars
    PlotIndexSetInteger(0,PLOT_SHIFT,InpShift);
    //--- set plot begin from the bar with number InpLookbackPeriod
    PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,InpLookbackPeriod);
    
    ArraySetAsSeries(ExtMedianBuffer,false);
    //--- initialization done
}

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,limit;

    //--- check for rates count
    if(rates_total<InpLookbackPeriod)
        return(0);

    //--- Checking and calculating the number of countable bars

   if(prev_calculated<InpLookbackPeriod)
      limit=InpLookbackPeriod;
   else limit=prev_calculated-1;

    double ExtPercentile = 0.5;
    
    //--- the main loop of calculations
    for(i=limit; i>=0 && !IsStopped(); i--)
    {
        double medianArray[];
        ArrayCopy(medianArray, close, 0, i-InpLookbackPeriod+1, InpLookbackPeriod);
        ExtMedianBuffer[i]=MathNumericalPercentile(medianArray,ExtPercentile);
    }

    //--- OnCalculate done. Return new prev_calculated.
   return(rates_total);
}
//+------------------------------------------------------------------+