Session Tick-Box
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © mbome237

//@version=5
indicator('Session Tick-Box', overlay=true)
import sosso_bott/SAT_LIB/45 as sat

groupset                    = '  ▶︎ ===========   Session Settings   =========== ◀︎'
groupset2                   = '  ▶︎ ===========   Display Settings   =========== ◀︎'

// ==========================================================================================================================================================================================================================================================================================
separateDays            = input.bool(true,  title="Show New Daily Session",                 inline="00",        group=groupset2)
color Day_Bg            = input.color(color.new(color.black, 45), "New Session BG",       inline="00",        group=groupset2)
colorDays               = input.bool(false,  title="Color Dayz Background",                 inline="00",        group=groupset2)  

colorSess               = input.bool(true,  title="",                                       inline="000",       group=groupset2) 
color cash_xo           = input.color(color.new(color.blue, 20),        "Cash",           inline="000",       group=groupset2)
color asian_xo          = input.color(color.new(color.olive, 20),       "Asian  ",        inline="000",       group=groupset2)
color europe_xo         = input.color(color.new(color.orange, 20),      "Europe",         inline="000",       group=groupset2)
color offset_xo         = input.color(color.new(color.red, 20),         "Offset",         inline="000",       group=groupset2)
offset_xo_fill          = input.int(87,   "fill",                                           inline="000",       group=groupset2)
newsessionPerc          = input.float(0.50, title="Session drawing Percentage",                                 group=groupset2) 
// ==========================================================================================================================================================================================================================================================================================
ref_res                 = input.timeframe(title='Reference Resolution', defval='D',                             group=groupset)
rth_ses                 = input.session(title='Cash Session',           defval='0830-1500',                     group=groupset)
rth_ses2                = input.session(title='Asian Session',          defval='1500-0400',                     group=groupset)
rth_ses3                = input.session(title='European Session',       defval='0400-0830',                     group=groupset)
rth_ses4                = input.session(title='offset Session',         defval='0400-0830',                     group=groupset)

color lundi             = input.color(color.new(color.yellow, 95),  "Monday   ",        inline="1",            group=groupset)
color mardi             = input.color(color.new(color.navy, 95),    "Tuesday  ",        inline="1",            group=groupset)
color mercredi          = input.color(color.new(color.olive, 95),   "Wednesday ",       inline="1",            group=groupset)
color jeudi             = input.color(color.new(color.purple, 95),  "Thursday  ",       inline="2",            group=groupset)
color vendredi          = input.color(color.new(color.silver, 95),  "Friday    ",       inline="2",            group=groupset)
color weekend           = input.color(color.new(color.teal, 95),    "Weekend   ",       inline="2",            group=groupset)



timeIsAllowed1          = time(timeframe.period, rth_ses)
timeIsAllowed2          = time(timeframe.period, rth_ses2)
timeIsAllowed3          = time(timeframe.period, rth_ses3)
timeIsAllowed4          = time(timeframe.period, rth_ses4)

[rth_low, rth_high, rth_fill_color, rth_open_bar, rth_is_open]       = sat.session_tick( rth_ses,  cash_xo,   ref_res) 
[rth_low2, rth_high2, rth_fill_color2, rth_open_bar2, rth_is_open2]  = sat.session_tick( rth_ses2, asian_xo,  ref_res) 
[rth_low3, rth_high3, rth_fill_color3, rth_open_bar3, rth_is_open3]  = sat.session_tick( rth_ses3, europe_xo, ref_res) 
[rth_low4, rth_high4, rth_fill_color4, rth_open_bar4, rth_is_open4]  = sat.session_tick( rth_ses4, offset_xo, ref_res) 

