﻿// UBFX - RSI
// © RussManJoe


//@version=6
indicator(title = 'UBFX - RSI', shorttitle = 'UBFX - RSI', overlay = false)




// Input variables
rsi_length = input.int(5, 'RSI Length')
price_type = input.string('Typical', 'Price Type', options = ['Close', 'Typical'])


smoothing_factor = input.int(1, title = 'RSI Smoothing Factor', display = display.none)


top_level = 100.0
overbought_level = input.float(70, 'Overbought Level', display = display.none)
buy_level = input.float(55, 'Buy Level', display = display.none)
sell_level = input.float(45, 'Sell Level', display = display.none)
oversold_level = input.float(30, 'Oversold Level', display = display.none)
bottom_level = 0.0




// Moving Average Settings
show_ma = input.bool(false, title = 'Show Moving Average')
ma_length = input.int(5, title = 'Moving Average Length', display = display.none)
ma_type = input.string('EMA', title = 'Moving Average Type', options = ['SMA', 'EMA'], display = display.none)




// Default to Typical price
selected_price = hlc3




// Assign selected price based on price type
if price_type == 'Close'
    selected_price := close
    selected_price
else if price_type == 'Typical'
    selected_price := (high + low + close) / 3
    selected_price




// Calculate RSI
rsi = ta.rsi(selected_price, rsi_length)




// Smoothing factor
smoothed_rsi = ta.sma(rsi, smoothing_factor)


// Set RSI color based on conditions
rsi_color = smoothed_rsi > buy_level and smoothed_rsi[1] < buy_level ? color.rgb(50, 205, 50) : (smoothed_rsi < sell_level and smoothed_rsi[1] > sell_level ? color.rgb(255, 0, 0) : color.rgb(41, 98, 255))


// Plot RSI line with the calculated color
plot(smoothed_rsi, 'RSI Colors', color = rsi_color, linewidth = 2)


// Plot horizontal lines for zones
l1 = hline(overbought_level, 'Overbought Level', color = color.rgb(120, 122, 134), linestyle = hline.style_solid)
l2 = hline(buy_level, 'Buy Level', color = color.rgb(159,0,13), linestyle = hline.style_solid)
l3 = hline(sell_level, 'Sell Level', color = color.rgb(159,0,13), linestyle = hline.style_solid)
l4 = hline(oversold_level, 'Oversold Level', color = color.rgb(120, 122, 134), linestyle = hline.style_solid)
 
// Plot Moving Average if enabled
ma = show_ma ? ma_type == 'SMA' ? ta.sma(smoothed_rsi, ma_length) : ta.ema(smoothed_rsi, ma_length) : na
plot(ma, title = 'Moving Average', color = color.orange, linewidth = 1)




// Enable Alerts
enable_buy_alerts = input.bool(true, 'Enable BUY Alerts')
enable_sell_alerts = input.bool(true, 'Enable SELL Alerts')
enable_rsi_ma_cross_alerts = input.bool(true, 'Enable RSI/MA Cross Alerts')




// Alert conditions
buy_signal = ta.crossover(smoothed_rsi, buy_level) and close > open
sell_signal = ta.crossunder(smoothed_rsi, sell_level) and close < open
rsi_ma_buy_signal = ta.crossover(smoothed_rsi, ma)
rsi_ma_sell_signal = ta.crossunder(smoothed_rsi, ma)




// Enable alerts if enable_alerts is true
alertcondition(enable_buy_alerts and buy_signal, title = 'BUY Signal', message = 'RSI BUY Signal')
alertcondition(enable_sell_alerts and sell_signal, title = 'SELL Signal', message = 'RSI SELL Signal')
alertcondition(enable_rsi_ma_cross_alerts and rsi_ma_buy_signal, title = 'BUY MA Cross Signal', message = 'RSI crossed above the Moving Average')
alertcondition(enable_rsi_ma_cross_alerts and rsi_ma_sell_signal, title = 'SELL MA Cross Signal', message = 'RSI crossed below the Moving Average')