Range Bound Channel Index (RBCI) w/ Expanded Source Types [Loxx]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © loxx

//@version=5
indicator("Range Bound Channel Index (RBCI) [Loxx]", 
     shorttitle = "RBCI [Loxx]", 
     overlay = false, 
     timeframe="", 
     timeframe_gaps = true)
     
import loxx/loxxexpandedsourcetypes/3

greencolor = #2DD204
redcolor = #D2042D 

lightgreencolor = #96E881 
lightredcolor =  #DF4F6C

smthtype = input.string("Kaufman", "Heiken - Ashi Better Smoothing", options = ["AMA", "T3", "Kaufman"], group=  "Source Settings")
srcoption = input.string("Close", "Source", group= "Source Settings", 
     options = 
     ["Close", "Open", "High", "Low", "Median", "Typical", "Weighted", "Average", "Average Median Body", "Trend Biased", "Trend Biased (Extreme)", 
     "HA Close", "HA Open", "HA High", "HA Low", "HA Median", "HA Typical", "HA Weighted", "HA Average", "HA Average Median Body", "HA Trend Biased", "HA Trend Biased (Extreme)",
     "HAB Close", "HAB Open", "HAB High", "HAB Low", "HAB Median", "HAB Typical", "HAB Weighted", "HAB Average", "HAB Average Median Body", "HAB Trend Biased", "HAB Trend Biased (Extreme)"])

BBPeriod= input.int(100, "Bollinger Bands Period", group = "Basic Settings")   
mult1 = input.int(1, "BB Multiplier 1", group = "Basic Settings")   
mult2 = input.int(2, "BB Multiplier 2", group = "Basic Settings")   



colorbars = input.bool(false, "Color bars?", group= "UI Options")

kfl=input.float(0.666, title="* Kaufman's Adaptive MA (KAMA) Only  -  Fast End", group = "Moving Average Inputs")
ksl=input.float(0.0645, title="* Kaufman's Adaptive MA (KAMA) Only  -  Slow End", group = "Moving Average Inputs")
amafl = input.int(2, title="* Adaptive Moving Average (AMA) Only  -  Fast", group = "Moving Average Inputs")
amasl = input.int(30, title="* Adaptive Moving Average (AMA) Only  -  Slow", group = "Moving Average Inputs")

haclose = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, close)
haopen = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, open)
hahigh = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, high)
halow = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, low)
hamedian = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, hl2)
hatypical = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, hlc3)
haweighted = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, hlcc4)
haaverage = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, ohlc4)


float src = switch srcoption
	"Close" => loxxexpandedsourcetypes.rclose()
	"Open" => loxxexpandedsourcetypes.ropen()
	"High" => loxxexpandedsourcetypes.rhigh()
	"Low" => loxxexpandedsourcetypes.rlow()
	"Median" => loxxexpandedsourcetypes.rmedian()
	"Typical" => loxxexpandedsourcetypes.rtypical()
	"Weighted" => loxxexpandedsourcetypes.rweighted()
	"Average" => loxxexpandedsourcetypes.raverage()
    "Average Median Body" => loxxexpandedsourcetypes.ravemedbody()
	"Trend Biased" => loxxexpandedsourcetypes.rtrendb()
	"Trend Biased (Extreme)" => loxxexpandedsourcetypes.rtrendbext()
	"HA Close" => loxxexpandedsourcetypes.haclose(haclose)
	"HA Open" => loxxexpandedsourcetypes.haopen(haopen)
	"HA High" => loxxexpandedsourcetypes.hahigh(hahigh)
	"HA Low" => loxxexpandedsourcetypes.halow(halow)
	"HA Median" => loxxexpandedsourcetypes.hamedian(hamedian)
	"HA Typical" => loxxexpandedsourcetypes.hatypical(hatypical)
	"HA Weighted" => loxxexpandedsourcetypes.haweighted(haweighted)
	"HA Average" => loxxexpandedsourcetypes.haaverage(haaverage)
    "HA Average Median Body" => loxxexpandedsourcetypes.haavemedbody(haclose, haopen)
	"HA Trend Biased" => loxxexpandedsourcetypes.hatrendb(haclose, haopen, hahigh, halow)
	"HA Trend Biased (Extreme)" => loxxexpandedsourcetypes.hatrendbext(haclose, haopen, hahigh, halow)
	"HAB Close" => loxxexpandedsourcetypes.habclose(smthtype, amafl, amasl, kfl, ksl)
	"HAB Open" => loxxexpandedsourcetypes.habopen(smthtype, amafl, amasl, kfl, ksl)
	"HAB High" => loxxexpandedsourcetypes.habhigh(smthtype, amafl, amasl, kfl, ksl)
	"HAB Low" => loxxexpandedsourcetypes.hablow(smthtype, amafl, amasl, kfl, ksl)
	"HAB Median" => loxxexpandedsourcetypes.habmedian(smthtype, amafl, amasl, kfl, ksl)
	"HAB Typical" => loxxexpandedsourcetypes.habtypical(smthtype, amafl, amasl, kfl, ksl)
	"HAB Weighted" => loxxexpandedsourcetypes.habweighted(smthtype, amafl, amasl, kfl, ksl)
	"HAB Average" => loxxexpandedsourcetypes.habaverage(smthtype, amafl, amasl, kfl, ksl)
    "HAB Average Median Body" => loxxexpandedsourcetypes.habavemedbody(smthtype, amafl, amasl, kfl, ksl)
	"HAB Trend Biased" => loxxexpandedsourcetypes.habtrendb(smthtype, amafl, amasl, kfl, ksl)
	"HAB Trend Biased (Extreme)" => loxxexpandedsourcetypes.habtrendbext(smthtype, amafl, amasl, kfl, ksl)
	=> haclose

