This ProBuilder code snippet demonstrates how to project the high of a Donchian channel forward by a specified number of bars, referred to as “decay”. The Donchian channel is a popular indicator used in trading that shows the highest high and the lowest low over a set period of bars.
decay = 5
hh = highest[20](high)
if barindex>1 then
drawsegment(barindex+decay,hh,barindex+(decay-1),hh[1])
endif
return
Here’s a step-by-step explanation of the code:
- decay = 5: This line sets the decay value to 5 bars. This means the indicator value will be projected 5 bars into the future.
- hh = highest[20](high): This line calculates the highest high of the last 20 bars. The variable hh stores this value.
- if barindex > 1 then: This conditional statement checks if the current bar index is greater than 1, ensuring that there is at least one previous bar to reference.
- drawsegment(barindex+decay,hh,barindex+(decay-1),hh[1]): This function draws a horizontal line segment from the current bar index plus the decay value to one bar less than the total decay, using the highest high values of the current and previous bars. This visually projects the high value forward by the decay amount.
- endif: Ends the conditional statement.
- return: Ends the execution of the script. This is typically used to ensure that the script stops processing further, which is particularly useful in scripts that might otherwise continue to execute unnecessary code.
This example is useful for visualizing how an indicator value might extend into the future, providing insights into potential resistance levels or trends.