-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathICT_BreakerBlocks.cpp
More file actions
552 lines (471 loc) · 17.2 KB
/
Copy pathICT_BreakerBlocks.cpp
File metadata and controls
552 lines (471 loc) · 17.2 KB
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
// ============================================================================
// ICT Breaker Blocks - Sierra Chart ACSIL study
// ----------------------------------------------------------------------------
// A breaker block is an order block that failed and then had its origin swing
// violated in the opposite direction. It flips polarity and is expected to act
// as support / resistance on the retest.
//
// BULLISH BREAKER
// swing low L1 -> swing high H1 -> lower low L2 (< L1)
// then a bar closes ABOVE H1 ==> the bearish order block that produced
// the drop from H1 has failed. Its range becomes a bullish breaker
// (support) and is projected forward.
//
// BEARISH BREAKER
// swing high H1 -> swing low L1 -> higher high H2 (> H1)
// then a bar closes BELOW L1 ==> the bullish order block that produced
// the rally from L1 has failed. Its range becomes a bearish breaker
// (resistance).
//
// The order block candle is the last up-close candle at the swing high
// (bullish breaker) or the last down-close candle at the swing low
// (bearish breaker), searched back a configurable number of bars.
//
// Detection runs on CLOSED bars only, so a zone appears on the bar after the
// break confirms. Pivots additionally lag by "Swing Strength" bars, which is
// inherent to any swing-based structure method.
// ============================================================================
#include "sierrachart.h"
#include <vector>
SCDLLName("ICT Breaker Blocks")
namespace ICTBreaker
{
struct s_Swing
{
int BarIndex;
float Price;
int IsHigh; // 1 = swing high, 0 = swing low
};
struct s_Breaker
{
int IsBullish;
int OBBarIndex; // left edge of the zone (order block candle)
int AnchorSwingBar; // the swing pivot this breaker was built from
int BreakBarIndex; // bar whose close confirmed the breaker
float Top;
float Bottom;
int Mitigated;
int MitigatedBarIndex;
int RectLineNumber;
int TextLineNumber;
};
// Keep the swing list strictly alternating high / low. If two swings of the
// same type arrive in a row, keep only the more extreme one. This produces a
// clean zig-zag that the structure rules can rely on.
inline void AddSwing(std::vector<s_Swing>& Swings, const s_Swing& NewSwing)
{
if (!Swings.empty() && Swings.back().IsHigh == NewSwing.IsHigh)
{
s_Swing& Last = Swings.back();
if (NewSwing.IsHigh)
{
if (NewSwing.Price >= Last.Price)
Last = NewSwing;
}
else
{
if (NewSwing.Price <= Last.Price)
Last = NewSwing;
}
return;
}
Swings.push_back(NewSwing);
if (Swings.size() > 200)
Swings.erase(Swings.begin());
}
}
using namespace ICTBreaker;
/*==========================================================================*/
SCSFExport scsf_ICTBreakerBlocks(SCStudyInterfaceRef sc)
{
SCSubgraphRef Subgraph_BullTop = sc.Subgraph[0];
SCSubgraphRef Subgraph_BullBottom = sc.Subgraph[1];
SCSubgraphRef Subgraph_BearTop = sc.Subgraph[2];
SCSubgraphRef Subgraph_BearBottom = sc.Subgraph[3];
SCInputRef Input_SwingStrength = sc.Input[0];
SCInputRef Input_MaxPerSide = sc.Input[1];
SCInputRef Input_OBLookback = sc.Input[2];
SCInputRef Input_BodyOnly = sc.Input[3];
SCInputRef Input_ExtendBars = sc.Input[4];
SCInputRef Input_BreakOnClose = sc.Input[5];
SCInputRef Input_MitigateOnClose = sc.Input[6];
SCInputRef Input_HideMitigated = sc.Input[7];
SCInputRef Input_BullColor = sc.Input[8];
SCInputRef Input_BearColor = sc.Input[9];
SCInputRef Input_Transparency = sc.Input[10];
SCInputRef Input_ShowLabels = sc.Input[11];
SCInputRef Input_EnableAlerts = sc.Input[12];
SCInputRef Input_ExportLevels = sc.Input[13];
// ---------------------------------------------------------------- defaults
if (sc.SetDefaults)
{
sc.GraphName = "ICT Breaker Blocks";
sc.StudyDescription = "Detects ICT bullish and bearish breaker blocks from swing structure and draws the failed order block ranges as zones.";
sc.GraphRegion = 0;
sc.AutoLoop = 0;
sc.ValueFormat = VALUEFORMAT_INHERITED;
sc.DrawZeros = 0;
sc.FreeDLL = 0;
sc.MaintainVolumeAtPriceData = 0;
Subgraph_BullTop.Name = "Bull Breaker Top";
Subgraph_BullTop.DrawStyle = DRAWSTYLE_IGNORE;
Subgraph_BullBottom.Name = "Bull Breaker Bottom";
Subgraph_BullBottom.DrawStyle = DRAWSTYLE_IGNORE;
Subgraph_BearTop.Name = "Bear Breaker Top";
Subgraph_BearTop.DrawStyle = DRAWSTYLE_IGNORE;
Subgraph_BearBottom.Name = "Bear Breaker Bottom";
Subgraph_BearBottom.DrawStyle = DRAWSTYLE_IGNORE;
Input_SwingStrength.Name = "Swing Strength (Bars Each Side)";
Input_SwingStrength.SetInt(5);
Input_SwingStrength.SetIntLimits(1, 100);
Input_MaxPerSide.Name = "Max Breakers Per Direction";
Input_MaxPerSide.SetInt(5);
Input_MaxPerSide.SetIntLimits(1, 50);
Input_OBLookback.Name = "Order Block Search Bars";
Input_OBLookback.SetInt(10);
Input_OBLookback.SetIntLimits(0, 100);
Input_BodyOnly.Name = "Use Candle Body Only For Zone";
Input_BodyOnly.SetYesNo(0);
Input_ExtendBars.Name = "Extend Zone Past Last Bar (Bars)";
Input_ExtendBars.SetInt(10);
Input_ExtendBars.SetIntLimits(0, 500);
Input_BreakOnClose.Name = "Structure Break Requires Bar Close";
Input_BreakOnClose.SetYesNo(1);
Input_MitigateOnClose.Name = "Mitigation Requires Bar Close";
Input_MitigateOnClose.SetYesNo(1);
Input_HideMitigated.Name = "Hide Mitigated Breakers";
Input_HideMitigated.SetYesNo(1);
Input_BullColor.Name = "Bullish Breaker Color";
Input_BullColor.SetColor(0, 160, 90);
Input_BearColor.Name = "Bearish Breaker Color";
Input_BearColor.SetColor(200, 60, 60);
Input_Transparency.Name = "Zone Fill Transparency (0-100)";
Input_Transparency.SetInt(80);
Input_Transparency.SetIntLimits(0, 100);
Input_ShowLabels.Name = "Show Text Labels";
Input_ShowLabels.SetYesNo(1);
Input_EnableAlerts.Name = "Enable Alerts On New Breaker";
Input_EnableAlerts.SetYesNo(0);
// The four subgraphs are a data feed for spreadsheet studies / alert
// conditions, not something to plot. They stay at zero unless this is
// switched on.
Input_ExportLevels.Name = "Export Zone Levels To Subgraphs";
Input_ExportLevels.SetYesNo(0);
return;
}
// -------------------------------------------------------- persistent state
std::vector<s_Swing>* p_Swings = (std::vector<s_Swing>*)sc.GetPersistentPointer(1);
std::vector<s_Breaker>* p_Breakers = (std::vector<s_Breaker>*)sc.GetPersistentPointer(2);
if (sc.LastCallToFunction)
{
if (p_Swings != NULL)
{
delete p_Swings;
sc.SetPersistentPointer(1, NULL);
}
if (p_Breakers != NULL)
{
delete p_Breakers;
sc.SetPersistentPointer(2, NULL);
}
return;
}
if (p_Swings == NULL)
{
p_Swings = new std::vector<s_Swing>();
sc.SetPersistentPointer(1, p_Swings);
}
if (p_Breakers == NULL)
{
p_Breakers = new std::vector<s_Breaker>();
sc.SetPersistentPointer(2, p_Breakers);
}
std::vector<s_Swing>& Swings = *p_Swings;
std::vector<s_Breaker>& Breakers = *p_Breakers;
int& LineNumberCounter = sc.GetPersistentInt(1);
int& LastProcessedBar = sc.GetPersistentInt(2);
int& LastBullSwingBar = sc.GetPersistentInt(3); // dedup guards
int& LastBearSwingBar = sc.GetPersistentInt(4);
const int SwingStrength = Input_SwingStrength.GetInt();
const int MaxPerSide = Input_MaxPerSide.GetInt();
const int OBLookback = Input_OBLookback.GetInt();
const bool BodyOnly = Input_BodyOnly.GetYesNo() != 0;
const int ExtendBars = Input_ExtendBars.GetInt();
const bool BreakOnClose = Input_BreakOnClose.GetYesNo() != 0;
const bool MitigateOnClose = Input_MitigateOnClose.GetYesNo() != 0;
const bool HideMitigated = Input_HideMitigated.GetYesNo() != 0;
const bool ShowLabels = Input_ShowLabels.GetYesNo() != 0;
const bool IsFullRecalculation = (sc.UpdateStartIndex == 0);
if (IsFullRecalculation)
{
Swings.clear();
Breakers.clear();
LineNumberCounter = 70000;
LastProcessedBar = -1;
LastBullSwingBar = -1;
LastBearSwingBar = -1;
sc.DeleteACSChartDrawing(sc.ChartNumber, TOOL_DELETE_ALL, 0);
}
if (sc.ArraySize < (SwingStrength * 2 + 5))
return;
// ================================================================
// PASS 1 - process newly closed bars: pivots, breakers, mitigation
// ================================================================
const int LastClosedBar = sc.ArraySize - 2;
for (int i = max(0, LastProcessedBar + 1); i <= LastClosedBar; i++)
{
// ---- confirm a pivot that sits SwingStrength bars back -------------
const int p = i - SwingStrength;
if (p - SwingStrength >= 0)
{
bool IsSwingHigh = true;
bool IsSwingLow = true;
for (int j = p - SwingStrength; j <= p + SwingStrength; j++)
{
if (j == p)
continue;
if (j < p)
{
if (sc.High[j] >= sc.High[p]) IsSwingHigh = false;
if (sc.Low[j] <= sc.Low[p]) IsSwingLow = false;
}
else
{
if (sc.High[j] > sc.High[p]) IsSwingHigh = false;
if (sc.Low[j] < sc.Low[p]) IsSwingLow = false;
}
}
if (IsSwingHigh)
{
s_Swing NewSwing;
NewSwing.BarIndex = p;
NewSwing.Price = sc.High[p];
NewSwing.IsHigh = 1;
AddSwing(Swings, NewSwing);
}
if (IsSwingLow)
{
s_Swing NewSwing;
NewSwing.BarIndex = p;
NewSwing.Price = sc.Low[p];
NewSwing.IsHigh = 0;
AddSwing(Swings, NewSwing);
}
}
// ---- look for a new breaker forming on bar i -----------------------
// The pattern is examined at the tail of the swing list. Offset 1 is
// checked as well in case a fresh pivot was confirmed on this same bar.
for (int Offset = 0; Offset <= 1; Offset++)
{
const int n = (int)Swings.size() - Offset;
if (n < 3)
break;
const s_Swing& S3 = Swings[n - 1]; // most recent
const s_Swing& S2 = Swings[n - 2];
const s_Swing& S1 = Swings[n - 3];
if (S3.BarIndex >= i)
continue;
// ---------------- bullish breaker: L1 -> H1 -> L2(lower) -> break up
if (!S1.IsHigh && S2.IsHigh && !S3.IsHigh
&& S3.Price < S1.Price
&& S2.BarIndex > LastBullSwingBar)
{
const float TestValue = BreakOnClose ? sc.Close[i] : sc.High[i];
if (TestValue > S2.Price)
{
// order block = last up-close candle at / before the swing high
int OBIndex = S2.BarIndex;
for (int j = S2.BarIndex; j >= max(0, S2.BarIndex - OBLookback); j--)
{
if (sc.Close[j] > sc.Open[j])
{
OBIndex = j;
break;
}
}
s_Breaker NewBreaker;
NewBreaker.IsBullish = 1;
NewBreaker.OBBarIndex = OBIndex;
NewBreaker.AnchorSwingBar = S2.BarIndex;
NewBreaker.BreakBarIndex = i;
NewBreaker.Top = BodyOnly ? max(sc.Open[OBIndex], sc.Close[OBIndex]) : sc.High[OBIndex];
NewBreaker.Bottom = BodyOnly ? min(sc.Open[OBIndex], sc.Close[OBIndex]) : sc.Low[OBIndex];
NewBreaker.Mitigated = 0;
NewBreaker.MitigatedBarIndex = 0;
NewBreaker.RectLineNumber = LineNumberCounter++;
NewBreaker.TextLineNumber = LineNumberCounter++;
Breakers.push_back(NewBreaker);
LastBullSwingBar = S2.BarIndex;
if (Input_EnableAlerts.GetYesNo() && !IsFullRecalculation && i == LastClosedBar)
sc.SetAlert(1, "ICT: bullish breaker block formed");
}
}
// ---------------- bearish breaker: H1 -> L1 -> H2(higher) -> break down
if (S1.IsHigh && !S2.IsHigh && S3.IsHigh
&& S3.Price > S1.Price
&& S2.BarIndex > LastBearSwingBar)
{
const float TestValue = BreakOnClose ? sc.Close[i] : sc.Low[i];
if (TestValue < S2.Price)
{
// order block = last down-close candle at / before the swing low
int OBIndex = S2.BarIndex;
for (int j = S2.BarIndex; j >= max(0, S2.BarIndex - OBLookback); j--)
{
if (sc.Close[j] < sc.Open[j])
{
OBIndex = j;
break;
}
}
s_Breaker NewBreaker;
NewBreaker.IsBullish = 0;
NewBreaker.OBBarIndex = OBIndex;
NewBreaker.AnchorSwingBar = S2.BarIndex;
NewBreaker.BreakBarIndex = i;
NewBreaker.Top = BodyOnly ? max(sc.Open[OBIndex], sc.Close[OBIndex]) : sc.High[OBIndex];
NewBreaker.Bottom = BodyOnly ? min(sc.Open[OBIndex], sc.Close[OBIndex]) : sc.Low[OBIndex];
NewBreaker.Mitigated = 0;
NewBreaker.MitigatedBarIndex = 0;
NewBreaker.RectLineNumber = LineNumberCounter++;
NewBreaker.TextLineNumber = LineNumberCounter++;
Breakers.push_back(NewBreaker);
LastBearSwingBar = S2.BarIndex;
if (Input_EnableAlerts.GetYesNo() && !IsFullRecalculation && i == LastClosedBar)
sc.SetAlert(2, "ICT: bearish breaker block formed");
}
}
}
// ---- mitigation ----------------------------------------------------
for (size_t k = 0; k < Breakers.size(); k++)
{
s_Breaker& B = Breakers[k];
if (B.Mitigated || i <= B.BreakBarIndex)
continue;
if (B.IsBullish)
{
const float TestValue = MitigateOnClose ? sc.Close[i] : sc.Low[i];
if (TestValue < B.Bottom)
{
B.Mitigated = 1;
B.MitigatedBarIndex = i;
}
}
else
{
const float TestValue = MitigateOnClose ? sc.Close[i] : sc.High[i];
if (TestValue > B.Top)
{
B.Mitigated = 1;
B.MitigatedBarIndex = i;
}
}
}
LastProcessedBar = i;
}
// ================================================================
// PASS 2 - trim to the configured maximum per direction
// ================================================================
for (int Direction = 0; Direction <= 1; Direction++)
{
int Count = 0;
for (int k = (int)Breakers.size() - 1; k >= 0; k--)
{
if (Breakers[k].IsBullish != Direction)
continue;
Count++;
if (Count > MaxPerSide)
{
sc.DeleteACSChartDrawing(sc.ChartNumber, TOOL_DELETE_CHARTDRAWING, Breakers[k].RectLineNumber);
sc.DeleteACSChartDrawing(sc.ChartNumber, TOOL_DELETE_CHARTDRAWING, Breakers[k].TextLineNumber);
Breakers.erase(Breakers.begin() + k);
}
}
}
// ================================================================
// PASS 3 - draw
// ================================================================
const int RightEdge = sc.ArraySize - 1 + ExtendBars;
for (size_t k = 0; k < Breakers.size(); k++)
{
const s_Breaker& B = Breakers[k];
if (B.Mitigated && HideMitigated)
{
sc.DeleteACSChartDrawing(sc.ChartNumber, TOOL_DELETE_CHARTDRAWING, B.RectLineNumber);
sc.DeleteACSChartDrawing(sc.ChartNumber, TOOL_DELETE_CHARTDRAWING, B.TextLineNumber);
continue;
}
const COLORREF ZoneColor = B.IsBullish ? Input_BullColor.GetColor() : Input_BearColor.GetColor();
const int EndIndex = B.Mitigated ? B.MitigatedBarIndex : RightEdge;
s_UseTool Rect;
Rect.Clear();
Rect.ChartNumber = sc.ChartNumber;
Rect.DrawingType = DRAWING_RECTANGLEHIGHLIGHT;
Rect.LineNumber = B.RectLineNumber;
Rect.BeginIndex = B.OBBarIndex;
Rect.EndIndex = EndIndex;
Rect.BeginValue = B.Bottom;
Rect.EndValue = B.Top;
Rect.Color = ZoneColor;
Rect.SecondaryColor = ZoneColor;
Rect.LineWidth = 1;
Rect.LineStyle = B.Mitigated ? LINESTYLE_DASH : LINESTYLE_SOLID;
Rect.TransparencyLevel = B.Mitigated ? min(95, Input_Transparency.GetInt() + 10)
: Input_Transparency.GetInt();
Rect.AddMethod = UTAM_ADD_OR_ADJUST;
sc.UseTool(Rect);
if (ShowLabels)
{
s_UseTool Label;
Label.Clear();
Label.ChartNumber = sc.ChartNumber;
Label.DrawingType = DRAWING_TEXT;
Label.LineNumber = B.TextLineNumber;
Label.BeginIndex = B.OBBarIndex;
Label.BeginValue = B.IsBullish ? B.Bottom : B.Top;
Label.Color = ZoneColor;
Label.FontSize = 8;
Label.FontBold = 0;
Label.Text = B.IsBullish ? "Bullish Breaker" : "Bearish Breaker";
Label.TextAlignment = B.IsBullish ? (DT_LEFT | DT_TOP) : (DT_LEFT | DT_BOTTOM);
Label.AddMethod = UTAM_ADD_OR_ADJUST;
sc.UseTool(Label);
}
}
// ================================================================
// PASS 4 - subgraph values (nearest active breaker per bar)
// ================================================================
if (!Input_ExportLevels.GetYesNo())
return;
// Values written on earlier calls go stale as breakers are mitigated or
// trimmed, so the whole affected span is rebuilt rather than just the tail.
int SubgraphStart = sc.UpdateStartIndex;
for (size_t k = 0; k < Breakers.size(); k++)
SubgraphStart = min(SubgraphStart, Breakers[k].BreakBarIndex);
SubgraphStart = max(0, SubgraphStart);
for (int i = SubgraphStart; i < sc.ArraySize; i++)
{
Subgraph_BullTop[i] = 0.0f;
Subgraph_BullBottom[i] = 0.0f;
Subgraph_BearTop[i] = 0.0f;
Subgraph_BearBottom[i] = 0.0f;
for (int k = (int)Breakers.size() - 1; k >= 0; k--)
{
const s_Breaker& B = Breakers[k];
if (B.BreakBarIndex > i)
continue;
if (B.Mitigated && B.MitigatedBarIndex <= i)
continue;
if (B.IsBullish && Subgraph_BullTop[i] == 0.0f)
{
Subgraph_BullTop[i] = B.Top;
Subgraph_BullBottom[i] = B.Bottom;
}
else if (!B.IsBullish && Subgraph_BearTop[i] == 0.0f)
{
Subgraph_BearTop[i] = B.Top;
Subgraph_BearBottom[i] = B.Bottom;
}
}
}
}