Price to reverse trend of SMA

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #173237 quote
    Khaled
    Participant
    Veteran

    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
    #173238 quote
    Khaled
    Participant
    Veteran

    I realize a made a few mistakes. In addition, my code tries to find what price is required to move the EMA by “1”, which is not what I’m trying to acheive.

    I’d like to find the price that reverses the EMA trend.

    Thanks for your help!

    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    DEFPARAM DRAWONLASTBARONLY=TRUE
    N=20
    EMA20= ExponentialAverage[N](close)
    ATR=AverageTrueRange[14](close)
    IF EMA7[1]<EMA7[2] THEN //currently in downtrend and waiting for a reverse
    x =round((1 + EMA7 EMA7[1] * (1 2 / (N+1)))*(N+1)/2)
    ENDIF
    IF EMA7[1]>EMA7[2] THEN //currently in uptrend and waiting for a reverse
    x =round((1 + EMA7 EMA7[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
    #173252 quote
    JC_Bywan
    Moderator
    Master

    Hi, here is post #96165 from french forum with answer to same question: https://www.prorealcode.com/topic/formulation-ema/page/2/#post-96165

    #173256 quote
    Khaled
    Participant
    Veteran

    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 + (1alpha)*CustomEMA[1]
    for i = irange to irange do
      pricetest=(Close+i*ticksize)
      calc = alpha*pricetest + (1alpha)*CustomEMA[1]
      mincalc = min(mincalc,abs(calcCustomEMA[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”
    #173259 quote
    Khaled
    Participant
    Veteran

    Noobywan, As you mentionned yourself in post #96165, the code only provides the EMA[1], but not the EMA at which the trend reverses.

    Capture-decran-2021-07-06-214023.png Capture-decran-2021-07-06-214023.png
    #173269 quote
    JC_Bywan
    Moderator
    Master

    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]
    Khaled thanked this post
Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.

Price to reverse trend of SMA


ProBuilder: Indicators & Custom Tools

New Reply
Author
author-avatar
Khaled @khaled Participant
Summary

This topic contains 5 replies,
has 2 voices, and was last updated by JC_Bywan
4 years, 7 months ago.

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 07/06/2021
Status: Active
Attachments: 1 files
Logo Logo
Loading...