-
Notifications
You must be signed in to change notification settings - Fork 1
/
ADX + Stochastic_alerts.mq4
312 lines (286 loc) · 10.2 KB
/
ADX + Stochastic_alerts.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#property indicator_separate_window
#property indicator_buffers 5
#property indicator_color1 DodgerBlue
#property indicator_color2 Lime
#property indicator_color3 Red
#property indicator_color4 Yellow
#property indicator_color5 Magenta
#property indicator_maximum 100
#property indicator_minimum 0
//---- input parameters ADX
extern bool al=true;
extern bool iBuy=true;
extern bool iSell=true;
extern bool pDi_Uv=false;
extern bool pDi_Um=false;
extern bool mDi_Uv=false;
extern bool mDi_Um=false;
extern bool mcb=false;
extern double mb=20;
extern bool mcm=false;
extern double mm=20;
extern bool pDI_c_b=false;
extern bool pDI_c_m=false;
extern double pDI=20;
extern bool crs_up=false;
extern bool crs_dwn=false;
extern bool adx=false;
extern bool adxUm=false;
extern bool adxBol=false;
extern double ADX_u=20;
extern int ADXPeriod=14;
//_________________
extern bool lrt=false;
extern bool drebezg=false;
extern bool LevelMainMore=false;
extern double levelTop=0;
extern bool LevelMainLess=false;
extern double levelLower=0;
extern bool crossA=false;
extern bool crossB=false;
extern double level_high_A=70;
extern double level_low_B=30;
//_______________________________________
extern int Kperiod = 5;
extern int Dperiod = 3;
extern int slowing = 3;
extern int method = 0; // 0 - SMA, 1- EMA, 2 -
extern int price_field = 0; // 0 - High/Low; 1 - Close/Close
//---- buffers
double ADXBuffer[];
double PlusDiBuffer[];
double MinusDiBuffer[];
double PlusSdiBuffer[];
double MinusSdiBuffer[];
double TempBuffer[];
int LB;
//---- buffers
double Stoch[];
double Signal[];
//int i;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init() {
//---- 3 additional buffers are used for counting.
IndicatorBuffers(8);
//---- indicator buffers
SetIndexBuffer(0,ADXBuffer);
SetIndexBuffer(1,PlusDiBuffer);
SetIndexBuffer(2,MinusDiBuffer);
SetIndexBuffer(5,PlusSdiBuffer);
SetIndexBuffer(6,MinusSdiBuffer);
SetIndexBuffer(7,TempBuffer);
//---- name for DataWindow and indicator subwindow label
IndicatorShortName("sA("+ADXPeriod+")");
SetIndexLabel(0,"ADX");
SetIndexLabel(1,"+DI");
SetIndexLabel(2,"-DI");
//----
SetIndexDrawBegin(0,ADXPeriod);
SetIndexDrawBegin(1,ADXPeriod);
SetIndexDrawBegin(2,ADXPeriod);
//----
//---- drawing settings
SetIndexStyle(3,DRAW_LINE);
SetIndexDrawBegin(3,0);
SetIndexStyle(4,DRAW_LINE,STYLE_DOT);
SetIndexDrawBegin(4,0);
SetIndexBuffer(3,Stoch);
SetIndexBuffer(4,Signal);
return(0); }//int init()
//+------------------------------------------------------------------+
int start() {
double a, b, c,d;
//-----adx
double po,p1,mo,m1,a0,a1;
double pdm,mdm,tr;
double price_high,price_low;
int starti,i,counted_bars=IndicatorCounted();
//---------
int limi;
//int counted_bars=IndicatorCounted();
int q,j,k;
if(counted_bars<0) return(-1);
if(counted_bars>0) counted_bars--;
limi=Bars-counted_bars;
for (q = limi;q>=0;q--)
{
Stoch[q]=iStochastic(NULL,0,Kperiod,Dperiod,slowing,method,price_field,MODE_MAIN,q);
Signal[q]=iStochastic(NULL,0,Kperiod,Dperiod,slowing,method,price_field,MODE_SIGNAL,q);
}
//--------------------------------------------------------------------$$$$$$$$$$$$$$$$$$$
a=iStochastic(NULL,0,Kperiod,Dperiod,slowing,method,price_field,MODE_MAIN,0);// 0 áàð
b=iStochastic(NULL,0,Kperiod,Dperiod,slowing,method,price_field,MODE_MAIN, 1);// 1 áàð
c=iStochastic(NULL,0,Kperiod,Dperiod,slowing,method,price_field,MODE_SIGNAL,0);// 0 áàð
d=iStochastic(NULL,0,Kperiod,Dperiod,slowing,method,price_field,MODE_SIGNAL,1);// 1 áàð
//--------------------------------------------------------------------
//----
i=Bars-2;
PlusSdiBuffer[i+1]=0;
MinusSdiBuffer[i+1]=0;
if(counted_bars>=i) i=Bars-counted_bars-1;
starti=i;
//----
while(i>=0)
{
price_low=Low[i];
price_high=High[i];
//----
pdm=price_high-High[i+1];
mdm=Low[i+1]-price_low;
if(pdm<0) pdm=0; // +DM
if(mdm<0) mdm=0; // -DM
if(pdm==mdm) { pdm=0; mdm=0; }
else if(pdm<mdm) pdm=0;
else if(mdm<pdm) mdm=0;
//---- ????????? ???????? ????????
double num1=MathAbs(price_high-price_low);
double num2=MathAbs(price_high-Close[i+1]);
double num3=MathAbs(price_low-Close[i+1]);
tr=MathMax(num1,num2);
tr=MathMax(tr,num3);
//---- counting plus/minus direction
if(tr==0) { PlusSdiBuffer[i]=0; MinusSdiBuffer[i]=0; }
else { PlusSdiBuffer[i]=100.0*pdm/tr; MinusSdiBuffer[i]=100.0*mdm/tr; }
//----
i--;
}
//---- last counted bar will be recounted
if(counted_bars>0) counted_bars--;
int limit=Bars-counted_bars;
//---- apply EMA to +DI
for(i=0; i<=limit; i++)
PlusDiBuffer[i]=iMAOnArray(PlusSdiBuffer,Bars,ADXPeriod,0,MODE_EMA,i);
//---- apply EMA to -DI
for(i=0; i<=limit; i++)
MinusDiBuffer[i]=iMAOnArray(MinusSdiBuffer,Bars,ADXPeriod,0,MODE_EMA,i);
//---- Directional Movement (DX)
i=Bars-2;
TempBuffer[i+1]=0;
i=starti;
while(i>=0)
{
double div=MathAbs(PlusDiBuffer[i]+MinusDiBuffer[i]);
if(div==0.00) TempBuffer[i]=0;
else TempBuffer[i]=100*(MathAbs(PlusDiBuffer[i]-MinusDiBuffer[i])/div);
i--;
}
//---- ADX is exponential moving average on DX
for(i=0; i<limit; i++)
ADXBuffer[i]=iMAOnArray(TempBuffer,Bars,ADXPeriod,0,MODE_EMA,i);
//----/&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
po=iADX(NULL, 0,14, PRICE_CLOSE, MODE_PLUSDI, 0);
p1=iADX(NULL, 0,14, PRICE_CLOSE, MODE_PLUSDI, 1);
mo=iADX(NULL, 0,14, PRICE_CLOSE, MODE_MINUSDI, 0);
m1=iADX(NULL, 0,14, PRICE_CLOSE, MODE_MINUSDI,1);
a0=iADX(NULL, 0,14, PRICE_CLOSE, MODE_MAIN,0);
a1=iADX(NULL, 0,14, PRICE_CLOSE, MODE_MAIN,1);
if(al){
if(iBuy){
if(a>b && a<80 && po>p1 && po>mo)Alert("Moro:Buy ",Symbol(),Period());
}
if(iSell){
if(a<b && a>20 && mo>m1 && po<mo)Alert("Moro:Sell ",Symbol(),Period());
}
if(mcb){
if(mo>mb)Alert("sa4:","-DI>",mb,"--",Symbol(),Period());
}
if(mcm)
{if(mo<mm)Alert("sa4:","-DI<",mm,"--",Symbol(),Period());
}
if(pDI_c_b)
{
if(po>pDI)Alert("sa4:","+DI>",pDI,"--",Symbol(),Period());
}
if(pDI_c_m)
{if(po<pDI)Alert("sa4:","-DI<",pDI,"--",Symbol(),Period());
}
if(crs_up)
{
if(po>mo)Alert("sa4:","+di cross -di","--",Symbol(),Period());
}
if(crs_dwn)
{
if(po<mo)Alert("sa4:","-di cross +di","--",Symbol(),Period());
}
if(pDi_Uv)
{
if(po>p1)Alert("sa4:","+di ðàñòåò","--",Symbol(),Period());
}
if(pDi_Um)
{
if(po<p1)Alert("sa4:","+di ïàäàåò","--",Symbol(),Period());
}
if(mDi_Uv)
{
if(mo>m1)Alert("sa4:","-di ðàñòåò","--",Symbol(),Period());
}
if(mDi_Um)
{
if(mo<m1)Alert("sa4:","-di ïàäàåò","--",Symbol(),Period());
}
if(adx){
if(a0>a1)Alert("sa4:","ADX ðàñòåò","--",Symbol(),Period());
}
if(adxUm){
if(a1>a0)Alert("sa4:","ADX ïàäàåò","--",Symbol(),Period());
}
if(adxBol){
if(a0>ADX_u)Alert("sa4:","ADX áîëüøå 20","--",Symbol(),Period());
}
}
//*/
if(lrt)
{
if(a < c && b>=d)
{
Alert("SS:","Ïåðåñå÷åíèåÂíèç",Symbol(),Period());
lrt=false;
//PlaySound("alert2.wav");
}
if(a > c && b<=d) {
Alert("SS:","Ïåðåñå÷åíèåÂâåðõ",Symbol(),Period());
lrt=false;
//PlaySound("alert2.wav");
}
}
if(LevelMainMore)
{
if(a>levelTop)Alert("SS:",a,"--",Symbol(),Period());
LevelMainMore=false;
}
if(LevelMainLess)
{
if(a<levelLower)Alert("SS:",a,"--",Symbol(),Period());
LevelMainLess=false;
}
if(crossA)
{
if(a < c && b>=d && a>level_high_A) Alert("SS:","crossA",Symbol(),Period());
if(a > c && b<=d && a>level_high_A) Alert("SS:","crossA",Symbol(),Period());
crossA=false;
}
if(crossB)
{
if(a < c && b>=d && a<level_low_B) Alert("SS:","crossB",Symbol(),Period());
if(a > c && b<=d && a<level_low_B) Alert("SS:","crossB",Symbol(),Period());
crossB=false;
}
if(drebezg)
{
if(a < c && b>=d)
{
Alert("SS:","Ïåðåñå÷åíèåÂíèç",Symbol(),Period());
//drebezg=false;
//PlaySound("alert2.wav");
}
if(a > c && b<=d) {
Alert("SS:","Ïåðåñå÷åíèåÂâåðõ",Symbol(),Period());
//lrt=false;
//PlaySound("alert2.wav");
}
}
//$$$$$$$$$$$$$$$$$$$$$$$3###
return(0);
}