Rma Stdev Bands
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Arranger77
// Stdev @gorx1
// Renko @umur.ozkul

//@version=5
indicator("Rma Stdev Bands", overlay=true)

wstdev(x, y, b) =>
    sum   = 0.0
    w_sum = 0.0
    
    for i = 0 to y - 1
        w      = y - i
        sum   := sum + math.pow(x[i] - b, 2) * w
        w_sum := w_sum + w 

    math.sqrt(sum/w_sum)

W='RMA STDEV BANDS'
_MA    = input.string(defval="RMA",title="",options=["RMA","SMA"],group=W,inline='1')
source = input(ohlc4 , ''         , inline='1',group=W)
len    = input(365  , ''          , inline='1',group=W)
Upper  = input(false    , 'Upper'          , inline='2',group=W)
Lower  = input(false    , 'Lower'          , inline='2',group=W)
log    = input(false, 'math.exp'   , inline='2',group=W)
m_u    = input.float(1,step=0.1,title='Upper',inline='3',group=W)
m_u2   = input.float(2,step=0.1,title='',inline='3',group=W)
m_l    = input.float(-1,step=0.1,title='Lower',inline='4',group=W)
m_l2   = input.float(-2,step=0.1,title='',inline='4',group=W)

src         = log ? math.log(source) : source
Ma          = _MA=="RMA"?ta.rma(src,len):ta.sma(src,len) 
dev         = wstdev(src, len, Ma)
dev_u       = Ma + dev * m_u
dev_u2      = Ma + dev * m_u2
dev_l       = Ma + dev * m_l
dev_l2      = Ma + dev * m_l2
color_Ma = Ma > Ma[1] ? color.green : color.orange 

MA=log?math.exp(Ma):Ma, Dev_u=log?math.exp(dev_u):dev_u, Dev_u2=log?math.exp(dev_u2):dev_u2,
Dev_l=log?math.exp(dev_l):dev_l, Dev_l2=log?math.exp(dev_l2):dev_l2

plot(MA, 'Moving Average', color_Ma ,linewidth=2)
plot(Upper?Dev_u:na, 'Upper band'   , color.orange)
plot(Upper?Dev_u2:na, 'Upper band2'   , color.purple)
f2=plot(Lower?Dev_l:na, 'Lower band'   , color.green)
f1=plot(Lower?Dev_l2:na, 'Lower band2' , Dev_l2<0?color.black:color.purple)

percentL2=input(true,"Upper2 %",inline='5',group=W)
_prcnt=input.float(93,"",step=0.1,inline='5',group=W),   _Z=Dev_u2*(1-_prcnt/100)
F1=plot(percentL2?_Z:na,color=_Z>_Z[1]?color.new(color.green,0):color.new(color.red,0),linewidth=1,title="Upper2 Percent") 
__prcnt=input.float(83,"",step=0.1,inline='5',group=W), __Z=Dev_u2*(1-__prcnt/100)
F2=plot(percentL2?__Z:na,color=__Z>__Z[1]?color.new(color.green,0):color.new(color.red,0),linewidth=1,title="Upper2 Percent")

///////////////////////////////////////////////////////////////////////////////////////////////////////////

Renko=input(true,title="",group="Renko",inline="Renko")
IgnorePercent=input(50.,"%",group="Renko",inline="Renko")
H=input.string(defval="Upper2",options=["Upper2","Upper","Lower","Lower2"],group="Renko",inline="Renko",title="Src")
R_info=input(true,title="Renko İnfo",group="Renko",inline="Renko_")
Table=input(true,title="Chart History",group="Renko",inline="Renko_")
oth=input(true,title="L2",group="Renko",inline="Renko_",tooltip="L2:Previous LOWER Value, Last Not Trend Box and Average(Last not trendbox@first not trendbox)")

