First Ema Cross on PineScript

41 Views Asked by At

I would like to identify first ema7/13 cross once price closes under ema100(for sell) or over ema100(for Buy)..

Can you pls help on that?

Actually, I can find number of bars under ema 100 but unale to identify firt ema 7/13 cross..

1

There are 1 best solutions below

0
Arun_K_Bhaskar On

I have assumed that ema 7 cross below ema 13 when both the ema are below 100.

As per your conditions:

//@version=5
indicator(title='EMA 7, 13, 100', overlay=true)

//EMA
ema_1 = ta.ema(close, 7)
ema_2 = ta.ema(close, 13)
ema_3 = ta.ema(close, 100)

plot(series=ema_1, title='EMA 1', color=color.green, linewidth=1)
plot(series=ema_2, title='EMA 2', color=color.red, linewidth=1)
plot(series=ema_3, title='EMA 3', color=color.blue, linewidth=1)

// Conditions
long_condition = barstate.isconfirmed and
                 ema_1 > ema_3 and
                 ema_2 > ema_3 and
                 ta.crossover(ema_1, ema_2)

short_condition = barstate.isconfirmed and
                 ema_1 < ema_3 and
                 ema_2 < ema_3 and
                 ta.crossunder(ema_1, ema_2)

// Function to Remove Excessive Signals
exrem(condition_1, condition_2) =>
    var entry_signal = 0
    entry_signal := condition_1 ? 1 : condition_2 ? -1 : entry_signal[1]
    entry = entry_signal != entry_signal[1]
    buy = entry and entry_signal == 1
    sell = entry and entry_signal == -1
    [buy, sell]

// Signals
[long_signal, short_signal] = exrem(long_condition, short_condition)

// Plot
plotshape(long_signal, style=shape.triangleup, color=color.green, text='BUY', textcolor=#FFFFFF, editable=false, location=location.belowbar, size=size.small)
plotshape(short_signal, style=shape.triangledown, color=color.red, text='SELL', textcolor=#FFFFFF, editable=false, location=location.abovebar, size=size.small)

Image:

enter image description here