Renko Emulator - Rev NR - Released - 12-29-22
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © hockeydude84

//@version=5
indicator("Renko Emulator - Rev NR - Released - 12-29-22", overlay=true)

//Renko Emulator - Rev NR - Released 12-29-22
//By Hockeydude84
//Emulates Renko behavior on a standard candle stick chart. Proides "brick" options and inhibiting, with various plotting options. 

//inputs for emulator
var emulator = "Renko Emulator Settings"
var emulatorInputs = "Renko Emulator Inputs"
var emulatorPlots = "Emulator Plot Settings"
sourceBrick = input(close, "Brick Source (Default is Close)", group = emulator)
brickType =input(true, "Choose Brick Size - Default (True) is Standard Bricks; False is ATR", group = emulator)
atrInhib = input(false, "For ATR Emulator Settings - Use 'Standard Brick Size (in Percent)' as an Inhibitor", group = emulator)
standardBrick = input.float(1, "Standard Brick Size (In Percent)", minval=0, step=0.1, maxval=100, group = emulatorInputs)/100
atrLength = input.int(100, "ATR Length for Renko (Default = 100)", minval=1, step=1, group = emulatorInputs)
atrMod = input.int(5, "ATR Modifier (Default = 5)", minval=1, step=1, group = emulatorInputs)
useBrickPlots =input(true, "Plot Emulated Bricks", group = emulatorPlots)
useFill =input(true, "Fill between Emulated Bricks", group = emulatorPlots)
useSmoothingPlots = input(false, "Plot 'Smoothed' Emulated Bricks (Helpful with determining trends)", group = emulatorPlots)
smoothingLength = input.int(7, "Smoothing Length", minval=1, group = emulatorPlots)

//Get data for the ATR
atrGet = ta.atr(atrLength)
atrHigh = ta.highest(atrGet,atrLength)
atrLow = ta.lowest(atrGet, atrLength)

//Calculate ATR valuesand if we're doing any inhibiting
atrBrick = ((atrHigh-atrLow)/sourceBrick) *atrMod 
atrBrickInhib = atrBrick > standardBrick ? atrBrick : standardBrick
atrBrickFinal = atrInhib==false ? atrBrick : atrBrickInhib
brick = brickType == true ? standardBrick : atrBrickFinal

//initialize variables for emulated Renko Blocks/sections.... whatever we're gonna call them
float baseValue = sourceBrick
var updateBase = 1

//determine base (center) and upper/lower extremes
baseValue := updateBase==1 ? sourceBrick : baseValue[1]
brickUpper = baseValue*(1+brick)
brickLower = baseValue*(1-brick)

//determine if candle values put us above/below limits, and update if we break range
calculateUpper = sourceBrick > brickUpper ? 1 : 0
calculateLower = sourceBrick < brickLower ? -1 : 0
updateBase := calculateUpper==1 or calculateLower==-1 ? 1 : 0

//Generate Moving Averages to help define trend
baseMA= ta.vwma(baseValue, smoothingLength)
upperMA = ta.vwma(brickUpper, smoothingLength)
lowerMA = ta.vwma(brickLower, smoothingLength)

//Plot our emulated renko plots
plot(useBrickPlots == true ? baseValue : na, color=color.aqua, linewidth=1)
plot(useBrickPlots == true and useFill==false ? brickUpper : na, color=color.lime, linewidth=1)
plot(useBrickPlots == true and useFill==false ? brickLower : na, color=color.fuchsia, linewidth=1)
U = plot(useBrickPlots == true and useFill==true  ? brickUpper : na, color=color.black, linewidth=1)
L = plot(useBrickPlots == true and useFill==true ? brickLower : na, color=color.black, linewidth=1)

//fill plotting for emulated bricks
var getFillColor = 0
getFillColor :=  baseValue > baseValue[1] ? 1 :  baseValue < baseValue[1] ? -1 : getFillColor[1]
fillColor = getFillColor == 1 ? color.lime : getFillColor ==-1 ? color.fuchsia : color.orange //theres probably a cleaner way to perform this with the [1] and colors, but I dont know it and I'm lazy
fill(U, L, color=fillColor)

//plot smoothed versions of emulator plots
plot(useSmoothingPlots == true ? baseMA : na, color=color.blue, linewidth=2)
plot(useSmoothingPlots == true ? upperMA : na, color=color.green, linewidth=2)
plot(useSmoothingPlots == true ? lowerMA : na, color=color.red, linewidth=2)

//EOF - Done baby :P


