﻿// UBFX - Moving Average
// © RussManJoe




//@version=6
indicator(title = 'UBFX - Moving Average', shorttitle = 'UBFX - Moving Average', overlay = true)




// Inputs for moving average
typeMA = input.string(defval = 'EMA', title = 'MA Type', options = ['SMA', 'EMA', 'WMA', 'HMA'])
ma(source, length, type) =>
    switch type
        'SMA' => ta.sma(source, length)
        'EMA' => ta.ema(source, length)
        'WMA' => ta.wma(source, length)
        'HMA' => ta.wma(2 * ta.wma(source, length / 2) - ta.wma(source, length), math.floor(math.sqrt(length)))
length = input.int(34, minval = 1, title = 'Length')
source_option = input.string(title = 'Source', defval = 'Close', options = ['Close', 'Open', 'High', 'Low', 'Typical (hlc3)', 'Median (hl2)', 'Average (ohlc4)', 'Weighted Close (hlcc4)'])
src = switch source_option
    'Open' => open
    'High' => high
    'Low' => low
    'Typical (hlc3)' => hlc3
    'Median (hl2)' => hl2
    'Average (ohlc4)' => ohlc4
    'Weighted Close (hlcc4)' => hlcc4
    => close
offset = input.int(title = 'Shift', defval = 0, minval = -500, maxval = 500, display = display.none)




// Calculate moving average
ma_value = ma(src, length, typeMA)




// Plot moving average
plot(ma_value, color = ma_value > ma_value[1] ? color.rgb(0, 255, 0) : color.rgb(255, 0, 0), linewidth = 2, offset = offset)




// Enable alerts
enable_buy_alert = input.bool(true, 'Enable BUY Alerts ==> Candle Closes Above MA')
enable_sell_alert = input.bool(true, 'Enable SELL Alerts ==> Candle Closes Below MA')


enable_buy_touch_alert = input.bool(true, 'Enable BUY Touch Alerts ==> Candle Touches MA From Above')
enable_sell_touch_alert = input.bool(true, 'Enable SELL Touch Alerts ==> Candle Touches MA From Below')


enable_turnup_alert = input.bool(true, 'Enable Turn UP Alert ==> MA Turns Upwards')
enable_turndown_alert = input.bool(true, 'Enable Turn DOWN Alert ==> MA Turns Downwards')




// Alert conditions
buy_signal = ta.crossover(close, ma_value) and close[1] < ma_value[1] and close > open
sell_signal = ta.crossunder(close, ma_value) and close[1] > ma_value[1] and close < open


touch_signal_from_above = low <= ma_value and close[1] > ma_value[1]
touch_signal_from_below = high >= ma_value and close[1] < ma_value[1]


turnup_signal = ma_value > ma_value[1] and ma_value[1] < ma_value[2]
turndown_signal = ma_value < ma_value[1] and ma_value[1] > ma_value[2]




// Enable alerts if enable_alerts is true
alertcondition(enable_buy_alert and buy_signal, title = 'BUY Signal', message = 'Moving Average BUY Signal - Price Closed Above Moving Average')
alertcondition(enable_sell_alert and sell_signal, title = 'SELL Signal', message = 'Moving Average SELL Signal - Price Closed Below Moving Average')


alertcondition(enable_buy_touch_alert and touch_signal_from_above, title = 'BUY TOUCH Signal', message = 'Moving Average BUY Signal - Price Touched MA From Above')
alertcondition(enable_sell_touch_alert and touch_signal_from_below, title = 'SELL TOUCH Signal', message = 'Moving Average SELL Signal - Price Touched MA From Below')


alertcondition(enable_turnup_alert and turnup_signal, title = 'Turn UP Signal', message = 'Moving Average TURNED UP Signal')
alertcondition(enable_turndown_alert and turndown_signal, title = 'Turn DOWN Signal', message = 'Moving Average TURNED DOWN Signal')