Searching for first occurrence


I want to find the first past occurrence of a condition which compares past prices to the price of the bar being referenced.

For example, find the first low that is lower than today's low.

My understanding is that I cannot use ValueWhen(EXPRESSION, ARRAY, n = 1) as the EXPRESSION is built exlusively with arrays that either reference the same bar or bars a fixed distance in the past by using Ref().

In the end, what I need is something like ValueWhen( Low < "Today's Low", Low, 1), where Low is an array and "Today's Low" is the low of the bar that is being evaluated. If I wanted to find the barnumber of that same bar, it would be ValueWhen( Low < "Today's Low", BarIndex(), 1).

Is there a way to accomplish this without having to loop through individual bars?

Maybe this is really simple and I am conufsing myself ...

Thanks for any help,


I don't believe there is a built-in function to do what you want so you would need to resort to a loop. You should also read this post: Doubt when using self referencing

@schling there was a similar topic looking for Higher highs.

If this is what you are looking for, perhaps you can alter this with the appropriate functions and inputs for Lower Lows

It isn't clear what you really are after (a single value, or array, if array then what about different days than "today"). Generally for "today" you can simply write

Condition = Low < LastValue( Low );
FirstTimeConditionIsTrue = ExRem( Condition, 0 );
FirstLow = ValueWhen( FirstTimeConditionIsTrue, Low );

So generally the code for condition being true first time is:

FirstTime = ExRem( Condition, 0 );

Thanks everybody! My problem is excatly as described in the similar topic highlighed by @portfoliobuilder.

@mradtke: when I wrote the post I had already scratched my head for an hour and walked the dog, but not slept on the problem ...

Nested loops it will be then ...