[Mad] L4 Entryboxes
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © djmad
//@version=5
indicator("[Mad] L4 Entryboxes", overlay = true)

int    OFFSET       = input.int(10,"OFFSET TIME")*1000000

bool        Shortbox_2_Active   = input.bool(true,"Shortbox 2 Active")
int         Shortbox_2_Time_L   = input.time(timestamp("2020-02-20"), "Shortbox 2 Left", inline="Short_2_Left_Top", confirm = true)+OFFSET
float       Shortbox_2_Price_T  = input.price(100, "Shortbox 1 Top", inline="Short_2_Left_Top", confirm=true)
int         Shortbox_2_Time_R   = input.time(timestamp("2020-02-19"), "Shortbox 2 Right", inline="Short_2_Right_Bottom", confirm = true)+OFFSET
float       Shortbox_2_Price_B  = input.price(100, "Shortbox 2 Bottom", inline="Short_2_Right_Bottom", confirm=true)
        
bool        Shortbox_1_Active   = input.bool(true,"Shortbox 1 Active")
int         Shortbox_1_Time_L   = input.time(timestamp("2020-02-20"), "Shortbox 1 Left", inline="Short_1_Left_Top", confirm = true)+OFFSET
float       Shortbox_1_Price_T  = input.price(100, "Shortbox 1 Top", inline="Short_1_Left_Top", confirm=true)
int         Shortbox_1_Time_R   = input.time(timestamp("2020-02-19"), "Shortbox 1 Right", inline="Short_1_Right_Bottom", confirm = true)+OFFSET
float       Shortbox_1_Price_B  = input.price(100, "Shortbox 1 Bottom", inline="Short_1_Right_Bottom", confirm=true)
        
bool        Longbox_1_Active    = input.bool(true,"Longbox 1 Active")
int         Longbox_1_Time_L    = input.time(timestamp("2020-02-20"), "Longbox 1 Left", inline="Long_1_Left_Top", confirm = true)+OFFSET
float       Longbox_1_Price_T   = input.price(100, "Longbox 1 Top", inline="Long_1_Left_Top", confirm=true)
int         Longbox_1_Time_R    = input.time(timestamp("2020-02-19"), "Longbox 1 Right", inline="Long_1_Right_Bottom", confirm = true)+OFFSET
float       Longbox_1_Price_B   = input.price(100, "Longbox 1 Bottom", inline="Long_1_Right_Bottom", confirm=true)
        
bool        Longbox_2_Active    = input.bool(true,"Longbox 2 Active")
int         Longbox_2_Time_L    = input.time(timestamp("2020-02-20"), "Longbox 2 Left", inline="Long_2_Left_Top", confirm = true)+OFFSET
float       Longbox_2_Price_T   = input.price(100, "Longbox 1 Top", inline="Long_2_Left_Top", confirm=true)
int         Longbox_2_Time_R    = input.time(timestamp("2020-02-19"), "Longbox 2 Right", inline="Long_2_Right_Bottom", confirm = true)+OFFSET
float       Longbox_2_Price_B   = input.price(100, "Longbox 2 Bottom", inline="Long_2_Right_Bottom", confirm=true)

int         transparency        = input.int(50, "Transparency of Boxes", minval = 0, maxval = 100)

f_drawbox(Left, Top, Right, Bottom, Color_given,src,src2) =>
    Box1 = box.new(Left,Top,Right,Bottom, xloc=xloc.bar_time, border_style=line.style_dashed, border_width=2, bgcolor=src>Bottom and src<Top and src2>Left and src2<Right?color.new(color.yellow,transparency):Color_given ,border_color=(Left>Right) or (Bottom>Top)?color.yellow:na)
    box.delete(Box1[1])

f_drawtext(Left, Top, Right, Bottom, Text1,src,src2) =>
    Label1 = label.new(Left, Top, text=Text1, xloc=xloc.bar_time, style=label.style_xcross, textcolor=color.yellow)
    label.delete(Label1[1])

f_filtersignal(Left, Top, Right, Bottom, src,src2) =>
    Filter = (src>Bottom and src<Top and src2>Left and src2<Right)?true:false

