Hello Community,
I’m looking for the simplest way to estimate the price at which the SMA20 would reverse from downtrend to uptrend or the opposite.
I tried this code. Can you please review and comment?
Thank you for your time.
DEFPARAM DRAWONLASTBARONLY=TRUE
N=20
SMA7= Average[N](close)
ATR=AverageTrueRange[14](close)
IF SMA7[1]<SMA7[2] THEN //currently in downtrend and waiting for a reverse
x =round((1 + SMA7 - SMA7[1] * (1 - 2 / (N+1)))*(N+1)/2)
ENDIF
IF SMA7[1]>SMA7[2] THEN //currently in uptrend and waiting for a reverse
x =round((-1 + SMA7 - SMA7[1] * (1 - 2 / (N+1)))*(N+1)/2)
ENDIF
drawtext(" SMA7 #x# ",barindex,0.5*ATR+highest[20](BollingerUp[20](close)),SansSerif,Bold,16)coloured(0,0,255)
Return x
Thank you for your feeback.
Doyou mean this code to find the “Close” at which EMA reverses?
defparam drawonlastbaronly=true
once customEMA=close
once prevEMA=close
EMALength = 10
alpha = 2/(EMALength+1)
once irange = 200 //ticks
mincalc = prevEMA*100
lasttest=mincalc
if barindex>EMALength then
CustomEMA= alpha*Close + (1–alpha)*CustomEMA[1]
for i = –irange to irange do
pricetest=(Close+i*ticksize)
calc = alpha*pricetest + (1–alpha)*CustomEMA[1]
mincalc = min(mincalc,abs(calc–CustomEMA[1]))
if mincalc<=ticksize and mincalc<lasttest then
lasttest=mincalc
result=pricetest
//break
endif
next
DRAWTEXT(” ———- #result# EMA”,barindex,result,dialog,standard,20) coloured(200,0,204)
endif
return result as “Close result”,mincalc as “min distance found”,customEMA coloured(200,0,0) style(line,2) as “real EMA”,CustomEMA[1] coloured(255,0,255) as “prevema”
Noobywan, As you mentionned yourself in post #96165, the code only provides the EMA[1], but not the EMA at which the trend reverses.
Hi, for ema case it’s both:
- the ema at which it reverses is ema[1] (“by definition” because it is the limit for ema between going up or down), so that’s what was used in the post to create the equation to solve,
- and the solution to the equation ema=ema[1] in the post was close=ema[1] (if not mistaken at the time I did it)
For comparison, if I also come back to first post and look at an sma of N periods case, then:
- similarily the level where sma would reverse is sma=sma[1],
- that would be for close=sma[N]