Custom Candle Body Width
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © a_dema

//@version=5

indicator(title="Custom Candle Body Width", shorttitle="CCBW", overlay=true, max_lines_count=500)

candleBodyColor_Up = input.color(defval=color.new(color.green, 0), title="Color Up", group="Candle Body", inline="010")
candleBodyColor_Dn = input.color(defval=color.new(color.red, 0), title="Down", group="Candle Body", inline="010")
candleBodyWidth = input.int(defval=6, title="Fixed Width (pixels)", minval=1, maxval=500, group="Candle Body", inline="020")
mintickRangeDivisor = input.int(defval=10, title="Minimum tick division", minval=1
  , tooltip="Without this, the candle bodies for same open/close candles do not display at all.\n\nIf a symbol moves in 0.01 ticks, and a 10 value is specified here, this will plot a 0.001 range body."
  , group="Candle Body", inline="040")
barCount = input.int(defval=100, title="Bar Count (1-500)", minval=1, maxval=500, group="Range/Scope"
  , tooltip="The number of bars this indicator will apply to.\n\nThe more bars, the more processing this indicator needs to do. Size appropriately."
  , inline="060")
candleBodyColor_Other = input.bool(defval=false, title="Enabled?"
  , tooltip="In lieu of no TV function to read the current bar color, this indicator can reference another indicator's color value (if implemented).\n\nRefer to sample code in comments. Very similar code to that of barcolor()."
  , group="Source Body Color From Other Indicator", inline="070")
candleBodyColor_Source = input.source(defval=close, title="Source"
  , tooltip="Referenced plot must be in RGBT format (3 digits each, 0-padded)\n\nFor example fuchsia @ 75% transparency would be 224064251075.\n\nRefer to sample code in comments."
  , group="Source Body Color From Other Indicator", inline="080")



barcolor(color=bar_index > last_bar_index - barCount ? color.new(color.white, 100) : na, display=display.all)



// *****************************************************************************************************
// Sample code to generate a float series for the RGBT value, and plot to data-window-only (not visible on chart)
// Copy/paste the below into the other indicator, uncomment (obviously!), and adjust as needed

// // Populate color series with up/down bar logic
// color barcolor_series = close >= open ? color.new(color.fuchsia, 77) : color.new(color.orange, 88)

// // Build string of RGBT values (3 digits each, 0-padded) and convert to float
// float barcolor_rgbt = str.tonumber(str.tostring(color.r(barcolor_series), "000")
//   + str.tostring(color.g(barcolor_series), "000")
//   + str.tostring(color.b(barcolor_series), "000")
//   + str.tostring(color.t(barcolor_series), "000"))

// plot(barcolor_rgbt, title="Barcolor RGBT", display=display.data_window)
// *****************************************************************************************************



int rgb_t = na
int rgb_b = na
int rgb_g = na
int rgb_r = na

if candleBodyColor_Other
    rgb_t := int( ( candleBodyColor_Source/1000 - int(candleBodyColor_Source/1000) )                   * 1000)
    rgb_b := int( ( candleBodyColor_Source/1000000 - int(candleBodyColor_Source/1000000) )             * 1000)
    rgb_g := int( ( candleBodyColor_Source/1000000000 - int(candleBodyColor_Source/1000000000) )       * 1000)
    rgb_r := int( ( candleBodyColor_Source/1000000000000 - int(candleBodyColor_Source/1000000000000) ) * 1000)



if bar_index > last_bar_index - barCount

    line.new(x1=time
      , y1=close == open ? open - syminfo.mintick / mintickRangeDivisor : open
      , x2=time
      , y2=close == open ? close + syminfo.mintick / mintickRangeDivisor : close
      , xloc=xloc.bar_time
      , extend=extend.none
      , color=candleBodyColor_Other == false ? close >= open ? candleBodyColor_Up : candleBodyColor_Dn : color.rgb(rgb_r, rgb_g, rgb_b, rgb_t)
      , style=line.style_solid
      , width=candleBodyWidth)



plot(candleBodyColor_Other ? candleBodyColor_Source : na, display=display.data_window)


