pine-scriptalgorithmic-trading

Pinescript, How to code a conditional trigger?


So I've got a trigger for a buy alert, which is when RSI is say < 20 and when price closes below the lowest of 3 EMA's. My problem is in trying to code for the 'lowest' of 3 EMA's.

Using a:

buyzone = ema1 < ema2 ? ema2 < ema3 : ema1 : na : na

Will get me buyzone = ema1 when they're in order of 3, 2, 1 top to bottom - but how would I translate that to a buy condition?

I tried the above code but when I trigger a buy, I cannot use a boolean string and it does not run.


Solution

  • You math.min() to find out the lowest EMA value. Then use ta.crossunder() on that to find the cross.

    lowest_ma = math.min(ema1, ema2, ema3)
    is_lowest_ma_crossed = ta.crossunder(close, lowest_ma)