SRC=H=="Lower2"?Dev_l2 : H=="Lower"?Dev_l: H=="Upper"?Dev_u : Dev_u2
float CENTER = -1
float prevCENTER = 0
float UPPER = 0
float LOWER = 0
goup = SRC>nz(UPPER[1])
godown = SRC<nz(LOWER[1]) 
CENTERRequired = na(CENTER[1]) or goup or godown
prevCENTER:= CENTERRequired ? CENTER[1] : prevCENTER[1]
CENTER:= CENTERRequired  ? SRC : CENTER[1]
UPPER:= CENTERRequired ? CENTER * (1+IgnorePercent/100) : UPPER[1]
LOWER:= CENTERRequired ? CENTER / (1+IgnorePercent/100) : LOWER[1]

trending= nz(CENTER) > nz(prevCENTER)

//--- Renko Count
Barscountt=ta.barssince(trending),Barscounttt=ta.barssince(not trending), trendnottrend=not trending?Barscountt:Barscounttt
plotchar(trendnottrend,"RENKO COUNT","",color=not trending?color.red:color.green)
barscountt=ta.barssince(nz(CENTER) != nz(CENTER)[1]),plotchar(barscountt+1,"RENKO COUNT2","",color=trending?color.green:color.red)

TP=plot(Renko?UPPER:na,  "Upper - Line1",    color=color.black, style=plot.style_stepline,linewidth=1)
Avg1=math.avg(UPPER,CENTER),plot(Renko?Avg1:na,"Avg(Upper,Center) - Line2",style=plot.style_stepline)
mp=plot(Renko?CENTER:na, "Center - Line3",   color=color.gray,  style=plot.style_stepline,linewidth=2)
Avg2=math.avg(LOWER,CENTER),plot(Renko?Avg2:na,"Avg(Lower,Center) - Line4",style=plot.style_stepline)
bp=plot(Renko?LOWER:na,  "Lower - Line5",    color=color.black, style=plot.style_stepline,linewidth=1)

