I am trying get the running maximum value of a series in a chart, with the caveat that instead of getting the maximum values of all previous values, it gets the maximum value of all future bars. I have a few ideas on how to do this, but either they don't work, or are too slow, so I am asking for additional ideas on how this can be done. Anyone have ideas on how I can accomplish this in a more elegant way?
Method 1)
This method would do what I need it to, but it does not work. It returns nothing, implying that forward values cannot be fed into the function.
Method 2)
This method works but it requires search_length iterations for every bar, making it incredibly slow - too slow to be used in a scan for reasonable search_length values (i.e. search_length=2000) (there might be other limitations with the way scans use studies as well, I'm not sure). The image below shows the desired result.
Method 1)
Code:
MaxSeries = CompoundValue(1, max(MaxSeries[-1], high, 0);
Method 2)
Code:
def FWD_max = fold k = -search_length to 1 with B = 0 do max(B, if !IsNaN(high[k]) then high[k] else 0);