Bar Color Long / Short Indicator With Advised SL Rev 1
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © CR-JE

//@version=5
indicator(title='Bar Color Long / Short Indicator With Advised SL Rev 4', shorttitle='SI', overlay=true)

// Stop Loss Finder
length = input.int(title='Length', defval=90, minval=1)
smoothing = input.string(title='Smoothing', defval='RMA', options=['RMA', 'SMA', 'EMA', 'WMA'])
m = input(1.5, 'Multiplier')
src1 = high
src2 = low
pline = true
show_bar_colors = input.bool(title='Bar Colors Orange-Blue and Signal Arrows ON/OFF', defval=false)
show_bar_colorsLSonly = input.bool(title='Bar Colors Long-Short ON?OFF', defval=true)
show_bar_colorsLS = input.bool(title='Bar Colors Red Yellow Green ON/OFF', defval=false)
Arrow_colors = input.bool(title='Arrow Colors Black/White', defval=true)
BG_colors = input.bool(title='Bg Colors ON/OFF', defval=false)
Advised_SL = input.bool(title='Advised SL ON/OFF', defval=false)


collong = not show_bar_colors ? color.green : color.orange
colshort = not show_bar_colors ? color.red : color.blue

ma_function(source, length) =>
    if smoothing == 'RMA'
        ta.rma(source, length)
    else
        if smoothing == 'SMA'
            ta.sma(source, length)
        else
            if smoothing == 'EMA'
                ta.ema(source, length)
            else
                ta.wma(source, length)

a = ma_function(ta.tr(true), length) * m
x = ma_function(ta.tr(true), length) * m + src1
x2 = src2 - ma_function(ta.tr(true), length) * m

// Custom max function to handle series and float arguments
max_custom(x, y) =>
    x > y ? x : y

// User input for custom risk calculation
customRiskPeriod = 14

// Custom risk calculation (resembling RSI)
priceDelta = close - close[1]
avgGain = ta.sma(max_custom(priceDelta, 0), customRiskPeriod)
avgLoss = ta.sma(max_custom(-priceDelta, 0), customRiskPeriod)
customRS = avgGain / avgLoss
customRisk = 100 - (100 / (1 + customRS))

// Define risk levels
lowRisk = 30
mediumRisk = 50
highRisk = 70

// Custom sentiment calculation (resembling EMA, MACD, and ROC)
src = close
shortPeriod = 9
longPeriod = 21

customShort = ta.vwap(src)
customLong = ta.wma(src, longPeriod)
customSignal = customShort > customLong ? 1 : -1

priceDelta2 = (src - src[1]) / src[1] * 100
customROC = ta.sma(priceDelta2, shortPeriod)
rocSignal = customROC > 0 ? 1 : -1

sentimentScore = (customSignal + rocSignal) / 2
sentimentPositive = sentimentScore > 0

// Bollinger Bands Percent calculation
length1 = 20
mult = 2
basis = ta.sma(close, length1)
dev = mult * ta.stdev(close, length1)
upper = basis + dev
lower = basis - dev
bbPercent = (close - lower) / (upper - lower)

// Combine custom risk, sentiment score, and BB Percent for the new signal
combinedSignal = customRisk + sentimentScore * 10 + bbPercent * 10

// EMA10 calculation
ema10 = ta.ema(close, 10)
ema20 = ta.ema(close, 20)
sma50 = ta.sma(close, 50)

// Long and Short
long = sentimentPositive and (open > ema10 or close > ema10) and (ema10 > ema20) and (sma50 >= sma50[1])
short = not sentimentPositive and (open < ema10 or close < ema10) and (ema10 < ema20) and (sma50 <= sma50[1])

// Close Open Confirmation
confirmOpenL = close >= open
confirmOpenS = close <= open

// Signal once Long & Short
var isLong = false
var isShort = false

//Long
buy = not isLong and long and confirmOpenL

//Short
sell = not isShort and short and confirmOpenS

if (long)
    isLong := true
    isShort := false

if (short)
    isLong := false
    isShort := true

// Bar color based on Condition
candle_color = long ? color.orange : short ? color.blue : color.gray
candle_colorB = long ? color.green : short ? color.red : color.yellow
candle_colorC = buy and BG_colors ? color.rgb(6, 223, 247, 85) : sell and BG_colors ? color.rgb(247, 5, 230, 85) : na
candle_colorD = buy ? color.rgb(60, 233, 252) : sell ? color.rgb(245, 59, 232) : na

// Shape color
ShapeColor = Arrow_colors ? color.rgb(0, 1, 4) : color.white

barcolor(show_bar_colors ? candle_color : show_bar_colorsLS ? candle_colorB : show_bar_colorsLSonly ? candle_colorD : na)
bgcolor(candle_colorC)

// Alert conditions
alertcondition(buy, title="long Signal", message="Buy Signal triggered")
alertcondition(sell, title="short Signal", message="Sell Signal triggered")
alertcondition(buy or sell, title="long or short Signal", message="Long or short Signal triggered")

// Added large arrow up and down for buy and sell signals
plotshape(buy and not show_bar_colors, style=shape.arrowup, location=location.belowbar, color=ShapeColor, size=size.large)
plotshape(sell and not show_bar_colors, style=shape.arrowdown, location=location.abovebar, color=ShapeColor, size=size.large)

// Plot SL line at the last candle
if barstate.islast and Advised_SL
    line.new(x1=bar_index, y1=x, x2=bar_index+1, y2=x, width=1, color=colshort)
    line.new(x1=bar_index, y1=x2, x2=bar_index+1, y2=x2, width=1, color=collong)
    label.new(x=bar_index + 1, y=x, text='Advised Short Stop Loss: ' + str.tostring(x, '#.##'), color=colshort, style=label.style_label_lower_left, textcolor=color.white)
    label.new(x=bar_index + 1, y=x2, text='Advised Long Stop Loss: ' + str.tostring(x2, '#.##'), color=collong, style=label.style_label_upper_left, textcolor=color.white)

