-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCustom Moving Averages.mq4
190 lines (188 loc) · 6.91 KB
/
Custom Moving Averages.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//+------------------------------------------------------------------+
//| Custom Moving Averages.mq4 |
//| Copyright 2005-2015, MetaQuotes Software Corp. |
//| http://www.mql4.com |
//+------------------------------------------------------------------+
#property copyright "2005-2015, MetaQuotes Software Corp."
#property link "http://www.mql4.com"
#property description "Moving Average"
#property strict
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Red
//--- indicator parameters
input int InpMAPeriod=13; // Period
input int InpMAShift=0; // Shift
input ENUM_MA_METHOD InpMAMethod=MODE_SMA; // Method
//--- indicator buffer
double ExtLineBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit(void)
{
string short_name;
int draw_begin=InpMAPeriod-1;
//--- indicator short name
switch(InpMAMethod)
{
case MODE_SMA : short_name="SMA("; break;
case MODE_EMA : short_name="EMA("; draw_begin=0; break;
case MODE_SMMA : short_name="SMMA("; break;
case MODE_LWMA : short_name="LWMA("; break;
default : return(INIT_FAILED);
}
IndicatorShortName(short_name+string(InpMAPeriod)+")");
IndicatorDigits(Digits);
//--- check for input
if(InpMAPeriod<2)
return(INIT_FAILED);
//--- drawing settings
SetIndexStyle(0,DRAW_LINE);
SetIndexShift(0,InpMAShift);
SetIndexDrawBegin(0,draw_begin);
//--- indicator buffers mapping
SetIndexBuffer(0,ExtLineBuffer);
//--- initialization done
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Moving Average |
//+------------------------------------------------------------------+
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[])
{
//--- check for bars count
if(rates_total<InpMAPeriod-1 || InpMAPeriod<2)
return(0);
//--- counting from 0 to rates_total
ArraySetAsSeries(ExtLineBuffer,false);
ArraySetAsSeries(close,false);
//--- first calculation or number of bars was changed
if(prev_calculated==0)
ArrayInitialize(ExtLineBuffer,0);
//--- calculation
switch(InpMAMethod)
{
case MODE_EMA: CalculateEMA(rates_total,prev_calculated,close); break;
case MODE_LWMA: CalculateLWMA(rates_total,prev_calculated,close); break;
case MODE_SMMA: CalculateSmoothedMA(rates_total,prev_calculated,close); break;
case MODE_SMA: CalculateSimpleMA(rates_total,prev_calculated,close); break;
}
//--- return value of prev_calculated for next call
return(rates_total);
}
//+------------------------------------------------------------------+
//| simple moving average |
//+------------------------------------------------------------------+
void CalculateSimpleMA(int rates_total,int prev_calculated,const double &price[])
{
int i,limit;
//--- first calculation or number of bars was changed
if(prev_calculated==0)
{
limit=InpMAPeriod;
//--- calculate first visible value
double firstValue=0;
for(i=0; i<limit; i++)
firstValue+=price[i];
firstValue/=InpMAPeriod;
ExtLineBuffer[limit-1]=firstValue;
}
else
limit=prev_calculated-1;
//--- main loop
for(i=limit; i<rates_total && !IsStopped(); i++)
ExtLineBuffer[i]=ExtLineBuffer[i-1]+(price[i]-price[i-InpMAPeriod])/InpMAPeriod;
//---
}
//+------------------------------------------------------------------+
//| exponential moving average |
//+------------------------------------------------------------------+
void CalculateEMA(int rates_total,int prev_calculated,const double &price[])
{
int i,limit;
double SmoothFactor=2.0/(1.0+InpMAPeriod);
//--- first calculation or number of bars was changed
if(prev_calculated==0)
{
limit=InpMAPeriod;
ExtLineBuffer[0]=price[0];
for(i=1; i<limit; i++)
ExtLineBuffer[i]=price[i]*SmoothFactor+ExtLineBuffer[i-1]*(1.0-SmoothFactor);
}
else
limit=prev_calculated-1;
//--- main loop
for(i=limit; i<rates_total && !IsStopped(); i++)
ExtLineBuffer[i]=price[i]*SmoothFactor+ExtLineBuffer[i-1]*(1.0-SmoothFactor);
//---
}
//+------------------------------------------------------------------+
//| linear weighted moving average |
//+------------------------------------------------------------------+
void CalculateLWMA(int rates_total,int prev_calculated,const double &price[])
{
int i,limit;
static int weightsum;
double sum;
//--- first calculation or number of bars was changed
if(prev_calculated==0)
{
weightsum=0;
limit=InpMAPeriod;
//--- calculate first visible value
double firstValue=0;
for(i=0;i<limit;i++)
{
int k=i+1;
weightsum+=k;
firstValue+=k*price[i];
}
firstValue/=(double)weightsum;
ExtLineBuffer[limit-1]=firstValue;
}
else
limit=prev_calculated-1;
//--- main loop
for(i=limit; i<rates_total && !IsStopped(); i++)
{
sum=0;
for(int j=0;j<InpMAPeriod;j++)
sum+=(InpMAPeriod-j)*price[i-j];
ExtLineBuffer[i]=sum/weightsum;
}
//---
}
//+------------------------------------------------------------------+
//| smoothed moving average |
//+------------------------------------------------------------------+
void CalculateSmoothedMA(int rates_total,int prev_calculated,const double &price[])
{
int i,limit;
//--- first calculation or number of bars was changed
if(prev_calculated==0)
{
limit=InpMAPeriod;
double firstValue=0;
for(i=0; i<limit; i++)
firstValue+=price[i];
firstValue/=InpMAPeriod;
ExtLineBuffer[limit-1]=firstValue;
}
else
limit=prev_calculated-1;
//--- main loop
for(i=limit; i<rates_total && !IsStopped(); i++)
ExtLineBuffer[i]=(ExtLineBuffer[i-1]*(InpMAPeriod-1)+price[i])/InpMAPeriod;
//---
}
//+------------------------------------------------------------------+