+ Average Candle Bodies Range
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ClassicScott

// ACBR, or, Average Candle Bodies Range is a volatility and momentum indicator designed to indicate periods of increasing volatility and/or
// momentum. The genesis of the idea formed from my pondering what a trend trader is really looking for in terms of a volatility indicator. Most
// indicators I've come across haven't, in my opinion, done a satisfactory job of highlighting this. I kept thinking about the ATR (I use it for
// stops and targets) but I realized I didn't care about highs or lows in regards to a candle's volatility or momentum, nor do I care about their
// relation to a previous close. What really matters to me is candle body expansion. That is all. So, I created this.

// ACBR is extremely simple at its heart. I made it more complicated of course, because why would I want anything for myself to be simple?
// Originally it was envisaged to be a simple volatility indicator highlighting areas of increasing and decreasing volatility. Then I decided
// some folks might want an indicator that could show this in a directional manner, i.e., an oscillator, so I spent some more hours tackling that

// To start, the original version of the indicator simply subtracts opening price from closing price if the candle closes above the open, and
// subtracts the close from the open if the candle closes below the open. This way we get a positive number that simply measures candle expansion.
// We then apply a moving average to these values in order to smooth them (if you want). To get an oscillator we always subtract the close from
// the open, thus when a candle closes below its open we get a negative number.

// I've naturally added an optional signal line as a helpful way of gauging volatility because obviously the values themselves may not tell you
// much. But I've also added something that I call a baseline. You can use this in a few ways, but first let me explain the two options for how
// the baseline can be calculated. And what do I mean by 'baseline?' I think of it as an area of the indicator where if the ACBR is below you will
// not want to enter into any trades, and if the ACBR is above then you are free to enter trades based on your system (or you might want to enter
// in areas of low volatility if your system calls for that). Waddah Attar Explosion is another indicator that implements something similar.
// The baseline is calculated in two different ways: one of which is making a Donchian Channel of the ACBR, and then using the basis as the
// baseline, while the other is applying an RMA to the cb_dif, which is the base unit that makes up the ACBR. Now, the basis of a Donchian Channel
// typically is the average of the highs and the lows. If we did that here we would have a baseline much too high (but maybe not...), however,
// I've made the divisor user adjustable. In this way you can adjust the height (or I guess you might say 'width' if it's an oscillator) however
// you like, thus making the indicator more or less sensitive. In the case of using the ACBR as the baseline we apply a multiplier to the values
// in order to adjust the height. Apologies if I'm being overly verbose. If you want to skip all of this I have tooltips in the settings for all of
// the inputs that I think need an explanation.

// When using the indicator as an oscillator there are baselines above and below the zero line. One funny thing: if using the ACBR as calculation
// type for the baselines in oscillator mode, the baselines themselves will oscillate around the zero line. There is no way to fix this due to
// the calculation. That isn't necessarily bad (based on my eyeball test), but I probably wouldn't use it in such a way. But experiment! They
// could actually be a very fine entry or confirmation indicator. And while I'm on the topic of confirmation indicators, using this indicator as
// an oscillator naturally makes it a confirmation indicator. It just happens to have a volatility measurement baked into it. It may also be used
// as an exit and continuation indicator. And speaking of these things, there are optional shapes for indicating when you might want to exit or
// take a continuation trade. I've added alerts for these things too.

// Lastly, oscillator mode is good for identifying divergences.


// ****** \\
// *** A note on colors in the style tab
// I truly do not understand why TradingView has every single color grouped together under (nearly) every single adjustable element of the indicator.
// It makes no sense to me because only certain colors apply to certain aspects of it. I'm going to help you out here by giving you a cheat sheet
// so you can make color changes more easily.
// Colors 0 and 1 apply to the ACBR plot.
// Colors 2, 3, and 4 apply to the background coloring.
// Colors 5 and 6 apply to advancing and declining volatility (bar color for crosses)
// Colors 7, 8, and 9 apply to candle colors


//@version=5
indicator("+ Average Candle Bodies Range", shorttitle='+ AVG CBR', format=format.inherit , timeframe='', timeframe_gaps=true)

////INPUTS\\\\

//ACBR INPUTS
average_type = input.string(defval='EMA', options=['EMA', 'HMA', 'RMA', 'SMA', 'VWMA', 'WMA'], title='Smoothing Type', group='Candle Bodies Inputs')
period = input.int(defval=4, title='Period', group='Candle Bodies Inputs')

