Box Trading (Malaysia Stock Market)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © taufan_ganas

//@version=5
indicator('Box Trading', overlay=true)

//T+ Candles Title

grp_1 = 'T0 - T3 Candle Settings'

//Input Variables

T0Height = input.int(title='Bids Suggestions: Below RM 1 (6 bids) / RM 1 to RM 10 (8 to 12 bids) / Above RM 100 (50 bids)', minval=1, maxval=100, defval=6, group = grp_1)

//Bids

y = close < 1 ? 0.005 : close >= 1 and close < 10 ? 2 * 0.005 : close >= 10 and close < 100 ? 4 * 0.005 : 0.10
change = close - open
bids = change / y

//Scanner Algorithms

T0candle = bids >= T0Height
T4candle = bids[4] >= T0Height and close >= low[4] 

//T+ Candle

barcolor(color = close < 1 and bids >= T0Height? color.lime : close >= 1 and close < 10 and bids >= T0Height? color.aqua :close >= 10 and close < 100 and bids >= T0Height ? color.blue : na, title='T0 Candle', show_last=input(title='Last Candles Done - T0 Candle', defval=5, group = grp_1))

//Condition T+ Resistance Line

resistT0 = T0candle ? high : na

if(resistT0)
    resistT0_line = line.new(bar_index, high, bar_index[1], high,
       color = #f517a5,
       style=line.style_dashed,
       extend = extend.left,
       width=1)
    line.delete(resistT0_line[1])

//Condition T0 Support Line

supportT0 = T0candle ? low : na

if(supportT0)
    supportT0_line = line.new(bar_index, low, bar_index[1], low,
       color = #f517a5,
       style=line.style_dashed,
       extend = extend.left,
       width=1)
    line.delete(supportT0_line[1])
    
//T+ Count

var barCount1 = 0
barCount1 := T0candle? 0 : barCount1 + 1

//Plot T+ Count

T = label.new(time + 1440, high, '◄ T+' + str.tostring(barCount1),
     style = label.style_label_left,
     color = barCount1 <= 3? #FFFFFF00 : na,
     textcolor = barCount1 <= 3? #E040FB : na,
     size = size.small,
     xloc = xloc.bar_time,
     tooltip = 'Days before Force Selling on T+3 at 1230H')
label.delete(T[1])

//Breakout Resistance

bullBO = ta.valuewhen(resistT0,high,0)
plotshape(ta.crossover(close,bullBO), size = size.tiny, style = shape.triangleup, location = location.belowbar, title='Breakout T0 Candle (Resistance)', show_last=input(title='Last Candles Done - Breakout T0 Candle (Resistance)', defval=5, group = grp_1)) 

//Breakout Support

bearBO = ta.valuewhen(supportT0,low,0)
plotshape(ta.crossover(bearBO,close), size = size.tiny, style = shape.triangledown, location = location.abovebar, color = color.red, title='Breakout T0 Candle (Support)', show_last=input(title='Last Candles Done - Breakout T0 Candle (Support)', defval=5, group = grp_1)) 



