-
Notifications
You must be signed in to change notification settings - Fork 1
/
Dynamic Fx - sFractals.mq4
202 lines (202 loc) · 7.4 KB
/
Dynamic Fx - sFractals.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
//+------------------------------------------------------------------+
//| sFractals.mq4 |
//| Copyright © 2005, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"
//----
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 Blue
//---- buffers
double ExtUpFractalsBuffer[];
double ExtDownFractalsBuffer[];
// Íîìåð áàðà, ïî êîòîðîìó áóäåò èñêàòüñÿ ñèãíàë
#define SIGNAL_BAR 2
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicator buffers mapping
SetIndexBuffer(0, ExtUpFractalsBuffer);
SetIndexBuffer(1, ExtDownFractalsBuffer);
//---- drawing settings
SetIndexStyle(0, DRAW_ARROW);
SetIndexArrow(0, 119);
SetIndexStyle(1, DRAW_ARROW);
SetIndexArrow(1, 119);
//----
SetIndexEmptyValue(0, 0.0);
SetIndexEmptyValue(1, 0.0);
//---- name for DataWindow
SetIndexLabel(0, "sFractal Up");
SetIndexLabel(1, "sFractal Down");
//---- initialization done
return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int i, nCountedBars;
bool bFound;
double dCurrent;
nCountedBars = IndicatorCounted();
//---- last counted bar will be recounted
if(nCountedBars <= 2)
i = Bars - nCountedBars - 3;
if(nCountedBars > 2)
{
nCountedBars--;
i = Bars - nCountedBars - 1;
}
//----Up and Down Fractals
while(i >= 2)
{
//----Fractals up
bFound = false;
dCurrent = High[i];
if(dCurrent > High[i+1] && dCurrent > High[i+2] && dCurrent > High[i-1] &&
dCurrent > High[i-2])
{
bFound = true;
ExtUpFractalsBuffer[i] = dCurrent;
}
//----6 bars Fractal
if(!bFound && (Bars - i - 1) >= 3)
{
if(dCurrent == High[i+1] && dCurrent > High[i+2] && dCurrent > High[i+3] &&
dCurrent > High[i-1] && dCurrent > High[i-2])
{
bFound = true;
ExtUpFractalsBuffer[i] = dCurrent;
}
}
//----7 bars Fractal
if(!bFound && (Bars - i - 1) >= 4)
{
if(dCurrent >= High[i+1] && dCurrent == High[i+2] && dCurrent > High[i+3] &&
dCurrent > High[i+4] && dCurrent > High[i-1] && dCurrent > High[i-2])
{
bFound = true;
ExtUpFractalsBuffer[i] = dCurrent;
}
}
//----8 bars Fractal
if(!bFound && (Bars - i - 1) >= 5)
{
if(dCurrent >= High[i+1] && dCurrent == High[i+2] && dCurrent == High[i+3] &&
dCurrent > High[i+4] && dCurrent > High[i+5] && dCurrent > High[i-1] &&
dCurrent > High[i-2])
{
bFound = true;
ExtUpFractalsBuffer[i] = dCurrent;
}
}
//----9 bars Fractal
if(!bFound && (Bars - i - 1) >= 6)
{
if(dCurrent >= High[i+1] && dCurrent == High[i+2] && dCurrent >= High[i+3] &&
dCurrent == High[i+4] && dCurrent > High[i+5] && dCurrent > High[i+6] &&
dCurrent > High[i-1] && dCurrent > High[i-2])
{
bFound = true;
ExtUpFractalsBuffer[i] = dCurrent;
}
}
//----Fractals down
bFound = false;
dCurrent = Low[i];
if(dCurrent < Low[i+1] && dCurrent < Low[i+2] && dCurrent < Low[i-1] &&
dCurrent < Low[i-2])
{
bFound = true;
ExtDownFractalsBuffer[i] = dCurrent;
}
//----6 bars Fractal
if(!bFound && (Bars - i - 1) >= 3)
{
if(dCurrent == Low[i+1] && dCurrent < Low[i+2] && dCurrent < Low[i+3] &&
dCurrent < Low[i-1] && dCurrent < Low[i-2])
{
bFound = true;
ExtDownFractalsBuffer[i] = dCurrent;
}
}
//----7 bars Fractal
if(!bFound && (Bars - i - 1) >= 4)
{
if(dCurrent <= Low[i+1] && dCurrent == Low[i+2] && dCurrent < Low[i+3] &&
dCurrent < Low[i+4] &&
dCurrent < Low[i-1] && dCurrent < Low[i-2])
{
bFound = true;
ExtDownFractalsBuffer[i] = dCurrent;
}
}
//----8 bars Fractal
if(!bFound && (Bars - i - 1) >= 5)
{
if(dCurrent <= Low[i+1] && dCurrent == Low[i+2] && dCurrent==Low[i+3] &&
dCurrent < Low[i+4] && dCurrent < Low[i+5] && dCurrent < Low[i-1] &&
dCurrent < Low[i-2])
{
bFound = true;
ExtDownFractalsBuffer[i] = dCurrent;
}
}
//----9 bars Fractal
if(!bFound && (Bars - i- 1) >= 6)
{
if(dCurrent <= Low[i+1] && dCurrent == Low[i+2] && dCurrent <= Low[i+3] &&
dCurrent == Low[i+4] && dCurrent < Low[i+5] && dCurrent < Low[i+6] &&
dCurrent < Low[i-1] && dCurrent < Low[i-2])
{
bFound = true;
ExtDownFractalsBuffer[i] = dCurrent;
}
}
i--;
}
// Ñòàòè÷åñêèå ïåðåìåííûå, â êîòîðûõ õðàíÿòñÿ âðåìÿ ïîñëåäíåãî áàðà è íàïðàâëåíèå
// ïîñëåäíåãî ñèãíàëà
static int PrevSignal = 0, PrevTime = 0;
// Åñëè áàðîì äëÿ àíàëèçà âûáðàí íå 0-é, íàì íåò ñìûñëà ïðîâåðÿòü ñèãíàë íåñêîëüêî ðàç.
// Åñëè íå íà÷àëñÿ íîâûé áàð, âûõîäèì.
if(SIGNAL_BAR > 0 && Time[0] <= PrevTime)
return(0);
// Îòìå÷àåì, ÷òî ýòîò áàð ïðîâåðåí
PrevTime = Time[0];
// Åñëè ïðåäûäóùèé ñèãíàë áûë ÑÅËË èëè ýòî ïåðâûé çàïóñê (PrevSignal=0)
if(PrevSignal <= 0)
{
if(ExtDownFractalsBuffer[SIGNAL_BAR] > 0)
{
PrevSignal = 1;
Alert("sFractals (", Symbol(), ", ", Period(), ") - BUY!!!");
}
}
if(PrevSignal >= 0)
{
if(ExtUpFractalsBuffer[SIGNAL_BAR] > 0)
{
PrevSignal = -1;
Alert("sFractals (", Symbol(), ", ", Period(), ") - SELL!!!");
}
}
//----
return(0);
}
//+------------------------------------------------------------------+