RBCI =  - 35.524181940 * 
     src[0] - 29.333989650 * 
     src[1] - 18.427744960 * 
     src[2] - 5.3418475670 * 
     src[3] + 7.0231636950 * 
     src[4] + 16.176281560 * 
     src[5] + 20.656621040 * 
     src[6] + 20.326611580 * 
     src[7] + 16.270239060 * 
     src[8] + 10.352401270 * 
     src[9] + 4.5964239920 * 
     src[10] + 0.5817527531 * 
     src[11] - 0.9559211961 * 
     src[12] - 0.2191111431 * 
     src[13] + 1.8617342810 * 
     src[14] + 4.0433304300 * 
     src[15] + 5.2342243280 * 
     src[16] + 4.8510862920 * 
     src[17] + 2.9604408870 * 
     src[18] + 0.1815496232 * 
     src[19] - 2.5919387010 * 
     src[20] - 4.5358834460 * 
     src[21] - 5.1808556950 * 
     src[22] - 4.5422535300 * 
     src[23] - 3.0671459820 * 
     src[24] - 1.4310126580 * 
     src[25] - 0.2740437883 * 
     src[26] + 0.0260722294 * 
     src[27] - 0.5359717954 * 
     src[28] - 1.6274916400 * 
     src[29] - 2.7322958560 * 
     src[30] - 3.3589596820 * 
     src[31] - 3.2216514550 * 
     src[32] - 2.3326257940 * 
     src[33] - 0.9760510577 * 
     src[34] + 0.4132650195 * 
     src[35] + 1.4202166770 * 
     src[36] + 1.7969987350 * 
     src[37] + 1.5412722800 * 
     src[38] + 0.8771442423 * 
     src[39] + 0.1561848839 * 
     src[40] - 0.2797065802 * 
     src[41] - 0.2245901578 * 
     src[42] + 0.3278853523 * 
     src[43] + 1.1887841480 * 
     src[44] + 2.0577410750 * 
     src[45] + 2.6270409820 * 
     src[46] + 2.6973742340 * 
     src[47] + 2.2289941280 * 
     src[48] + 1.3536792430 * 
     src[49] + 0.3089253193 * 
     src[50] - 0.6386689841 * 
     src[51] - 1.2766707670 * 
     src[52] - 1.5136918450 * 
     src[53] - 1.3775160780 * 
     src[54] - 1.6156173970 * 
     src[55]
     
avg = ta.sma(RBCI, BBPeriod)
dev = ta.stdev(RBCI, BBPeriod)

up1 = avg + dev * mult1
up2 = avg + dev * mult2

dn1 = avg - dev * mult1
dn2 = avg - dev * mult2

colorout = -RBCI >= - avg ? greencolor : redcolor

plot(-RBCI, color = colorout, linewidth = 2) 
plot(-up1, color = bar_index % 2 ? color.gray : na, linewidth = 1) 
plot(-up2, color = bar_index % 2 ? color.gray : na, linewidth = 2) 

plot(-dn1, color = bar_index % 2 ? color.gray : na, linewidth = 1) 
plot(-dn2, color = bar_index % 2 ? color.gray : na, linewidth = 2) 

ld = plot(-avg, color = color.white)

barcolor(colorbars ? colorout : na)




     