//SIGNAL LINE INPUTS
signal_type = input.string(defval='HMA', options=['EMA', 'HMA', 'RMA', 'SMA', 'VWMA', 'WMA'], title='Signal Type', group='Signal Line Inputs')
signal_period = input.int(defval=14, title='Period', group='Signal Line Inputs')

//BASELINE INPUTS
bline_type = input.string(defval='Donchian', options=['Donchian', 'ACBR'], title='Baseline Calculation Type', group='Baseline Inputs', tooltip='How the baseline is calculated to determine volatility is either via a Donchian Channel, whereby the highest and lowest of the average range is calculated over a determined period, and the basis is used as the baseline (this can be adjusted with the height input), or the Average Candle Bodies Range, which is adjusted by its own height input. If you are using this as an oscillator I dont recommend using the ACBR as a baseline because of the way it calculates, however, if you do try to use it this way at least have signal line momentum turned on.')
vol_period_type = input.string(defval='RexDog Self-Adjusting', options=['RexDog Self-Adjusting', 'User Selected'], title='Baseline Period Type', group='Baseline Inputs', tooltip='RexDog Self-Adjusting is based on the RexDog Average by xkavalis. It is an average of six different averages, thus it is dynamic, or self-adjusting, and can not be changed by the user. If you want to be able to choose an averaging period yourself select User Selected.')
vol_period = input.int(defval=200, title='Period', group='Baseline Inputs', tooltip='You can ignore this if using the RexDog BPT above, otherwise, a smaller number is going to be more reactive to changes in volatility, whereas a larger number will not. There is no right or wrong answer here.')
divisor = input.float(defval=3, title='Donchian Calculated Height', minval=0.1, step=0.1, group='Baseline Inputs', tooltip='This adjusts the height of your baseline, and, if using the RexDog BPT, is the only way to adjust the sensitivity. A smaller number makes for a baseline farther away from zero. Probably want to stay above two. Three to three and a half is a good range to start.')
cb_mult = input.float(defval=1, title='ACBR Calculated Height', minval=0.1, step=0.1, group='Baseline Inputs', tooltip='Same as above but for two things. One: contrary to the Donchian height, a larger number increases the distance of the baseline from zero (I recommend no greater than 1 in non-directional mode, and even that, in my opinion, is too high). Two: if using as an oscillator, the baseline period plays heavily into how you will adjust the height. Larger numbers like 200 need a larger height, like 3. However, as stated in the above first tooltip, I dont recommend it being used this way.')

//GENERAL OPTIONS
signal_rise_fall = input.bool(defval=true, title='Require Increasing Momentum of Signal Line', group='General Options', tooltip='Require that the signal line be rising in non-directional mode, or, when in oscillator mode, rising when the ACBR is above zero and falling when it is below zero to highlight increasing volatility. Remember, a moving average (which is what a signal line is) is a measure of the momentum of the underlying source, in this case volatility. In oscillator mode a falling signal line would be defined as increasing in momentum once the ACBR crosses below zero.')
avg_range_style = input.bool(defval=false, title='ACBR is Directional', group='General Options', tooltip='If checked the ACBR oscillates around a zero line indicating bullish or bearish volatility. If unchecked it is a measure of market volatility only, and does not indicate a direction to trade in.')

//BAR COLOR AND BACKGROUND COLOR SIGNAL LINE OPTIONS
bg_signal = input.bool(defval=false, title='Color Background', group='Signal Line Options', tooltip='Colors background if the ACBR is beyond the signal line.')
barcolor_signal = input.bool(defval=false, title='Color Bars', group='Signal Line Options', tooltip='Colors bars if the ACBR is beyond the signal line.')
strong_signal = input.bool(false, 'Volatility Advancing', group='Signal Line Options', tooltip='Colors the bar when the ACBR crosses above the signal line in non-directional mode. Colors the bar when the ACBR crosses above the signal line when above zero or below the signal line when below zero in oscillator mode.')
weak_signal = input.bool(false, 'Volatility Declining', group='Signal Line Options', tooltip='Colors the bar when the ACBR crosses below the signal line when in non-directional mode. Colors the bar when the ACBR crosses below the signal line when above zero or above the signal line when below zero in oscillator mode.')

