Not sure I fully understood the meaning of the exchange.
May I suggest the following: You have elaborated a function (BlankMaxY) returning the maximum within a given window (162). You can duplicate this function but then returning the X value instead of Y.
Then you can use for both values a “.sortwith” function, sorted against either X or Y and then you get side by side the Y values and their corresponding X values. You can then proceed to any calculation.
Apologize if I misunderstood the request.