1
votes

I would like to code the RSI Failure Swings. Things I need for the calculation:

  1. A new closing high/low with overbought/sold conditions - Done
  2. After that a higher/lower close with normal RSI conditions (divergence) - Done
  3. Take the lowest/highest RSI reading between those new closing highs/lows - Problem
  4. When RSI crosses the RSI reading mentioned at point 3, give feedback of completed failure swing - Quasi-done

I already managed to give a heads up when the divergence happens, so point 1 and 2 are fine. But how do a get the lowest/highest RSI reading between those bars?

I tried something like this for a Top Failure Swing (AKA Bearish Failure Swing):

lowestrsi = valuewhen(bearishdivergence, lowest(rsi, barssince(overbought)), 0)

My thinking was: find the bar with bearish divergence and return the lowest RSI reading between that bar and the bar prior to that with a overbought condition.

But that doesn't work because the second argument of lowest() can't be a series! If you replace that argument with a number, let's say 10, it works perfectly and I get notified of a failure swing. But the problem is, is that there's no default value for something like this. Meaning you can't hardcode something like '10' or some other number.

Question: How do I get the lowest RSI between the bars mentioned at point 1 and 2? Or how do I turn a series into an integer? Is that even possible? Or is it possible to get a single integer from a series?

1
I have the same problem. I'm annoyed with pine-script because of problems like these - ragamufin

1 Answers

0
votes

You might wanna try RicardoSantos' Highest/Lowest function. It helped me as a workaround to the similar problem in the past: https://www.tradingview.com/script/32ohT5SQ-Function-Highest-Lowest/

//@version=2
study(title='Function Highest/Lowest', overlay=true)
src = input(close)
length = input(10)

f_highest(_src, _length)=>
    _adjusted_length = _length < 1 ? 1 : _length
    _value = _src
    for _i = 0 to (_adjusted_length-1)
        _value := _src[_i] >= _value ? _src[_i] : _value
    _return = _value

f_lowest(_src, _length)=>
    _adjusted_length = _length < 1 ? 1 : _length
    _value = _src
    for _i = 0 to (_adjusted_length-1)
        _value := _src[_i] <= _value ? _src[_i] : _value
    _return = _value

h = f_highest(src, length)
l = f_lowest(src, length)

plot(h)
plot(l)