A momentum oscillator that measures the speed and change of price movements. Buy when RSI enters oversold territory and sell when it enters overbought territory.
When RSI falls below the oversold level (typically 30) and then crosses back above it, it indicates the asset may be undervalued. Enter a long position.
When RSI rises above the overbought level (typically 70) and then crosses back below it, it indicates the asset may be overvalued. Enter a short position or close long.
| Parameter | Default | Description |
|---|---|---|
| RSI Length | 14 | Number of periods for RSI calculation |
| Oversold Level | 30 | RSI level indicating oversold condition |
| Overbought Level | 70 | RSI level indicating overbought condition |
//@version=5
strategy("RSI Strategy", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=10)
// Input parameters
rsiLength = input.int(14, title="RSI Length", minval=1)
overSold = input.int(30, title="Oversold Level", minval=1, maxval=100)
overBought = input.int(70, title="Overbought Level", minval=1, maxval=100)
src = input.source(close, title="Source")
// Calculate RSI
rsi = ta.rsi(src, rsiLength)
// Plot RSI
plot(rsi, color=color.purple, title="RSI")
hline(overBought, color=color.red, linestyle=hline.style_dashed, title="Overbought")
hline(overSold, color=color.green, linestyle=hline.style_dashed, title="Oversold")
hline(50, color=color.gray, linestyle=hline.style_dotted)
// Generate signals
longCondition = ta.crossover(rsi, overSold)
shortCondition = ta.crossunder(rsi, overBought)
// Entry conditions
if (longCondition)
strategy.entry(id="Long", direction=strategy.long)
if (shortCondition)
strategy.entry(id="Short", direction=strategy.short)
// Exit conditions - close opposite positions
if (longCondition)
strategy.close(id="Short")
if (shortCondition)
strategy.close(id="Long")
// Plot signals on chart
plotshape(longCondition, title="Buy Signal", location=location.belowbar, color=color.green, style=shape.triangleup, size=size.tiny, text="RSI BUY")
plotshape(shortCondition, title="Sell Signal", location=location.abovebar, color=color.red, style=shape.triangledown, size=size.tiny, text="RSI SELL")
// Alert conditions
alertcondition(longCondition, title="Buy Alert", message="RSI Oversold - Enter Long")
alertcondition(shortCondition, title="Sell Alert", message="RSI Overbought - Enter Short")