-
Notifications
You must be signed in to change notification settings - Fork 1
/
ma_colored.mq5
188 lines (162 loc) · 5.57 KB
/
ma_colored.mq5
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
/**
* Classic Moving Averages with colors
*
* Simple moving average
* Exponential moving average
* Smoothed moving average
* Linear weighted moving average
* Smoothed moving average
*
* The indicator displays a colored moving average.
*
* It has three parameters:
* * Period calculation period
* * Method calculation method
* * Price applied prise used for calculation
*
* This version is faster and code is more flexible and reusable
*/
//+------------------------------------------------------------------
#property copyright "Copyright © 2020, mplavonil"
#property description "Classic Moving Averages with colors"
#property version "1.0"
#property indicator_chart_window
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots 1
#property indicator_type1 DRAW_COLOR_LINE
#property indicator_color1 clrLimeGreen,clrRed
#property indicator_style1 STYLE_SOLID
#property indicator_width1 2
enum enMATypes
{
_sma, // Simple
_ema, // Exponential
_smma, // Smoothed
_lwma // Linear weighted
};
input int MaPeriod = 9; // Period
input enMATypes MaMethod = _sma; // Method
input ENUM_APPLIED_PRICE Price = PRICE_CLOSE; // Price
double MaBuffer[];
double ColorBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- indicator buffers mapping
SetIndexBuffer(0,MaBuffer,INDICATOR_DATA);
SetIndexBuffer(1,ColorBuffer,INDICATOR_COLOR_INDEX);
IndicatorSetString(INDICATOR_SHORTNAME,shortName(MaMethod));
//---
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 &TickVolume[],
const long &Volume[],
const int &Spread[])
{
int i=(int)MathMax(prev_calculated-1,0);for (; i<rates_total && !IsStopped(); i++)
{
double price = getPrice(Price,open,close,high,low,i,rates_total,0);
MaBuffer[i] = iCustomMa(MaMethod,price,MaPeriod,rates_total,i);
if (i>0)
{
ColorBuffer[i] = ColorBuffer[i-1];
if (MaBuffer[i]>MaBuffer[i-1]) {ColorBuffer[i]=0;}
if (MaBuffer[i]<MaBuffer[i-1]) {ColorBuffer[i]=1; }
} else ColorBuffer[i]=0;
}
return(rates_total);
}
//+------------------------------------------------------------------+
string shortName(int mode)
{
switch(mode)
{
case _sma : return "Simple MA ("+(string)MaPeriod+")";
case _ema : return "Exponential MA ("+(string)MaPeriod+")";
case _smma : return "Smoothed MA ("+(string)MaPeriod+")";
case _lwma : return "Linear weighted MA ("+(string)MaPeriod+")";
default : return "Moving Average ("+(string)MaPeriod+")";
}
}
double iCustomMa(int mode,double price,int period,int bars,int r)
{
switch(mode)
{
case _sma : return(iSMA(price,period,bars,r));
case _ema : return(iEMA(price,period,bars,r));
case _smma : return(iSMMA(price,period,bars,r));
case _lwma : return(iLWMA(price,period,bars,r));
default : return(price);
}
}
double maArray[];
double iSMA(double price, int period, int bars, int r)
{
if (ArraySize(maArray)!=bars) ArrayResize(maArray,bars);
maArray[r] = price;
double avg = price;
int k=1;
for(; k<period && (r-k)>=0; k++)
avg += maArray[r-k];
return(avg/(double)k);
}
double iEMA(double price,double period,int bars, int r)
{
if (ArraySize(maArray)!=bars) ArrayResize(maArray,bars);
maArray[r]=price;
if(r>0 && period>1)
maArray[r]=maArray[r-1]+(2.0/(1.0+period))*(price-maArray[r-1]);
return(maArray[r]);
}
double iSMMA(double price,double period,int bars, int r)
{
if (ArraySize(maArray)!=bars) ArrayResize(maArray,bars);
maArray[r]=price;
if(r>1 && period>1)
maArray[r]=maArray[r-1]+(price-maArray[r-1])/period;
return(maArray[r]);
}
double iLWMA(double price,double period,int bars, int r)
{
if (ArraySize(maArray)!=bars) ArrayResize(maArray,bars);
maArray[r] = price; if(period<1) return(price);
double sumw = period;
double sum = period*price;
for(int k=1; k<period && (r-k)>=0; k++)
{
double weight=period-k;
sumw += weight;
sum += weight*maArray[r-k];
}
return(sum/sumw);
}
//
double getPrice(ENUM_APPLIED_PRICE tprice,const double &open[],const double &close[],const double &high[],const double &low[],int i,int _bars,int instanceNo=0)
{
switch(tprice)
{
case PRICE_CLOSE: return(close[i]);
case PRICE_OPEN: return(open[i]);
case PRICE_HIGH: return(high[i]);
case PRICE_LOW: return(low[i]);
case PRICE_MEDIAN: return((high[i]+low[i])/2.0);
case PRICE_TYPICAL: return((high[i]+low[i]+close[i])/3.0);
case PRICE_WEIGHTED: return((high[i]+low[i]+close[i]+close[i])/4.0);
}
return(0);
}
//+---------------------------------------------------------