//BAR COLOR AND BACKGROUND COLOR BASELINE OPTIONS
bg_bline = input.bool(defval=false, title='Color Background', group='Baseline Options', tooltip='Colors background if the ACBR is beyond the baseline(s).')
barcolor_bline = input.bool(defval=true, title='Color Bars', group='Baseline Options', tooltip='Colors bars if the ACBR is beyond the baseline(s).')
strong_bline = input.bool(false, 'Volatility Advancing', group='Baseline Options', tooltip='Colors the bar when the ACBR crosses above the baseline in non-directional mode. Colors the bar when the ACBR crosses above the upper baseline or below the lower baseline when in oscillator mode.')
weak_bline = input.bool(false, 'Volatility Declining', group ='Baseline Options', tooltip='Colors the bar when the ACBR crosses below the baseline in non-directional mode. Colors the bar when the ACBR crosses below the upper baseline or above the lower baseline when in oscillator mode.')


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


////CANDLE BODY CALCULATION
float cb_dif = 0.0
if not avg_range_style and close > open 
    cb_dif := close - open
else if not avg_range_style and close < open
    cb_dif := open - close
else if avg_range_style
    cb_dif := close - open


////MOVING AVERAGE TO USE TO CREATE THE AVERAGE
cb_range(type, src, period) =>
    float result = 0
    if type == 'EMA'
        result := ta.ema(cb_dif, period)
    if type == 'HMA'
        result := ta.hma(cb_dif, period)
    if type == 'RMA'
        result := ta.rma(cb_dif, period)
    if type == 'SMA'
        result := ta.sma(cb_dif, period)
    if type == 'VWMA'
        result := ta.vwma(cb_dif, period)
    if type == 'WMA'
        result := ta.wma(cb_dif, period)
    result

avg_range = cb_range(average_type, cb_dif, period)


////SIGNAL LINE CHOICES
signal(type, src, signal_period) =>
    float result = 0
    if type == 'EMA'
        result := ta.ema(avg_range, signal_period)
    if type == 'HMA'
        result := ta.hma(avg_range, signal_period)
    if type == 'RMA'
        result := ta.rma(avg_range, signal_period)
    if type == 'SMA'
        result := ta.sma(avg_range, signal_period)
    if type == 'VWMA'
        result := ta.vwma(avg_range, signal_period)
    if type == 'WMA'
        result := ta.wma(avg_range, signal_period)
    result

signal_line = signal(signal_type, avg_range, signal_period)


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


////CALCULATIONS FOR BASELINE VOLATILITY
//DONCHIAN CHANNEL
highest_baseline = vol_period_type == 'RexDog Self-Adjusting' ? (ta.highest(avg_range, 5) + ta.highest(avg_range, 9) + ta.highest(avg_range, 24) + ta.highest(avg_range, 50) + ta.highest(avg_range, 100) + ta.highest(avg_range, 200)) / 6 : ta.highest(avg_range, vol_period)
lowest_baseline = vol_period_type == 'RexDog Self-Adjusting' ? (ta.lowest(avg_range, 5) + ta.lowest(avg_range, 9) + ta.lowest(avg_range, 24) + ta.lowest(avg_range, 50) + ta.lowest(avg_range, 100) + ta.lowest(avg_range, 200)) / 6 : ta.lowest(avg_range, vol_period)

//ACBR
acbr_baseline = vol_period_type == 'RexDog Self-Adjusting' ? (ta.rma(cb_dif, 5) + ta.rma(cb_dif, 9) + ta.rma(cb_dif, 24) + ta.rma(cb_dif, 50) + ta.rma(cb_dif, 100) + ta.rma(cb_dif, 200)) / 6 : ta.rma(cb_dif, vol_period)

//COMPLETE
high_baseline = bline_type == 'Donchian' and avg_range_style ? highest_baseline / divisor : bline_type == 'Donchian' and not avg_range_style ? (highest_baseline + lowest_baseline) / divisor : bline_type == 'ACBR' ? acbr_baseline * cb_mult : na
low_baseline = bline_type == 'Donchian' and avg_range_style ? lowest_baseline / divisor : bline_type == 'ACBR' ? acbr_baseline * cb_mult * -1 : na


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


