apozdnyakov

Sort pseudo-array

34
This pine script sorts pseudo-array a(). Kind of.

Open-source script

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in a publication is governed by House Rules. You can favorite it to use it on a chart.

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.

Want to use this script on a chart?
//@version=2
study("Sort pseudo-array")
length = 5
a(i) => 
    close[i] + close[i+1]

sort(a, i) => 
    min = -10000
    for j = 0 to i
        min_local = 10000
        for l = 0 to length - 1
            if (a(l) <= min)
                continue
            min_local := min(min_local, max(min, a(l)))
        min := min_local
    min

plot(a(0))
plot(a(1))
plot(a(2))
plot(a(3))
plot(a(4))

plot(sort(a, 0), color=red, linewidth=3)
plot(sort(a, 1), color=orange, linewidth=3)
plot(sort(a, 2), color=green, linewidth=3)
plot(sort(a, 3), color=blue, linewidth=3)
plot(sort(a, 4), color=purple, linewidth=3)