Conditions, signals and alerts
if blocks, crossovers, markers, bar colors and alert conditions.
indicator("RSI signals", overlay = true)
r = rsi(close, 14)
oversold = crossover(r, 30)
overbought = crossunder(r, 70)
signal(oversold, "RSI ↑", position = below, color = green)
signal(overbought, "RSI ↓", position = above, color = red)
barcolor(r > 70 ? fade(red, 70) : r < 30 ? fade(green, 70) : na)
alert(oversold, "RSI crossed back above 30")Use if when several lines depend on the same condition. Lines inside the block are indented by four spaces:
fast = ema(close, 9)
slow = ema(close, 21)
bull = crossover(fast, slow)
bear = crossunder(fast, slow)
if bull
signal(true, "Bull cross", position = below, color = green)
else if bear
signal(true, "Bear cross", position = above, color = red)
plot(fast, "Fast", color = amber)
plot(slow, "Slow", color = violet)Key takeaways
- crossover/crossunder detect the bar where one series crosses another.
- signal, barcolor and bgcolor make conditions visible.
- alert() marks bars where an alert condition is met.