-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple 3MA visualization.mq4
169 lines (148 loc) · 4.7 KB
/
simple 3MA visualization.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
#property indicator_buffers 3
#property indicator_separate_window
#property strict
// global var
input int inputMaxLookBack = 450;
input int inputMaPeriod = 14;
// arrays
double upHistogram[];
double dnHistogram[];
double maLine[];
double divData[];
//+------------------------------------------------------------------+
//| init event |
//+------------------------------------------------------------------+
int init(){
AdjustArray(divData, inputMaxLookBack);
return(0);
}
//+------------------------------------------------------------------+
//| pertick event |
//+------------------------------------------------------------------+
int start(){
BufferConfig(0, upHistogram, clrLime);
BufferConfig(1, dnHistogram, clrRed);
BufferConfig(2, maLine, clrYellow, DRAW_LINE);
MainLoop(inputMaxLookBack, inputMaPeriod);
return(0);
}
//+------------------------------------------------------------------+
//| loop event |
//+------------------------------------------------------------------+
void MainLoop(int maxBar, int maPeriod){
int limit = ArraySize(Close);
int maMethod = MODE_EMA;
for(int i = 0; i <= limit; i++){
if(i >= maxBar) continue;
double fastMA = GetMaValue(maPeriod*1, maMethod, i);
double slowMA = GetMaValue(maPeriod*2, maMethod, i);
double signalMA = GetMaValue(maPeriod*3, maMethod, i);
GetDivValue(fastMA, slowMA, divData, i);
if(divData[i] != EMPTY_VALUE){
PlotMaHistogram(upHistogram, dnHistogram, i);
}
}
}
//+------------------------------------------------------------------+
//| buffer config |
//+------------------------------------------------------------------+
void BufferConfig(
int index,
double &array[],
color clr,
int draw = DRAW_HISTOGRAM
){
int style = STYLE_SOLID;
int zoom = (int)ChartGetInteger(0, CHART_SCALE);
int width = 1;
if(draw != DRAW_LINE){
switch(zoom){
case 5: width = 13; break;
case 4: width = 06; break;
case 3: width = 03; break;
case 2: width = 02; break;
case 1: width = 01; break;
}
}
CreateBuffer(0, array, index, draw, style, width, clr);
}
//+------------------------------------------------------------------+
//| ma - get div value |
//+------------------------------------------------------------------+
void GetDivValue(
double fastMA,
double slowMA,
double &array[],
int index
){
double div = (fastMA / slowMA) - 1;
array[index] = div;
}
//+------------------------------------------------------------------+
//| ma - plot ma histograms |
//+------------------------------------------------------------------+
void PlotMaHistogram(
double &upHisto[],
double &dnHisto[],
int index
){
double div = divData[index];
double empty = EMPTY_VALUE;
if(div > 0){
upHisto[index] = div;
} else {
upHisto[index] = empty;
}
if(div < 0){
dnHisto[index] = div;
} else {
dnHisto[index] = empty;
}
}
//+------------------------------------------------------------------+
//| ma - get ma value |
//+------------------------------------------------------------------+
double GetMaValue(int period, int maMethod, int index){
// ma parameters
string symbol = Symbol();
int timeframe = PERIOD_CURRENT;
int priceType = PRICE_CLOSE;
double ma = iMA(
symbol, timeframe, period, 0, maMethod, priceType, index
);
return(ma);
}
//+------------------------------------------------------------------+
//| utils function |
//+------------------------------------------------------------------+
void AdjustArray(int &array[], int size){
ArrayInitialize(array, EMPTY_VALUE);
ArraySetAsSeries(array, true);
ArrayResize(array, size+1);
}
void AdjustArray(double &array[], int size){
ArrayInitialize(array, EMPTY_VALUE);
ArraySetAsSeries(array, true);
ArrayResize(array, size+1);
}
void CreateBuffer(
int arrowCode,
double &array[],
int index,
int draw,
int style,
int width,
color clr
){
string bufferName = StringFormat(
"buffer(%d)", index
);
ArrayInitialize(array, EMPTY_VALUE);
SetIndexBuffer(index, array);
SetIndexEmptyValue(index, EMPTY_VALUE);
SetIndexLabel(index, bufferName);
ArraySetAsSeries(array, true);
// appearence
SetIndexStyle(index, draw, style, width, clr);
if(draw == DRAW_ARROW) SetIndexArrow(index, arrowCode);
}