-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchart_patterns.pinescript
More file actions
369 lines (317 loc) · 20.6 KB
/
chart_patterns.pinescript
File metadata and controls
369 lines (317 loc) · 20.6 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
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ActiveQuants
// Created by ActiveQuants
// Last updated: May 8, 2025
//@version=6
indicator(title = "Chart Patterns [ActiveQuants]", overlay = true, max_bars_back = 500, max_lines_count = 500, max_labels_count = 500)
//#region ------ USER INPUTS
// Visibility and Common Styling
int showLast = input.int(defval = 3000, title = "Show Last History (Bars)", minval = 10, step = 10, tooltip = "Number of bars back the indicator can plot.", group = "visibility and common styling", confirm = true)
string patternsVisibility = input.string(defval = "All", title = "Patterns", options = ["All", "Active", "Confirmed", "Invalid"], group = "visibility and common styling")
int patternLineWidth = input.int(defval = 1, title = "Pattern Line Width", minval = 1, maxval = 10, group = "visibility and common styling")
color bearishColor = input.color(defval = color.red, title = "Bearish Color", group = "visibility and common styling")
color bullishColor = input.color(defval = color.green, title = "Bullish Color", group = "visibility and common styling")
// Pivot Points
int period = input.int(defval = 10, title = "Period", minval = 2, group = "pivot points")
bool showPivotHighs = input.bool(defval = true, title = "Show Pivot Highs", group = "pivot points")
bool showPivotLows = input.bool(defval = true, title = "Show Pivot Lows", group = "pivot points")
color pivotHighColor = input.color(defval = #ff5252, title = "Pivot Highs Color", inline = "pivots color", group = "pivot points")
color pivotLowColor = input.color(defval = #089981, title = "Pivot Lows Color", inline = "pivots color", group = "pivot points")
// Patterns
bool showLowerLow = input.bool(defval = true, title = "Lower Low", group = "patterns")
bool showLowerLowLowerHigh = input.bool(defval = true, title = "Lower Low & Lower High", group = "patterns")
bool showHigherHigh = input.bool(defval = true, title = "Higher High", group = "patterns")
bool showHigherHighHigherLow = input.bool(defval = true, title = "Higher High & Higher Low", group = "patterns")
bool showDoubleTops = input.bool(defval = true, title = "Double Tops", group = "patterns")
bool showDoubleBottoms = input.bool(defval = true, title = "Double Bottoms", group = "patterns")
//#endregion
//#region ------ USER-DEFINED TYPES
type PatternInfo
string patternType
string status
int breakBarIdx
int p1Idx
int p2Idx
int p3Idx
int p4Idx
int p5Idx
int p6Idx
float p1Price
float p2Price
float p3Price
float p4Price
float p5Price
float p6Price
float supportSlope
float resistanceSlope
array<line> connectingLines
int firstConnectingLineBreakIdx
int lastConnectingLineBreakIdx
line supportLine
line resistanceLine
int supportLineBreakIdx
int resistanceLineBreakIdx
linefill patternFill
label mainLabel
label label1
label label2
label label3
//#endregion
//#region ----- FUNCTIONS
// @function Gets pivot points and updates global price/index arrays.
// @param period (int) The number of bars back to check.
// @returns "ph", "phx", "pl", "plx", "newPivotDetected"
getPivotPoints(int period, array<float> pivotsPrices, array<int> pivotsBarIndices) =>
var int lastPivot = 1
var int phx = na
var int plx = na
float upper = ta.highest(period)
float lower = ta.lowest(period)
lastPivot := high[period] > upper ? 1 : low[period] < lower ? -1 : lastPivot[1]
float ph = lastPivot == 1 and lastPivot[1] != 1 ? high[period] : na
phx := lastPivot == 1 and lastPivot[1] != 1 ? bar_index[period] : phx
float pl = lastPivot == -1 and lastPivot[1] != -1 ? low[period] : na
plx := lastPivot == -1 and lastPivot[1] != -1 ? bar_index[period] : plx
bool newPivotDetected = false
if not na(ph)
array.push(pivotsPrices, ph)
array.push(pivotsBarIndices, phx)
newPivotDetected := true
if not na(pl)
array.push(pivotsPrices, pl)
array.push(pivotsBarIndices, plx)
newPivotDetected := true
[ph, phx, pl, plx, newPivotDetected]
getPivotsSlope(float p1Price, float p2Price, int p1Index, int p2Index) =>
if na(p1Price) or na(p2Price) or p1Index == p2Index
na
else
(p2Price - p1Price) / (p2Index - p1Index)
method deletePattern(PatternInfo pattern) =>
if not na(pattern.connectingLines)
for lineId in pattern.connectingLines
lineId.delete()
pattern.connectingLines.clear()
pattern.supportLine.delete()
pattern.resistanceLine.delete()
pattern.mainLabel.delete()
pattern.label1.delete()
pattern.label2.delete()
pattern.label3.delete()
method updatePattern(PatternInfo pattern) =>
if (pattern.patternType == "Double Top" and close < pattern.p5Price) or (pattern.patternType == "Double Bottom" and close > pattern.p5Price)
pattern.lastConnectingLineBreakIdx := bar_index
pattern.status := "Confirmed"
pattern.mainLabel.set_x(pattern.lastConnectingLineBreakIdx)
if not na(pattern.connectingLines) and pattern.connectingLines.size() >= 2
pattern.connectingLines.get(-2).set_x2(pattern.lastConnectingLineBreakIdx)
pattern.connectingLines.get(-1).set_x2(pattern.lastConnectingLineBreakIdx)
else if pattern.patternType == "Double Top" or pattern.patternType == "Double Bottom"
pattern.mainLabel.set_x(bar_index)
if not na(pattern.connectingLines) and pattern.connectingLines.size() >= 2
pattern.connectingLines.get(-2).set_x2(bar_index)
pattern.connectingLines.get(-1).set_x2(bar_index)
lowerLow(float p1, float p2, float p3, float p4, float p5) =>
p1 < p2 and p1 < p3 and
p2 > p3 and p2 < p4 and
p3 < p4 and
p4 > p5 and
p5 < p3
lowerLowLowerHigh(float p1, float p2, float p3, float p4, float p5, float p6) =>
p1 < p2 and p1 < p3 and
p2 > p3 and p2 < p4 and
p3 < p4 and
p4 > p5 and p4 > p6 and
p5 < p3 and p5 < p6
higherHigh(float p1, float p2, float p3, float p4, float p5) =>
p1 > p2 and p1 > p3 and
p2 < p3 and p2 > p4 and
p3 > p4 and
p4 < p5 and
p5 > p3
higherHighHigherLow(float p1, float p2, float p3, float p4, float p5, float p6) =>
p1 > p2 and p1 > p3 and
p2 < p3 and p2 > p4 and
p3 > p4 and
p4 < p5 and p4 < p6 and
p5 > p3 and p5 > p6
doubleTop(float p2, float p3, float p4, int p1Idx, int p4Idx, float atr) =>
close[bar_index - p1Idx] < p3 and p2 > p3 and p4 > p3 and math.abs(p2 - p4) <= atr[bar_index - p4Idx]/6
doubleBottom(float p2, float p3, float p4, int p1Idx, int p4Idx, float atr) =>
close[bar_index - p1Idx] > p3 and p2 < p3 and p4 < p3 and math.abs(p2 - p4) <= atr[bar_index - p4Idx]/6
//#endregion
//#region ------ CALCULATIONS
var array<float> pivotsPrices = array.new<float>()
var array<int> pivotsBarIndices = array.new<int>()
var array<PatternInfo> activePatterns = array.new<PatternInfo>()
[ph, phx, pl, plx, newPivotDetected] = getPivotPoints(period, pivotsPrices, pivotsBarIndices)
float atr = ta.atr(14)
if array.size(pivotsPrices) >= 6 and newPivotDetected and barstate.isconfirmed
i1 = array.get(pivotsBarIndices, array.size(pivotsBarIndices) - 6)
i2 = array.get(pivotsBarIndices, array.size(pivotsBarIndices) - 5)
i3 = array.get(pivotsBarIndices, array.size(pivotsBarIndices) - 4)
i4 = array.get(pivotsBarIndices, array.size(pivotsBarIndices) - 3)
i5 = array.get(pivotsBarIndices, array.size(pivotsBarIndices) - 2)
i6 = array.get(pivotsBarIndices, array.size(pivotsBarIndices) - 1)
p1 = array.get(pivotsPrices, array.size(pivotsPrices) - 6)
p2 = array.get(pivotsPrices, array.size(pivotsPrices) - 5)
p3 = array.get(pivotsPrices, array.size(pivotsPrices) - 4)
p4 = array.get(pivotsPrices, array.size(pivotsPrices) - 3)
p5 = array.get(pivotsPrices, array.size(pivotsPrices) - 2)
p6 = array.get(pivotsPrices, array.size(pivotsPrices) - 1)
if lowerLow(p2, p3, p4, p5, p6) and showLowerLow
PatternInfo newPattern = PatternInfo.new(
patternType = "LL",
status = "Confirmed",
p1Idx = i1, p1Price = p1,
p2Idx = i2, p2Price = p2,
p3Idx = i3, p3Price = p3,
p4Idx = i4, p4Price = p4,
p5Idx = i5, p5Price = p5,
p6Idx = i6, p6Price = p6,
connectingLines = array.new_line(),
mainLabel = label.new(i6, close, "LL", yloc = yloc.belowbar, color = bearishColor, style = label.style_label_up, size = size.small, tooltip = "Lower Low")
)
newPattern.connectingLines.push(line.new(newPattern.p2Idx, newPattern.p2Price, newPattern.p3Idx, newPattern.p3Price, color = bearishColor, width = patternLineWidth))
newPattern.connectingLines.push(line.new(newPattern.p3Idx, newPattern.p3Price, newPattern.p4Idx, newPattern.p4Price, color = bearishColor, width = patternLineWidth))
newPattern.connectingLines.push(line.new(newPattern.p4Idx, newPattern.p4Price, newPattern.p5Idx, newPattern.p5Price, color = bearishColor, width = patternLineWidth))
newPattern.connectingLines.push(line.new(newPattern.p5Idx, newPattern.p5Price, newPattern.p6Idx, newPattern.p6Price, color = bearishColor, width = patternLineWidth))
activePatterns.push(newPattern)
if lowerLowLowerHigh(p1, p2, p3, p4, p5, p6) and showLowerLowLowerHigh
PatternInfo newPattern = PatternInfo.new(
patternType = "LL & LH",
status = "Confirmed",
p1Idx = i1, p1Price = p1,
p2Idx = i2, p2Price = p2,
p3Idx = i3, p3Price = p3,
p4Idx = i4, p4Price = p4,
p5Idx = i5, p5Price = p5,
p6Idx = i6, p6Price = p6,
connectingLines = array.new_line(),
mainLabel = label.new(i6, close, "LL & LH", yloc = yloc.abovebar, color = bearishColor, style = label.style_label_down, size = size.small, tooltip = "Lower Low & Lower High")
)
newPattern.connectingLines.push(line.new(newPattern.p1Idx, newPattern.p1Price, newPattern.p2Idx, newPattern.p2Price, color = bearishColor, width = patternLineWidth))
newPattern.connectingLines.push(line.new(newPattern.p2Idx, newPattern.p2Price, newPattern.p3Idx, newPattern.p3Price, color = bearishColor, width = patternLineWidth))
newPattern.connectingLines.push(line.new(newPattern.p3Idx, newPattern.p3Price, newPattern.p4Idx, newPattern.p4Price, color = bearishColor, width = patternLineWidth))
newPattern.connectingLines.push(line.new(newPattern.p4Idx, newPattern.p4Price, newPattern.p5Idx, newPattern.p5Price, color = bearishColor, width = patternLineWidth))
newPattern.connectingLines.push(line.new(newPattern.p5Idx, newPattern.p5Price, newPattern.p6Idx, newPattern.p6Price, color = bearishColor, width = patternLineWidth))
activePatterns.push(newPattern)
if higherHigh(p2, p3, p4, p5, p6) and showHigherHigh
PatternInfo newPattern = PatternInfo.new(
patternType = "HH",
status = "Confirmed",
p1Idx = i1, p1Price = p1,
p2Idx = i2, p2Price = p2,
p3Idx = i3, p3Price = p3,
p4Idx = i4, p4Price = p4,
p5Idx = i5, p5Price = p5,
p6Idx = i6, p6Price = p6,
connectingLines = array.new_line(),
mainLabel = label.new(i6, close, "HH", yloc = yloc.abovebar, color = bullishColor, style = label.style_label_down, size = size.small, tooltip = "Higher High")
)
newPattern.connectingLines.push(line.new(newPattern.p2Idx, newPattern.p2Price, newPattern.p3Idx, newPattern.p3Price, color = bullishColor, width = patternLineWidth))
newPattern.connectingLines.push(line.new(newPattern.p3Idx, newPattern.p3Price, newPattern.p4Idx, newPattern.p4Price, color = bullishColor, width = patternLineWidth))
newPattern.connectingLines.push(line.new(newPattern.p4Idx, newPattern.p4Price, newPattern.p5Idx, newPattern.p5Price, color = bullishColor, width = patternLineWidth))
newPattern.connectingLines.push(line.new(newPattern.p5Idx, newPattern.p5Price, newPattern.p6Idx, newPattern.p6Price, color = bullishColor, width = patternLineWidth))
activePatterns.push(newPattern)
if higherHighHigherLow(p1, p2, p3, p4, p5, p6) and showHigherHighHigherLow
PatternInfo newPattern = PatternInfo.new(
patternType = "HH & HL",
status = "Confirmed",
p1Idx = i1, p1Price = p1,
p2Idx = i2, p2Price = p2,
p3Idx = i3, p3Price = p3,
p4Idx = i4, p4Price = p4,
p5Idx = i5, p5Price = p5,
p6Idx = i6, p6Price = p6,
connectingLines = array.new_line(),
mainLabel = label.new(i6, close, "HH & HL", yloc = yloc.belowbar, color = bullishColor, style = label.style_label_up, size = size.small, tooltip = "Higher High & Higher Low")
)
newPattern.connectingLines.push(line.new(newPattern.p1Idx, newPattern.p1Price, newPattern.p2Idx, newPattern.p2Price, color = bullishColor, width = patternLineWidth))
newPattern.connectingLines.push(line.new(newPattern.p2Idx, newPattern.p2Price, newPattern.p3Idx, newPattern.p3Price, color = bullishColor, width = patternLineWidth))
newPattern.connectingLines.push(line.new(newPattern.p3Idx, newPattern.p3Price, newPattern.p4Idx, newPattern.p4Price, color = bullishColor, width = patternLineWidth))
newPattern.connectingLines.push(line.new(newPattern.p4Idx, newPattern.p4Price, newPattern.p5Idx, newPattern.p5Price, color = bullishColor, width = patternLineWidth))
newPattern.connectingLines.push(line.new(newPattern.p5Idx, newPattern.p5Price, newPattern.p6Idx, newPattern.p6Price, color = bullishColor, width = patternLineWidth))
activePatterns.push(newPattern)
if doubleTop(p4, p5, p6, i3, i6, atr) and showDoubleTops
PatternInfo newPattern = PatternInfo.new(
patternType = "Double Top",
status = "Active",
p1Idx = i1, p1Price = p1,
p2Idx = i2, p2Price = p2,
p3Idx = i3, p3Price = p3,
p4Idx = i4, p4Price = p4,
p5Idx = i5, p5Price = p5,
p6Idx = i6, p6Price = p6,
connectingLines = array.new_line(),
label1 = label.new(i4, close, "Top 1", yloc = yloc.abovebar, color = bearishColor, style = label.style_label_down, size = size.small),
label2 = label.new(i6, close, "Top 2", yloc = yloc.abovebar, color = bearishColor, style = label.style_label_down, size = size.small)
)
for i=newPattern.p3Idx to newPattern.p4Idx
if getPivotsSlope(newPattern.p3Price, newPattern.p4Price, newPattern.p3Idx, newPattern.p4Idx) * (i - newPattern.p3Idx) + newPattern.p3Price >= newPattern.p5Price
newPattern.firstConnectingLineBreakIdx := i
break
for i=newPattern.p6Idx to bar_index
int x = bar_index - i
if close[x] < newPattern.p5Price
newPattern.lastConnectingLineBreakIdx := i
newPattern.status := "Confirmed"
break
newPattern.mainLabel := label.new(na(newPattern.lastConnectingLineBreakIdx) ? bar_index : newPattern.lastConnectingLineBreakIdx, close, "Double Top", yloc = yloc.abovebar, color = bearishColor, style = label.style_label_down, size = size.small)
newPattern.connectingLines.push(line.new(newPattern.p3Idx, newPattern.p3Price, newPattern.p4Idx, newPattern.p4Price, color = bearishColor, width = patternLineWidth))
newPattern.connectingLines.push(line.new(newPattern.p4Idx, newPattern.p4Price, newPattern.p5Idx, newPattern.p5Price, color = bearishColor, width = patternLineWidth))
newPattern.connectingLines.push(line.new(newPattern.p5Idx, newPattern.p5Price, newPattern.p6Idx, newPattern.p6Price, color = bearishColor, width = patternLineWidth))
newPattern.connectingLines.push(line.new(newPattern.p6Idx, newPattern.p6Price, na(newPattern.lastConnectingLineBreakIdx) ? bar_index : newPattern.lastConnectingLineBreakIdx, newPattern.p5Price, color = bearishColor, width = patternLineWidth))
newPattern.connectingLines.push(line.new(na(newPattern.firstConnectingLineBreakIdx) ? bar_index : newPattern.firstConnectingLineBreakIdx, newPattern.p5Price, na(newPattern.lastConnectingLineBreakIdx) ? bar_index : newPattern.lastConnectingLineBreakIdx, newPattern.p5Price, color = bearishColor, style = line.style_dotted, width = patternLineWidth + 1))
activePatterns.push(newPattern)
if doubleBottom(p4, p5, p6, i3, i6, atr) and showDoubleBottoms
PatternInfo newPattern = PatternInfo.new(
patternType = "Double Bottom",
status = "Active",
p1Idx = i1, p1Price = p1,
p2Idx = i2, p2Price = p2,
p3Idx = i3, p3Price = p3,
p4Idx = i4, p4Price = p4,
p5Idx = i5, p5Price = p5,
p6Idx = i6, p6Price = p6,
connectingLines = array.new_line(),
label1 = label.new(i4, close, "Bottom 1", yloc = yloc.belowbar, color = bullishColor, style = label.style_label_up, size = size.small),
label2 = label.new(i6, close, "Bottom 2", yloc = yloc.belowbar, color = bullishColor, style = label.style_label_up, size = size.small)
)
for i=newPattern.p3Idx to newPattern.p4Idx
if getPivotsSlope(newPattern.p3Price, newPattern.p4Price, newPattern.p3Idx, newPattern.p4Idx) * (i - newPattern.p3Idx) + newPattern.p3Price <= newPattern.p5Price
newPattern.firstConnectingLineBreakIdx := i
break
for i=newPattern.p6Idx to bar_index
int x = bar_index - i
if close[x] > newPattern.p5Price
newPattern.lastConnectingLineBreakIdx := i
newPattern.status := "Confirmed"
break
newPattern.mainLabel := label.new(na(newPattern.lastConnectingLineBreakIdx) ? bar_index : newPattern.lastConnectingLineBreakIdx, close, "Double Bottom", yloc = yloc.belowbar, color = bullishColor, style = label.style_label_up, size = size.small)
newPattern.connectingLines.push(line.new(newPattern.p3Idx, newPattern.p3Price, newPattern.p4Idx, newPattern.p4Price, color = bullishColor, width = patternLineWidth))
newPattern.connectingLines.push(line.new(newPattern.p4Idx, newPattern.p4Price, newPattern.p5Idx, newPattern.p5Price, color = bullishColor, width = patternLineWidth))
newPattern.connectingLines.push(line.new(newPattern.p5Idx, newPattern.p5Price, newPattern.p6Idx, newPattern.p6Price, color = bullishColor, width = patternLineWidth))
newPattern.connectingLines.push(line.new(newPattern.p6Idx, newPattern.p6Price, na(newPattern.lastConnectingLineBreakIdx) ? bar_index : newPattern.lastConnectingLineBreakIdx, newPattern.p5Price, color = bullishColor, width = patternLineWidth))
newPattern.connectingLines.push(line.new(na(newPattern.firstConnectingLineBreakIdx) ? bar_index : newPattern.firstConnectingLineBreakIdx, newPattern.p5Price, na(newPattern.lastConnectingLineBreakIdx) ? bar_index : newPattern.lastConnectingLineBreakIdx, newPattern.p5Price, color = bullishColor, style = line.style_dotted, width = patternLineWidth + 1))
activePatterns.push(newPattern)
//#endregion
//#region ------ PLOTTING
plot(showPivotHighs ? ph : na, "Swing High", color.new(pivotHighColor, 50), 5, plot.style_circles, offset = -period, show_last = showLast)
plot(showPivotLows ? pl : na, "Swing Low", color.new(pivotLowColor, 50), 5, plot.style_circles, offset = -period, show_last = showLast)
for pattern in activePatterns
if pattern.status == "Active"
pattern.updatePattern()
if newPivotDetected and pattern.p6Idx < bar_index - period and pattern.status == "Active"
if (low[period] >= pattern.p5Price and pattern.patternType == "Double Top") or (low[period] <= pattern.p5Price and pattern.patternType == "Double Bottom")
pattern.status := "Invalid"
pattern.mainLabel.set_x(bar_index - period), pattern.mainLabel.set_text("Invalid " + pattern.mainLabel.get_text()), pattern.mainLabel.set_tooltip("This pivot was created before the " + pattern.patternType + " neckline breakout!")
if pattern.connectingLines.size() >= 2
pattern.connectingLines.get(-2).set_x2(bar_index - period)
pattern.connectingLines.get(-1).set_x2(bar_index - period)
if pattern.p1Idx < bar_index - showLast
pattern.deletePattern()
if barstate.islastconfirmedhistory and
((patternsVisibility == "Active" and pattern.status != "Active") or (patternsVisibility == "Confirmed" and pattern.status != "Confirmed") or (patternsVisibility == "Invalid" and pattern.status != "Invalid"))
pattern.deletePattern()
//#endregion