Supertrend on EMA

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #186760 quote
    Loop
    Participant
    Senior

    Dear All,

    I would like to request your help for the conversion of the attached pinescript code for a supertrend indicator based on an exponential moving average.

    You can follow the original script either here:

    https://www.tradingview.com/script/xEtpZd0t/

    or in the attached word file (where you can find also a picture of the aspect of the indicator).

    Many thanks in advance

    Loop

    #186819 quote
    supertiti
    Participant
    Master
    //@version=2
    study("Supertrend with EMA", overlay=true)
    EmaPer = input(4, minval = 1, maxval = 100)
    TrPer = input(7, minval = 1, maxval = 100)
    factor=input(1.7, minval = 1, type = float, maxval = 10)
    
    ema = ema(close, EmaPer)
    up = ema - (factor * atr(TrPer))
    down = ema + (factor * atr(TrPer))
    
    TUp=ema[1]>TUp[1]? max(up,TUp[1]) : up
    TDown=ema[1]<TDown[1]? min(down,TDown[1]) : down
    Trend = ema >TDown[1] ? 1: ema <TUp[1]? -1: nz(Trend[1],1)
    
    Trailingsl = Trend == 1 ?TUp :TDown
    linecolor = Trend == 1 ? lime : red
    
    plot(Trailingsl, color = linecolor, style = line, linewidth = 2)
    plotarrow(Trend == 1 and Trend[1] == -1 ? Trend :na, title="Up Trend Arrow", colorup=blue, maxheight = 40, minheight = 30, transp=0)
    plotarrow(Trend == -1 and Trend[1] == 1 ? Trend : na, title="Down Trend Arrow", colordown=black, maxheight = 40, minheight = 30, transp=0)
    
    
    
    
    #186883 quote
    Loop
    Participant
    Senior

    Thanks for your prompt reply, however the script is not really working…

    Have you tried it?

    Loop

    #188300 quote
    robertogozzi
    Moderator
    Master

    It doesn’t work because it’s not written in the ProRealTime language.

    #188447 quote
    Nicolas
    Keymaster
    Master

    Nothing too fancy,  a Supertrend calculated on EMA of X periods:

    Emaper=4
    multiplier=1.7
    period=7
    
    moy=averagetruerange[period](close)
    price=average[emaper,1]
    up=price+multiplier*moy
    dn=price-multiplier*moy
    once trend=1
    if close>up[1] then
    trend=1
    elsif close<dn[1] then
    trend=-1
    endif
    if trend<0 and trend[1]>0 then
    flag=1
    else
    flag=0
    endif
    if trend>0 and trend[1]<0 then
    flagh=1
    else
    flagh=0
    endif
    if trend>0 and dn<dn[1] then
    dn=dn[1]
    endif
    if trend<0 and up>up[1] then
    up=up[1]
    endif
    if flag=1 then
    up=price+multiplier*moy
    endif
    if flagh=1 then
    dn=price-multiplier*moy
    endif
    if trend=1 then
    mysupertrend=dn
    else
    mysupertrend=up
    endif
    if mysupertrend > mysupertrend[1] then
    color1=0
    color2=255
    color3=0
    elsif mysupertrend < mysupertrend[1] then
    color1=255
    color2=0
    color3=0
    endif
    
    return mysupertrend coloured (color1,color2,color3) as "SuperTrend"
    #195866 quote
    johann8
    Participant
    New

    Hello Nicolas,

    it doesn’t work for me. I get an error on line 6 and 7. Could you correct the error for me?

    Thanks.

    Johann

    #195868 quote
    JS
    Participant
    Senior

    “price” is reserved for STOP and TARGET pricing

    Change all “price” in xPrice.

    #195885 quote
    johann8
    Participant
    New

    thank you js!

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.

Supertrend on EMA


ProBuilder: Indicators & Custom Tools

New Reply
Author
author-avatar
Loop @loop Participant
Summary

This topic contains 7 replies,
has 6 voices, and was last updated by johann8
3 years, 7 months ago.

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 01/27/2022
Status: Active
Attachments: 3 files
Logo Logo
Loading...