Search code examples
financepine-script

How to get max of subseries in Pine script


Lets suppose we have series of numbers. It contains some values [..., 3, 6, 4, 7]. I would like to get maximum of 100 last elements.

I tried max(series[100]), but it looks like series[100] operator returns sub-series discarding last 100 elements.


Solution

  • That's correct. In Pine-script everything become a series, even a constant once you are using it, since all functions return a series. This means, that you can always put (non-series) constants in, but you can never get them out.

    I think what you want is:

    //@version=3
    study("Max of N", shorttitle="max", overlay=false)
    nmax = highest(n, 100) // n is the series of ALL bars
    plot(nmax, style=line)