chart_patterns.pinescript
Copy
// 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
Show all 369 lines