Immediate rebalance
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © minerlancer   

//@version=5
indicator('Immediate rebalance' , overlay = true , max_lines_count = 500)

Timezone = 'America/New_York'

DOMD  = (timeframe.multiplier <= 1440)
DOM60 = (timeframe.multiplier == 60) 
DOM30 = (timeframe.multiplier <= 30) 

//Input
tf = input.timeframe(defval = '' , title = '' , inline = '0' , group = 'Timeframe')

Show_macro     = input.bool(defval = false , title = 'Rebalance in Macro' , inline = '0' , group = 'Rebalance in Macro')
Show_macro_1h  = input.bool(defval = false , title = '1h'    , inline = '1' , group = 'Rebalance in Macro')
Show_macro_ltf = input.bool(defval = false , title = 'ltf   '   , inline = '1' , group = 'Rebalance in Macro')
x_line_Macro   = input.int(defval = 5 , title = 'Max Show' , minval = 1 , maxval = 10 , step = 1 , inline = '1' , group = 'Rebalance in Macro')

Show_normal = input.bool(defval = false , title = 'Normal Rebalance' , inline = '0' , group = 'Normal Rebalance')
Show_bull   = input.bool(defval = false , title = 'Bullish' , inline = '1' , group = 'Normal Rebalance')
Show_bear   = input.bool(defval = false , title = 'Bearish     ' , inline = '1' , group = 'Normal Rebalance')
x_line_     = input.int(defval = 1 , title = 'Max Show' , minval = 1 , maxval = 10 , step = 1 , inline = '1' , group = 'Normal Rebalance')

col_bull = input.color(defval = color.green , title = ''    , inline = '1' , group = 'Color Rebalance')
col_bear = input.color(defval = color.red   , title = '/'   , inline = '1' , group = 'Color Rebalance')
col_50   = input.color(defval = color.white , title = '50%' , inline = '2' , group = 'Color Rebalance')
col_75   = input.color(defval = color.gray  , title = '75%' , inline = '2' , group = 'Color Rebalance')
col_25   = input.color(defval = color.gray  , title = '25%' , inline = '2' , group = 'Color Rebalance')

// Timeframe
HTF(tf,ex) =>
    request.security(syminfo.tickerid , tf , ex , gaps = barmerge.gaps_off , lookahead = barmerge.lookahead_on)
    
O_1 = HTF(tf , open[1])
O_2 = HTF(tf , open[2])
H_2 = HTF(tf , high[2])
C_1 = HTF(tf , close[1])
C_2 = HTF(tf , close[2])
L_2 = HTF(tf , low[2])

// time Macro
// 1h 
is_macro_3  = time(timeframe.period, '0200-0301', Timezone) , is_macro_4  = time(timeframe.period, '0300-0401', Timezone) , is_macro_9  = time(timeframe.period, '0800-0901', Timezone) 
is_macro_10 = time(timeframe.period, '0900-1001', Timezone) , is_macro_11 = time(timeframe.period, '1000-1101', Timezone) , is_macro_12 = time(timeframe.period, '1100-1201', Timezone)
is_macro_15 = time(timeframe.period, '1400-1501', Timezone)
// ltf 
t_Macro_LN_0 = time(timeframe.period , '0233-0300' , Timezone) , t_Macro_LN_1 = time(timeframe.period , '0403-0430' , Timezone)

t_Macro_NY_0 = time(timeframe.period , '0850-0910' , Timezone) , t_Macro_NY_1 = time(timeframe.period , '0950-1010' , Timezone) , t_Macro_NY_2 = time(timeframe.period , '1050-1110' , Timezone) 
t_Macro_NY_3 = time(timeframe.period , '1135-1145' , Timezone) , t_Macro_NY_4 = time(timeframe.period , '1150-1210' , Timezone)

t_Macro_PM_0 = time(timeframe.period , '1310-1340' , Timezone)  , t_Macro_PM_1 = time(timeframe.period , '1450-1510' , Timezone) , t_Macro_PM_2 = time(timeframe.period , '1515-1545' , Timezone)

