pinescript - plot cross when EMA crosses over SMA while above/ below 200 moving average
Asked Answered
B

1

6

I am very NEW to pinescript and I am stuck at this point... I would like to plot a cross ONLY when 10 EMA crosses over 21 EMA while 21 is above 50 EMA and 50 EMA is above 200 EMA. This is to indicate Long signal. And at the same time, when 10 EMA crosses over 21 EMA while 21 is below 50 EMA and 50 EMA is below 200 EMA. This is to indicate Short signal.

I have this much code but I don't know how to proceed further:

//@version=3
study(title="MA Cross ATTEMPT", overlay=true)

s10ema = ema(close, 10)
s21ema = ema(close, 21)
s50ema = ema(close, 50)
s200ema = ema(close, 200)

plot(s10ema, color = red, linewidth = 1, transp=0)
plot(s21ema, color = aqua, linewidth = 1, transp=0)
plot(s50ema, color = aqua, linewidth = 2, transp=0)
plot(s200ema, color = red, linewidth = 2, transp=0)

mycond = s200ema < s50ema and s50ema < s21ema and s21ema < s10ema
EMACross = cross(s10ema, s21ema) ? s10ema : na, style = cross, linewidth = 4, color = yellow, transp=0

plot(?????)

Any help would be greatly appreciated

Brennen answered 9/10, 2018 at 21:43 Comment(0)
U
11

The way to do this would be using the plotshape() function. There are also different plot functions but I prefer using plotshape() for this purpose. Definitely check out other plot functions as well. Tradingview has a nice documentation for pine-script.

Also, cross() returns 1 if two series has crossed each other. It could be from below or above, it doesn't matter. However, you want to trigger your condition when a crossover happens. There is a function called crossover() for that purpose (also see crossunder() for the opposite).

//@version=3
study(title="MA Cross ATTEMPT", overlay=true)

s10ema = ema(close, 10)
s21ema = ema(close, 21)
s50ema = ema(close, 50)
s200ema = ema(close, 200)

plot(s10ema, title="Ema 10", color = red, linewidth = 1, transp=0)
plot(s21ema, title="Ema 21", color = aqua, linewidth = 1, transp=0)
plot(s50ema, title="Ema 50", color = orange, linewidth = 2, transp=0)
plot(s200ema, title="Ema 200", color = blue, linewidth = 2, transp=0)

longCond = crossover(s10ema, s21ema) and (s21ema > s50ema) and (s50ema > s200ema)
shortCond = crossunder(s10ema, s21ema) and (s21ema < s50ema) and (s50ema < s200ema)

plotshape(series=longCond, title="Long", style=shape.triangleup, location=location.belowbar, color=green, text="LONG", size=size.small)
plotshape(series=shortCond, title="Short", style=shape.triangledown, location=location.abovebar, color=red, text="SHORT", size=size.small)

enter image description here

Unreadable answered 10/10, 2018 at 7:49 Comment(2)
Thank you for the effort, it is VERY MUCH APPRECIATE ... this is almost exactly what I was looking for :-). When I say almost, one thing that needs to be changed is for the triangle to appear, 10ema needs to cross down over 21ema for a short signal and opposite for the long. If you take a look at the screenshot, red triangle appeared while 10ema (red) was crossing up over 21ema (aqua). This signal is the one that should be omitted. I will take a look to see if I can figure this out and post here. Otherwise, feel free to make a change if you have time :-). Thank you again for the effort :-)Brennen
Well, you said in your question when 10 EMA crosses over 21 EMA for the short signal. But, no worries. You can simply use crossunder() to achieve what you really want. Have look at the new code and screenshot.Unreadable

© 2022 - 2024 — McMap. All rights reserved.