On Balance Volume wi Normalization (SRC)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © dandrideng

//@version=5
indicator(title="On Balance Volume wi Normalization (SRC)", shorttitle="OBV-wi-N (SRC)", overlay=true, max_bars_back=1000, max_lines_count=400, max_labels_count=400)

//import library
import dandrideng/on_balance_volume/1 as nobv
import dandrideng/divergence/2 as div

//nobv paramters
nobv_ma_type = input.string(defval="EMA", title="MA Type", options=["SMA", "EMA", "DEMA", "TEMA", "WMA", "VWMA", "SMMA", "HullMA", "SSMA", "TMA"], group="Normalized On Balance Volume")
nobv_ma_len = input.int(defval=14, title="MA Period", minval=1, group="Normalized On Balance Volume")
nobv_sigma = input.float(defval=2.0, title="NOBV Sigma", minval=0, step=0.1, group="Normalized On Balance Volume")

nobv = nobv.obv_diff_norm(nobv_ma_type, nobv_ma_len)
// plot(series=nobv, title="nobv", linewidth=2, color=#2962FF)

// hline (0.0, title="nobv middle line", color=#787B86, linestyle=hline.style_dotted)
// obLevel = hline(price= nobv_sigma, title="nobv upper range", color=#787B86, linestyle=hline.style_dotted)
// osLevel = hline(price=-nobv_sigma, title="nobv lower range", color=#787B86, linestyle=hline.style_dotted)
// fill(hline1=obLevel, hline2=osLevel, title="nobv background", color=color.rgb(33, 150, 243, 90))

// bgcolor(color=nobv > nobv_sigma ? color.new(color.green, 85) : nobv < -nobv_sigma ? color.new(color.red, 85) : na)

//nobv divergence paramters
lbR = input.int(defval=2, title="Pivot Lookback Right", group="Normalized On Balance Volume Divergence")
lbL = input.int(defval=5, title="Pivot Lookback Left", group="Normalized On Balance Volume Divergence")
range_upper = input.int(defval=60, title="Max of Lookback Range", group="Normalized On Balance Volume Divergence")
range_lower = input.int(defval=5, title="Min of Lookback Range", group="Normalized On Balance Volume Divergence")
tolerance = input.int(defval=2, title="Tolerant Kline Number", group="Normalized On Balance Volume Divergence")
cov_thresh = input.float(defval=-0.1, title="Cov Threshold", group="Normalized On Balance Volume Divergence")
plot_rbull = input.bool(defval=true, title="Plot Bullish", group="Normalized On Balance Volume Divergence")
plot_hbull = input.bool(defval=false, title="Plot Hidden Bullish", group="Normalized On Balance Volume Divergence")
plot_rbear = input.bool(defval=true, title="Plot Bearish", group="Normalized On Balance Volume Divergence")
plot_hbear = input.bool(defval=false, title="Plot Hidden Bearish", group="Normalized On Balance Volume Divergence")

osc = nobv

to_intstr(x) => str.tostring(x, "#")
to_floatstr(x) => str.tostring(x, "#.###")
    
reg_bull  = div.regular_bull(low, osc, lbL, lbR, range_lower, range_upper, tolerance)
reg_bull_lsv = array.get(reg_bull, 0)
reg_bull_lsb = int(array.get(reg_bull, 1))
reg_bull_lov = array.get(reg_bull, 2)
reg_bull_lob = int(array.get(reg_bull, 3))
reg_bull_rsv = array.get(reg_bull, 4)
reg_bull_rsb = int(array.get(reg_bull, 5))
reg_bull_rov = array.get(reg_bull, 6)
reg_bull_rob = int(array.get(reg_bull, 7))
reg_bull_cov = array.get(reg_bull, 8)
if not na(reg_bull_lsv) and plot_rbull and reg_bull_cov < cov_thresh
    reg_bull_label = label.new(x=bar_index-lbR-reg_bull_rsb, y=reg_bull_rsv)
    label.set_text(reg_bull_label, "REG "+ to_floatstr(reg_bull_cov) +"\nSRC(" + to_intstr(reg_bull_rsb) + "-" + to_intstr(reg_bull_lsb) + ")\nOSC(" + to_intstr(reg_bull_rob) + "-" + to_intstr(reg_bull_lob) + ")")
    label.set_color(reg_bull_label, color.new(color.green, 20))
    label.set_textcolor(reg_bull_label, color.white)
    label.set_style(reg_bull_label, label.style_label_up)
    
    reg_bull_line = line.new(x1=bar_index-lbR-reg_bull_lsb, y1=reg_bull_lsv, x2=bar_index-lbR-reg_bull_rsb, y2=reg_bull_rsv)
    line.set_color(reg_bull_line, color.new(color.green, 20))
    line.set_width(reg_bull_line, 2)

