-
Notifications
You must be signed in to change notification settings - Fork 1
/
RunMedian.mq4
108 lines (88 loc) · 3.09 KB
/
RunMedian.mq4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//+------------------------------------------------------------------+
//| RunMedian.mq4 |
//| Copyright © 2012, Stephane Coulondre |
//| http://stephane.coulondre.info |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2012, Stephane Coulondre"
#property link "http://stephane.coulondre.info/"
/*
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=MathMax(limit,period);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;
if(counted_bars==0) limit--;
// function call
runmedian(limit);
// done
return(0);
}