Dev The Hall
Dev The Hall

Reputation: 7

How to detect if a moving average has been touched or broken

If the Stochastic has crossed in the direction of the trend and price has been pulled back from the trend into a moving average then an entry is valid.

A pull back is shown in the image 1.

I have tried coding it but I am new to pine-script so all i have managed so far is.

     ta.lowest(low,5) < ta.ema(close,100) and CrossUp and Uptrend

That seems to detect all valid entries but it enters on some invalid entries such as when the lowest of the last 5 candles is lower then the current EMA but none of the lows of the last five candles is below any of their own EMAs. How can one detect if any of the last candles lows have been lower than their own EMA.

By own EMA I mean the value of EMA at the close of that candle.

What the pullbacks look like

Upvotes: 0

Views: 1546

Answers (1)

Matteo Buffagni
Matteo Buffagni

Reputation: 322

you can use ta.crossunder(close, ema)

reference

To check if it happened in the last five candles you can use barssince() like this:

if ta.barssince(ta.crossunder(low, ema)) < 5

If this condition is true it means price crossed the ema in the last 5 candles

Upvotes: 0

Related Questions