//--- Previous LOWER Value
prevl=nz(CENTER) > nz(CENTER)[1]
Prevlower=ta.valuewhen(prevl,LOWER[1],0)
//--- Last Not Trend Box Lower
lastnot=nz(CENTER) != nz(CENTER)[1] and not trending 
lastnottrendbox=ta.valuewhen(lastnot,LOWER,0)
//--- AVERAGE(LAST NOT TREND BOX @ FIRST NOT TREND BOX)
average=ta.valuewhen(lastnottrendbox[1]!=lastnottrendbox,math.avg(lastnottrendbox[1],LOWER),0)
//---
AV_=math.avg(lastnottrendbox,Prevlower)
//--- Plot
color1=Prevlower >= LOWER?color.new(color.purple,100):color.new(color.purple,50)
color2=average >= LOWER?color.new(color.orange,100):color.new(color.orange,0)
color3=lastnottrendbox >= LOWER?color.new(color.blue,100):color.new(color.blue,0)
plot(oth?Prevlower:na,    color=color1,style=plot.style_stepline,linewidth=2,title="Previous LOWER Value")  
plot(ta.valuewhen(oth and trending and AV_<Prevlower,math.avg(lastnottrendbox,Prevlower),0),style=plot.style_stepline,linewidth=2,color=#8ee5ee,title="Average Previous LOWER Value, Last Not Trend Box Lower")  
plot(oth?lastnottrendbox:na,   color=color3, style=plot.style_stepline,linewidth=2,title="Last Not Trend Box Lower") 
plot(oth?average:na,color=color2,style=plot.style_stepline,linewidth=2,title="Average(Last not trendbox @ first not trendbox)")

fill(TP,bp, color= CENTERRequired?na:(trending ? color.new(color.lime,90) : color.new(color.orange,90)),title="Renko fill")  
fill(mp,bp, color=   not trending?na:(trending ? color.new(color.lime,80) : color.new(color.orange,80)),title="Renko fill") 
fill(mp,TP, color=       trending?na:(trending ? color.new(color.lime,80) : color.new(color.orange,80)),title="Renko fill") 

filcolor=ta.crossunder(Dev_u,LOWER)?(trending?color.new(color.gray,transp=0):color.new(color.green,transp=0)):color.new(color.green,transp=93)
fill(f1,f2,color=filcolor,title="STDEV Lower band2 - lower band fill")
filcolor_=ta.crossunder(Dev_u,LOWER)?(trending?color.new(color.gray,transp=0):color.new(color.green,transp=0)):color.new(color.green,transp=93)
fill(F1,F2,color=filcolor,title="UPPER2 PERCENT93-PERCENT83 FILL")  

//RENKO Information
distance1=((LOWER/UPPER)-1)*100                   //UPPER-LOWER Percent
distance2=((LOWER/math.avg(UPPER,CENTER))-1)*100  //avg(UPPER,CENTER)-LOWER Percent
distance3=((LOWER/CENTER)-1)*100                  //CENTER-LOWER Percent
distance4=((LOWER/math.avg(LOWER,CENTER))-1)*100  //avg(LOWER,CENTER)-LOWER Percent

distance5=((math.avg(LOWER,CENTER)/LOWER)-1)*100  //LOWER+avg(LOWER,CENTER)Percent
distance6=((CENTER/LOWER)-1)*100                  //LOWER+CENTER Percent
distance7=((math.avg(UPPER,CENTER)/LOWER)-1)*100  //LOWER+avg(UPPER,CENTER)Percent
distance8=((UPPER/LOWER)-1)*100                   //LOWER+UPPER Percent

if Renko and R_info
    label Label=label.new(time, CENTER,
     text="\n                 🤹 Renko Information 🤹"
     + "\n━━━━━━━━━━━━━━━━━"
     + "\n🎈   Barssince single box |" + str.tostring(barscountt+1,"#.##")
     + "\n🎈   Barssince trend        |" +str.tostring(trendnottrend,"#.##")
     + "\n━━━━━━━━━━━━━━━━━"
     + "\n🎈   Line1 - Line5 Percent |" + str.tostring(distance1,"#.##")
     + "\n🎈   Line2 - Line5 Percent |" + str.tostring(distance2,"#.##")
     + "\n🎈   Line3 - Line5 Percent |" + str.tostring(distance3,"#.##")
     + "\n🎈   Line4 - Line5 Percent |" + str.tostring(distance4,"#.##")
     + "\n━━━━━━━━━━━━━━━━━"
     + "\n🎈   Line5 + Line1 Percent  |" + str.tostring(distance8,"#.##")
     + "\n🎈   Line5 + Line2 Percent  |" + str.tostring(distance7,"#.##")
     + "\n🎈   Line5 + Line3 Percent  |" + str.tostring(distance6,"#.##")
     + "\n🎈   Line5 + Line4 Percent  |" + str.tostring(distance5,"#.##"),
    
     color=color.new(color.black,25), xloc= xloc.bar_time, style=label.style_label_left, textcolor=color.new(#ffffff,0), textalign=text.align_left) 
    label.set_x(Label, label.get_x(Label) + math.round(ta.change(time)*20))
    label.delete(Label[1])



//Chart History
var int first_hour=hour, var int first_day=dayofmonth, var int first_year=year, var int first_minute=minute, var int first_month=month
Month=first_month==1?'January':first_month==2?'February':first_month==3?'March':first_month==4?'April':first_month==5?'May':first_month==6?'June':
 first_month==7?'July':first_month==8?'August':first_month==9?'September':first_month==10?'October':first_month==11?'November':'December'
datestring= str.tostring(first_day) + '.' +  Month + '.' + str.tostring(first_year)
Bar_index=  str.tostring((first_day)+bar_index) 

var table perfTable = table.new(position.top_right, 1, 3, border_width = 0)
_cellText = datestring, C1=color.new(color.green, 88), C2=color.new(color.black, 0)
table.cell(perfTable, 0, 0, _cellText, bgcolor = Table==true?C1:na, text_color = Table==true?C2:na, width = 10, text_size=size.normal) 

_cellText_2 = "Bar Index = " + Bar_index 
table.cell(perfTable, 0, 1, _cellText_2, bgcolor = Table==true?C1:na, text_color = Table==true?C2:na, width = 10, text_size=size.normal)

///////////////////////////////////////////////////////////////////////////////////////////////////////////
