Bear Bull Ratio (BBR)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © peacefulLizard50262

//@version=5
indicator("Bear Bull Ratio")

true_range = input.bool(false, "Enable True Range Mode")
length = input.int(20, "Windowed Length", 1)

bear_bull_ratio()=>
    var float open_count = 0
    var float close_count = 0 
    if open > close and close < close[1]
        open_count := nz(open_count[1]) + 1 
    if open < close and close < close[1] 
        open_count := nz(open_count[1]) + 1
    if open < close and close > close[1]
        close_count := nz(close_count[1]) + 1
    if open > close and close > close[1]
        close_count := nz(close_count[1]) + 1
    bear_bull_ratio = (close_count/(open_count + close_count)) * 100

bear_bull_ratio_average()=>
    var float open_count = 0
    var float close_count = 0
    if open > close and close < close[1]
        open_count := nz(open_count[1]) + 1 
    if open < close and close < close[1] 
        open_count := nz(open_count[1]) + 1
    if open < close and close > close[1]
        close_count := nz(close_count[1]) + 1
    if open > close and close > close[1]
        close_count := nz(close_count[1]) + 1
    bear_bull_ratio = (close_count/(open_count + close_count)) * 100
    var float average_open_close = 0
    var int average_open_close_counter = 0
    if close == close
        average_open_close_counter := nz(average_open_close_counter[1]) + 1
        average_open_close := nz(average_open_close[1]) + bear_bull_ratio
    average_ratio = average_open_close/average_open_close_counter

bear_bull_ratio_true_range()=>
    var float open_count = 0
    var float close_count = 0
    if open > close and close < close[1]
        open_count := nz(open_count[1]) + (high - low) 
    if open < close and close < close[1] 
        open_count := nz(open_count[1]) + (high - low)
    if open < close and close > close[1]
        close_count := nz(close_count[1]) + (high - low)
    if open > close and close > close[1]
        close_count := nz(close_count[1]) + (high - low)
    bear_bull_ratio = (close_count/(open_count + close_count)) * 100

bear_bull_ratio_average_true_range()=>
    var float open_count = 0
    var float close_count = 0
    if open > close and close < close[1]
        open_count := nz(open_count[1]) + (high - low) 
    if open < close and close < close[1] 
        open_count := nz(open_count[1]) + (high - low)
    if open < close and close > close[1]
        close_count := nz(close_count[1]) + (high - low)
    if open > close and close > close[1]
        close_count := nz(close_count[1]) + (high - low)
    bear_bull_ratio = (close_count/(open_count + close_count)) * 100
    var float average_open_close = 0
    var int average_open_close_counter = 0
    if close == close
        average_open_close_counter := nz(average_open_close_counter[1]) + 1
        average_open_close := nz(average_open_close[1]) + bear_bull_ratio
    average_ratio = average_open_close/average_open_close_counter

plot(true_range ? bear_bull_ratio_true_range() : bear_bull_ratio(), "Bear Bull Ratio", color.orange)
plot(ta.wma(true_range ? bear_bull_ratio_true_range() : bear_bull_ratio(), length), "Windowed Average Bear Bull Ratio")
plot(true_range ? bear_bull_ratio_average_true_range() : bear_bull_ratio_average(), "Average Bear Bull Ratio", color.gray)
