TrendingNow
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © jac001
//@version=5
indicator("TrendingNow", overlay=true)

// Define input parameters
length = input.int(14, minval=1, title="Length")
multiplier = input.float(2.0, minval=0.1, title="Multiplier")
trailPercent = input.float(1.0, title="Trailing Stop Percentage")
confirmationLength = input.int(10, minval=1, title="Confirmation Length")
momentumLength = input.int(14, minval=1, title="Momentum Length")
overboughtLevel = input(70, title="Overbought Level")
oversoldLevel = input(30, title="Oversold Level")
volumeThreshold = input(2.0, title="Volume Threshold")
volatilityMultiplier = input.float(1.5, minval=0.1, title="Volatility Multiplier")


// Calculate the moving average
sma = ta.sma(close, length)

// Calculate the deviation from the moving average
deviation = multiplier * ta.stdev(close, length)

// Calculate the upper and lower bands
upperBand = sma + deviation
lowerBand = sma - deviation

// Calculate price reversal
reversedUpperBand = ta.crossunder(close, upperBand)
reversedLowerBand = ta.crossover(close, lowerBand)


// Determine if the current price is above the upper band (uptrend)
isUptrend = close > upperBand

// Determine if the current price is below the lower band (downtrend)
isDowntrend = close < lowerBand

isSideways = (isUptrend ==isDowntrend ==false)
// Define trailing stop variables
trailingStopLong = high * (1 - trailPercent / 100)
trailingStopShort = low * (1 + trailPercent / 100)

// Calculate trend confirmation using moving average
maConfirmation = ta.sma(close, confirmationLength)

// Calculate momentum oscillator
momentum = ta.rsi(close, momentumLength)

// Calculate volume average
volumeAvg = ta.sma(volume, length)

// Calculate volume trend confirmation
isVolumeTrendingUp = ta.change(volume) > 0 and volume > volumeAvg

// Calculate volatility filter
volatility = ta.atr(length)
volatilityThreshold = volatility * volatilityMultiplier

//alert
plotshape((isUptrend and (ta.crossover(high, upperBand) or momentum < oversoldLevel) or (ta.crossover(close, maConfirmation) and maConfirmation < sma and isVolumeTrendingUp) or (reversedLowerBand and isVolumeTrendingUp)), title = "Buy", text = 'Buy', style = shape.labelup, location = location.belowbar, color= color.green,textcolor = color.white,  size = size.tiny)
plotshape((isDowntrend and (ta.crossunder(low,lowerBand) or momentum > overboughtLevel) or (ta.crossunder(close, maConfirmation) and maConfirmation > sma and isVolumeTrendingUp) or (reversedUpperBand and isVolumeTrendingUp)), title = "Sell", text = 'Sell', style = shape.labeldown, location = location.abovebar, color= color.red,textcolor = color.white,  size = size.tiny)
alertcondition((isUptrend and (ta.crossover(high, upperBand) or momentum < oversoldLevel) or (ta.crossover(close, maConfirmation) and maConfirmation < sma and isVolumeTrendingUp) or (reversedLowerBand and isVolumeTrendingUp)), title='Buy', message='Buy')
alertcondition((isDowntrend and (ta.crossunder(low,lowerBand) or momentum > overboughtLevel) or (ta.crossunder(close, maConfirmation) and maConfirmation > sma and isVolumeTrendingUp) or (reversedUpperBand and isVolumeTrendingUp)), title='Sell', message='Sell')

// Plotting
plot(sma, color=color.blue, title="SMA")
plot(upperBand, color=color.red, title="Upper Band")
plot(lowerBand, color=color.green, title="Lower Band")
plot(maConfirmation, color=color.orange, title="MA Confirmation")