Essentially, it just combines the 2 only EMA’s (the 9 and 21) with an ATR based analysis to calculate the average range a ticker undergoes after an EMA 9 / 21 Cross-over and Cross-under.
Author: Steversteves
//PRC_EMA 9-21 WITH TARGETS
//version = 0
//13.02.24
//Iván González @ www.prorealcode.com
//Sharing ProRealTime knowledge
//////inputs
arraylookback = 500 //Lookback length
//////indicators
ema9 = average[9,1](close)
ema21 = average[21,1](close)
//////Crossover points
ema9crossema21 = ema9 crosses over ema21
ema21crossema9 = ema21 crosses over ema9
//////Plot arrows on the chart at the crossover points
if ema9crossema21 then
drawtext("▲",barindex,low-0.1*averagetruerange[10](close))coloured(0,250,0)
elsif ema21crossema9 then
drawtext("▼",barindex,high+0.1*averagetruerange[10](close))coloured(250,0,0)
endif
//////Sentiment
if close >= ema9 and close >= ema21 then
bullish = 1
bearish = 0
neutral = 0
r = 0
g = 230
b = 118
elsif close <= ema9 and close <= ema21 then
bullish = 0
bearish = 1
neutral = 0
r = 255
g = 82
b = 82
else
bullish = 0
bearish = 0
neutral = 1
r = 120
g = 123
b = 134
endif
if barindex > 21 then
COLORBETWEEN(ema9,ema21,r,g,b)
endif
//////Target prices
aboveema = close >= ema9 and close >= ema21
belowema = close <= ema9 and close <= ema21
crossover = ema9crossema21
crossunder = ema21crossema9
for i=0 to arraylookback do
if aboveema[i] then
$bulla[i] = close[i]
endif
if crossover[i] then
$crossovera[i] = close[i]
$opena[i] = open[i]
endif
if belowema[i] then
$beara[i] = close[i]
endif
if crossunder then
$crossundera[i] = close[i]
endif
next
opena = $opena[0]
///max close when close >= ema9 and close >= ema21
maxabove = arraymax($bulla)
///average of close when crossover
if ema9 crosses over ema21 then
crosstimes = 1+crosstimes
avgcrossover = ($crossovera[0]+avgcrossover*crosstimes[1])/crosstimes
endif
///min close when close <= ema9 and close <= ema21
maxbelow = arraymin($beara)
///average of close when crossunder
if ema9 crosses under ema21 then
crossundtimes = 1 + crossundtimes
avgcrossund = ($crossundera[0]+avgcrossund*crossundtimes[1])/crossundtimes
endif
bulldif1 = (maxabove-avgcrossover)/2
bulldif2 = (maxabove-avgcrossover)
beardif1 = (avgcrossund-maxbelow)/2
beardif2 = (avgcrossund-maxbelow)
if crossover then
if isset($opena[0]) then
op = $opena[0]
bulltgt1 = op+bulldif1
bulltgt2 = op+bulldif2
endif
endif
if crossunder then
if isset($opena[0]) then
op = $opena[0]
beartgt1 = op-beardif1
beartgt2 = op-beardif2
endif
endif
if IsLastBarUpdate then
drawsegment(barindex,bulltgt1,barindex+20,bulltgt1)coloured(0,250,0)
drawsegment(barindex,bulltgt2,barindex+20,bulltgt2)coloured(0,250,0)
drawsegment(barindex,beartgt1,barindex+20,beartgt1)coloured(250,0,0)
drawsegment(barindex,beartgt2,barindex+20,beartgt2)coloured(250,0,0)
endif
return ema9 as "Ema9"COLOURED(r,g,b), ema21 as "Ema21"coloured(r,g,b)