LoopParticipant
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
//@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)
LoopParticipant
Senior
Thanks for your prompt reply, however the script is not really working…
Have you tried it?
Loop
It doesn’t work because it’s not written in the ProRealTime language.
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"
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
JSParticipant
Senior
“price” is reserved for STOP and TARGET pricing
Change all “price” in xPrice.