Imbalance Identifier With Target Box
// 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=5
indicator('Imbalance Finder With Pip Target Box', overlay=true, max_lines_count=500)

showema = input(defval=true, title='Show all EMAs', group='Plot EMA ')
Overridetargetsize = input(defval=false, title='Change Standard Target Size', group='Targets')
Targetssize = input.float(title='Pips for Target', defval=0.0001, minval=0.0001, group = 'Targets')

color black100 = color.new(color.black,100)
color green75 = color.new(color.green,75)
color green0 = color.new(color.green,0)
color aqua0 = color.new(color.aqua, 0)
color black0 = color.new(color.black, 0)
color blue0 = color.new(#0F52BA, 0)
color gray0 = color.new(color.gray, 0)
color orange0 = color.new(#FF8C00,0)
color red0 = color.new(color.red, 0)
color red75 = color.new(color.red,75)
color white0 = color.new(color.white, 0)
color yellow0 = color.new(#FFF700, 0)
bool showgreydiamond = input(defval=false, title='Show Diamond For Back Testing' ,group='=== Information ===')

LabelDigitDisplay =
 syminfo.ticker == 'AUDCAD' == true ? '#.#####' : syminfo.ticker == 'AUDCHF' == true ? '#.#####' : syminfo.ticker == 'AUDJPY' == true ? '#.###'   : syminfo.ticker == 'AUDNZD' == true ? '#.#####'  :
 syminfo.ticker == 'AUDUSD' == true ? '#.#####' : syminfo.ticker == 'CADCHF' == true ? '#.#####' : syminfo.ticker == 'CADJPY' == true ? '#.###'   : syminfo.ticker == 'CHFJPY' == true ? '#.###'    : 
 syminfo.ticker == 'EURAUD' == true ? '#.#####' : syminfo.ticker == 'EURCAD' == true ? '#.#####' : syminfo.ticker == 'EURCHF' == true ? '#.#####' : syminfo.ticker == 'EURGBP' == true ? '#.#####' :
 syminfo.ticker == 'EURJPY' == true ?  '#.###'  : syminfo.ticker == 'EURNZD' == true ? '#.#####' : syminfo.ticker == 'EURUSD' == true ? '#.#####' : syminfo.ticker == 'GBPAUD' == true ? '#.#####' :
 syminfo.ticker == 'GBPCAD' == true ? '#.#####' : syminfo.ticker == 'GBPCHF' == true ? '#.#####' : syminfo.ticker == 'GBPJPY' == true ? '#.###'   : syminfo.ticker == 'GBPNZD' == true ? '#.#####' : 
 syminfo.ticker == 'GBPUSD' == true ? '#.#####' : syminfo.ticker == 'NZDCAD' == true ? '#.#####' : syminfo.ticker == 'NZDCHF' == true ? '#.#####' : syminfo.ticker == 'NZDJPY' == true ? '#.###': 
 syminfo.ticker == 'NZDUSD' == true ? '#.#####' : syminfo.ticker == 'USDCAD' == true ? '#.#####' : syminfo.ticker == 'USDCHF' == true ? '#.#####' : syminfo.ticker == 'USDJPY' == true ? '#.###': 
 syminfo.ticker == 'GOLD'   == true ? '#.##'    : syminfo.ticker == 'US100'  == true ? '#.##'    : syminfo.ticker == 'US30'   == true ? '#.##'    : syminfo.ticker == 'US500'  == true ? '#.##'    : syminfo.ticker == 'DE40' == true ? '#.##' : '#.#####'
 
PipsMultiplier = 
 syminfo.ticker == 'AUDCAD' == true ? 0.00050 : syminfo.ticker == 'AUDCHF' == true ? 0.00050 : syminfo.ticker == 'AUDJPY' == true ? 00.050   : syminfo.ticker == 'AUDNZD' == true ? 0.00050 :
 syminfo.ticker == 'AUDUSD' == true ? 0.00050 : syminfo.ticker == 'CADCHF' == true ? 0.00050 : syminfo.ticker == 'CADJPY' == true ? 00.050   : syminfo.ticker == 'CHFJPY' == true ? 00.050: 
 syminfo.ticker == 'EURAUD' == true ? 0.00050 : syminfo.ticker == 'EURCAD' == true ? 0.00050 : syminfo.ticker == 'EURCHF' == true ? 0.00050 : syminfo.ticker == 'EURGBP' == true ? 0.00050 :
 syminfo.ticker == 'EURJPY' == true ? 00.050   : syminfo.ticker == 'EURNZD' == true ? 0.00050 : syminfo.ticker == 'EURUSD' == true ? 0.00050 : syminfo.ticker == 'GBPAUD' == true ? 0.00050 :
 syminfo.ticker == 'GBPCAD' == true ? 0.00050 : syminfo.ticker == 'GBPCHF' == true ? 0.00050 : syminfo.ticker == 'GBPJPY' == true ? 00.050   : syminfo.ticker == 'GBPNZD' == true ? 0.00050 : 
 syminfo.ticker == 'GBPUSD' == true ? 0.00050 : syminfo.ticker == 'NZDCAD' == true ? 0.00050 : syminfo.ticker == 'NZDCHF' == true ? 0.00050 : syminfo.ticker == 'NZDJPY' == true ? 00.050: 
 syminfo.ticker == 'NZDUSD' == true ? 0.00050 : syminfo.ticker == 'USDCAD' == true ? 0.00050 : syminfo.ticker == 'USDCHF' == true ? 0.00050 : syminfo.ticker == 'USDJPY' == true ? 00.050:
 syminfo.ticker == 'GOLD' == true ? 1.5 : syminfo.ticker == 'US100' == true ? 10 : syminfo.ticker == 'US30' == true ? 25   : syminfo.ticker == 'US500' == true ? 10 : syminfo.ticker == 'DE40' == true ? 10 : 1

Targettouse = Overridetargetsize ? Targetssize : PipsMultiplier

Imbcol = input.color(yellow0, 'Imbalance Colour', inline="1" ,group='=== Information ===')
Dimcol = input.color(gray0, 'Imbalance Colour', inline="1" ,group='=== Information ===')
TopImbalance = low[2] <= open[1] and high[0] >= close[1]
TopImbalancesize = low[2] - high[0]
if TopImbalance and TopImbalancesize > 0
    BOX1 = box.new(left=bar_index[1], top=low[2], right=bar_index[0], bottom=high[0])
    box.set_bgcolor(BOX1, Imbcol )
    box.set_border_color(BOX1, Imbcol )

BottomInbalance = high[2] >= open[1] and low[0] <= close[1]
BottomInbalancesize = low[0] - high[2]
if BottomInbalance and BottomInbalancesize > 0
    BOX2 = box.new(left=bar_index[1], top=low[0], right=bar_index[0], bottom=high[2])
    box.set_bgcolor(BOX2, Imbcol )
    box.set_border_color(BOX2, Imbcol )

DownImbalance = TopImbalance and TopImbalancesize > 0
plotshape(DownImbalance[0] and showgreydiamond, style=shape.diamond, location=location.abovebar, color=Dimcol, size=size.tiny)

UpImbalance = BottomInbalance and BottomInbalancesize > 0
plotshape(UpImbalance[0] and showgreydiamond, style=shape.diamond, location=location.belowbar, color=Dimcol, size=size.tiny)

DownSell = close[1]>open[1] and close<open
UpBuy    = close[1]<open[1] and close>open

boxcolourtop        = UpBuy    ? green0 : black100
boxcolourbottom     = DownSell ? red0   : black100
boxcolourtopfill    = UpBuy    ? green75 : black100
boxcolourbottomfill = DownSell ? red75   : black100

BottomLabel = label.new(x=bar_index, y=high+(Targettouse/3), xloc=xloc.bar_index)
label.set_text(BottomLabel, text
 = "\n \n Entry = " + str.tostring(close,LabelDigitDisplay) 
 + "\n Stop Loss = " + str.tostring(high,LabelDigitDisplay) 
 + "\n Take Profit = " + str.tostring(close-PipsMultiplier,LabelDigitDisplay)
 + "\n Target Size = " + str.tostring(PipsMultiplier,LabelDigitDisplay))
label.set_textalign(BottomLabel, text.align_left)    
label.set_style(BottomLabel, label.style_label_left)
label.set_color(BottomLabel,  black100)
label.set_textcolor(BottomLabel, boxcolourbottom) 
label.delete(BottomLabel[1])
Bottom = box.new(left=bar_index[0], top=low, right=bar_index[0], bottom=low-Targettouse, xloc=xloc.bar_index)
box.set_right(Bottom,right=box.get_right(id=Bottom)+10)
box.set_bgcolor(Bottom , color=boxcolourbottomfill )
box.set_border_color(Bottom , color=boxcolourbottom)
box.delete(Bottom [1]) 

TopLabel = label.new(x=bar_index, y=low-(Targettouse/3), xloc=xloc.bar_index)
label.set_text(TopLabel, text
 = "\n \n Entry = " + str.tostring(close,LabelDigitDisplay) 
 + "\n Stop Loss = " + str.tostring(low,LabelDigitDisplay) 
 + "\n Take Profit = " + str.tostring(close+Targettouse,LabelDigitDisplay)
 + "\n Target Size = " + str.tostring(PipsMultiplier,LabelDigitDisplay))
label.set_textalign(TopLabel, text.align_left)    
label.set_style(TopLabel, label.style_label_left)
label.set_color(TopLabel,  black100)
label.set_textcolor(TopLabel, boxcolourtop ) 
label.delete(TopLabel[1])   
Top =  box.new(left=bar_index[0], top=high+Targettouse, right=bar_index[0], bottom=high, xloc=xloc.bar_index)
box.set_right(Top,right=box.get_right(id=Top)+10)
box.set_bgcolor(Top , color=boxcolourtopfill  )
box.set_border_color(Top, color=boxcolourtop )
box.delete(Top[1]) 

ema13colour = showema ? red0    : black100
ema35colour = showema ? orange0 : black100
ema50colour = showema ? aqua0   : black100

offset = input.int(title='Offset', defval=0, minval=-500, maxval=500)

emalen1513 =26 //input.int(13, minval=1, title='Length 13 EMA 15 Min', group='=== EMA Information ===')
emasrc1513 = close
EMA13 = request.security(syminfo.tickerid, '1', ta.ema(emasrc1513, emalen1513))
plot(EMA13, title='EMA 13 15 Min', color=ema13colour, linewidth=1, offset=offset)

emalen1535 = 63 //input.int(35, minval=1, title='Length 35 EMA 15 Min', group='=== EMA Information ===')
emasrc1535 = close
EMA35 = request.security(syminfo.tickerid, '1', ta.ema(emasrc1535, emalen1535))
plot(EMA35, title='EMA 35 15 Min', color=ema35colour, linewidth=1, offset=offset)

emalen1550 = 100 //input.int(50, minval=1, title='Length 50 EMA 15 Min', group='=== EMA Information ===')
emasrc1550 = close
EMA50 = request.security(syminfo.tickerid, '1', ta.ema(emasrc1550, emalen1550))
plot(EMA50, title='EMA 50 15 Min', color=ema50colour, linewidth=2, offset=offset)

Up   = EMA13 > EMA35 and EMA35 > EMA50
Down = EMA13 < EMA35 and EMA35 < EMA50

plotshape(Up , title='Up',   style=shape.square,   location=location.bottom, color=green0, size=size.tiny) 
plotshape(Down , title='Down',   style=shape.square,   location=location.bottom, color=red0, size=size.tiny) 
plotshape(not Down and not Up , title='Warning',   style=shape.square,   location=location.bottom, color=orange0, size=size.tiny) 

alertcondition(Up and not Up[1] ,  title='Buy  Switch On', message='Buy  Switch On')
alertcondition(Down and not Down[1],  title='Sell Switch On', message='Sell Switch On')
alertcondition(Up[1] and not Up ,  title='Warning Stop any More Buys', message='Warning Stop any More Buys')
alertcondition(Down[1] and not Down,  title='Warning Stop any More Sells', message='Warning Stop any More Sells')

alertcondition(open > close or close <= open ,  title='Every Bar Alert', message='Every Bar Alert')

