Trend Reversal Probability Calculator
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © cyatophilum

//@version=5
indicator("Trend Reversal Probability Calculator",overlay=true)

// Inputs {
theme = input.string('Light','Color Theme',options=['Dark','Light'])
groupEMA = 'Moving Averages'
length1 = input.int(10, 'MA 1', group = groupEMA)
length2 = input.int(20, 'MA 2', group = groupEMA)
length3 = input.int(30,'MA 3', group = groupEMA)
length4 = input.int(40,'MA 4', group = groupEMA)
length5 = input.int(50,'MA 5', group = groupEMA)
length6 = input.int(60,'MA 6', group = groupEMA)
length7 = input.int(70,'MA 7', group = groupEMA)
length8 = input.int(80,'MA 8', group = groupEMA)
length9 = input.int(90,'MA 9', group = groupEMA)
length10 = input.int(100,'MA 10', group = groupEMA)
length11 = input.int(200,'MA 11 (long term)', group = groupEMA)

groupTrend = 'Trend'
rocLength = input.int(14,'ROC length (Trend sensitivity)', group = groupTrend)
signalPercentage = input.int(5,'Reversal sensitivity (1 to 9)',minval = 1,maxval = 9, group = groupTrend)
//}

ma1 = ta.sma(close,length1)
ma2 = ta.sma(close,length2)
ma3 = ta.sma(close,length3)
ma4 = ta.sma(close,length4)
ma5 = ta.sma(close,length5)
ma6 = ta.sma(close,length6)
ma7 = ta.sma(close,length7)
ma8 = ta.sma(close,length8)
ma9 = ta.sma(close,length9)
ma10 = ta.sma(close,length10)
ma11 = ta.sma(close,length11)

roc1 = ta.roc(ma1,rocLength)
roc2 = ta.roc(ma2,rocLength)
roc3 = ta.roc(ma3,rocLength)
roc4 = ta.roc(ma4,rocLength)
roc5 = ta.roc(ma5,rocLength)
roc6 = ta.roc(ma6,rocLength)
roc7 = ta.roc(ma7,rocLength)
roc8 = ta.roc(ma8,rocLength)
roc9 = ta.roc(ma9,rocLength)
roc10 = ta.roc(ma10,rocLength)
roc11 = ta.roc(ma11,rocLength)

//  If the short-term moving average starts to slope in the opposite direction of the long-term moving average (e.g., 200-day), it may indicate a potential trend reversal.
trendReversalBull1 = roc1 > 0 and roc11 < 0
trendReversalBull2 = roc2 > 0 and roc11 < 0
trendReversalBull3 = roc3 > 0 and roc11 < 0
trendReversalBull4 = roc4 > 0 and roc11 < 0
trendReversalBull5 = roc5 > 0 and roc11 < 0
trendReversalBull6 = roc6 > 0 and roc11 < 0
trendReversalBull7 = roc7 > 0 and roc11 < 0
trendReversalBull8 = roc8 > 0 and roc11 < 0
trendReversalBull9 = roc9 > 0 and roc11 < 0
trendReversalBull10 = roc10 > 0 and roc11 < 0

countBull = 
 (trendReversalBull1 ? 1 : 0) +
 (trendReversalBull2 ? 1 : 0) +
 (trendReversalBull3 ? 1 : 0) +
 (trendReversalBull4 ? 1 : 0) +
 (trendReversalBull5 ? 1 : 0) +
 (trendReversalBull7 ? 1 : 0) +
 (trendReversalBull8 ? 1 : 0) +
 (trendReversalBull9 ? 1 : 0) +
 (trendReversalBull10 ? 1 : 0)

trendReversalBear1 = roc1 < 0 and roc11 > 0
trendReversalBear2 = roc2 < 0 and roc11 > 0
trendReversalBear3 = roc3 < 0 and roc11 > 0
trendReversalBear4 = roc4 < 0 and roc11 > 0
trendReversalBear5 = roc5 < 0 and roc11 > 0
trendReversalBear6 = roc6 < 0 and roc11 > 0
trendReversalBear7 = roc7 < 0 and roc11 > 0
trendReversalBear8 = roc8 < 0 and roc11 > 0
trendReversalBear9 = roc9 < 0 and roc11 > 0
trendReversalBear10 = roc10 < 0 and roc11 > 0

countBear = 
 (trendReversalBear1 ? 1 : 0) +
 (trendReversalBear2 ? 1 : 0) +
 (trendReversalBear3 ? 1 : 0) +
 (trendReversalBear4 ? 1 : 0) +
 (trendReversalBear5 ? 1 : 0) +
 (trendReversalBear7 ? 1 : 0) +
 (trendReversalBear8 ? 1 : 0) +
 (trendReversalBear9 ? 1 : 0) +
 (trendReversalBear10 ? 1 : 0)