low_route               = float(na)
high_route              = float(na)
low_route               := timeIsAllowed1 ? rth_low         : timeIsAllowed2 ? rth_low2      : timeIsAllowed3 ? rth_low3    : low_route[1]
high_route              := timeIsAllowed1 ? rth_high        : timeIsAllowed2 ? rth_high2     : timeIsAllowed3 ? rth_high3   : high_route[1]
route_color             = timeIsAllowed1  ? cash_xo         : timeIsAllowed2 ? asian_xo      : timeIsAllowed3 ? europe_xo   : timeIsAllowed4 ? offset_xo : na
retxt_color             = rth_open_bar    ? "CASH"          : rth_open_bar2  ? "ASIAN"       : rth_open_bar3  ? "EUROPE"    : rth_open_bar4  ? "OFFSET"  : na
route_color2            = rth_open_bar    ? cash_xo         : rth_open_bar2  ? asian_xo      : rth_open_bar3  ? europe_xo   : rth_open_bar4  ? offset_xo : na
route_cond              = rth_open_bar    ?      true       : rth_open_bar2  ?       true    : rth_open_bar3  ?     true    : rth_open_bar4  ?     true  : na


mid                     = sat.lineBetweenLines(high_route, low_route, 0.5)
low_route_perc          = sat.AbsPercentChange(low_route)
high_route_perc         = sat.AbsPercentChange(high_route)
// ==========================================================================================================================================================================================================================================================================================
lol2                    = dayofweek(time) == dayofweek.monday ? lundi :  dayofweek(time) == dayofweek.tuesday ? mardi : dayofweek(time) == dayofweek.wednesday ? mercredi : dayofweek(time) == dayofweek.thursday? jeudi : dayofweek(time) == dayofweek.friday ? vendredi : weekend
xxx                     = sat.is_newbar(ref_res) ? 1 : 0

bgcolor(color=colorDays         ? lol2:na)
bgcolor(color=not separateDays  ? na :  xxx==1 ?Day_Bg:na)

label lb_high           = na
label lb_low            = na
label lb_high2          = na
label lb_low2           = na
    
float ylow              = na
float yhigh             = na 
float ylow2             = na
float yhigh2            = na 

if route_cond and colorSess
    ylow                := ( low_route[1]  * (1 - ( newsessionPerc / 100) ) )
    yhigh               := ( high_route[1] * (1 + ( newsessionPerc / 100) ) )
    ylow2               := ( low_route[1]  * (1 - ( (newsessionPerc + (newsessionPerc)/2) / 100) ) )
    yhigh2              := ( high_route[1] * (1 + ( (newsessionPerc + (newsessionPerc)/2) / 100) ) )

    lb_high             := label.new(bar_index, yhigh,      color=route_color2,     textcolor=route_color2,                     style=label.style_triangledown,   xloc = xloc.bar_index,    textalign=text.align_center,    size=size.tiny)
    lb_low              := label.new(bar_index, ylow,       color=route_color2,     textcolor=route_color2,                     style=label.style_triangleup,     xloc = xloc.bar_index,    textalign=text.align_center,    size=size.tiny)
    lb_high2            := label.new(bar_index, yhigh2,     color=route_color2,     textcolor=color.white, text=retxt_color,  style=label.style_label_down,                               textalign=text.align_center,    size=size.normal)
    lb_low2             := label.new(bar_index, ylow2,      color=route_color2,     textcolor=color.white, text=retxt_color,  style=label.style_label_up,                                 textalign=text.align_center,    size=size.normal)
    line.new(x1=bar_index, y1=ylow, x2=bar_index, y2=yhigh, color=route_color2, width=1, style=line.style_solid)

// plot(route_cond ? high_route[1]: na, color=color.red)
// plot(route_cond ? low_route[1] : na, color=color.red)
// plot(mid, linewidth = 2)
// plotchar(low_route_perc)
// plotchar(high_route_perc)


rth_plot_low5       = plot(low_route,   title='RTH Low5',   color=route_color, linewidth=2)
rth_plot_high5      = plot(high_route,  title='RTH High5',  color=route_color, linewidth=2)
fill(rth_plot_low5, rth_plot_high5, title='RTH Range5', color=color.new(route_color, offset_xo_fill), transp=90)