Problem with valueWhen for future reference

#1
I am trying to fetch value from bars that are towards the right of the chart. That means if I am in bar number 10 then I want to fetch the value in bar number 11 and onwards. For example I want to fetch the low of the bar that has high equal to 199.7. I use valueWhen with the third parameter set to 0. It works in most places but fails in others. The code is as below. I have attached the data file of SBI here https://easyupload.io/ezyua0. You can try it in 1 minute timeframe. I have checked in exploration also. for the first instance it is returning null. You can see the trace in pic below. The bar with the yellow dot has high equal to 199.7. It was expected that valueWhen returns low of that bar but instead it returned null. Why?

valueWhenError.png


But later in the chart it starts working. see the picture below.
valueWhenError2.png


Code:
_TRACE("!CLEAR!");
_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates|chartWrapTitle);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}} ", O, H, L, C, SelectedValue( ROC( C, 1 ))) );
PlotOHLC( O, H, L, C, "Traded", colorRed, styleBar , Null, Null, 0, 1, 1);
_SECTION_END();

h1=199.7;
cond=H==h1;
val2=ValueWhen(cond, L, 0);
_TRACE("val2="+val2);

PlotShapes( IIF( cond, shapeSmallCircle, shapeNone), coloryellow, 0, L-ATR(5)); //
Filter=1;
AddColumn(ValueWhen(cond, L, 0), "ValueWhen(H=="+h1+", L, 0)");
 
Last edited:

Romeo1998

Well-Known Member
#2
I am trying to fetch value from bars that are towards the right of the chart. That means if I am in bar number 10 then I want to fetch the value in bar number 11 and onwards. For example I want to fetch the low of the bar that has high equal to 199.7. I use valueWhen with the third parameter set to 0. It works in most places but fails in others. The code is as below. I have attached the data file of SBI here https://easyupload.io/ezyua0. You can try it in 1 minute timeframe. I have checked in exploration also. for the first instance it is returning null. You can see the trace in pic below. The bar with the yellow dot has high equal to 199.7. It was expected that valueWhen returns low of that bar but instead it returned null. Why?

View attachment 45732

But later in the chart it starts working. see the picture below.
View attachment 45733

Code:
_TRACE("!CLEAR!");
_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates|chartWrapTitle);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}} ", O, H, L, C, SelectedValue( ROC( C, 1 ))) );
PlotOHLC( O, H, L, C, "Traded", colorRed, styleBar , Null, Null, 0, 1, 1);
_SECTION_END();

h1=199.7;
cond=H==h1;
val2=ValueWhen(cond, L, 0);
_TRACE("val2="+val2);

PlotShapes( IIF( cond, shapeSmallCircle, shapeNone), coloryellow, 0, L-ATR(5)); //
Filter=1;
AddColumn(ValueWhen(cond, L, 0), "ValueWhen(H=="+h1+", L, 0)");
I think u want output like this :)
good luck :)
1616394462565.png


Code:
_TRACE("!CLEAR!");
_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates|chartWrapTitle);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}} ", O, H, L, C, SelectedValue( ROC( C, 1 ))) );
PlotOHLC( O, H, L, C, "Traded", colorRed, styleBar , Null, Null, 0, 1, 1);
_SECTION_END();

h1=199.7;
cond=H==h1;
val2=ValueWhen(cond, L);
t2 = ValueWhen(cond,TimeNum());
cond2 = TimeNum()==t2;

_TRACE("val2="+val2);

PlotShapes( IIF( cond AND cond2 , shapeSmallCircle, shapeNone), coloryellow, 0, L-ATR(5)); //
Filter=cond AND cond2;
AddColumn(L, "When H=="+h1+",  L = ", 1.2);
 
#3
Thanks for responding romeo But this is not the answer.
Imagine this. The time is 12:00 . Value is computed at 12:00. I refer the bar at 11:00 ref (var, -barRef11) and from here I want the value that was computed at 12:00. It is called refering to future. It is possible with valueWhen with the 3rd parameter set to 0.
This is working in mostly but failing in first 300 odd bars of the chart. Why ?
 
#6
BUG Confirmed in amibroker!!! WORKAROUND FOUND
Workaround is to set the first element of the concerned array variable to 1 or higher. In our case that variable is cond. See the code below.
When this was reported (with supporting code and data) to amibroker support Mr TOMAS JANESKO (developer amibroker) he flat out refused to acknowledge the bug. Mr.TOMAS arrogantly claims there are no defects in Amibroker whereas the truth is something else.
If a limitation is not documented then it is a Bug. Period.


Code:
/*
AMIBROKER AFL ValueWhen(EXPRESSION, ARRAY, n = 1) BUG WORKAROUND
ValueWhen() when used for referencing future (3rd parameter set to 0) does not work for the first occurance of the EXPRESSION
When this was reported with supporting code and data to amibroker support Mr TOMAS JANESKO (developer amibroker) he flat out refused to acknowledge the bug
Mr.TOMAS arrogantly claims there are no defects in Amibroker whereas the truth is something else
*/

_TRACE("!CLEAR!");
_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates|chartWrapTitle);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}} ", O, H, L, C, SelectedValue( ROC( C, 1 ))) );
PlotOHLC( O, H, L, C, "Traded", colorRed, styleBar , Null, Null, 0, 1, 1);
_SECTION_END();

h1=199.7;
cond=H==h1;
val2=ValueWhen(cond, L, 0);
_TRACE("val2="+val2);

//Due to Bug in amibroker valueWhen() fails for the first occurance of the expression when referencing future
//WORKAROUND set the first element of concerned array variable to 1 or higher
cond[0]=1;
val3=ValueWhen(cond, L, 0);
_TRACE("val3="+val3);


PlotShapes( IIF( cond, shapeSmallCircle, shapeNone), coloryellow, 0, L-ATR(5)); //
Filter=1;

//see the difference between val2 and val3 in exploration
AddColumn(val2, "ValueWhen(H=="+h1+", L, 0)");

AddColumn(val3, "with workaround ValueWhen(H=="+h1+", L, 0)");
valueWhenExplore2.png
 
Last edited:

Similar threads

Zerodha – Open Paperless Account

Open online account with Zerodha. Free delivery trading and Max Rs 20 for Intraday, F&O, Currency and Commodity Trading. Intraday High leverage with MIS, CO and BO.

Name:Phone:
Email:City:
State:
Are you a day trader?