OPEN-SOURCE SCRIPT

Today's Unbroken Support/Resistance grok 1

24
//version=6
indicator("Today's Unbroken Support/Resistance", shorttitle="Today S/R", overlay=true, max_lines_count=500)

// Input parameters
lookback = input.int(5, title="Lookback Period", minval=1, maxval=50)
show_resistance = input.bool(true, title="Show Resistance Lines")
show_support = input.bool(true, title="Show Support Lines")
resistance_color = input.color(color.red, title="Resistance Color")
support_color = input.color(color.green, title="Support Color")
line_width = input.int(2, title="Line Width", minval=1, maxval=4)

// Get current date for today's filter
current_date = dayofmonth(time)
current_month = month(time)

// Variables to track lines and their status
var line[] resistance_lines = array.new<line>()
var line[] support_lines = array.new<line>()
var float[] resistance_levels = array.new<float>()
var float[] support_levels = array.new<float>()
var int[] resistance_dates = array.new<int>()
var int[] support_dates = array.new<int>()

// Function to check if current bar is a peak
is_peak() =>
ta.pivothigh(high, lookback, lookback)

// Function to check if current bar is a valley
is_valley() =>
ta.pivotlow(low, lookback, lookback)

// Function to check if a resistance level is broken
is_resistance_broken(level) =>
close > level

// Function to check if a support level is broken
is_support_broken(level) =>
close < level

// Function to remove broken lines
remove_broken_levels() =>
// Check resistance levels
if array.size(resistance_levels) > 0
for i = array.size(resistance_levels) - 1 to 0
level = array.get(resistance_levels, i)
if is_resistance_broken(level)
// Remove broken resistance
line_to_delete = array.get(resistance_lines, i)
line.delete(line_to_delete)
array.remove(resistance_lines, i)
array.remove(resistance_levels, i)
array.remove(resistance_dates, i)

// Check support levels
if array.size(support_levels) > 0
for i = array.size(support_levels) - 1 to 0
level = array.get(support_levels, i)
if is_support_broken(level)
// Remove broken support
line_to_delete = array.get(support_lines, i)
line.delete(line_to_delete)
array.remove(support_lines, i)
array.remove(support_levels, i)
array.remove(support_dates, i)

// Function to remove previous days' lines
remove_old_levels() =>
// Remove old resistance levels
if array.size(resistance_levels) > 0
for i = array.size(resistance_levels) - 1 to 0
line_date = array.get(resistance_dates, i)
if line_date != current_date
// Remove old resistance
line_to_delete = array.get(resistance_lines, i)
line.delete(line_to_delete)
array.remove(resistance_lines, i)
array.remove(resistance_levels, i)
array.remove(resistance_dates, i)

// Remove old support levels
if array.size(support_levels) > 0
for i = array.size(support_levels) - 1 to 0
line_date = array.get(support_dates, i)
if line_date != current_date
// Remove old support
line_to_delete = array.get(support_lines, i)
line.delete(line_to_delete)
array.remove(support_lines, i)
array.remove(support_levels, i)
array.remove(support_dates, i)

// Main logic
remove_old_levels()
remove_broken_levels()

// Check for new resistance peaks
if show_resistance
peak_price = is_peak()
if not na(peak_price)
// Check if this level already exists (avoid duplicates)
level_exists = false
if array.size(resistance_levels) > 0
for i = 0 to array.size(resistance_levels) - 1
existing_level = array.get(resistance_levels, i)
if math.abs(peak_price - existing_level) < (peak_price * 0.001) // Within 0.1%
level_exists := true
break

if not level_exists
// Create new resistance line extending to infinity
new_line = line.new(x1=bar_index - lookback, y1=peak_price, x2=bar_index - lookback, y2=peak_price, color=resistance_color, width=line_width, extend=extend.right)
array.push(resistance_lines, new_line)
array.push(resistance_levels, peak_price)
array.push(resistance_dates, current_date)

// Check for new support valleys
if show_support
valley_price = is_valley()
if not na(valley_price)
// Check if this level already exists (avoid duplicates)
level_exists = false
if array.size(support_levels) > 0
for i = 0 to array.size(support_levels) - 1
existing_level = array.get(support_levels, i)
if math.abs(valley_price - existing_level) < (valley_price * 0.001) // Within 0.1%
level_exists := true
break

if not level_exists
// Create new support line extending to infinity
new_line = line.new(x1=bar_index - lookback, y1=valley_price, x2=bar_index - lookback, y2=valley_price, color=support_color, width=line_width, extend=extend.right)
array.push(support_lines, new_line)
array.push(support_levels, valley_price)
array.push(support_dates, current_date)

// Clean up old lines if too many (keep last 50 of each type)
if array.size(resistance_lines) > 50
old_line = array.shift(resistance_lines)
line.delete(old_line)
array.shift(resistance_levels)
array.shift(resistance_dates)

if array.size(support_lines) > 50
old_line = array.shift(support_lines)
line.delete(old_line)
array.shift(support_levels)
array.shift(support_dates)

// Optional: Plot small markers for today's new levels
today_resistance = show_resistance and not na(is_peak()) and dayofmonth(time[lookback]) == current_date
today_support = show_support and not na(is_valley()) and dayofmonth(time[lookback]) == current_date

plotshape(today_resistance, title="Today's Resistance", location=location.abovebar, color=resistance_color, style=shape.triangledown, size=size.tiny)
plotshape(today_support, title="Today's Support", location=location.belowbar, color=support_color, style=shape.triangleup, size=size.tiny)

Aviso legal

As informações e publicações não devem ser e não constituem conselhos ou recomendações financeiras, de investimento, de negociação ou de qualquer outro tipo, fornecidas ou endossadas pela TradingView. Leia mais em Termos de uso.