Indicator · Pine Script v4 · On the chart

Cup Finder

by theonetrade· code credited to LonesomeTheBlue

Cup Finder is an overlay indicator that automatically scans price history to identify bullish and bearish cup patterns.

View on GitHub

GitHub stars34on the repository
Pine versionv4older, still runs
Lines of code112
Settings10inputs
LicenceMIT
Last update2 weeks ago8 Nov 2026

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

112 lines of Pine Script v4 from assets/indicator/bit-exact/cup_finder_bv2ARQND.pine, published under MIT. Keep the author's credit and licence lines when you reuse it.

cup_finder_bv2ARQND.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © LonesomeTheBlue

//@version=4
study("Cup Finder", "CF", overlay = true, max_lines_count = 500)
loopback = input(defval = 150, title = "Number of Bars to search", minval = 30, maxval = 250)
widthr = input(defval = 5., title = "Channel Width of the Cup", minval = 1)
check_hc = input(defval = "Close", title = "Source for Breakout", options = ["Close", "High/Low"])
include_rate = input(75., title = "Contained Bar Rate %", minval = 10, maxval = 100) / 100
show_channel = input(false, title = "Show Channels of Cups")
no_label = input(false, title = "Don't Show Labels")
remove_old_lines = input(false, title = "Remove Old Lines")
remove_old_labels = input(false, title = "Remove Old Labels")
bullcolor = input(defval = color.lime, title = "Colors", inline = "colors")
bearcolor = input(defval = color.red, title = "", inline = "colors")
 
// used for calculation&drawing of circles
float width = (highest(292) - lowest(292)) / 100
float uwidth = width * widthr
bool highestbar = highestbars(30) == 0
bool lowestbar = lowestbars(30) == 0
float bullsrc = check_hc == "Close" ? close : high
float bearsrc = check_hc == "Close" ? close : low
 
float hh = 0.
float lasthh = 0.
int ind = 0
bool bull = false
for x = 1 to loopback
    lasthh := max(lasthh, high[x])
    if bullsrc <= lasthh
        break
    
    if x >= 20 and bullsrc > high[x] and close[1] < high[x] and high[x] >= lasthh and highestbar[x]
        hh := high[x]
        ind := x
        bull := true

float ll = 0.
lastll = highest(292)
if ind == 0
    for x = 1 to loopback
        lastll := min(lastll, low[x])
        if bearsrc >= lastll
            break
        
        if x >= 20 and bearsrc < low[x] and close[1] > low[x] and low[x] <= lastll and lowestbar[x]
            ll := low[x]
            ind := x

Round(val)=> round(val / syminfo.mintick) * syminfo.mintick

var cup_lines = array.new_line(0)
var label cup_label = na
levs = array.new_float(0)
bullish_cup = false
bearish_cup = false
int included = na
if ind != 0
    float pow_radius = pow(ind / 2, 2)
    end = round((ind - (ind % 2)) / 2)
    if ind % 2 == 0
        array.push(levs, bull ? hh - sqrt(pow_radius) * width : ll + sqrt(pow_radius) * width)

    for x = 1 to end 
        ycoord = sqrt(pow_radius - pow(x, 2)) * width
        array.push(levs, bull ? hh - ycoord : ll + ycoord)
        array.unshift(levs, bull ? hh - ycoord : ll + ycoord)
    if ind % 2 == 1
        array.push(levs, bull ? hh : ll)
        array.unshift(levs, bull ? hh : ll)
        
    in_cnt = 0
    for x = 0 to array.size(levs) - 1
        if (min(array.get(levs, x) + uwidth, high[x]) - max(array.get(levs, x) - uwidth, low[x]) > 0)
            in_cnt := in_cnt + 1
    
    if in_cnt / (ind + 1) >= include_rate
        if not no_label
            if remove_old_labels
                label.delete(cup_label)
            cup_label := label.new(bar_index, array.get(levs, 0), 
                      text = "Cup\nRadius = " + tostring(Round(width * ind / 2)) + "\nIncluded = " + tostring(100 * in_cnt / (ind + 1), '#.#') +"%",
                      color = bull ? bullcolor : bearcolor, 
                      textcolor = bull ? color.black : color.white,
                      textalign = text.align_left, 
                      style = bull ? label.style_label_lower_right : label.style_label_upper_right)
                
        if remove_old_lines and array.size(cup_lines) > 0
            for x = 0 to array.size(cup_lines) - 1
                line.delete(array.get(cup_lines, x))
        
        array.push(cup_lines, line.new(x1 = bar_index, y1 = array.get(levs, 0), x2 = bar_index - ind + 1, y2 = array.get(levs, 0), color = bull ? bullcolor : bearcolor, style = line.style_dashed))
        xloc = ind
        for x = 0 to array.size(levs) - 2
            array.push(cup_lines, line.new(x1 = bar_index - xloc, y1 = array.get(levs, x), x2 = bar_index - xloc + 1, y2 = array.get(levs, x + 1), color = bull ? bullcolor : bearcolor, width = 3))
            if show_channel and x % 2 == 0 and x > 0 and x < array.size(levs) - 2
                array.push(cup_lines, line.new(x1 = bar_index - xloc, y1 = array.get(levs, x) + uwidth, x2 = bar_index - xloc + 1, y2 = array.get(levs, x + 1) + uwidth, color = bull ? bullcolor : bearcolor))
                array.push(cup_lines, line.new(x1 = bar_index - xloc, y1 = array.get(levs, x) - uwidth, x2 = bar_index - xloc + 1, y2 = array.get(levs, x + 1) - uwidth, color = bull ? bullcolor : bearcolor))
            
            xloc := xloc - 1
        
        included := round(100 * in_cnt / (ind + 1))
        if bull 
            bullish_cup := true
        else
            bearish_cup := true

