Supertrend on EMA

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #186760 quote
    LoopLoop
    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

    Supertrend-on-EMA.docx
    #186819 quote
    supertitisupertiti
    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
    LoopLoop
    Participant
    Senior

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

    Have you tried it?

    Loop

    #188300 quote
    robertogozzirobertogozzi
    Moderator
    Legend

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

    #188447 quote
    NicolasNicolas
    Keymaster
    Legend

    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"
    supertrend-on-ema.png supertrend-on-ema.png
    #195866 quote
    johann8johann8
    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

    ST-on-EMA.jpg ST-on-EMA.jpg
    #195868 quote
    JSJS
    Participant
    Master

    “price” is reserved for STOP and TARGET pricing

    Change all “price” in xPrice.

    #195885 quote
    johann8johann8
    Participant
    New

    thank you js!

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

Stuck on this ProBuilder code?

Describe what this topic is trying to build, in plain English, and ProRealAI writes the ProRealTime™ indicator, screener or system for you.

Available in 7 languages
Try ProRealAI

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 johann8johann8
4 years, 3 months ago.

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