bull = ta.crossover(roc11,0)
bear = ta.crossunder(roc11,0)
reversalBull = ta.crossover(countBull,signalPercentage) and not bear
reversalBear = ta.crossover(countBear,signalPercentage) and not bull


// Render {
color colorRibbon = na
color colorRibbonA = na
color frameColor = na

if theme == 'Light'
    colorRibbon  := roc11 > 0 ? color.rgb(255, 255 - 255/10*countBear, 255 - 255/10*countBear)     : color.rgb(255 - 255/10*countBull, 255, 255 - 255/10*countBull)
    colorRibbonA := roc11 > 0 ? color.rgb(255 ,255 - 255/10*countBear, 255 - 255/10*countBear, 70) : color.rgb(255 - 255/10*countBull ,255, 255 - 255/10*countBull, 70)
    frameColor   := roc11 > 0 ? color.rgb(0,255,0) : color.rgb(255,0,0)
else
    colorRibbon  := roc11 > 0 ? color.rgb(255/10*countBear, 0, 0)     : color.rgb(0, 255/10*countBull, 0)
    colorRibbonA := roc11 > 0 ? color.rgb(255/10*countBear, 0, 0, 70) : color.rgb(0, 255/10*countBull, 0, 70)
    frameColor   := roc11 > 0 ? color.rgb(0, 255, 0, 50) : color.rgb(255,0,0,50)






if barstate.islast
    info = table.new(position.top_right,2,2,bgcolor = colorRibbonA,frame_color = frameColor, frame_width = 2)
    table.cell(info,0,0,'Current Trend:',text_color = chart.fg_color)
    table.cell(info,1,0,roc11 > 0 ? 'Bull' : 'Bear',text_color = roc11 > 0 ? color.green : color.rgb(224, 0, 0), text_font_family = font.family_monospace,text_size = size.large)
    table.cell(info,0,1,roc11 > 0 ? 'Reversal Bear Probability:' : 'Reversal Bull Probability:',text_color = chart.fg_color)
    table.cell(info,1,1,roc11 > 0 ? str.tostring(countBear*10) + '%' : str.tostring(countBull*10) + '%',text_color = chart.fg_color)


plot(roc1,'roc1',display = display.data_window)
plot(roc2,'roc2',display = display.data_window)
plot(roc3,'roc3',display = display.data_window)


p1 = plot(ma1,'MA 1',  colorRibbon)
p2 = plot(ma2,'MA 2',  colorRibbon)
p3 = plot(ma3,'MA 3',  colorRibbon)
p4 = plot(ma4,'MA 4',  colorRibbon)
p5 = plot(ma5,'MA 5',  colorRibbon)
p6 = plot(ma6,'MA 6',  colorRibbon)
p7 = plot(ma7,'MA 7',  colorRibbon)
p8 = plot(ma8,'MA 8',  colorRibbon)
p9 = plot(ma9,'MA 9',  colorRibbon)
p10 = plot(ma10,'MA 10',colorRibbon)
p11 = plot(ma11,'MA 11',linewidth = 3,  color = frameColor)

fill(p1,p2,color=colorRibbonA)
fill(p2,p3,color=colorRibbonA)
fill(p3,p4,color=colorRibbonA)
fill(p4,p5,color=colorRibbonA)
fill(p5,p6,color=colorRibbonA)
fill(p6,p7,color=colorRibbonA)
fill(p7,p8,color=colorRibbonA)
fill(p8,p9,color=colorRibbonA)
fill(p9,p10,color=colorRibbonA)

plotcandle(open,high,low,close,'candles',roc11 > 0 ?  color.rgb(0,255,0) : color.rgb(255,0,0))


labelColorBull = theme == 'Light' ? color.lime : color.rgb(0, 230, 119, 50)
labelColorBear = theme == 'Light' ? color.red : color.rgb(230, 0, 0, 50)
labelTextColor = theme == 'Light' ? color.black : color.white

plotshape(bull, 'Bull',shape.labelup,  location.belowbar,labelColorBull,0,'Bull',labelTextColor,size=size.small)
plotshape(bear, 'Bear',shape.labeldown,location.abovebar,labelColorBear,0, 'Bear',labelTextColor,size=size.small)

plotshape(reversalBear, 'Reversal Bear',shape.labeldown,location.abovebar,labelColorBear,0, 'ReversalBear',labelTextColor,size=size.small)
plotshape(reversalBull, 'Reversal Bull',shape.labelup,  location.belowbar,labelColorBull,0,'Reversal Bull',labelTextColor,size=size.small)
// }

// Alerts {
alertcondition(bull,         'Bull Signal')
alertcondition(bear,         'Bear Signal')
alertcondition(reversalBull, 'Reversal Bull')
alertcondition(reversalBear, 'Reversal Bear')

//}