Renko Ichimoku Cloud
  // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © PerpetuallyTryingFutures

//@version=5
indicator('Renko Ichimoku Cloud', shorttitle= 'Renko Ichimoku', overlay=true)
mode =input.string(title = 'Input Method', defval ='ATR', options=['Traditional', 'ATR'])
modevalue = input.int(title ='ATR Brick Size', defval = 14, minval = 1)
boxsize = input.float(title ='Traditional Brick Size', defval = 1, step= 0.001, minval = 0.0000001)

//Calculations//
conv_atr(value)=>
    a = 0
    num = syminfo.mintick
    s = value
    if na(s)
        s := syminfo.mintick
    if num < 1
        for i = 1 to 20
            num := num * 10
            if num > 1
                break
            a := a +1
    for x = 1 to a 
        s := s * 10
    s := math.round(s)
    for x = 1 to a
        s := s / 10
    s := s < syminfo.mintick  ? syminfo.mintick : s
    s


atrboxsize = conv_atr(ta.atr(modevalue))

float box = na
box := na(box[1]) ? mode == 'ATR' ? atrboxsize : boxsize : box[1] 

reversal = 2
top = 0.0, bottom = 0.0
trend = 0
trend := barstate.isfirst ? 0 : nz(trend[1])
currentprice = 0.0
currentprice := close 
float beginprice = na
beginprice := barstate.isfirst ? math.floor(open / box) * box : nz(beginprice[1])
openprice = 0.0
closeprice = 0.0

if trend == 0 and box * reversal <= math.abs(beginprice - currentprice)
    if beginprice > currentprice
        numcell = math.floor(math.abs(beginprice - currentprice) / box)
        openprice := beginprice
        closeprice := beginprice - numcell * box
        trend := -1
    if beginprice < currentprice
        numcell = math.floor(math.abs(beginprice - currentprice) / box)
        openprice := beginprice
        closeprice := beginprice + numcell * box
        trend := 1

if trend == -1
    nok = true
    if beginprice > currentprice and box <= math.abs(beginprice - currentprice)
        numcell = math.floor(math.abs(beginprice - currentprice) / box)
        closeprice := beginprice - numcell * box
        trend := -1
        beginprice := closeprice
        nok := false
    else
        openprice := openprice == 0 ? nz(openprice[1]) : openprice
        closeprice := closeprice == 0 ? nz(closeprice[1]) : closeprice
    
    tempcurrentprice = close 
    if beginprice < tempcurrentprice and box * reversal <= math.abs(beginprice - tempcurrentprice) and nok //new column
        numcell = math.floor(math.abs(beginprice - tempcurrentprice) / box)
        openprice := beginprice + box
        closeprice := beginprice + numcell * box
        trend := 1
        beginprice := closeprice
    else
        openprice := openprice == 0 ? nz(openprice[1]) : openprice
        closeprice := closeprice == 0 ? nz(closeprice[1]) : closeprice
else        
    if trend == 1
        nok = true
        if beginprice < currentprice and box <= math.abs(beginprice - currentprice)
            numcell = math.floor(math.abs(beginprice - currentprice) / box)
            closeprice := beginprice + numcell * box
            trend := 1
            beginprice := closeprice
            nok := false
        else
            openprice := openprice == 0 ? nz(openprice[1]) : openprice
            closeprice := closeprice == 0 ? nz(closeprice[1]) : closeprice
    
        tempcurrentprice = close
        if beginprice > tempcurrentprice and box * reversal <= math.abs(beginprice - tempcurrentprice) and nok //new column
            numcell = math.floor(math.abs(beginprice - tempcurrentprice) / box)
            openprice := beginprice - box
            closeprice := beginprice - numcell * box
            trend := -1
            beginprice := closeprice
        else
            openprice := openprice == 0 ? nz(openprice[1]) : openprice
            closeprice := closeprice == 0 ? nz(closeprice[1]) : closeprice    


box := ta.change(closeprice) ?  mode == 'ATR' ? atrboxsize :  boxsize : box


//Ichimoku//
TenkenSenL = input.int(9, minval=1, title='Tenken Sen Length')
KijunSenL = input.int(26, minval=1, title='Kijun Sen Length')
SenkouBL = input.int(52, minval=1, title='Senkou B Length')
chikouoffset = input.int(26, minval=1, title='Chikou Span Offset')


donchian(src, len) => math.avg(ta.lowest(src,len), ta.highest(src, len))
TenkenSen = donchian(closeprice, TenkenSenL)
KijunSen= donchian(closeprice,KijunSenL)
SenkouA = math.avg(TenkenSen, KijunSen)
SenkouB = donchian(closeprice, SenkouBL)

//Plots//
plot(TenkenSen, color=color.blue, title='Tenken Sen')
plot(KijunSen, color=#B71C1C, title='Kijun Sen')
plotshape(ta.crossover(TenkenSen,KijunSen), title = 'Tenken/Kijun Crossover', style= shape.cross, color=color.blue, size= size.small, location= location.bottom)
plotshape(ta.crossunder(TenkenSen,KijunSen), title = 'Tenken/Kijun Crossunder', style= shape.cross, color=color.red, size = size.small, location= location.bottom)
plot(closeprice, offset = -chikouoffset + 1, color=color.new(color.gray,50), title='Chikou Span')
p1 = plot(SenkouA, offset = chikouoffset - 1, color=color.lime,
	 title='Senkou A')
p2 = plot(SenkouB, offset = chikouoffset - 1, color=color.red,
	 title='Senkou B')
fill(p1, p2, color = SenkouA > SenkouB ? color.new(color.lime,90) : color.new(color.red, 90))
plotshape(ta.crossover(SenkouA,SenkouB), offset= chikouoffset -1, title = 'Bullish Kumo Twist', style= shape.arrowup, color=color.green, size= size.normal, location= location.bottom)
plotshape(ta.crossunder(SenkouA,SenkouB), offset= chikouoffset -1, title = 'Bearish Kumo Twist', style= shape.arrowdown, color=color.red, size = size.normal, location= location.bottom)