-
Notifications
You must be signed in to change notification settings - Fork 2
/
4-tf-ergodic-cci-arrows-alerts.mq4
361 lines (337 loc) · 17.5 KB
/
4-tf-ergodic-cci-arrows-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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
//------------------------------------------------------------------
#property copyright "www.forex-station.com"
#property link "www.forex-station.com"
// https://https://forex-station.com/viewtopic.php?p=1295412104#p1295412104
//------------------------------------------------------------------
#property indicator_separate_window
#property indicator_buffers 8
#property indicator_color1 clrLimeGreen
#property indicator_color2 clrPaleVioletRed
#property indicator_color3 clrLimeGreen
#property indicator_color4 clrPaleVioletRed
#property indicator_color5 clrLimeGreen
#property indicator_color6 clrPaleVioletRed
#property indicator_color7 clrLimeGreen
#property indicator_color8 clrPaleVioletRed
#property indicator_minimum 0
#property indicator_maximum 5
extern string TimeFrame1 = "Current time frame";
extern string TimeFrame2 = "next1";
extern string TimeFrame3 = "next2";
extern string TimeFrame4 = "next3";
//extern string note1="----------------------------------------------"; do not put this line here; strange; will crash
extern int pq = 2;
extern int pr = 10;
extern int ps = 5;
extern int trigger = 3;
extern string UniqueID = "4TF Ergodic CCI Trend";
extern string note2="----------------------------------------------";
extern int LinesWidth = 0;
extern color LabelsColor = clrDarkGray;
extern int LabelsHorizontalShift = 5;
extern double LabelsVerticalShift = 1.5;
extern string note3="----------------------------------------------";
extern bool alertsOn = false;
extern int alertsHowManyTimeFrameAligned = 3;
extern bool alertsMessage = true;
extern bool alertsSound = false;
extern bool alertsEmail = false;
input bool alertsNotify = false;
extern string note4="----------------------------------------------";
extern bool DisplayArrows = true;
extern int DrawArrowsWhenTimeFrameAligned = 4;
extern int ArrowGap = 0;
extern int ArrowWidth = 1;
extern int UpArrowWingDingsFontCode = 233;
extern int DownArrowWingDingsFontCode = 234;
extern color UpArrowColor = clrLime;
extern color DownArrowColor = clrRed;
extern string note5="----------------------------------------------";
extern int TimeFrame1WingdingsFontCode = 110;
extern int TimeFrame2WingdingsFontCode = 110;
extern int TimeFrame3WingdingsFontCode = 108;
extern int TimeFrame4WingdingsFontCode = 108;
extern int MaximumCandlesToDisplay = 500;
extern string note6="----------------------------------------------";
double trends[], ForexStation1Up[], ForexStation1Down[], ForexStation2Up[], ForexStation2Down[], ForexStation3Up[], ForexStation3Down[], ForexStation4Up[], ForexStation4Down[];
int timeFrames[4];
bool returnBars, calculateValue;
string indicatorFileName;
//------------------------------------------------------------------
int OnInit()
{
IndicatorBuffers(9);
SetIndexBuffer(0,ForexStation1Up);
SetIndexBuffer(1,ForexStation1Down);
SetIndexBuffer(2,ForexStation2Up);
SetIndexBuffer(3,ForexStation2Down);
SetIndexBuffer(4,ForexStation3Up);
SetIndexBuffer(5,ForexStation3Down);
SetIndexBuffer(6,ForexStation4Up);
SetIndexBuffer(7,ForexStation4Down);
SetIndexBuffer(8,trends);
indicatorFileName = WindowExpertName();
returnBars = (TimeFrame1=="returnBars"); if (returnBars) return(0);
calculateValue = (TimeFrame1=="calculateValue"); if (calculateValue) return(0);
SetIndexStyle(0,DRAW_ARROW,EMPTY,LinesWidth); SetIndexArrow(0,TimeFrame1WingdingsFontCode);
SetIndexStyle(1,DRAW_ARROW,EMPTY,LinesWidth); SetIndexArrow(1,TimeFrame1WingdingsFontCode);
SetIndexStyle(2,DRAW_ARROW,EMPTY,LinesWidth); SetIndexArrow(2,TimeFrame2WingdingsFontCode);
SetIndexStyle(3,DRAW_ARROW,EMPTY,LinesWidth); SetIndexArrow(3,TimeFrame2WingdingsFontCode);
SetIndexStyle(4,DRAW_ARROW,EMPTY,LinesWidth); SetIndexArrow(4,TimeFrame3WingdingsFontCode);
SetIndexStyle(5,DRAW_ARROW,EMPTY,LinesWidth); SetIndexArrow(5,TimeFrame3WingdingsFontCode);
SetIndexStyle(6,DRAW_ARROW,EMPTY,LinesWidth); SetIndexArrow(6,TimeFrame4WingdingsFontCode);
SetIndexStyle(7,DRAW_ARROW,EMPTY,LinesWidth); SetIndexArrow(7,TimeFrame4WingdingsFontCode);
timeFrames[0] = stringToTimeFrame(TimeFrame1);
timeFrames[1] = stringToTimeFrame(TimeFrame2);
timeFrames[2] = stringToTimeFrame(TimeFrame3);
timeFrames[3] = stringToTimeFrame(TimeFrame4);
alertsHowManyTimeFrameAligned = MathMin(MathMax(alertsHowManyTimeFrameAligned,2),4);
IndicatorShortName(UniqueID);
return(0);
}
//------------------------------------------------------------------
int deinit()
{
for (int t=0; t<4; t++) ObjectDelete(UniqueID+t);
for (int i = ObjectsTotal()-1; i >= 0; i--)
if (StringSubstr(ObjectName(i), 0, StringLen(UniqueID)) == UniqueID)
ObjectDelete(ObjectName(i));
return(0);
}
//------------------------------------------------------------------
double trend[][2];
#define _up 0
#define _dn 1
int start()
{
int i,r,counted_bars=IndicatorCounted();
if(counted_bars < 0) return(-1);
if(counted_bars>0) counted_bars--;
int limit = MathMin(Bars-counted_bars,Bars-1);
if (limit>MaximumCandlesToDisplay) limit = MaximumCandlesToDisplay;
if (returnBars) { ForexStation1Up[0] = limit+1; return(0); }
if (calculateValue) { calculateByIT(limit); return(0); }
if (timeFrames[0] != Period()) limit = MathMax(limit,MathMin(MaximumCandlesToDisplay,iCustom(NULL,timeFrames[0],indicatorFileName,"returnBars",0,0)*timeFrames[0]/Period()));
if (timeFrames[1] != Period()) limit = MathMax(limit,MathMin(MaximumCandlesToDisplay,iCustom(NULL,timeFrames[1],indicatorFileName,"returnBars",0,0)*timeFrames[1]/Period()));
if (timeFrames[2] != Period()) limit = MathMax(limit,MathMin(MaximumCandlesToDisplay,iCustom(NULL,timeFrames[2],indicatorFileName,"returnBars",0,0)*timeFrames[2]/Period()));
if (timeFrames[3] != Period()) limit = MathMax(limit,MathMin(MaximumCandlesToDisplay,iCustom(NULL,timeFrames[3],indicatorFileName,"returnBars",0,0)*timeFrames[3]/Period()));
if (ArrayRange(trend,0)!=Bars) ArrayResize(trend,Bars);
static bool initialized = false;
if (!initialized)
{
initialized = true;
int window = WindowFind(UniqueID);
for (int t=0; t<4; t++)
{
string label = timeFrameToString(timeFrames[t]);
ObjectCreate (UniqueID+t,OBJ_TEXT,window,0,0);
ObjectSet (UniqueID+t,OBJPROP_COLOR,LabelsColor);
ObjectSet (UniqueID+t,OBJPROP_PRICE1,t+LabelsVerticalShift);
ObjectSetText(UniqueID+t,label,8,"Arial");
}
}
for (t=0; t<4; t++) ObjectSet(UniqueID+t,OBJPROP_TIME1,Time[0]+Period()*LabelsHorizontalShift*60);
for(i = limit, r=Bars-i-1; i >= 0; i--,r++)
{
trend[r][_up] = 0;
trend[r][_dn] = 0;
for (int k=0; k<4; k++)
{
// ----- Start of replacing this area -----
int y = iBarShift(NULL,timeFrames[k],Time[i]);
double spanA = iCustom(NULL,timeFrames[k],indicatorFileName,"calculateValue","","","",pq,pr,ps,trigger,0,y);
bool isUp = (spanA>0);
// ----- End of replacing this area -----
switch (k)
{
case 0 : if (isUp) { ForexStation1Up[i] = k+1; ForexStation1Down[i] = EMPTY_VALUE;} else { ForexStation1Down[i] = k+1; ForexStation1Up[i] = EMPTY_VALUE; } break;
case 1 : if (isUp) { ForexStation2Up[i] = k+1; ForexStation2Down[i] = EMPTY_VALUE;} else { ForexStation2Down[i] = k+1; ForexStation2Up[i] = EMPTY_VALUE; } break;
case 2 : if (isUp) { ForexStation3Up[i] = k+1; ForexStation3Down[i] = EMPTY_VALUE;} else { ForexStation3Down[i] = k+1; ForexStation3Up[i] = EMPTY_VALUE; } break;
case 3 : if (isUp) { ForexStation4Up[i] = k+1; ForexStation4Down[i] = EMPTY_VALUE;} else { ForexStation4Down[i] = k+1; ForexStation4Up[i] = EMPTY_VALUE; } break;
}
if (isUp)
trend[r][_up] += 1;
else trend[r][_dn] += 1;
}
trends[i]=trends[i+1];
if (DrawArrowsWhenTimeFrameAligned == 4)
{
if(trends[i]!=1 && ForexStation1Up[i]!=EMPTY_VALUE && ForexStation2Up[i]!=EMPTY_VALUE && ForexStation3Up[i]!=EMPTY_VALUE && ForexStation4Up[i]!=EMPTY_VALUE)
{
trends[i] = 1;
if (DisplayArrows) arrows_wind(i,"Up",ArrowGap ,UpArrowWingDingsFontCode,UpArrowColor,ArrowWidth,false);
}else{
ObjectDelete(UniqueID+"Up"+TimeToStr(Time[i]));
}
if(trends[i]!=-1 && ForexStation1Down[i]!=EMPTY_VALUE && ForexStation2Down[i]!=EMPTY_VALUE && ForexStation3Down[i]!=EMPTY_VALUE && ForexStation4Down[i]!=EMPTY_VALUE)
{
trends[i] =-1;
if (DisplayArrows) arrows_wind(i,"Dn",ArrowGap ,DownArrowWingDingsFontCode,DownArrowColor,ArrowWidth,true);
}else{
ObjectDelete(UniqueID + "Dn" + TimeToStr(Time[i],TIME_DATE|TIME_SECONDS));
}
if(trends[i]== 1 && (ForexStation1Up[i] ==EMPTY_VALUE || ForexStation2Up[i] ==EMPTY_VALUE || ForexStation3Up[i] ==EMPTY_VALUE || ForexStation4Up[i] ==EMPTY_VALUE)) trends[i] = 0;
if(trends[i]==-1 && (ForexStation1Down[i]==EMPTY_VALUE || ForexStation2Down[i]==EMPTY_VALUE || ForexStation3Down[i]==EMPTY_VALUE || ForexStation4Down[i]==EMPTY_VALUE)) trends[i] = 0;
} // if (alertsHowManyTimeFrameAligned == 4)
else
if (DrawArrowsWhenTimeFrameAligned == 3)
{
if(trends[i]!=1 && ForexStation1Up[i]!=EMPTY_VALUE && ForexStation2Up[i]!=EMPTY_VALUE && ForexStation3Up[i]!=EMPTY_VALUE)
{
trends[i] = 1;
if (DisplayArrows) arrows_wind(i,"Up",ArrowGap ,UpArrowWingDingsFontCode,UpArrowColor,ArrowWidth,false);
}else{
ObjectDelete(UniqueID+"Up"+TimeToStr(Time[i]));
}
if(trends[i]!=-1 && ForexStation1Down[i]!=EMPTY_VALUE && ForexStation2Down[i]!=EMPTY_VALUE && ForexStation3Down[i]!=EMPTY_VALUE)
{
trends[i] =-1;
if (DisplayArrows) arrows_wind(i,"Dn",ArrowGap ,DownArrowWingDingsFontCode,DownArrowColor,ArrowWidth,true);
}else{
ObjectDelete(UniqueID + "Dn" + TimeToStr(Time[i],TIME_DATE|TIME_SECONDS));
}
if(trends[i]== 1 && (ForexStation1Up[i] ==EMPTY_VALUE || ForexStation2Up[i] ==EMPTY_VALUE || ForexStation3Up[i] ==EMPTY_VALUE )) trends[i] = 0;
if(trends[i]==-1 && (ForexStation1Down[i]==EMPTY_VALUE || ForexStation2Down[i]==EMPTY_VALUE || ForexStation3Down[i]==EMPTY_VALUE )) trends[i] = 0;
} // else if (alertsHowManyTimeFrameAligned == 3)
else
if (DrawArrowsWhenTimeFrameAligned == 2)
{
if(trends[i]!=1 && ForexStation1Up[i]!=EMPTY_VALUE && ForexStation2Up[i]!=EMPTY_VALUE)
{
trends[i] = 1;
if (DisplayArrows) arrows_wind(i,"Up",ArrowGap ,UpArrowWingDingsFontCode,UpArrowColor,ArrowWidth,false);
}else{
ObjectDelete(UniqueID+"Up"+TimeToStr(Time[i]));
}
if(trends[i]!=-1 && ForexStation1Down[i]!=EMPTY_VALUE && ForexStation2Down[i]!=EMPTY_VALUE)
{
trends[i] =-1;
if (DisplayArrows) arrows_wind(i,"Dn",ArrowGap ,DownArrowWingDingsFontCode,DownArrowColor,ArrowWidth,true);
}else{
ObjectDelete(UniqueID + "Dn" + TimeToStr(Time[i],TIME_DATE|TIME_SECONDS));
}
if(trends[i]== 1 && (ForexStation1Up[i] ==EMPTY_VALUE || ForexStation2Up[i] ==EMPTY_VALUE )) trends[i] = 0;
if(trends[i]==-1 && (ForexStation1Down[i]==EMPTY_VALUE || ForexStation2Down[i]==EMPTY_VALUE )) trends[i] = 0;
} // else if (alertsHowManyTimeFrameAligned == 2)
}
manageAlerts();
return(0);
}
//+------------------------------------------------------------------+
void calculateByIT(int limit)
{
for (int i=limit; i>=0; i--)
{
double var1 = iEma(iEma(iEma( Close[i]-Close[i+1] ,pq,i,0),pr,i,1),ps,i,2);
double var2 = iEma(iEma(iEma(MathAbs(Close[i]-Close[i+1]),pq,i,3),pr,i,4),ps,i,5);
if (var2 != 0)
ForexStation4Up[i] = 500.0 * var1/var2;
else ForexStation4Up[i] = 0;
ForexStation4Down[i] = iEma(ForexStation4Up[i],trigger,i,6);
ForexStation1Up[i] = ForexStation1Up[i+1];
if (ForexStation4Up[i]>ForexStation4Down[i]) ForexStation1Up[i] = 1;
if (ForexStation4Up[i]<ForexStation4Down[i]) ForexStation1Up[i] = -1;
}
}
//------------------------------------------------------------------
double workEma[][7];
double iEma(double price, double period, int r, int instanceNo=0)
{
if (ArrayRange(workEma,0)!= Bars) ArrayResize(workEma,Bars); r=Bars-r-1;
//
//
//
//
//
double alpha = 2.0 / (1.0+period);
workEma[r][instanceNo] = workEma[r-1][instanceNo]+alpha*(price-workEma[r-1][instanceNo]);
return(workEma[r][instanceNo]);
}
//+-------------------------------------------------------------------
void arrows_wind(int k, string N,int ots,int Code,color clr, int ArrowSize,bool up)
{
string objName = UniqueID+N+TimeToStr(Time[k]);
double gap = ots*Point;
ObjectCreate(objName, OBJ_ARROW,0,Time[k],0);
ObjectSet (objName, OBJPROP_COLOR, clr);
ObjectSet (objName, OBJPROP_ARROWCODE,Code);
ObjectSet (objName, OBJPROP_WIDTH,ArrowSize);
if (up)
{
ObjectSet(objName, OBJPROP_ANCHOR,ANCHOR_BOTTOM);
ObjectSet(objName,OBJPROP_PRICE1,High[k]+gap);
} else {
ObjectSet(objName, OBJPROP_ANCHOR,ANCHOR_TOP);
ObjectSet(objName,OBJPROP_PRICE1,Low[k]-gap);
}
}
//-------------------------------------------------------------------
void manageAlerts()
{
if (alertsOn)
{
int whichBar = Bars-1;
if (trend[whichBar][_up] >= alertsHowManyTimeFrameAligned || trend[whichBar][_dn] >= alertsHowManyTimeFrameAligned)
{
if (trend[whichBar][_up] >= alertsHowManyTimeFrameAligned) doAlert("up" ,trend[whichBar][_up]);
if (trend[whichBar][_dn] >= alertsHowManyTimeFrameAligned) doAlert("down",trend[whichBar][_dn]);
}
}
}
//-------------------------------------------------------------------
void doAlert(string doWhat, int howMany)
{
static string previousAlert="nothing";
static datetime previousTime;
string message;
if (previousAlert != doWhat || previousTime != Time[0]) {
previousAlert = doWhat;
previousTime = Time[0];
message = Symbol()+" at "+TimeToStr(TimeLocal(),TIME_SECONDS)+" "+howMany+" time frames of Ergodic CCI are aligned "+doWhat;
if (alertsMessage) Alert(message);
if (alertsEmail) SendMail(Symbol()+" 4TF Ergodic CCI",message);
if (alertsNotify) SendNotification(message);
if (alertsSound) PlaySound("alert2.wav");
}
}
//-------------------------------------------------------------------
string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};
int toInt(double value) { return(value); }
int stringToTimeFrame(string tfs)
{
tfs = stringUpperCase(tfs);
int max = ArraySize(iTfTable)-1, add=0;
int nxt = (StringFind(tfs,"NEXT1")>-1); if (nxt>0) { tfs = ""+Period(); add=1; }
nxt = (StringFind(tfs,"NEXT2")>-1); if (nxt>0) { tfs = ""+Period(); add=2; }
nxt = (StringFind(tfs,"NEXT3")>-1); if (nxt>0) { tfs = ""+Period(); add=3; }
nxt = (StringFind(tfs,"NEXT4")>-1); if (nxt>0) { tfs = ""+Period(); add=4; }
nxt = (StringFind(tfs,"NEXT5")>-1); if (nxt>0) { tfs = ""+Period(); add=5; }
nxt = (StringFind(tfs,"NEXT6")>-1); if (nxt>0) { tfs = ""+Period(); add=6; }
nxt = (StringFind(tfs,"NEXT7")>-1); if (nxt>0) { tfs = ""+Period(); add=7; }
for (int i=max; i>=0; i--)
if (tfs==sTfTable[i] || tfs==""+iTfTable[i]) return(MathMax(iTfTable[toInt(MathMin(max,i+add))],Period()));
return(Period());
}
//-------------------------------------------------------------------
string timeFrameToString(int tf)
{
for (int i=ArraySize(iTfTable)-1; i>=0; i--)
if (tf==iTfTable[i]) return(sTfTable[i]);
return("");
}
//-------------------------------------------------------------------
string stringUpperCase(string str)
{
string s = str;
for (int length=StringLen(str)-1; length>=0; length--)
{
int tchar = StringGetChar(s, length);
if((tchar > 96 && tchar < 123) || (tchar > 223 && tchar < 256))
s = StringSetChar(s, length, tchar - 32);
else if(tchar > -33 && tchar < 0)
s = StringSetChar(s, length, tchar + 224);
}
return(s);
}
//-------------------------------------------------------------------