This Pine Script(tm) indicator("RSI Trail [UAlgo]", shorttitle="RSI Trail [UAlgo]", overlay=true) // Configuration i_ma_type = input.string("EMA", title="Moving Average Type", options=["SMA", "EMA", "WMA", "RMA", "McGinley"], group="RSI Trailing Stop [UAlgo]") f_rsi_lower = input.float(40, title="RSI Lower Bound", inline="rsi_bounds", group="RSI Trailing Stop [UAlgo]", minval=0, maxval=100) f_rsi_upper = input.float(60, title="RSI Upper Bound", inline="rsi_bounds", group="RSI Trailing Stop [UAlgo]", minval=0, maxval=100) b_show_midline = input.bool(false, title='Show Midline', inline="display", group="RSI Trailing Stop [UAlgo]") b_color_candles = input.bool(false, title='Color Candles', inline="display", group="RSI Trailing Stop [UAlgo]") // Moving Average Functions f_sma(_src, _length) => ta.sma(_src, _length) f_ema(_src, _length) => ta.ema(_src, _length) f_wma(_src, _length) => ta.wma(_src, _length) f_rma(_src, _length) => ta.rma(_src, _length) f_mcginley(_src, _length) => var float md = na md := na(md[1]) ? _src : (md[1] + (_src - md[1]) / (0.6 * _length * math.pow((_src / md[1]), 4))) md // Function to select Moving Average f_get_ma(_type, _src, _length) => switch _type "SMA" => f_sma(_src, _length) "EMA" => f_ema(_src, _length) "WMA" => f_wma(_src, _length) "RMA" => f_rma(_src, _length) "McGinley" => f_mcginley(_src, _length) // Calculate indicator values f_calculate_bounds(float _ma, float _range, float _upper, float _lower) => float upper_bound = _ma + (_upper - 50) / 10 * _range float lower_bound = _ma - (50 - _lower) / 10 * _range [upper_bound, lower_bound] f_volatility = ta.atr(27) f_ma_base = f_get_ma(i_ma_type, ohlc4, 27) [f_upper_bound, f_lower_bound] = f_calculate_bounds(f_ma_base, f_volatility, f_rsi_upper, f_rsi_lower) // Determine market state var bool is_bullish = false var bool is_bearish = false bool bull_signal = false bool bear_signal = false if ta.crossover(ohlc4, f_upper_bound) bull_signal := not is_bullish is_bullish := true is_bearish := false if ta.crossunder(close, f_lower_bound) bear_signal := not is_bearish is_bullish := false is_bearish := true // Plotting plot(f_lower_bound, color = is_bullish ? #089981 : na, linewidth = 2) plot(f_upper_bound, color = is_bearish ? #f23645 : na, linewidth = 2) plot(b_show_midline ? f_ma_base : na, color = color.new(color.gray, 70), linewidth = 2) plotshape(bull_signal ? f_lower_bound : na, style = shape.triangleup, location = location.absolute, color = #089981, size = size.tiny) plotshape(bear_signal ? f_upper_bound : na, style = shape.triangledown, location = location.absolute, color = #f23645, size = size.tiny) f_candle_color = b_color_candles ? (is_bullish ? #089981 : #f23645) : na barcolor(f_candle_color) // Alerts f_generate_alert(bool _condition, string _message) => if _condition alert(_message + syminfo.tickerid + " on " + timeframe.period + " chart", freq = alert.freq_once_per_bar_close) f_generate_alert(bull_signal, "Bullish Signal: ") f_generate_alert(bear_signal, "Bearish Signal: ") alertcondition(bull_signal, title = "Bullish Signal", message = "Bullish signal detected!") alertcondition(bear_signal, title = "Bearish Signal", message = "Bearish signal detected!")