//@version=4
study("Consolidation Zones - Live with EMA and Targets", overlay = true,
max_bars_back = 1100)
// Input parameters
prd = input(defval = 10, title="Loopback Period", minval = 2, maxval = 50)
conslen = input(defval = 5, title="Min Consolidation Length", minval = 2,
maxval = 20)
paintcons = input(defval = true, title = "Paint Consolidation Area ")
zonecol = input(defval = [Link]([Link], 70), title = "Zone Color")
// Calculate Consolidation Zones
float hb_ = highestbars(prd) == 0 ? high : na
float lb_ = lowestbars(prd) == 0 ? low : na
var int dir = 0
float zz = na
float pp = na
dir := iff(hb_ and na(lb_), 1, iff(lb_ and na(hb_), -1, dir))
if hb_ and lb_
if dir == 1
zz := hb_
else
zz := lb_
else
zz := iff(hb_, hb_, iff(lb_, lb_, na))
for x = 0 to 1000
if na(close) or dir != dir[x]
break
if zz[x]
if na(pp)
pp := zz[x]
else
if dir[x] == 1 and zz[x] > pp
pp := zz[x]
if dir[x] == -1 and zz[x] < pp
pp := zz[x]
var int conscnt = 0
var float condhigh = na
var float condlow = na
float H_ = highest(conslen)
float L_ = lowest(conslen)
var line upline = na
var line dnline = na
bool breakoutup = false
bool breakoutdown = false
if change(pp)
if conscnt > conslen
if pp > condhigh
breakoutup := true
if pp < condlow
breakoutdown := true
if conscnt > 0 and pp <= condhigh and pp >= condlow
conscnt := conscnt + 1
else
conscnt := 0
else
conscnt := conscnt + 1
if conscnt >= conslen
if conscnt == conslen
condhigh := H_
condlow := L_
else
[Link](upline)
[Link](dnline)
condhigh := max(condhigh, high)
condlow := min(condlow, low)
upline := [Link](bar_index, condhigh, bar_index - conscnt, condhigh,
color = [Link], style = line.style_dashed)
dnline := [Link](bar_index, condlow , bar_index - conscnt, condlow,
color = [Link], style = line.style_dashed)
fill(plot(condhigh, color = na, style = plot.style_stepline),
plot(condlow, color = na, style = plot.style_stepline),
color = paintcons and conscnt > conslen ? zonecol :
[Link]([Link], 100))
alertcondition(breakoutup and conscnt <= conslen, title='Breakout Up',
message='Breakout Up')
alertcondition(breakoutdown and conscnt <= conslen, title='Breakout Down',
message='Breakout Down')
// Calculate EMA 5 and EMA 9
ema5 = ema(close, 5)
ema9 = ema(close, 9)
// Plot EMAs
plot(ema5, color=[Link], title="EMA 5")
plot(ema9, color=[Link], title="EMA 9")
// Buy and Sell Signals on crossover
buySignal = crossover(ema5, ema9) and conscnt <= conslen
sellSignal = crossunder(ema5, ema9) and conscnt <= conslen
// Plot Buy and Sell Arrows
plotshape(buySignal, style=[Link], location=[Link],
color=[Link], size=[Link], title="Buy Signal")
plotshape(sellSignal, style=[Link], location=[Link],
color=[Link], size=[Link], title="Sell Signal")
// Stop Loss and Targets
var float entryPrice = na
var float stopLoss = na
var float target1 = na
var float target2 = na
var float target3 = na
if buySignal
entryPrice := close
stopLoss := low
target1 := entryPrice + (entryPrice - stopLoss) * 2
target2 := entryPrice + (entryPrice - stopLoss) * 3
target3 := entryPrice + (entryPrice - stopLoss) * 5
if sellSignal
entryPrice := close
stopLoss := high
target1 := entryPrice - (stopLoss - entryPrice) * 2
target2 := entryPrice - (stopLoss - entryPrice) * 3
target3 := entryPrice - (stopLoss - entryPrice) * 5
// Plot Stop Loss and Targets
plot(stopLoss, color=[Link], title="Stop Loss")
plot(target1, color=[Link], title="Target 1")
plot(target2, color=[Link], title="Target 2")
plot(target3, color=[Link], title="Target 3")