Count number of bars in the future [...]

I would like to count the number of bars that have elapsed in future before one emerges with a low that is lower than the present bar low. One problem I face is most amibroker functions like BarsSince() can look backword but not look forward.

I tried to write a function to achieve this goal by looking at the future 20 bars ahead and count but the logic is wrong.

function count_bars_elapsed_undercut_low()
	num_bars_elapsed_cut_present_low = Sum(Ref(Low, 20) >= Low, 20);
	return num_bars_elapsed_cut_present_low;
  1. You should name functions/variables with reasonable names but not with names being as long as Chinese wall. Names are not there for creating novels. :slight_smile: In order to describe what function or variable is doing you can use commenting via double slash // at beginning of comment.

  2. Now as for question... AFAIU you look for something like this:

function FwdSum(period, fwd_bars) { 
	// function to count bars having elapsed in future
	// 1. summing
	cs = Sum(L > Ref(L, -period), period);
	// 2. looking forward
	result = Ref(cs, abs(fwd_bars));
	return result;

Plot( FwdSum(20, 20), "cnt", colorRed );

PS: In addition you should use reasonable thread subject text too. It is not made for novels too. That's what posts of thread are made for.