-
Notifications
You must be signed in to change notification settings - Fork 2
/
VolatilityStop.mq4
298 lines (255 loc) · 10 KB
/
VolatilityStop.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
//+------------------------------------------------------------------+
//| VoltyChannel_Stop_v2.1.mq4 |
//| Copyright © 2007, TrendLaboratory |
//| http://finance.groups.yahoo.com/group/TrendLaboratory |
//| E-mail: [email protected] |
//+------------------------------------------------------------------+
//| VoltyChannel_Stop_v2.1_TRO_MODIFIED_VERSION |
//| MODIFIED BY AVERY T. HORTON, JR. AKA [email protected] |
//| I am NOT the ORIGINAL author
// and I am not claiming authorship of this indicator.
// All I did was modify it. I hope you find my modifications useful.|
//| |
//+------------------------------------------------------------------+
//2008fxtsd mod
#property copyright "Copyright © 2007, TrendLaboratory"
#property link "http://finance.groups.yahoo.com/group/TrendLaboratory"
//----
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 Blue
#property indicator_color2 Red
#property indicator_color3 Blue
#property indicator_color4 Red
#property indicator_width1 1
#property indicator_width2 1
#property indicator_width3 1
#property indicator_width4 1
//+--------- TRO MODIFICATION ---------------------------------------+
extern bool Sound.Alert = false ;
extern bool Show.PriceBox = true ;
extern int myBoxWidth = 3;
//---- input parameters
extern int MA_Price =0; //Applied Price: 0-C,1-O,2-H,3-L,4-Median,5-Typical,6-Weighted
extern int MA_Length =1; //MA's Period
extern int MA_Mode =0; //MA's Method:0-SMA,1-EMA,2-SMMA,3-LWMA
extern int ATR_Length =10;//ATR's Period
extern double Kv =4; //Volatility's Factor or Multiplier
extern double MoneyRisk =1; //Offset Factor
extern bool usePrice_HiLoBreak =true;
extern bool useMA_HiLoEnvelope =false;
extern int AlertMode =0; //0-alert off,1-on
extern int VisualMode =0; //0-lines,1-dots
extern string Applied_Price_ ="0-C,1-O,2-H,3-L,4-Median(H+L)/2,5-Typical(H+L+C)/3,6-Weighted(H+L+C+C)/4";
extern string MAmethod_mode_ ="0-SMA,1-EMA,2-SMMA,3-LWMA";
extern string Visual_Mode___ ="0-lines,1-dots ";
//---- indicator buffers
double UpBuffer[];
double DnBuffer[];
double UpSignal[];
double DnSignal[];
double smin[];
double smax[];
double trend[];
bool UpTrendAlert=false, DownTrendAlert=false;
//+--------- TRO MODIFICATION ---------------------------------------+
string symbol, tChartPeriod, tShortName ;
int digits, period ;
bool Trigger ;
int OldBars = -1 ;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//+--------- TRO MODIFICATION ---------------------------------------+
period = Period() ;
tChartPeriod = TimeFrameToString(period) ;
symbol = Symbol() ;
digits = Digits ;
tShortName = "tbb"+ symbol + tChartPeriod ;
string short_name;
//---- indicator line
IndicatorBuffers(7);
SetIndexBuffer(0,UpBuffer);
SetIndexBuffer(1,DnBuffer);
SetIndexBuffer(2,UpSignal);
SetIndexBuffer(3,DnSignal);
SetIndexBuffer(4,smin);
SetIndexBuffer(5,smax);
SetIndexBuffer(6,trend);
//----
if(VisualMode==0)
{
SetIndexStyle(0,DRAW_LINE);
SetIndexStyle(1,DRAW_LINE);
}
else
{
SetIndexStyle(0,DRAW_ARROW);
SetIndexStyle(1,DRAW_ARROW);
SetIndexArrow(0,159);
SetIndexArrow(1,159);
}
//----
SetIndexStyle(2,DRAW_ARROW);
SetIndexStyle(3,DRAW_ARROW);
SetIndexArrow(2,108);
SetIndexArrow(3,108);
//---- name for DataWindow and indicator subwindow label
short_name="VoltyChannel_Stop("+MA_Length+","+ATR_Length+","+DoubleToStr(Kv,3)+")";
IndicatorShortName(short_name);
SetIndexLabel(0,"UpTrend");
SetIndexLabel(1,"DnTrend");
SetIndexLabel(2,"UpSignal");
SetIndexLabel(3,"DnSignal");
//----
SetIndexDrawBegin(0,MA_Length+ATR_Length);
SetIndexDrawBegin(1,MA_Length+ATR_Length);
SetIndexDrawBegin(2,MA_Length+ATR_Length);
SetIndexDrawBegin(3,MA_Length+ATR_Length);
//----
return(0);
}
int deinit()
{
//+--------- TRO MODIFICATION ---------------------------------------+
ObjectDelete(tShortName+"01");
ObjectDelete(tShortName+"02");
//----
return(0);
}
//+------------------------------------------------------------------+
//| VoltyChannel_Stop_2 |
//+------------------------------------------------------------------+
int start()
{
//+--------- TRO MODIFICATION ---------------------------------------+
if( Bars != OldBars ) { Trigger = True ; }
int shift,limit, counted_bars=IndicatorCounted();
//----
if(counted_bars > 0) limit=Bars-counted_bars;
if(counted_bars < 0) return(0);
if(counted_bars ==0) limit=Bars-MA_Length-1;
for(shift=limit;shift>=0;shift--)
{
if(useMA_HiLoEnvelope)
{
double bprice=iMA(NULL,0,MA_Length,0,MA_Mode,2,shift);
double sprice=iMA(NULL,0,MA_Length,0,MA_Mode,3,shift);
}
else
{
bprice=iMA(NULL,0,MA_Length,0,MA_Mode,MA_Price,shift);
sprice=iMA(NULL,0,MA_Length,0,MA_Mode,MA_Price,shift);
}
smax[shift]=bprice + Kv*iATR(NULL,0,ATR_Length,shift);
smin[shift]=sprice - Kv*iATR(NULL,0,ATR_Length,shift);
trend[shift]=trend[shift+1];
if(usePrice_HiLoBreak)
{
if(High[shift] > smax[shift+1])trend[shift]= 1;
if(Low[shift] < smin[shift+1])trend[shift]=-1;
}
else
{
if(bprice > smax[shift+1])trend[shift]= 1;
if(sprice < smin[shift+1])trend[shift]=-1;
}
if(trend[shift] >0)
{
if(smin[shift] < smin[shift+1]) smin[shift]=smin[shift+1];
UpBuffer[shift]=smin[shift] - (MoneyRisk - 1)*iATR(NULL,0,ATR_Length,shift);
if(UpBuffer[shift] < UpBuffer[shift+1] && UpBuffer[shift+1]!=EMPTY_VALUE) UpBuffer[shift]=UpBuffer[shift+1];
if(trend[shift+1]!=trend[shift]) UpSignal[shift]=UpBuffer[shift];
else UpSignal[shift]=EMPTY_VALUE;
DnBuffer[shift]=EMPTY_VALUE;
DnSignal[shift]=EMPTY_VALUE;
}
else
if(trend[shift] <0)
{
if(smax[shift]>smax[shift+1]) smax[shift]=smax[shift+1];
DnBuffer[shift]=smax[shift] + (MoneyRisk - 1)*iATR(NULL,0,ATR_Length,shift);
if(DnBuffer[shift] > DnBuffer[shift+1]) DnBuffer[shift]=DnBuffer[shift+1];
if(trend[shift+1]!=trend[shift]) DnSignal[shift]=DnBuffer[shift];
else DnSignal[shift]=EMPTY_VALUE;
UpBuffer[shift]=EMPTY_VALUE;
UpSignal[shift]=EMPTY_VALUE;
}
}
//----
string Message;
if(trend[2]<0 && trend[1]>0 && Volume[0]>1 && !UpTrendAlert)
{
Message=" "+Symbol()+" M"+Period()+": VCS Signal for BUY";
if(AlertMode>0)Alert (Message);
UpTrendAlert=true; DownTrendAlert=false;
}
if(trend[2]>0 && trend[1]<0 && Volume[0]>1 && !DownTrendAlert)
{
Message=" "+Symbol()+" M"+Period()+": VCS Signal for SELL";
if(AlertMode>0)Alert (Message);
DownTrendAlert=true; UpTrendAlert=false;
}
//----
//+--------- TRO MODIFICATION ---------------------------------------+
if ( Trigger && Sound.Alert )
{
if( Close[0] > UpBuffer[0] && UpBuffer[0]!= EMPTY_VALUE ) { Trigger = False ; Alert(symbol," ", tChartPeriod, " Price above Upper Volty "+ DoubleToStr(UpBuffer[0] ,digits)); }
if( Close[0] < DnBuffer[0] && DnBuffer[0]!= EMPTY_VALUE ) { Trigger = False ; Alert(symbol," ", tChartPeriod, " Price below Lower Volty "+ DoubleToStr(DnBuffer[0] ,digits)); }
}
if(Show.PriceBox) { DoBox() ; }
OldBars = Bars ;
//+--------- TRO MODIFICATION ---------------------------------------+
//----
return(0);
}
//+------------------------------------------------------------------+
//+--------- TRO MODIFICATION ---------------------------------------+
void DoBox()
{
if (ObjectFind(tShortName+"01") != 0)
{
ObjectCreate(tShortName+"01",OBJ_ARROW,0,Time[0],UpBuffer[0]);
ObjectSet(tShortName+"01",OBJPROP_ARROWCODE,SYMBOL_RIGHTPRICE);
ObjectSet(tShortName+"01",OBJPROP_COLOR,indicator_color1);
ObjectSet(tShortName+"01",OBJPROP_WIDTH,myBoxWidth);
}
else
{
ObjectMove(tShortName+"01",0,Time[0],UpBuffer[0]);
ObjectSet(tShortName+"01",OBJPROP_COLOR,indicator_color1);
}
if (ObjectFind(tShortName+"02") != 0)
{
ObjectCreate(tShortName+"02",OBJ_ARROW,0,Time[0],DnBuffer[0]);
ObjectSet(tShortName+"02",OBJPROP_ARROWCODE,SYMBOL_RIGHTPRICE);
ObjectSet(tShortName+"02",OBJPROP_COLOR,indicator_color2);
ObjectSet(tShortName+"02",OBJPROP_WIDTH,myBoxWidth);
}
else
{
ObjectMove(tShortName+"02",0,Time[0],DnBuffer[0]);
ObjectSet(tShortName+"02",OBJPROP_COLOR,indicator_color2);
}
return(0);
}
//+--------- TRO MODIFICATION ---------------------------------------+
string TimeFrameToString(int tf)
{
string tfs;
switch(tf) {
case PERIOD_M1: tfs="M1" ; break;
case PERIOD_M5: tfs="M5" ; break;
case PERIOD_M15: tfs="M15" ; break;
case PERIOD_M30: tfs="M30" ; break;
case PERIOD_H1: tfs="H1" ; break;
case PERIOD_H4: tfs="H4" ; break;
case PERIOD_D1: tfs="D1" ; break;
case PERIOD_W1: tfs="W1" ; break;
case PERIOD_MN1: tfs="MN";
}
return(tfs);
}
//+--------- TRO MODIFICATION ---------------------------------------+