f_drawbox(Longbox_1_Time_L,Longbox_1_Price_T,Longbox_1_Time_R,Longbox_1_Price_B,color.new(color.green,transparency),close,time)
f_drawtext(Longbox_1_Time_L,Longbox_1_Price_T,Longbox_1_Time_R,Longbox_1_Price_B,"Longbox 1",close,time)

f_drawbox(Longbox_2_Time_L,Longbox_2_Price_T,Longbox_2_Time_R,Longbox_2_Price_B,color.new(color.green,transparency),close,time)
f_drawtext(Longbox_2_Time_L,Longbox_2_Price_T,Longbox_2_Time_R,Longbox_2_Price_B,"Longbox 2",close,time)

f_drawbox(Shortbox_1_Time_L,Shortbox_1_Price_T,Shortbox_1_Time_R,Shortbox_1_Price_B,color.new(color.red,transparency),close,time)
f_drawtext(Shortbox_1_Time_L,Shortbox_1_Price_T,Shortbox_1_Time_R,Shortbox_1_Price_B,"Shortbox1",close,time)

f_drawbox(Shortbox_2_Time_L,Shortbox_2_Price_T,Shortbox_2_Time_R,Shortbox_2_Price_B,color.new(color.red,transparency),close,time)
f_drawtext(Shortbox_2_Time_L,Shortbox_2_Price_T,Shortbox_2_Time_R,Shortbox_2_Price_B,"Shortbox2",close,time)

Long_1_enabled =  Longbox_1_Active      ?   f_filtersignal(Longbox_1_Time_L,Longbox_1_Price_T,Longbox_1_Time_R,Longbox_1_Price_B,close,time):false
Long_2_enabled =  Longbox_2_Active      ?   f_filtersignal(Longbox_2_Time_L,Longbox_2_Price_T,Longbox_2_Time_R,Longbox_2_Price_B,close,time):false
Short_1_enabled = Shortbox_1_Active     ?   f_filtersignal(Shortbox_1_Time_L,Shortbox_1_Price_T,Shortbox_1_Time_R,Shortbox_1_Price_B,close,time):false
Short_2_enabled = Shortbox_2_Active     ?   f_filtersignal(Shortbox_2_Time_L,Shortbox_2_Price_T,Shortbox_2_Time_R,Shortbox_2_Price_B,close,time):false

var bool outputSignal_long = na
var bool outputSignal_short = na

if (Long_1_enabled or Long_2_enabled)
    outputSignal_long := true
else
    outputSignal_long := false

if (Short_1_enabled or Short_2_enabled)
    outputSignal_short := true 
else
    outputSignal_short := false


// SIGNAL Daisychain {

string  inputtype           =   input.string("NoInput" , title="Signal Type", group='Multibit signal config', options=["MultiBit", "MultiBit_pass", "NoInput"], tooltip='Multibit Daisychain with and without infusing\nMutlibit is the Signal-Type used in my Backtestsystem',inline='3a')
float   inputModule         =   input(title='Select L1 Indicator Signal', group='Multibit signal config', defval=close, inline='3a')

Signal_Channel_Line1=   input.int(-1,   "L1 long channel",   minval=-1,   maxval=15,group='Multibit',inline='1a')
Signal_Channel_Line2=   input.int(-1,   "L1 short channel",  minval=-1,   maxval=15,group='Multibit',inline='1a')

//*********** Simplesignal Implementation

signalout = 0
signalout := outputSignal_long ? 1 : outputSignal_short ? -1 : 0
BM_color_signal = outputSignal_long ? #4C9900 : outputSignal_short ? #CC0000 : color.black
L_digital_signal = plot(signalout, title='Digitalsignal', color=BM_color_signal, style=plot.style_columns, display=display.none)

//*********** MULTIBIT Implementation
import djmad/Signal_transcoder_library/7 as transcode
bool [] Multibit = array.new<bool>(16,false)
if inputtype == "MultiBit" or inputtype == "MultiBit_pass"
    Multibit := transcode._16bit_decode(inputModule)

if inputtype != "MultiBit_pass"
    transcode.f_infuse_signal(Signal_Channel_Line1, outputSignal_long, Signal_Channel_Line2, outputSignal_short, Multibit)

float plot_output = transcode._16bit_encode(Multibit)

plot(plot_output,title='MultiBit Signal',display=display.none)

//}