plot(included, title = "included", display = display.none)
alertcondition(bullish_cup, title = "Bullish Cup Found", message = 'Bullish Cup Found, Included:{{plot("included")}}')
alertcondition(bearish_cup, title = "Bearish Cup Found", message = 'Bearish Cup Found, Included:{{plot("included")}}')
alertcondition(bullish_cup or bearish_cup, title = "Cup Found", message = 'Cup Found, Included:{{plot("included")}}')

Overview

Cup Finder calculates circular boundary levels across a user-defined loopback range and checks whether a specified percentage of historical bars fall within the computed channel width. When a valid cup structure is detected, the indicator displays informative text labels on the price chart showing the pattern's radius and inclusion rate, with optional channel lines. Main inputs allow adjusting the search loopback length, channel width tolerance, breakout source, and contained bar percentage threshold.

Details

Published by

theonetrade on GitHub

Code credit

LonesomeTheBlue, as written in the script's header

Type

Indicator, Pine Script v4

Draws

On the price chart

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Topics

algorithmic-tradingbacktestingbitcoinccxtcrypto-tradingllm-agentoffline-backtestingopen-source-tradingpine-scriptpine-to-pythonpinescriptpynecorepythonquantquantitative-financestrategy-backtestingtechnical-analysistrading-indicatorstrading-strategiestradingview

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
Number of Bars to searchint150loopback
Channel Width of the Cupinput5.widthr
Source for BreakoutstringClosecheck_hc
Contained Bar Rate %input75.include_rate
Show Channels of Cupsboolfalseshow_channel
Don't Show Labelsboolfalseno_label
Remove Old Linesboolfalseremove_old_lines
Remove Old Labelsboolfalseremove_old_labels
Colorscolorcolor.limebullcolor
bearcolorcolorcolor.redbearcolor
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

Sideway Zones Live

by dearvn
257 stars

Sideway Zones Live is an overlay indicator that estimates inner and outer sideways bands from recent highs and lows with a rational quadratic kernel. It tracks swing pivots over a loopback period and counts bars that remain between the outer high and low. Afte

Indicator v5 TrendSupport & resistance
Updated 2 months ago · None stated GitHub repository of Sideway Zones Live
228 stars

This indicator plots dynamic support and resistance levels on the price chart. It detects pivot highs and lows using the pivot length and tracks touches to build strength-based levels. Support and resistance zones appear as colored boxes while lines mark key l

Indicator v6 Support & resistanceBreakout
Updated 4 months ago · None stated GitHub repository of Dynamic Support & Resistance
220 stars

This indicator identifies High Of Low Price (HOLP) and Low Of High Price (LOHP) candlestick setups based on price extremes over a specified lookback period. A HOLP signal triggers when the current bar closes above the high of a recent session low, whereas a LO

Indicator v5 Candlestick patternsBreakout
Updated 1 year ago · None stated GitHub repository of Candlestick Patterns - HOLP-LOHP

Donchian Channels (DC)

by mihakralj
99 stars

This indicator calculates and plots Donchian Channels directly on the price chart. It determines the highest high and lowest low over a user-selected lookback period using an optimized monotonic deque algorithm, as well as a middle basis line computed as their

Indicator v6 Volatility & bandsSupport & resistance
Updated 8 weeks ago · Apache-2.0 GitHub repository of Donchian Channels (DC)

Lows from New Highs

by be-thomas
65 stars

This indicator tracks bar lows during consecutive rising highs using Pine Script arrays. When at least four lows accumulate or the price breaches the minimum low in the stack, the script sorts the recorded lows in ascending order and flushes them. It plots up

Indicator v4 Support & resistanceBreakout
Updated 4 weeks ago · GPL-3.0 GitHub repository of Lows from New Highs

This indicator tracks smart money market structure directly on the price chart. It calculates dual-period swing highs and swing lows to detect Changes of Character (CHoCH), Breaks of Structure (BOS), inducements, and liquidity sweeps. When structural levels ar

Indicator v5 SMC / ICT / price actionSupport & resistance
Updated 1 year ago · Apache-2.0 GitHub repository of Market Structure with Inducements & Sweeps [LuxAlgo]

More Support & resistance scripts

Related on ForexR

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers