OPEN-SOURCE SCRIPT

Punk_indicator

//version=5
indicator("Support and Resistance Levels Table with Trend", overlay=true)

// Function to find significant support and resistance levels based on swings
getSwingSupport(src, length) =>
var float swingSupport = na
if bar_index > length
swingSupport := ta.lowest(src, length) // Calculate the lowest swing price over the lookback period
swingSupport

getSwingResistance(src, length) =>
var float swingResistance = na
if bar_index > length
swingResistance := ta.highest(src, length) // Calculate the highest swing price over the lookback period
swingResistance

// Input options for lookback periods
lookbackHours = input(24, title="Hourly Lookback Period")
lookbackDays = input(30, title="Daily Lookback Period")
lookbackWeeks = input(52, title="Weekly Lookback Period")
lookbackMonths = input(12, title="Monthly Lookback Period")

// Fetch swing support and resistance levels for different timeframes
hourlySwingSupport = request.security(syminfo.tickerid, "60", getSwingSupport(low, lookbackHours))
dailySwingSupport = request.security(syminfo.tickerid, "D", getSwingSupport(low, lookbackDays))
weeklySwingSupport = request.security(syminfo.tickerid, "W", getSwingSupport(low, lookbackWeeks))
monthlySwingSupport = request.security(syminfo.tickerid, "M", getSwingSupport(low, lookbackMonths))

hourlySwingResistance = request.security(syminfo.tickerid, "60", getSwingResistance(high, lookbackHours))
dailySwingResistance = request.security(syminfo.tickerid, "D", getSwingResistance(high, lookbackDays))
weeklySwingResistance = request.security(syminfo.tickerid, "W", getSwingResistance(high, lookbackWeeks))
monthlySwingResistance = request.security(syminfo.tickerid, "M", getSwingResistance(high, lookbackMonths))

// Trend identification using SMA
smaLength = input(50, title="SMA Length")
hourlyTrend = request.security(syminfo.tickerid, "60", close > ta.sma(close, smaLength) ? "Up" : "Down")
dailyTrend = request.security(syminfo.tickerid, "D", close > ta.sma(close, smaLength) ? "Up" : "Down")
weeklyTrend = request.security(syminfo.tickerid, "W", close > ta.sma(close, smaLength) ? "Up" : "Down")
monthlyTrend = request.security(syminfo.tickerid, "M", close > ta.sma(close, smaLength) ? "Up" : "Down")

// Create a table to show the support and resistance levels along with the trend
var table levelsTable = table.new(position.top_right, 4, 5, bgcolor=color.new(color.white, 0)) // Set to 4 columns...

// Populate the header of the table
if bar_index == 0
table.cell(levelsTable, 0, 0, "Timeframe", bgcolor=color.new(color.gray, 0))
table.cell(levelsTable, 1, 0, "Support Level", bgcolor=color.new(color.gray, 0))
table.cell(levelsTable, 2, 0, "Resistance Level", bgcolor=color.new(color.gray, 0))
table.cell(levelsTable, 3, 0, "Trend", bgcolor=color.new(color.gray, 0)) // Adding Trend column here

// Add rows for each level with colors
table.cell(levelsTable, 0, 1, "Hourly", bgcolor=color.new(color.green, 90))
table.cell(levelsTable, 0, 2, "Daily", bgcolor=color.new(color.blue, 90))
table.cell(levelsTable, 0, 3, "Weekly", bgcolor=color.new(color.orange, 90))
table.cell(levelsTable, 0, 4, "Monthly", bgcolor=color.new(color.red, 90))

// Update table with the current levels and trends
if not na(hourlySwingSupport)
table.cell(levelsTable, 1, 1, str.tostring(hourlySwingSupport), bgcolor=color.new(color.green, 90))
table.cell(levelsTable, 3, 1, hourlyTrend, bgcolor=color.new(color.green, 90)) // Trend for Hourly
line.new(bar_index[1], hourlySwingSupport, bar_index, hourlySwingSupport, color=color.green, width=1)

if not na(hourlySwingResistance)
table.cell(levelsTable, 2, 1, str.tostring(hourlySwingResistance), bgcolor=color.new(color.green, 90))
line.new(bar_index[1], hourlySwingResistance, bar_index, hourlySwingResistance, color=color.green, width=1)

if not na(dailySwingSupport)
table.cell(levelsTable, 1, 2, str.tostring(dailySwingSupport), bgcolor=color.new(color.blue, 90))
table.cell(levelsTable, 3, 2, dailyTrend, bgcolor=color.new(color.blue, 90)) // Trend for Daily
line.new(bar_index[1], dailySwingSupport, bar_index, dailySwingSupport, color=color.blue, width=1)

if not na(dailySwingResistance)
table.cell(levelsTable, 2, 2, str.tostring(dailySwingResistance), bgcolor=color.new(color.blue, 90))
line.new(bar_index[1], dailySwingResistance, bar_index, dailySwingResistance, color=color.blue, width=1)

if not na(weeklySwingSupport)
table.cell(levelsTable, 1, 3, str.tostring(weeklySwingSupport), bgcolor=color.new(color.orange, 90))
table.cell(levelsTable, 3, 3, weeklyTrend, bgcolor=color.new(color.orange, 90)) // Trend for Weekly
line.new(bar_index[1], weeklySwingSupport, bar_index, weeklySwingSupport, color=color.orange, width=1)

if not na(weeklySwingResistance)
table.cell(levelsTable, 2, 3, str.tostring(weeklySwingResistance), bgcolor=color.new(color.orange, 90))
line.new(bar_index[1], weeklySwingResistance, bar_index, weeklySwingResistance, color=color.orange, width=1)

if not na(monthlySwingSupport)
table.cell(levelsTable, 1, 4, str.tostring(monthlySwingSupport), bgcolor=color.new(color.red, 90))
table.cell(levelsTable, 3, 4, monthlyTrend, bgcolor=color.new(color.red, 90)) // Trend for Monthly
line.new(bar_index[1], monthlySwingSupport, bar_index, monthlySwingSupport, color=color.red, width=1)

if not na(monthlySwingResistance)
table.cell(levelsTable, 2, 4, str.tostring(monthlySwingResistance), bgcolor=color.new(color.red, 90))
line.new(bar_index[1], monthlySwingResistance, bar_index, monthlySwingResistance, color=color.red, width=1)

// Draw labels on the lines
if not na(hourlySwingSupport)
label.new(bar_index, hourlySwingSupport, "H", color=color.new(color.green, 0), style=label.style_label_down, textcolor=color.white)

if not na(weeklySwingSupport)
label.new(bar_index, weeklySwingSupport, "W", color=color.new(color.orange, 0), style=label.style_label_down, textcolor=color.white)

if not na(monthlySwingSupport)
label.new(bar_index, monthlySwingSupport, "M", color=color.new(color.red, 0), style=label.style_label_down, textcolor=color.white)
Trend Analysis

Open-source script

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in publication is governed by House rules. You can favorite it to use it on a chart.

Want to use this script on a chart?

Disclaimer