instrument {name = "ADR-Signal-NHL-KL", icon = "indicators:CCI", overlay = true} 
 
-- // variables and functions - support 
 
zero = make_series() 
zero:set(0) 
 
function ternary(cond, T, F) 
 if (cond) then return T else return F end 
end 
 
function is_up(ps) 
 return open[ps] < close[ps] 
end 
 
function is_down(ps) 
 return open[ps] > close[ps] 
end 
 
function get_top(ps) 
 if open[ps] > close[ps] then return open[ps] else return close[ps] end 
end 
 
function get_bottom(ps) 
 if open[ps] > close[ps] then return close[ps] else return open[ps] end 
end 
 
function has_wick_top(ps) 
 return high[ps] > get_top(ps) 
end 
 
function has_wick_bottom(ps) 
 return low[ps] < get_bottom(ps) 
end 
 
-- 1: high | 2: body top (open|close) | 3: body bottom (open|close) | 4: low 
function get_candle_point(ps) 
    local _p1 = high[ps] 
    local _p2 = get_top(ps) 
    local _p3 = get_bottom(ps) 
    local _p4 = low[ps] 
    return _p1, _p2, _p3, _p4 
end 
 
-- // execute 
 
local ps_a = 1 --  position a 
local ps_b = 0 -- position b 
 
local a1, a2, a3, a4 = get_candle_point(ps_a) 
local b1, b2, b3, b4 = get_candle_point(ps_b)  
 
local show_na1 = false 
local show_na2 = false 
local show_na3 = false 
local show_na4 = false 
local show_na5 = false 
 
local show_nb1 = false 
local show_nb2 = false 
local show_nb3 = false 
local show_nb4 = false 
local show_nb5 = false 
 
-- UP => (a = b) => continuation 
if (is_up(ps_a) and is_up(ps_b)) then 
    show_na1 = has_wick_top(ps_a) and has_wick_top(ps_b) and has_wick_bottom(ps_b) 
        and b2 > a1 and b1 > b2 
 
-- UP => (a <> b) => reversal 
elseif (is_down(ps_a) and is_up(ps_b)) then 
    show_na2 = has_wick_bottom(ps_a) and has_wick_bottom(ps_b) and has_wick_top(ps_b) 
        and b2 > a1 and b1 > b2 and b4 > a4 
    show_na3 = has_wick_bottom(ps_a) and b3 == b4 and has_wick_top(ps_b) 
        and b1 > a1 and b2 < a2 
    show_na4 = has_wick_bottom(ps_a) and has_wick_bottom(ps_b) and has_wick_top(ps_b) 
        and b2 > a2 and b2 < a1 and b1 > a1 and b1 > b2 and b4 > a4 
    show_na5 = has_wick_bottom(ps_a) and has_wick_bottom(ps_b) and has_wick_top(ps_b) 
        and b1 > a1 and b2 < a2 and b4 > a4 
end 
 
-- DOWN => (a = b) => continuation 
if (is_down(ps_a) and is_down(ps_b)) then 
    show_nb1 = has_wick_bottom(ps_a) and has_wick_bottom(ps_b) and has_wick_top(ps_b) 
        and b3 < a4 and b4 < b3 
 
-- DOWN => (a <> b) => reversal 
elseif (is_up(ps_a) and is_down(ps_b)) then 
    show_nb2 = has_wick_top(ps_a) and has_wick_top(ps_b) and has_wick_bottom(ps_b) 
        and b3 < a4 and b4 < b3 and b1 < a1 
    show_nb3 = has_wick_top(ps_a) and b2 == b1 and has_wick_bottom(ps_b) 
        and b4 < a4 and b3 > a3 
    show_nb4 = has_wick_top(ps_a) and has_wick_top(ps_b) and has_wick_bottom(ps_b) 
        and b3 < a3 and b3 > a4 and b4 < a4 and b4 < b3 and b1 < a1 
    show_nb5 = has_wick_top(ps_a) and has_wick_top(ps_b) and has_wick_bottom(ps_b) 
        and b4 < a4 and b3 > a3 and b1 < a1 
end 
 
-- plot 
 
plot_shape(show_na1, "show_na1", shape_style.labelup, shape_size.large, "green", shape_location.belowbar, 0, "NA1", "white") 
plot_shape(show_na2, "show_na2", shape_style.labelup, shape_size.large, "green", shape_location.belowbar, 0, "NA2", "white") 
plot_shape(show_na3, "show_na3", shape_style.labelup, shape_size.large, "green", shape_location.belowbar, 0, "NA3", "white") 
plot_shape(show_na4, "show_na4", shape_style.labelup, shape_size.large, "green", shape_location.belowbar, 0, "NA4", "white") 
plot_shape(show_na5, "show_na5", shape_style.labelup, shape_size.large, "green", shape_location.belowbar, 0, "NA5", "white")
plot_shape(show_nb1, "show_nb1", shape_style.labeldown, shape_size.large, "red", shape_location.abovebar, 0, "NB1", "white") 
plot_shape(show_nb2, "show_nb2", shape_style.labeldown, shape_size.large, "red", shape_location.abovebar, 0, "NB2", "white") 
plot_shape(show_nb3, "show_nb3", shape_style.labeldown, shape_size.large, "red", shape_location.abovebar, 0, "NB3", "white") 
plot_shape(show_nb4, "show_nb4", shape_style.labeldown, shape_size.large, "red", shape_location.abovebar, 0, "NB4", "white") 
plot_shape(show_nb5, "show_nb5", shape_style.labeldown, shape_size.large, "red", shape_location.abovebar, 0, "NB5", "white") 

-- // end: ADR-Signal-NHL-KL