/
DonchianChannel.mqh
259 lines (241 loc) · 12.6 KB
/
DonchianChannel.mqh
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
//+--------------------------------------------------------------------+
//| Copyright: (C) 2016 Forex Software Ltd. |
//| Website: http://forexsb.com/ |
//| Support: http://forexsb.com/forum/ |
//| License: Proprietary under the following circumstances: |
//| |
//| This code is a part of Forex Strategy Builder. It is free for |
//| use as an integral part of Forex Strategy Builder. |
//| One can modify it in order to improve the code or to fit it for |
//| personal use. This code or any part of it cannot be used in |
//| other applications without a permission. |
//| The contact information cannot be changed. |
//| |
//| NO LIABILITY FOR CONSEQUENTIAL DAMAGES |
//| |
//| In no event shall the author be liable for any damages whatsoever |
//| (including, without limitation, incidental, direct, indirect and |
//| consequential damages, damages for loss of business profits, |
//| business interruption, loss of business information, or other |
//| pecuniary loss) arising out of the use or inability to use this |
//| product, even if advised of the possibility of such damages. |
//+--------------------------------------------------------------------+
#property copyright "Copyright (C) 2016 Forex Software Ltd."
#property link "http://forexsb.com"
#property version "2.01"
#property strict
#include <Forexsb.com/Indicator.mqh>
#include <Forexsb.com/Enumerations.mqh>
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
class DonchianChannel : public Indicator
{
public:
DonchianChannel(SlotTypes slotType);
virtual void Calculate(DataSet &dataSet);
};
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void DonchianChannel::DonchianChannel(SlotTypes slotType)
{
SlotType = slotType;
IndicatorName = "Donchian Channel";
WarningMessage = "";
IsAllowLTF = true;
ExecTime = ExecutionTime_DuringTheBar;
IsSeparateChart = false;
IsDiscreteValues = false;
IsDefaultGroupAll = false;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void DonchianChannel::Calculate(DataSet &dataSet)
{
Data=GetPointer(dataSet);
string logicRule=ListParam[0].Text;
int period = (int) NumParam[0].Value;
int shift = (int) NumParam[1].Value;
int previous = CheckParam[0].Checked ? 1 : 0;
double upperBand[]; ArrayResize(upperBand, Data.Bars); ArrayInitialize(upperBand, 0);
double lowerBand[]; ArrayResize(lowerBand, Data.Bars); ArrayInitialize(lowerBand, 0);
int firstBar=period+shift+previous+2;
for(int bar=period+previous+2; bar<Data.Bars-shift; bar++)
{
double max = DBL_MIN;
double min = DBL_MAX;
for(int i=0; i<period; i++)
{
if(Data.High[bar-i]>max)
max=Data.High[bar-i];
if(Data.Low[bar-i]<min)
min=Data.Low[bar-i];
}
upperBand[bar + shift] = max;
lowerBand[bar + shift] = min;
}
ArrayResize(Component[0].Value,Data.Bars);
Component[0].CompName = "Upper Band";
Component[0].DataType = IndComponentType_IndicatorValue;
Component[0].FirstBar = firstBar;
ArrayCopy(Component[0].Value,upperBand);
ArrayResize(Component[1].Value,Data.Bars);
Component[1].CompName = "Lower Band";
Component[1].DataType = IndComponentType_IndicatorValue;
Component[1].FirstBar = firstBar;
ArrayCopy(Component[1].Value,lowerBand);
ArrayResize(Component[2].Value,Data.Bars);
Component[2].FirstBar=firstBar;
ArrayResize(Component[3].Value,Data.Bars);
Component[3].FirstBar=firstBar;
if(SlotType==SlotTypes_Open)
{
Component[2].DataType = IndComponentType_OpenLongPrice;
Component[2].CompName = "Long position entry price";
Component[3].DataType = IndComponentType_OpenShortPrice;
Component[3].CompName = "Short position entry price";
}
else if(SlotType==SlotTypes_OpenFilter)
{
Component[2].DataType = IndComponentType_AllowOpenLong;
Component[2].CompName = "Is long entry allowed";
Component[3].DataType = IndComponentType_AllowOpenShort;
Component[3].CompName = "Is short entry allowed";
}
else if(SlotType==SlotTypes_Close)
{
Component[2].DataType = IndComponentType_CloseLongPrice;
Component[2].CompName = "Long position closing price";
Component[3].DataType = IndComponentType_CloseShortPrice;
Component[3].CompName = "Short position closing price";
}
else if(SlotType==SlotTypes_CloseFilter)
{
Component[2].DataType = IndComponentType_ForceCloseLong;
Component[2].CompName = "Close out long position";
Component[3].DataType = IndComponentType_ForceCloseShort;
Component[3].CompName = "Close out short position";
}
if(SlotType==SlotTypes_Open || SlotType==SlotTypes_Close)
{
if(period>1)
{
for(int bar=firstBar; bar<Data.Bars; bar++)
{
// Covers the cases when the price can pass through the band without a signal.
double open=Data.Open[bar]; // Current open price
// Upper band
double valueUp=upperBand[bar-previous]; // Current value
double valueUp1=upperBand[bar-previous-1]; // Previous value
double tempValUp=valueUp;
if((valueUp1>Data.High[bar-1] && valueUp<open) || // The Data.Open price jumps above the indicator
(valueUp1 < Data.Low[bar-1] && valueUp > open) || // The Data.Open price jumps below the indicator
(Data.Close[bar-1] < valueUp && valueUp < open) || // The Data.Open price is in a positive gap
(Data.Close[bar-1] > valueUp && valueUp > open)) // The Data.Open price is in a negative gap
tempValUp=open; // The entry/exit level is moved to Data.Open price
// Lower band
double valueDown=lowerBand[bar-previous]; // Current value
double valueDown1=lowerBand[bar-previous-1]; // Previous value
double tempValDown=valueDown;
if((valueDown1>Data.High[bar-1] && valueDown<open) || // The Data.Open price jumps above the indicator
(valueDown1 < Data.Low[bar-1] && valueDown > open) || // The Data.Open price jumps below the indicator
(Data.Close[bar-1] < valueDown && valueDown < open) || // The Data.Open price is in a positive gap
(Data.Close[bar-1]>valueDown && valueDown>open)) // The Data.Open price is in a negative gap
tempValDown=open; // The entry/exit level is moved to Data.Open price
if(logicRule=="Enter long at Upper Band" ||
logicRule=="Exit long at Upper Band")
{
Component[2].Value[bar] = tempValUp;
Component[3].Value[bar] = tempValDown;
}
else
{
Component[2].Value[bar] = tempValDown;
Component[3].Value[bar] = tempValUp;
}
}
}
else
{
for(int bar=2; bar<Data.Bars; bar++)
{
if(logicRule=="Enter long at Upper Band" || logicRule=="Exit long at Upper Band")
{
Component[2].Value[bar] = upperBand[bar - previous];
Component[3].Value[bar] = lowerBand[bar - previous];
}
else
{
Component[2].Value[bar] = lowerBand[bar - previous];
Component[3].Value[bar] = upperBand[bar - previous];
}
}
}
}
else
{
if(logicRule=="The bar opens below Upper Band")
BandIndicatorLogic(firstBar,previous,upperBand,lowerBand,Component[2],Component[3],BandIndLogic_The_bar_opens_below_the_Upper_Band);
else if(logicRule=="The bar opens above Upper Band")
BandIndicatorLogic(firstBar,previous,upperBand,lowerBand,Component[2],Component[3],BandIndLogic_The_bar_opens_above_the_Upper_Band);
else if(logicRule=="The bar opens below Lower Band")
BandIndicatorLogic(firstBar,previous,upperBand,lowerBand,Component[2],Component[3],BandIndLogic_The_bar_opens_below_the_Lower_Band);
else if(logicRule=="The bar opens above Lower Band")
BandIndicatorLogic(firstBar,previous,upperBand,lowerBand,Component[2],Component[3],BandIndLogic_The_bar_opens_above_the_Lower_Band);
else if(logicRule=="The bar opens below Upper Band after opening above it")
BandIndicatorLogic(firstBar,previous,upperBand,lowerBand,Component[2],Component[3],BandIndLogic_The_bar_opens_below_Upper_Band_after_above);
else if(logicRule=="The bar opens above Upper Band after opening below it")
BandIndicatorLogic(firstBar,previous,upperBand,lowerBand,Component[2],Component[3],BandIndLogic_The_bar_opens_above_Upper_Band_after_below);
else if(logicRule=="The bar opens below Lower Band after opening above it")
BandIndicatorLogic(firstBar,previous,upperBand,lowerBand,Component[2],Component[3],BandIndLogic_The_bar_opens_below_Lower_Band_after_above);
else if(logicRule=="The bar opens above Lower Band after opening below it")
BandIndicatorLogic(firstBar,previous,upperBand,lowerBand,Component[2],Component[3],BandIndLogic_The_bar_opens_above_Lower_Band_after_below);
else if(logicRule=="The position opens above Upper Band")
{
Component[0].PosPriceDependence = PositionPriceDependence_PriceBuyHigher;
Component[1].PosPriceDependence = PositionPriceDependence_PriceSellLower;
Component[2].DataType = IndComponentType_Other;
Component[3].DataType = IndComponentType_Other;
Component[2].ShowInDynInfo = false;
Component[3].ShowInDynInfo = false;
}
else if(logicRule=="The position opens below Upper Band")
{
Component[0].PosPriceDependence = PositionPriceDependence_PriceBuyLower;
Component[1].PosPriceDependence = PositionPriceDependence_PriceSellHigher;
Component[2].DataType = IndComponentType_Other;
Component[3].DataType = IndComponentType_Other;
Component[2].ShowInDynInfo = false;
Component[3].ShowInDynInfo = false;
}
else if(logicRule=="The position opens above Lower Band")
{
Component[0].PosPriceDependence = PositionPriceDependence_PriceSellLower;
Component[1].PosPriceDependence = PositionPriceDependence_PriceBuyHigher;
Component[2].DataType = IndComponentType_Other;
Component[3].DataType = IndComponentType_Other;
Component[2].ShowInDynInfo = false;
Component[3].ShowInDynInfo = false;
}
else if(logicRule=="The position opens below Lower Band")
{
Component[0].PosPriceDependence = PositionPriceDependence_PriceSellHigher;
Component[1].PosPriceDependence = PositionPriceDependence_PriceBuyLower;
Component[2].DataType = IndComponentType_Other;
Component[3].DataType = IndComponentType_Other;
Component[2].ShowInDynInfo = false;
Component[3].ShowInDynInfo = false;
}
else if(logicRule=="The bar closes below Upper Band")
BandIndicatorLogic(firstBar,previous,upperBand,lowerBand,Component[2],Component[3],BandIndLogic_The_bar_closes_below_the_Upper_Band);
else if(logicRule=="The bar closes above Upper Band")
BandIndicatorLogic(firstBar,previous,upperBand,lowerBand,Component[2],Component[3],BandIndLogic_The_bar_closes_above_the_Upper_Band);
else if(logicRule=="The bar closes below Lower Band")
BandIndicatorLogic(firstBar,previous,upperBand,lowerBand,Component[2],Component[3],BandIndLogic_The_bar_closes_below_the_Lower_Band);
else if(logicRule=="The bar closes above Lower Band")
BandIndicatorLogic(firstBar,previous,upperBand,lowerBand,Component[2],Component[3],BandIndLogic_The_bar_closes_above_the_Lower_Band);
}
}
//+------------------------------------------------------------------+