I'll create a more interactive version where you can click to place a horizontal line anywhere on the chart:
//@version=5
indicator("Click to Draw Alert Line", overlay=true)
// Input settings
lineColor = input.color(color.blue, "Line Color")
lineWidth = input.int(2, "Line Width", minval=1, maxval=5)
showLabel = input.bool(true, "Show Price Label")
// Variables to store line
var line alertLine = na
var label priceLabel = na
var float linePrice = na
// Check for mouse click
if barstate.islast
// Get click coordinates
clickPrice = input.price(defval=close, title="Click to set alert level", confirm=true)
// Delete existing line and label
if not na(alertLine)
line.delete(alertLine)
if not na(priceLabel)
label.delete(priceLabel)
// Create new horizontal line
alertLine := line.new(bar_index[10], clickPrice, bar_index, clickPrice,
extend=extend.both, color=lineColor, width=lineWidth,
style=line.style_solid)
// Store line price
linePrice := clickPrice
// Create price label
if showLabel
priceLabel := label.new(bar_index, clickPrice, str.tostring(clickPrice, format.mintick),
style=label.style_label_left, color=lineColor,
textcolor=color.white, size=size.small)
// Check for crosses only if line exists
if not na(linePrice)
crossAbove = close[1] <= linePrice and close > linePrice
crossBelow = close[1] >= linePrice and close < linePrice
// Visual markers
plotshape(crossAbove, "Cross Above", shape.triangleup, location.belowbar, color.green, size=size.small)
plotshape(crossBelow, "Cross Below", shape.triangledown, location.abovebar, color.red, size=size.small)
// Alert conditions
alertcondition(crossAbove, title="Price Crossed Above Line", message="Price closed above alert line at {{close}}")
alertcondition(crossBelow, title="Price Crossed Below Line", message="Price closed below alert line at {{close}}")
Here's an even better version that uses the interactive drawing feature:
//@version=5
indicator("Interactive Horizontal Alert Line", overlay=true, max_lines_count=500)
// Inputs
lineColor = input.color(color.blue, "Line Color")
lineWidth = input.int(2, "Line Width", minval=1, maxval=5)
lineStyle = input.string("Solid", "Line Style", options=["Solid", "Dashed", "Dotted"])
// Convert line style
getLineStyle() =>
switch lineStyle
"Dashed" => line.style_dashed
"Dotted" => line.style_dotted
=> line.style_solid
// Store drawn lines
var array<line> drawnLines = array.new<line>()
// Function to check if a line is horizontal
isHorizontal(line l) =>
math.abs(line.get_y1(l) - line.get_y2(l)) < syminfo.mintick
// Check all lines on the chart
if barstate.islast
// Get all lines
allLines = line.all
// Process each line
for i = 0 to array.size(allLines) - 1
currentLine = array.get(allLines, i)
// Check if it's a new horizontal line
if isHorizontal(currentLine) and not array.includes(drawnLines, currentLine)
array.push(drawnLines, currentLine)
// Style the line
line.set_color(currentLine, lineColor)
line.set_width(currentLine, lineWidth)
line.set_style(currentLine, getLineStyle())
line.set_extend(currentLine, extend.both)
// Check for price crosses
var bool crossedAbove = false
var bool crossedBelow = false
if array.size(drawnLines) > 0
for i = 0 to array.size(drawnLines) - 1
l = array.get(drawnLines, i)
lineLevel = line.get_y1(l)
// Check crosses
if close[1] <= lineLevel and close > lineLevel
crossedAbove := true
if close[1] >= lineLevel and close < lineLevel
crossedBelow := true
// Reset crosses
if barstate.isconfirmed
if crossedAbove
alert("Price crossed above horizontal line at " + str.tostring(close), alert.freq_once_per_bar)
if crossedBelow
alert("Price crossed below horizontal line at " + str.tostring(close), alert.freq_once_per_bar)
crossedAbove := false
crossedBelow := false
// Alert conditions
alertcondition(crossedAbove, title="Cross Above Horizontal Line", message="Price crossed above horizontal line")
alertcondition(crossedBelow, title="Cross Below Horizontal Line", message="Price crossed below horizontal line")
// Instructions
var table infoTable = table.new(position.top_right, 1, 1)
if barstate.islast
table.cell(infoTable, 0, 0, "Draw horizontal lines using the line tool",
text_color=color.white, bgcolor=color.blue, text_size=size.small)
To use this indicator:
- Add the indicator to your 4-hour chart
- Use TradingView's built-in line drawing tool (on the left toolbar)
- Draw a horizontal line anywhere on the chart by clicking and dragging horizontally
- The indicator will automatically detect and style your horizontal lines
- Set up alerts by going to the Alerts panel and selecting either:
- "Cross Above Horizontal Line"
- "Cross Below Horizontal Line"
The indicator will monitor all horizontal lines you draw and trigger alerts when price closes above or below any of them. You can draw multiple lines if needed.