//@version=5
indicator("Chart Patterns Detector", overlay=true)
// Function to detect Head and Shoulders
isHeadAndShoulders() =>
    leftShoulder = high[5] < high[3] and high[3] > high[1] and high[3] > high[4]
    head = high[3] > high[2] and high[3] > high[0]
    rightShoulder = high[1] < high[3] and high[1] > high[2] and high[1] > high[4]
    leftShoulder and head and rightShoulder
// Function to detect Double Top
isDoubleTop() =>
    top1 = high[3] > high[1] and high[3] > high[5]
    top2 = high[1] > high[0] and high[1] > high[2]
    top1 and top2
// Function to detect Double Bottom
isDoubleBottom() =>
    bottom1 = low[3] < low[1] and low[3] < low[5]
    bottom2 = low[1] < low[0] and low[1] < low[2]
    bottom1 and bottom2
// Function to detect Ascending Triangle
isAscendingTriangle() =>
    low[3] < low[5] and low[1] < low[3] and high[1] == high[3]
// Function to detect Descending Triangle
isDescendingTriangle() =>
    high[3] > high[5] and high[1] > high[3] and low[1] == low[3]
// Plotting patterns
if isHeadAndShoulders()
    label.new(bar_index, high, "Head & Shoulders", color=color.red,
style=label.style_label_down)
if isDoubleTop()
    label.new(bar_index, high, "Double Top", color=color.red,
style=label.style_label_down)
if isDoubleBottom()
    label.new(bar_index, low, "Double Bottom", color=color.green,
style=label.style_label_up)
if isAscendingTriangle()
    label.new(bar_index, high, "Ascending Triangle", color=color.blue,
style=label.style_label_down)
if isDescendingTriangle()
    label.new(bar_index, low, "Descending Triangle", color=color.blue,
style=label.style_label_up)