Skip to content
All library documents

Custom Rolling Highest and Lowest Functions with Variable Lookbacks

Article TradingView scripts

Summary

This script demonstrates custom functions for finding the maximum and minimum values of a source series over a selected lookback. The functions scan prior observations in a loop, and the plots compare their results with built-in highest and lowest calculations. High and low inputs can be changed, and the lookback can switch between two lengths according to whether the current bar closes above or below its open.

The document also presents an alternative pair of functions that limits the scan using the current bar index, intended for shorter ranges and with stated limitations for long lengths. The main functions accept lengths from one through the documented upper bound. This is a programming example for rolling extrema rather than a complete trading strategy: it gives no entry or exit rules, market tests, or performance evidence. Its practical value is in illustrating lookback handling and comparison with platform functions.

Key ideas

  • The custom maximum and minimum functions scan a source series over a specified lookback.
  • The example compares custom rolling extrema with the platform's built-in equivalents.
  • Lookback length can alternate based on whether a bar is bullish or bearish.
  • A second function pair limits its scan by the current bar index and is intended for shorter lengths.
  • The script demonstrates indicator calculations without evaluating a trading strategy.

Tags

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.