// for Macro 1h
Macro_1h = Show_macro and DOM60 and Show_macro_1h ? is_macro_3 or is_macro_4 or is_macro_9 or is_macro_10 or is_macro_11 or is_macro_12 or is_macro_15 : na
// for Macro < 1h
Macro_ltf = Show_macro and DOM30 and Show_macro_ltf ? t_Macro_LN_0 or t_Macro_LN_1 or t_Macro_NY_0 or t_Macro_NY_1 or t_Macro_NY_2 or t_Macro_NY_3 or t_Macro_NY_4 or t_Macro_PM_0 or t_Macro_PM_1 or t_Macro_PM_2 : na

// Condition
Long  = O_1 < C_1 and C_1 > H_2
Ber_L_50 = Long and O_2 > C_2 ? (O_2+H_2)/2 : (C_2+H_2)/2 // Candel bearish for Short
Ber_L_75 = Long and O_2 > C_2 ? H_2+(O_2-H_2)*0.75 : H_2+(C_2-H_2)*0.75 // Candel bearish for Short
Ber_L_25 = Long and O_2 > C_2 ? H_2+(O_2-H_2)*0.25 : H_2+(C_2-H_2)*0.25 // Candel bearish for Short

Short = O_1 > C_1 and C_1 < L_2
Bul_S_50 = Short and O_2 < C_2 ? (O_2+L_2)/2 : (C_2+L_2)/2 // Candel bullish for Short
Bul_S_75 = Short and O_2 < C_2 ? L_2+(O_2-L_2)*0.75 : L_2+(C_2-L_2)*0.75 // Candel bullish for Short
Bul_S_25 = Short and O_2 < C_2 ? L_2+(O_2-L_2)*0.25 : L_2+(C_2-L_2)*0.25 // Candel bullish for Short

// Plotting lines for Long
if (Long and Show_normal and Show_bull and not Show_macro) or (Long and Show_macro and (Macro_1h or Macro_ltf))
    hh = line.new(bar_index[2] , H_2 , bar_index , H_2 , width = 1, color = col_bull , style = line.style_solid)
    hh_50 = line.new(bar_index[2] , Ber_L_50 , bar_index , Ber_L_50 , width = 1 , color = col_50 , style = line.style_dashed)
    hh_75 = line.new(bar_index[2] , Ber_L_75 , bar_index , Ber_L_75 , width = 1 , color = col_75 , style = line.style_dotted)
    hh_25 = line.new(bar_index[2] , Ber_L_25 , bar_index , Ber_L_25 , width = 1 , color = col_25 , style = line.style_dotted)
    if not (Macro_1h or Macro_ltf)
        line.delete(hh[x_line_]) , line.delete(hh_50[x_line_]) , line.delete(hh_75[x_line_]) , line.delete(hh_25[x_line_])
    else 
        line.delete(hh[x_line_Macro]) , line.delete(hh_50[x_line_Macro]) , line.delete(hh_75[x_line_Macro]) , line.delete(hh_25[x_line_Macro])
 
// Plotting lines for Short
if (Short and Show_normal and Show_bear and not Show_macro) or (Short and Show_macro and (Macro_1h or Macro_ltf))
    ll = line.new(bar_index[2] , L_2 , bar_index , L_2 , width = 1 , color = col_bear , style = line.style_solid)
    ll_50 = line.new(bar_index[2] , Bul_S_50 , bar_index , Bul_S_50 , width = 1 , color = col_50 , style = line.style_dashed)
    ll_75 = line.new(bar_index[2] , Bul_S_75 , bar_index , Bul_S_75 , width = 1 , color = col_75  , style = line.style_dotted)
    ll_25 = line.new(bar_index[2] , Bul_S_25 , bar_index , Bul_S_25 , width = 1 , color = col_25  , style = line.style_dotted)

    if not (Macro_1h or Macro_ltf)
        line.delete(ll[x_line_]) , line.delete(ll_50[x_line_]) , line.delete(ll_75[x_line_]) , line.delete(ll_25[x_line_])
    else 
        line.delete(ll[x_line_Macro]) , line.delete(ll_50[x_line_Macro]) , line.delete(ll_75[x_line_Macro]) , line.delete(ll_25[x_line_Macro])
