How to find the first occurrence of a bar?

Hello all,

I am trying to find the first occurrence of a bar and it’s low when it closes above yesterday high.

High = 100;
// Bar 1 closes above High and followed by 10 more bars that close above High 
Signal = C > High;
BarLowValue = Valuewhen(Signal,L,1); // This gives bar 10 but i want bar 1

Using valuewhen() I can find the recent bar that closes above the high but is there a way to find the first occurrence ?


@jvdeepak If I understand you correctly, you want to know the value of the Low, on the same bar that the Close is Closing above yesterday's High. One possible solution,

condition = Cross(C,Ref(H, -1));
LowAtCondition = ValueWhen(condition, Low, 1);

// Exploration //
Condition_bk_Color = IIf(condition, colorLime, colorDefault);

AddColumn(Low, "Low");
AddColumn(Ref(H, -1), " Yesterday's High");
AddColumn(Close, "Close");
AddColumn(condition, "condition", 1.0, colorDefault, Condition_bk_Color);
AddColumn(LowAtCondition, "LowAtCondition", 1.2, colorDefault, Condition_bk_Color);



@jvdeepak also note that you should never do this:

High = 100;

Because High is the name of a built-in AmiBroker price variable along with Open, Low, and Close. The variables O, H, L, and C are alternate names for the same variables.

Thank you. Much appreciate the response.

Understood. Will use proper variable names.

That is true that one should not do this, but little bit more explanation is required as to “why”.

Doing such kind of assignment temporarily overwrites High array with your data. Normally, as @mradtke wrote, you do NOT want to do this. It will have disastrous consequences (all indicator calculations wrong) if you do this without knowing what you are doing. Sometimes though it can be used to calculate some indicator based on data different than current symbol OHLC. For example if you wanted to calculate ADX or any other function that makes implicit use of OHLC and feed it with data other than your current symbol data then you could use ability to overwrite OHLC arrays. That is in fact what SetForeign does. It should be added that this assignment operates on private copy of OHLC data that current execution of your formula uses and does NOT affect other thread and does NOT affect the data in the database.


Thank you Tomasz. Appreciate the explanation.