////ACBR COLOR
avg_range_color = if not avg_range_style and avg_range > avg_range[1]
    color.new(#ff9800, 0)
else if not avg_range_style and avg_range < avg_range[1]
    color.new(#2962ff, 0)
else if avg_range > 0 and avg_range > avg_range[1] and avg_range_style
    color.new(#ff9800, 0)
else if avg_range > 0 and avg_range < avg_range[1] and avg_range_style
    color.new(#2962ff, 0)
else if avg_range < 0 and avg_range < avg_range[1] and avg_range_style
    color.new(#ff9800, 0)
else if avg_range < 0 and avg_range > avg_range[1] and avg_range_style
    color.new(#2962ff, 0)


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


////PLOTS
plot(avg_range, title='Average Candle Bodies Range', color=avg_range_color, linewidth=2, style=plot.style_histogram)
plot(signal_line, title='Signal Line', color=signal_line > 0 and signal_line > signal_line[1] or signal_line < 0 and signal_line < signal_line[1] ? color.new(#00bcd4, 0)  : signal_line < 0 and signal_line > signal_line[1] or signal_line > 0 and signal_line < signal_line[1] ? color.new(#e91e63, 0) : na , linewidth=2, style=plot.style_line)
plot(high_baseline, title='High Baseline', color=color.yellow, style=plot.style_line)
plot(avg_range_style ? low_baseline : na, title='Low Baseline', color=color.yellow, style=plot.style_line)


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


//SIGNAL LINE RISING/FALLING -- SEE signal_rise_fall UNDER GENERAL OPTIONS
signal_rising = signal_rise_fall and signal_line > signal_line[1]
signal_falling = signal_rise_fall and signal_line < signal_line[1]


/////////////////////////////////////////////////////////////////////////////////////
////BACKGROUND COLOR CONDITIONS AND COLORING


//DIRECTIONAL
dir_above_bline = bg_bline and avg_range_style and high_baseline > 0 and avg_range > high_baseline or bg_bline and low_baseline > 0 and avg_range > low_baseline
dir_below_bline = bg_bline and avg_range_style and low_baseline < 0 and avg_range < low_baseline or bg_bline and high_baseline < 0 and avg_range < high_baseline
dir_above_signal = bg_signal and avg_range_style and avg_range > 0 and avg_range > signal_line
dir_below_signal = bg_signal and avg_range_style and avg_range < 0 and avg_range < signal_line


//NON-DIRECTIONAL
above_bline = bg_bline and not avg_range_style and avg_range > high_baseline
below_bline = bg_bline and not avg_range_style and avg_range < high_baseline
above_signal = bg_signal and not avg_range_style and avg_range > signal_line
below_signal = bg_signal and not avg_range_style and avg_range < signal_line


//ABOVE BASELINE
AboveBaselineStrongSignal = if (signal_rising and above_bline)
    color.new(#00bcd4, 50)
else if (signal_rising and below_bline or signal_falling and below_bline)
    color.new(#e91e63, 100)
else if (signal_rising and dir_above_bline)
    color.new(#00bcd4, 50)
else if (signal_falling and dir_below_bline)
    color.new(#e91e63, 50)

AboveBaseline = if (not signal_rise_fall and above_bline)
    color.new(#00bcd4, 50)
else if (not signal_rise_fall and below_bline)
    color.new(#e91e63, 100)
else if (not signal_rise_fall and dir_above_bline)
    color.new(#00bcd4, 50)
else if (not signal_rise_fall and dir_below_bline)
    color.new(#e91e63, 50)

//ABOVE SIGNAL LINE
AboveSignalLineStrongSignal = if (signal_rising and above_signal)
    color.new(#00bcd4, 50)
else if (signal_rising and below_signal or signal_falling and below_signal)
    color.new(#e91e63, 100)
else if (signal_rising and dir_above_signal)
    color.new(#00bcd4, 50)
else if (signal_falling and dir_below_signal)
    color.new(#e91e63, 50)

AboveSignalLine = if (not signal_rise_fall and above_signal)
    color.new(#00bcd4, 50)
else if (not signal_rise_fall and below_signal)
    color.new(#e91e63, 100)
else if (not signal_rise_fall and dir_above_signal)
    color.new(#00bcd4, 50)
else if (not signal_rise_fall and dir_below_signal)
    color.new(#e91e63, 50)


bgcolor(AboveBaselineStrongSignal, title='Background | ACBR > Baseline & Signal Rising')
bgcolor(AboveBaseline, title='Background | ACBR > Baseline')
bgcolor(AboveSignalLineStrongSignal, title='Background | ACBR > Signal Line & Signal Rising')
bgcolor(AboveSignalLine, title='Background | ACBR > Signal Line')


/////////////////////////////////////////////////////////////////////////////////////
////BAR COLOR CONDITIONS AND COLORING (ACBR CROSSING ABOVE OR BELOW BASELINE/SIGNAL LINE)


//DIRECTIONAL
dir_barcolor_strong_bline_up = strong_bline and avg_range_style and high_baseline > 0 and ta.crossover(avg_range, high_baseline) or strong_bline and avg_range_style and low_baseline > 0 and ta.crossover(avg_range, low_baseline)
dir_barcolor_strong_bline_down = strong_bline and avg_range_style and high_baseline < 0 and ta.crossunder(avg_range, high_baseline) or strong_bline and avg_range_style and low_baseline < 0 and ta.crossunder(avg_range, low_baseline)
dir_barcolor_weak_bline_up = weak_bline and avg_range_style and high_baseline > 0 and ta.crossunder(avg_range, high_baseline) or weak_bline and avg_range_style and low_baseline > 0 and ta.crossunder(avg_range, low_baseline)
dir_barcolor_weak_bline_down = weak_bline and avg_range_style and high_baseline < 0 and ta.crossover(avg_range, high_baseline) or weak_bline and avg_range_style and low_baseline < 0 and ta.crossover(avg_range, low_baseline)
dir_barcolor_strong_signal_up = strong_signal and avg_range_style and avg_range > 0 and ta.crossover(avg_range, signal_line)
dir_barcolor_strong_signal_down = strong_signal and avg_range_style and avg_range < 0 and ta.crossunder(avg_range, signal_line)
dir_barcolor_weak_signal = weak_signal and avg_range_style and avg_range > 0 and ta.crossunder(avg_range, signal_line) or weak_signal and avg_range_style and avg_range < 0 and ta.crossover(avg_range, signal_line)


//NON-DIRECTIONAL
barcolor_strong_bline = not avg_range_style and strong_bline and ta.crossover(avg_range, high_baseline)
barcolor_weak_bline = not avg_range_style and weak_bline and ta.crossunder(avg_range, high_baseline)
barcolor_strong_signal = not avg_range_style and strong_signal and ta.crossover(avg_range, signal_line)
barcolor_weak_signal = not avg_range_style and weak_signal and ta.crossunder(avg_range, signal_line)


BarColorBaselineCrossStrongSignal = if (signal_rising and barcolor_strong_bline)
    #ffeb3b
else if (barcolor_weak_bline)
    #673ab7
else if (signal_rising and dir_barcolor_strong_bline_up or signal_falling and dir_barcolor_strong_bline_down)
    #ffeb3b
else if (dir_barcolor_weak_bline_up or dir_barcolor_weak_bline_down)
    #673ab7


BarColorBaselineCross = if (not signal_rise_fall and barcolor_strong_bline)
    #ffeb3b
else if (not signal_rise_fall and barcolor_weak_bline)
    #673ab7
else if (not signal_rise_fall and dir_barcolor_strong_bline_up or not signal_rise_fall and dir_barcolor_strong_bline_down)
    #ffeb3b
else if (dir_barcolor_weak_bline_up or dir_barcolor_weak_bline_down)
    #673ab7

BarColorSignalLineCrossStrongSignal = if (signal_rising and barcolor_strong_signal)
    #ffeb3b
else if (barcolor_weak_signal)
    #673ab7
else if (signal_rising and dir_barcolor_strong_signal_up)
    #ffeb3b
else if (signal_falling and dir_barcolor_strong_signal_down)
    #ffeb3b
else if (dir_barcolor_weak_signal)
    #673ab7

BarColorSignalLineCross = if (not signal_rise_fall and barcolor_strong_signal)
    #ffeb3b
else if (not signal_rise_fall and barcolor_weak_signal)
    #673ab7
else if (not signal_rise_fall and dir_barcolor_strong_signal_up)
    #ffeb3b
else if (not signal_rise_fall and dir_barcolor_strong_signal_down)
    #ffeb3b
else if (dir_barcolor_weak_signal)
    #673ab7


barcolor(BarColorBaselineCrossStrongSignal, title='Bar Color | ACBR Baseline Cross & Signal Rising')
barcolor(BarColorBaselineCross, title='Bar Color | ACBR Baseline Cross ')
barcolor(BarColorSignalLineCrossStrongSignal, title='Bar Color | ACBR Signal Line Cross & Signal Rising')
barcolor(BarColorSignalLineCross, title='Bar Color | ACBR Signal Line Cross')


/////////////////////////////////////////////////////////////////////////////////////
////BAR COLOR CONDITIONS AND COLORING (ACBR ABOVE OR BELOW BASELINE/SIGNAL LINE) 


//DIRECTIONAL
dir_barcolor_above_bline = barcolor_bline and avg_range_style and high_baseline > 0 and avg_range > high_baseline or barcolor_bline and low_baseline > 0 and avg_range > low_baseline
dir_barcolor_below_bline = barcolor_bline and avg_range_style and low_baseline < 0 and avg_range < low_baseline or barcolor_bline and high_baseline < 0 and avg_range < high_baseline
dir_barcolor_neutral_bline = barcolor_bline and avg_range_style and avg_range < high_baseline and avg_range > low_baseline or barcolor_bline and avg_range_style and avg_range > high_baseline and avg_range < low_baseline or barcolor_bline and avg_range_style and signal_falling and avg_range > high_baseline or barcolor_bline and avg_range_style and signal_rising and avg_range < low_baseline
dir_barcolor_above_signal = barcolor_signal and avg_range_style and avg_range > 0 and avg_range > signal_line
dir_barcolor_below_signal = barcolor_signal and avg_range_style and avg_range < 0 and avg_range < signal_line
dir_barcolor_neutral_signal = barcolor_signal and avg_range_style and avg_range > 0 and avg_range < signal_line or barcolor_signal and avg_range_style and avg_range < 0 and avg_range > signal_line or barcolor_signal and avg_range_style and avg_range > 0 and avg_range > signal_line and signal_falling or barcolor_signal and avg_range_style and avg_range < 0 and avg_range < signal_line and signal_rising

//NON-DIRECTIONAL
barcolor_above_bline = barcolor_bline and not avg_range_style and avg_range > high_baseline
barcolor_below_bline = barcolor_bline and not avg_range_style and avg_range < high_baseline
barcolor_above_signal = barcolor_signal and not avg_range_style and avg_range > signal_line
barcolor_below_signal = barcolor_signal and not avg_range_style and avg_range < signal_line


//ABOVE BASELINE
BarColorAboveBaselineStrongSignal = if (signal_rising and barcolor_above_bline)
    color.new(#00bcd4, 0)
else if (signal_rising and barcolor_below_bline or signal_falling and barcolor_above_bline or signal_falling and barcolor_below_bline)
    color.new(#e91e63, 0)
else if (signal_rising and dir_barcolor_above_bline)
    color.new(#00bcd4, 0)
else if (signal_falling and dir_barcolor_below_bline)
    color.new(#e91e63, 0)
else if (dir_barcolor_neutral_bline)
    color.new(color.gray, 0)

BarColorAboveBaseline = if (not signal_rise_fall and barcolor_above_bline)
    color.new(#00bcd4, 0)
else if (not signal_rise_fall and barcolor_below_bline)
    color.new(#e91e63, 0)
else if (not signal_rise_fall and dir_barcolor_above_bline)
    color.new(#00bcd4, 0)
else if (not signal_rise_fall and dir_barcolor_below_bline)
    color.new(#e91e63, 0)
else if (dir_barcolor_neutral_bline)
    color.new(color.gray, 0)

//ABOVE SIGNAL LINE
BarColorAboveSignalLineStrongSignal = if (signal_rising and barcolor_above_signal)
    color.new(#00bcd4, 0)
else if (signal_rising and barcolor_below_signal or signal_falling and barcolor_above_signal or signal_falling and barcolor_below_signal)
    color.new(#e91e63, 0)
else if (signal_rising and dir_barcolor_above_signal)
    color.new(#00bcd4, 0)
else if (signal_falling and dir_barcolor_below_signal)
    color.new(#e91e63, 0)
else if (dir_barcolor_neutral_signal)
    color.new(color.gray, 0)

BarColorAboveSignalLine = if (not signal_rise_fall and barcolor_above_signal)
    color.new(#00bcd4, 0)
else if (not signal_rise_fall and barcolor_below_signal)
    color.new(#e91e63, 0)
else if (not signal_rise_fall and dir_barcolor_above_signal)
    color.new(#00bcd4, 0)
else if (not signal_rise_fall and dir_barcolor_below_signal)
    color.new(#e91e63, 0)    
else if (dir_barcolor_neutral_signal)
    color.new(color.gray, 0)


barcolor(BarColorAboveBaselineStrongSignal, title='Bar Color | ACBR > Baseline & Signal Rising')
barcolor(BarColorAboveBaseline, title='Bar Color | ACBR > Baseline')
barcolor(BarColorAboveSignalLineStrongSignal, title='Bar Color | ACBR > Signal Line & Signal Rising')
barcolor(BarColorAboveSignalLine, title='Bar Color | ACBR > Signal Line')


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


///CONTINUATION TRADE SYMBOLS
bull_cont_trade = avg_range_style and avg_range > 0 and avg_range[1] < signal_line and ta.crossover(avg_range, signal_line)
bear_cont_trade = avg_range_style and avg_range < 0 and avg_range[1] > signal_line and ta.crossunder(avg_range, signal_line)

plotshape(bull_cont_trade, title='Bullish Continuation', style=shape.arrowup, location=location.bottom, color=color.blue)
plotshape(bear_cont_trade, title='Bearish Continuation', style=shape.arrowdown, location=location.top, color=color.red)


////EXIT TRADE SYMBOLS
exit_long_quickly = signal_line > 0 and signal_line < signal_line[1] and signal_line[1] > signal_line[2]
exit_long_slowly = signal_line > 0 and ta.crossunder(avg_range, signal_line)
exit_short_quickly = signal_line < 0 and signal_line > signal_line[1] and signal_line[1] < signal_line[2]
exit_short_slowly = signal_line < 0 and ta.crossover(avg_range, signal_line)

plotshape(exit_short_quickly, title='Quick Exit Short', style=shape.xcross, location=location.bottom, color=color.blue)
plotshape(exit_short_slowly, title='Slow Exit Short', style=shape.xcross, location=location.bottom, color=color.blue)
plotshape(exit_long_quickly, title='Quick Exit Long', style=shape.xcross, location=location.top, color=color.red)
plotshape(exit_long_slowly, title='Slow Exit Long', style=shape.xcross, location=location.top, color=color.red)

hline(0, title='Zero Line', color=color.silver, linestyle=hline.style_dotted)


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


////ALERT CONDITIONS
alertcondition(bull_cont_trade, title='Continuation Trade - Bullish', message='Bullish continuation trade by ACBR.')
alertcondition(bear_cont_trade, title='Continuation Trade - Bearish', message='Bearish continuation trade by ACBR.')
alertcondition(exit_long_quickly, title='Exit Long Quickly', message='Exit long trade, by ACBR.')
alertcondition(exit_long_slowly, title='Exit Long Slowly', message='Exit long trade, by ACBR.')
alertcondition(exit_short_quickly, title='Exit Short Quickly', message='Exit short trade, by ACBR.')
alertcondition(exit_short_slowly, title='Exit Short Slowly', message='Exit short trade, by ACBR.')
alertcondition(high_baseline > 0 and ta.crossover(avg_range, high_baseline) or low_baseline > 0 and ta.crossover(avg_range, low_baseline), title='ACBR Crossing Above Upper Baseline', message='ACBR has crossed above the upper baseline.')
alertcondition(high_baseline < 0 and ta.crossunder(avg_range, high_baseline) or low_baseline < 0 and ta.crossunder(avg_range, low_baseline), title='ACBR Crossing Below Lower Baseline', message='ACBR has crossed below the lower baseline.')
alertcondition(avg_range > 0 and ta.crossover(avg_range, signal_line), title='ACBR Crossing Above Signal Line Trade Entry', message='ACBR has crossed above the signal line. Possible trade entry.')
alertcondition(avg_range < 0 and ta.crossunder(avg_range, signal_line), title='ACBR Crossing Below Signal Line Trade Entry', message='ACBR has crossed below the signal line. Possible trade entry.')