hid_bull  = div.hidden_bull(low, osc, lbL, lbR, range_lower, range_upper, tolerance)
hid_bull_lsv = array.get(hid_bull, 0)
hid_bull_lsb = int(array.get(hid_bull, 1))
hid_bull_lov = array.get(hid_bull, 2)
hid_bull_lob = int(array.get(hid_bull, 3))
hid_bull_rsv = array.get(hid_bull, 4)
hid_bull_rsb = int(array.get(hid_bull, 5))
hid_bull_rov = array.get(hid_bull, 6)
hid_bull_rob = int(array.get(hid_bull, 7))
hid_bull_cov = array.get(hid_bull, 8)
if not na(hid_bull_lsv) and plot_hbull and hid_bull_cov < cov_thresh
    hid_bull_label = label.new(x=bar_index-lbR-hid_bull_rsb, y=hid_bull_rsv)
    label.set_text(hid_bull_label, "HID "+ to_floatstr(hid_bull_cov) +"\nSRC(" + to_intstr(hid_bull_rsb) + "-" + to_intstr(hid_bull_lsb) + ")\nOSC(" + to_intstr(hid_bull_rob) + "-" + to_intstr(hid_bull_lob) + ")")
    label.set_color(hid_bull_label, color.new(color.green, 50))
    label.set_textcolor(hid_bull_label, color.white)
    label.set_style(hid_bull_label, label.style_label_up)
    
    hid_bull_line = line.new(x1=bar_index-lbR-hid_bull_lsb, y1=hid_bull_lsv, x2=bar_index-lbR-hid_bull_rsb, y2=hid_bull_rsv)
    line.set_color(hid_bull_line, color.new(color.green, 50))
    line.set_width(hid_bull_line, 2)

reg_bear  = div.regular_bear(high, osc, lbL, lbR, range_lower, range_upper, tolerance)
reg_bear_lsv = array.get(reg_bear, 0)
reg_bear_lsb = int(array.get(reg_bear, 1))
reg_bear_lov = array.get(reg_bear, 2)
reg_bear_lob = int(array.get(reg_bear, 3))
reg_bear_rsv = array.get(reg_bear, 4)
reg_bear_rsb = int(array.get(reg_bear, 5))
reg_bear_rov = array.get(reg_bear, 6)
reg_bear_rob = int(array.get(reg_bear, 7))
reg_bear_cov = array.get(reg_bear, 8)
if not na(reg_bear_lsv) and plot_rbear and reg_bear_cov < cov_thresh
    reg_bear_label = label.new(x=bar_index-lbR-reg_bear_rsb, y=reg_bear_rsv)
    label.set_text(reg_bear_label, "REG "+ to_floatstr(reg_bear_cov) +"\nSRC(" + to_intstr(reg_bear_rsb) + "-" + to_intstr(reg_bear_lsb) + ")\nOSC(" + to_intstr(reg_bear_rob) + "-" + to_intstr(reg_bear_lob) + ")")
    label.set_color(reg_bear_label, color.new(color.red, 20))
    label.set_textcolor(reg_bear_label, color.white)
    label.set_style(reg_bear_label, label.style_label_down)
    
    reg_bear_line = line.new(x1=bar_index-lbR-reg_bear_lsb, y1=reg_bear_lsv, x2=bar_index-lbR-reg_bear_rsb, y2=reg_bear_rsv)
    line.set_color(reg_bear_line, color.new(color.red, 20))
    line.set_width(reg_bear_line, 2)

hid_bear  = div.hidden_bear(high, osc, lbL, lbR, range_lower, range_upper, tolerance)
hid_bear_lsv = array.get(hid_bear, 0)
hid_bear_lsb = int(array.get(hid_bear, 1))
hid_bear_lov = array.get(hid_bear, 2)
hid_bear_lob = int(array.get(hid_bear, 3))
hid_bear_rsv = array.get(hid_bear, 4)
hid_bear_rsb = int(array.get(hid_bear, 5))
hid_bear_rov = array.get(hid_bear, 6)
hid_bear_rob = int(array.get(hid_bear, 7))
hid_bear_cov = array.get(hid_bear, 8)
if not na(hid_bear_lsv) and plot_hbear and hid_bear_cov < cov_thresh
    hid_bear_label = label.new(x=bar_index-lbR-hid_bear_rsb, y=hid_bear_rsv)
    label.set_text(hid_bear_label, "HID "+ to_floatstr(hid_bear_cov) +"\nSRC(" + to_intstr(hid_bear_rsb) + "-" + to_intstr(hid_bear_lsb) + ")\nOSC(" + to_intstr(hid_bear_rob) + "-" + to_intstr(hid_bear_lob) + ")")
    label.set_color(hid_bear_label, color.new(color.red, 50))
    label.set_textcolor(hid_bear_label, color.white)
    label.set_style(hid_bear_label, label.style_label_down)
    
    hid_bear_line = line.new(x1=bar_index-lbR-hid_bear_lsb, y1=hid_bear_lsv, x2=bar_index-lbR-hid_bear_rsb, y2=hid_bear_rsv)
    line.set_color(hid_bear_line, color.new(color.red, 50))
    line.set_width(hid_bear_line, 2)

//end of file