-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBollinger Squeeze Basic.mq4
203 lines (181 loc) · 6.48 KB
/
Bollinger Squeeze Basic.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
//+--------------------------------------------------------------------------+
//| Bollinger Squeeze Basic.mq4 |
//| Copyright © 2022, EarnForex.com |
//| https://www.earnforex.com/metatrader-indicators/Bollinger-Squeeze-Basic/ |
//+--------------------------------------------------------------------------+
#property copyright "Copyright © 2022, EarnForex.com"
#property link "https://www.earnforex.com/metatrader-indicators/Bollinger-Squeeze-Basic/"
#property version "1.00"
#property strict
#property description "Bollinger Squeeze Basic shows the change of momentum histogram and BB / Keltner channel dots."
#property description "Red dots are shown when Bollinger bands are outside of the Keltner channel."
#property description "Blue dots are shown when Bollinger bands are inside the Keltner channel."
#property indicator_separate_window
#property indicator_buffers 6
#property indicator_color1 clrLimeGreen
#property indicator_type1 DRAW_HISTOGRAM
#property indicator_width1 2
#property indicator_color2 clrIndianRed
#property indicator_width1 2
#property indicator_label1 "Momentum Histo"
#property indicator_type2 DRAW_HISTOGRAM
#property indicator_width2 2
#property indicator_label2 "Momentum Histo"
#property indicator_color3 clrLightGreen
#property indicator_type3 DRAW_HISTOGRAM
#property indicator_width3 2
#property indicator_label3 "Momentum Histo"
#property indicator_color4 clrLightPink
#property indicator_type4 DRAW_HISTOGRAM
#property indicator_width4 2
#property indicator_label4 "Momentum Histo"
#property indicator_color5 clrBlue
#property indicator_type5 DRAW_ARROW
#property indicator_label5 "BB/Keltner"
#property indicator_color6 clrRed
#property indicator_type6 DRAW_ARROW
#property indicator_label6 "BB/Keltner"
enum enum_candle_to_check
{
Current,
Previous
};
input int MaxBars = 300;
input int BB_Period = 20; // BB Period
input double BB_Deviation = 2.0; // BB Deviation
input int Keltner_Period = 20; // Keltner Period
input double Keltner_Factor = 1.5; // Keltner Factor
input int Momentum_Period = 12; // Momentum Period
input bool EnableNativeAlerts = false;
input bool EnableEmailAlerts = false;
input bool EnablePushAlerts = false;
input enum_candle_to_check TriggerCandle = Previous;
// Indicator buffers:
double upB[];
double loB[];
double upK[];
double loK[];
double upB2[];
double loB2[];
// Global variables:
datetime LastAlertTime = D'01.01.1970';
int LastAlertDirection = 0;
void OnInit()
{
SetIndexBuffer(0, upB);
SetIndexEmptyValue(0, 0);
SetIndexBuffer(1, loB);
SetIndexEmptyValue(1, 0);
SetIndexBuffer(2, upB2);
SetIndexEmptyValue(2, 0);
SetIndexBuffer(3, loB2);
SetIndexEmptyValue(3, 0);
SetIndexBuffer(4, upK);
SetIndexEmptyValue(42, EMPTY_VALUE);
SetIndexArrow(4, 167);
SetIndexBuffer(5, loK);
SetIndexEmptyValue(5, EMPTY_VALUE);
SetIndexArrow(5, 167);
}
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
int limit = MaxBars;
if (limit > Bars) limit = Bars;
double d = 0, prev_d = 0;
for (int shift = limit - 1; shift >= 0; shift--)
{
d = iMomentum(NULL, 0, Momentum_Period, PRICE_CLOSE, shift) - 100;
if (d > 0)
{
if (d >= prev_d)
{
upB[shift] = d;
upB2[shift] = 0;
}
else
{
upB2[shift] = d;
upB[shift] = 0;
}
loB[shift] = 0;
loB2[shift] = 0;
}
else if (d < 0)
{
if (d <= prev_d)
{
loB[shift] = d;
loB2[shift] = 0;
}
else
{
loB2[shift] = d;
loB[shift] = 0;
}
upB[shift] = 0;
upB2[shift] = 0;
}
else
{
upB[shift] = 0.01;
upB2[shift] = 0.01;
loB[shift] = -0.01;
loB2[shift] = -0.01;
}
prev_d = d;
double diff = iATR(NULL, 0, Keltner_Period, shift) * Keltner_Factor;
double std = iStdDev(NULL, 0, BB_Period, MODE_SMA, 0, PRICE_CLOSE, shift);
double bbs = BB_Deviation * std / diff;
if (bbs < 1)
{
upK[shift] = 0;
loK[shift] = EMPTY_VALUE; // Sell
}
else
{
loK[shift] = 0;
upK[shift] = EMPTY_VALUE; // Buy
}
}
if (((TriggerCandle > 0) && (Time[0] > LastAlertTime)) || (TriggerCandle == 0))
{
string Text;
// Outside Alert
if ((loK[TriggerCandle] == 0) && (LastAlertDirection != 1))
{
if (LastAlertDirection != 0) // Skip actual alerts if it is the first run after attachment.
{
Text = "BB Squeeze: " + Symbol() + " - " + StringSubstr(EnumToString((ENUM_TIMEFRAMES)Period()), 7) + " - BB Outside Keltner.";
if (EnableNativeAlerts) Alert(Text);
if (EnableEmailAlerts) SendMail("BB Squeeze Alert", Text);
if (EnablePushAlerts) SendNotification(Text);
}
LastAlertTime = Time[0];
LastAlertDirection = 1;
}
// Inside Alert
if ((upK[TriggerCandle] == 0) && (LastAlertDirection != -1))
{
if (LastAlertDirection != 0) // Skip actual alerts if it is the first run after attachment.
{
Text = "BB Squeeze: " + Symbol() + " - " + StringSubstr(EnumToString((ENUM_TIMEFRAMES)Period()), 7) + " - BB Inside Keltner.";
if (EnableNativeAlerts) Alert(Text);
if (EnableEmailAlerts) SendMail("BB Squeeze Alert", Text);
if (EnablePushAlerts) SendNotification(Text);
}
LastAlertTime = Time[0];
LastAlertDirection = -1;
}
}
return rates_total;
}
//+------------------------------------------------------------------+