//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
// 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
// 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)