RunMedian
Download scriptThis technical tool is designed to provide traders with a "Median" price line on their charts. Unlike many standard indicators that use an average (where one extreme high or low price can significantly pull the result in one direction), this script identifies the middle value of a specific price range, offering a smoother and more balanced view of market sentiment.
How it Works
1. Identifying the Data Point Instead of looking solely at the closing price, the tool calculates the midpoint of every candle by taking the average of its highest and lowest points. This ensures that the entire range of price action within each period is considered.
2. The "Median" Calculation The script looks at a specific number of candles (the "period") defined by the user. It gathers all those midpoints and organizes them from lowest to highest. It then selects the exact value in the middle of that organized list. By choosing the center point rather than a mathematical average, the indicator becomes more resilient to "outliers"—those sudden, temporary price spikes or crashes that often distort traditional moving averages.
3. Smart Performance Logic To ensure the platform remains responsive and fast, the script uses two different methods of calculation:
- Initial Setup: When first loading the chart, it performs a comprehensive scan to establish the baseline values.
- Incremental Updates: As new candles form in real-time, the script doesn't recalculate everything from scratch. Instead, it intelligently "swaps" the oldest piece of data out and inserts the newest piece into its correct sorted position. This allows the indicator to update instantly without lagging your trading platform.
4. Automatic Adjustments The tool is designed with built-in reliability. Because a median calculation requires an odd number of data points to find a perfect center, the script includes a safety feature: if a user inputs an even number, the tool will automatically adjust it by one to ensure the math remains accurate.
Summary for the Trader
In short, this indicator provides a "central tendency" line. It filters out the noise of extreme volatility to show you where the "fair price" is currently sitting based on the middle of the recent price action. Because it uses a median-based approach rather than a standard average, it reacts more predictably to market trends while ignoring temporary price shocks.
//+------------------------------------------------------------------+
//| RunMedian.mq4 |
//| Copyright © 2012, HindmostFr |
//| http://hindmost.fr/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2012, HindmostFr http://hindmost.fr/"
#property link "http://hindmost.fr/"
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/.
*/
#include <stdlib.mqh>
// indicator settings
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 White
// indicator parameters
extern int period=13;
extern string comment="period must be odd - or +1 will be added";
// indicator buffers
double Median[];
void runmedian(int limit) {
double tab[];
int i,cursor,count,oldpos,sortedpos;
double value, oldvalue;
ArrayResize(tab,period);
count=0;
for (cursor=limit;cursor>=0;cursor--) {
value=(High[cursor]+Low[cursor])/2;
if (count<period) {
// Initial median (slow)
tab[count]=value;
if (count==(period-1)) {
ArraySort(tab);
Median[cursor]=tab[(period-1)/2];
}
} else {
// Incremental algorithm (fast)
// Remove old value
oldpos=cursor+period;
oldvalue=(High[oldpos]+Low[oldpos])/2;
sortedpos=ArrayBsearch(tab,oldvalue);
for (i=sortedpos;i<(period-1);i++) tab[i]=tab[i+1];
tab[period-1]=EMPTY_VALUE;
// Add new value at the right sorted place
sortedpos=ArrayBsearch(tab,value);
if (tab[sortedpos]<value) sortedpos++;
for (i=period-1;i>sortedpos;i--) tab[i]=tab[i-1];
tab[sortedpos]=value;
// tab is now sorted
Median[cursor]=tab[(period-1)/2];
}
count++;
}
}
int init()
{
if (MathMod(period,2)==0) period=period+1;
IndicatorBuffers(1);
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,Median);
SetIndexLabel(0,"Median ("+DoubleToStr(period,0)+")");
return(0);
}
int deinit()
{
return(0);
}
int start()
{
int counted_bars=IndicatorCounted();
// last counted bar will be recounted
if(counted_bars>0) counted_bars--;
int limit=Bars-counted_bars;
// function call
runmedian(limit);
// done
return(0);
}
Comments