Void Finder
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Travelling_Trader

//@version=4
study("Void Finder", overlay=true,max_lines_count=500)
boxlength = input(title="Box Length", type=input.integer, defval=1, minval=1, maxval=10, group = "Void Parameters")
color c_up = input(color.green, "Color Up", type = input.color, group = "Void Parameters")
color c_down = input(color.red, "Color Down", type = input.color, group = "Void Parameters")

bool showdiractionalvoids = input(type=input.bool, defval=false, title="Show Voids only of the direction of Moving Average", group = "Void Parameters")

len = input(3, minval=1, title="Length", group = "Moving Average Parameters")
src = input(close, title="Source", group = "Moving Average Parameters")
offset = input(title="Offset", type=input.integer, defval=0, minval=-500, maxval=500, group = "Moving Average Parameters")
out = sma(src, len)

plot(out, color=color.blue, title="MA", offset=offset)

if showdiractionalvoids
    UpVoid=close[1]<open[0] and (out[1] < out [0])
    UpVoidsize=open[0] - close[1]
    if UpVoid and UpVoidsize>0
        BOX1 = box.new(left=bar_index[1], top=open[0], right=bar_index[0]+boxlength, bottom=close[1])
        box.set_bgcolor (BOX1,c_up)
        box.set_border_color (BOX1,c_up)
else
    UpVoid=close[1]<open[0]
    UpVoidsize=open[0] - close[1]
    if UpVoid and UpVoidsize>0
        BOX1 = box.new(left=bar_index[1], top=open[0], right=bar_index[0]+boxlength, bottom=close[1])
        box.set_bgcolor (BOX1,c_up)
        box.set_border_color (BOX1,c_up)
        
if showdiractionalvoids
    BottomVoid=close[1] > open[0] and (out[0] < out [1])
    BottomVoidsize=close[1] - open[0]
    if BottomVoid and BottomVoidsize>0
        BOX2 = box.new(left=bar_index[1], top=close[1], right=bar_index[0]+boxlength, bottom=open[0])
        box.set_bgcolor (BOX2,c_down)
        box.set_border_color (BOX2,c_down)
else
    BottomVoid=close[1] > open[0]
    BottomVoidsize=close[1] - open[0]
    if BottomVoid and BottomVoidsize>0
        BOX2 = box.new(left=bar_index[1], top=close[1], right=bar_index[0]+boxlength, bottom=open[0])
        box.set_bgcolor (BOX2,c_down)
        box.set_border_color (BOX2,c_down)