Question on valuewhen function

I have a question on valuewhen function.
I doing an exploration on AMBC from 3/1/2021 to 3/8/2021.

The following code does not work.

myH = ValueWhen( EndBar, HighestSince( StartBar, High ) );
myh_bar = ValueWhen( High == myH, BarIndex());
myh_date = ValueWhen( High == myH, DateTime());

The value for myH is 18.4.
The following code works.

myH = ValueWhen( EndBar, HighestSince( StartBar, High ) );
myh_bar = ValueWhen( High == 18.4, BarIndex());
myh_date = ValueWhen( High == 18.4, DateTime());

Is it because the comparison can only work for array to number and not array to array. Thanks in advance.

See debugging-techniques-part-1-exploration (ValueWhen example).

1 Like

Thanks. I have been doing that for days with trial and error and that's how I found out that compare against number works. Anyway, using LastValue(myH) works.

StartBar and Enbar are undefined in your code.
You have to post reproducible code that does not return error (if you look for help).

If startbar and endbar are related to start of day and end of day you may do like so:

dn = DateNum();
newday = dn != Ref( dn, -1);

StartBar = newday;
Endbar = Ref(newday,1);

myH = HighestSince( StartBar, High );
myH = TimeFrameCompress(myH, inDaily, compressHigh);
myH = TimeFrameExpand(myH, inDaily, expandFirst);

myh_bar = ValueWhen( High == myH, BarIndex());
myh_date = ValueWhen( High == myH, DateTime());

Plot( C, "Price", colorDefault, styleBar );
Plot( myH, "myH", colorRed );
Plot( myh_bar, "myh_bar", colorRed, styleOwnScale );

BTW, this one

myH = HighestSince( StartBar, High );
myH = TimeFrameCompress(myH, inDaily, compressHigh);
myH = TimeFrameExpand(myH, inDaily, expandFirst);

Can be simplified to

myH = TimeFrameGetPrice("H", inDaily);

If startBar/Endbar is supposed to be start/end of week or start/end of month then just change second argument of TimeFrame functions.


If you look for something different then you have to be more specific.
See thread "How to ask good question".


Your upper one will not work most of the time.
It will work only if highest is at EndBar.

1 Like

Thanks. Some of the codes are from your old post.
Here is the full code. I have checked for several time segments and this code is able to locate the high of H and low of Low. What puzzle me is the comparing High and Low against a number works but not against array.

dt = DateTime();
StartDate = DateTimeConvert(2,Status("rangefromdate"));
EndDate = DateTimeConvert(2,Status("rangetodate"));
StartBar = dt == Lookup( dt, StartDate, 1);
EndBar = dt == Lookup( dt, EndDate, -1);
myH = ValueWhen( EndBar, HighestSince( StartBar, High ) );
myh_bar = ValueWhen( High == LastValue(myH), BarIndex());
myh_date = ValueWhen( High == LastValue(myH), DateTime());
myL = ValueWhen( EndBar, LowestSince( StartBar, Low ) );
myl_bar = ValueWhen( Low == LastValue(myL), BarIndex(),1);
myl_date = ValueWhen( Low == LastValue(myL), DateTime());

Filter = 1;
SetOption("NoDefaultColumns",True);
AddTextColumn(Name(),"Ticker");
AddColumn(DateTime(),"Date",formatDateTime);
AddColumn(myh_bar,"BI High",1.0);
AddColumn(myh_date,"Date High",formatDateTime);
AddColumn(myH,"High",1.2);
AddColumn(myl_bar,"BI Low",1.0);
AddColumn(myl_date,"Date Low",formatDateTime);
AddColumn(myL,"Low",1.2);

If used in analysis, you can probably simplify it to:

StartBar = Status("firstbarinrange");
EndBar = Status("lastbarinrange");
1 Like

You just have single range so you just need single value not array (if you look for highest high of range).

If you look for all DateTimes of date range where H == myH then you might do like this:

(Note: myH may be different value within range if there are new highs)

start_dn = Status("rangefromdate");
end_dn = Status("rangetodate");

dn = DateNum();
dn_cond = dn >= start_dn AND dn <= end_dn;

myH = HighestSince( StartBar, High );

val_cond = High == myH AND dn_cond;
myh_bar = ValueWhen(val_cond, BarIndex());
myh_date = ValueWhen(val_cond, DateTime());

OR

start_dn = Status("rangefromdate");
end_dn = Status("rangetodate");

dn = DateNum();
dn_cond = dn >= start_dn AND dn <= end_dn;

myH = IIf(dn_cond, HighestSince( StartBar, High ), 0);
myh_bar = ValueWhen( High == myH, BarIndex());
myh_date = ValueWhen( High == myH, DateTime());

OR....

1 Like

Thanks. Now I understand. It just so happen that the last element of myH is needed to compare against High. Comparing myH and High will not work because high value exist at a different barindex.

Thanks. I need to specify range as I am doing screening of regression channel with high and low points.

I think you misinterpret.
firstbarinrange and lastbarinrange are array markers which return true for first/last array index of your selected from-to date range.

So you could do like this also.

dt = DateTime();
bi = BarIndex();
StartBar = Status("firstbarinrange");
EndBar = Status("lastbarinrange");

myH = HighestSince(StartBar, H);
bars = HighestSinceBars(StartBar,myH);
myh_bar = ValueWhen(EndBar, Ref(bi,-bars));
myh_date = ValueWhen(EndBar, Ref(dt,-bars));

And if for some reason you need StartDate and EndDate in addition then you may do like so:

dt = DateTime();
StartDate = LastValue(ValueWhen(StartBar, dt));
EndBar = LastValue(ValueWhen(EndBar, dt));
1 Like

@beppe, @fxshrat,
Thank you for being patient with my ignorance and providing all the pointers.

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.