OPEN-SOURCE SCRIPT

Intraday Equal Peaks Levels (threshold immediate)

61
//version=6
indicator("Intraday Equal Peaks Levels (threshold immediate)", overlay=true, max_lines_count=500, max_labels_count=500)

// ===== INPUTS =====
leftBars = input.int(5, "Bars Left", minval=1)
rightBars = input.int(5, "Bars Right", minval=1)

marketType = input.string("Crypto", "Market Type", options=["Crypto", "Stocks"])
cryptoThresh = input.float(1.0, "Crypto Threshold %", minval=0.0, maxval=3.0, step=0.1)
stockThreshC = input.int(1, "Stocks Threshold (cents)", minval=0) // 1 -> 0.01 price units

showPanel = input.bool(true, "Show Equal Peaks Panel (top-right)")

// ===== DAY CHANGE =====
isNewDay = time("D") != time("D")[1] // true on first bar of new calendar day

// ===== PIVOT DETECTION =====
pHigh = ta.pivothigh(high, leftBars, rightBars)
pLow = ta.pivotlow(low, leftBars, rightBars)

// ===== THRESHOLD CHECK =====
f_within_threshold(oldLevel, newLevel) =>
if marketType == "Stocks"
threshPrice = stockThreshC * 0.01
math.abs(newLevel - oldLevel) <= threshPrice
else
pct = oldLevel != 0 ? math.abs((newLevel - oldLevel) / oldLevel * 100.0) : 0.0
pct <= cryptoThresh

f_exceed_threshold(oldLevel, price) =>
if marketType == "Stocks"
threshPrice = stockThreshC * 0.01
math.abs(price - oldLevel) > threshPrice
else
pct = oldLevel != 0 ? math.abs((price - oldLevel) / oldLevel * 100.0) : 0.0
pct > cryptoThresh

// ===== STORAGE =====
var array<line> arrLines = array.new_line()
var array<float> arrPrices = array.new_float()
var array<color> arrColors = array.new_color()
var array<int> arrTypes = array.new_int() // 1 = resistance, -1 = support

// ===== HELPER: CLEAR ALL =====
f_clear_all() =>
if array.size(arrLines) > 0
for i = array.size(arrLines) - 1 to 0
ln = array.get(arrLines, i)
if not na(ln)
line.delete(ln)
array.clear(arrLines)
array.clear(arrPrices)
array.clear(arrColors)
array.clear(arrTypes)

// ===== RESET ON NEW DAY =====
if isNewDay
f_clear_all()

// ===== ADD PEAK (grouping logic) =====
f_add_peak(level, isRes) =>
grouped = false
targetType = isRes ? 1 : -1
if array.size(arrPrices) > 0
for i = 0 to array.size(arrPrices) - 1
if array.get(arrTypes, i) == targetType
oldLvl = array.get(arrPrices, i)
if f_within_threshold(oldLvl, level)
grouped := true
if array.get(arrColors, i) != color.yellow
array.set(arrColors, i, color.yellow)
lnobj = array.get(arrLines, i)
line.set_color(lnobj, color.yellow)
break
if not grouped
startBar = bar_index - rightBars
lncol = isRes ? color.red : color.green
ln = line.new(x1 = startBar, y1 = level, x2 = bar_index, y2 = level, xloc = xloc.bar_index, extend = extend.right, color = lncol, width = 2)
array.push(arrLines, ln)
array.push(arrPrices, level)
array.push(arrColors, lncol)
array.push(arrTypes, targetType)

// ===== DELETE LINES WHEN THRESHOLD EXCEEDED =====
if array.size(arrPrices) > 0
for i = array.size(arrPrices) - 1 to 0
lvl = array.get(arrPrices, i)
ln = array.get(arrLines, i)
if f_exceed_threshold(lvl, high) or f_exceed_threshold(lvl, low)
line.delete(ln)
array.remove(arrLines, i)
array.remove(arrPrices, i)
array.remove(arrColors, i)
array.remove(arrTypes, i)

// ===== HANDLE NEW PIVOTS =====
if not na(pHigh)
f_add_peak(pHigh, true)
if not na(pLow)
f_add_peak(pLow, false)

// ===== INFO PANEL (top-right) =====
var table t = table.new(position.top_right, 1, 2, border_width = 1)
if showPanel and barstate.islast
yellow_count = 0
yellow_prices = array.new_string()
if array.size(arrPrices) > 0
for i = 0 to array.size(arrPrices) - 1
if array.get(arrColors, i) == color.yellow
yellow_count += 1
lvl = array.get(arrPrices, i)
s = str.tostring(lvl, format.mintick)
array.push(yellow_prices, s)
priceTxt = "None"
if array.size(yellow_prices) > 0
joined = ""
for j = 0 to array.size(yellow_prices) - 1
part = array.get(yellow_prices, j)
joined := j == 0 ? part : joined + ", " + part
priceTxt := joined
table.cell(t, 0, 0, "Equal peaks: " + str.tostring(yellow_count), text_halign = text.align_left, text_size = size.small, bgcolor = color.new(color.black, 0), text_color = color.white)
table.cell(t, 0, 1, "Equal peaks price: " + priceTxt, text_halign = text.align_left, text_size = size.small, bgcolor = color.new(color.black, 0), text_color = color.white)
else
table.clear(t, 0, 0)

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.