Get the first future occurance

The below should work for all bars if condition1 has already occurred at least once up till that bar. But how do i make this work for a bar when condition1 has not occurred even once. Example Let say condition1 is true for the very first time on this chart on bar number 50. Lets say I am at bar number 10 then how do i get the required value?

condition1=H>Ref(HHV(H,5), -1);
ClosePriceOfFuture = valueWhen(Condition1, C, 0);

http://www.amibroker.com/guide/afl/valuewhen.html

"and negative values for n - this enables referencing future"

Let use a better example. see the below code. The exploration (see screenshot below) is run in periodicity 10 minutes with the data attached below.

_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();

_TRACE( "!CLEAR!" );
_TRACE("barindex()="+barindex()); //532, 132

HigherThanPrevious5=H>Ref(HHV(H,5), -1);

BarIndexFutCondition=ValueWhen(HigherThanPrevious5, BarIndex(), 0);

_TRACE("BarIndexFutCondition="+BarIndexFutCondition+", HigherThanPrevious5="+HigherThanPrevious5+", isnull(HigherThanPrevious5)="+isnull(HigherThanPrevious5));


PlotShapes( IIF( HigherThanPrevious5, shapeSmallCircle, shapeNone ), colorYellow, 0, L - ATR(5)/3);

Filter=1;

AddColumn(BarIndex(), "BarIndex()");
AddColumn(HigherThanPrevious5, "HigherThanPrevious5");
AddColumn(BarIndexFutCondition, "BarIndexFutCondition");

Get data here https://uploadnow.io/f/Ksy1dsn

The question is why valueWhen returning blanks for the first few bars?

ValueWhen by design starts outputing values from very first "true" value in "condition" array.

1 Like

Ok, But that aspect/limitation of valuewhen is not documented anywhere.
What should i use to get future value before the first occurrence? is looping forward the only way?

I created this custom function that works in all conditions except the first bar (barindex=0). I have tested it to an extent so use it at your own risk. The exploration below makes my case.

function ValueWhenCustom(condition, array, nth)
{
cumCondition=Cum(condition);
nf=IIf(nth==0, 1, -1*nth + 1);
expr=ValueWhen(cumCondition==nf && Ref(cumCondition, -1)==nf-1, array);
retval=IIf(cumCondition==0 && nth<=0, Ref(expr, BarCount), ValueWhen(condition, array, nth));
//_TRACE("cumCondition="+cumCondition);
return(retval);
}
HigherThanPrevious5=H>Ref(HHV(H,5), -1);
BarIndexFutCondition=ValueWhen(HigherThanPrevious5, BarIndex(), 0);

_TRACE("BarIndexFutCondition="+BarIndexFutCondition+", HigherThanPrevious5="+HigherThanPrevious5+", isnull(HigherThanPrevious5)="+isnull(HigherThanPrevious5));

BarIndexFutCondition1=ValueWhenCustom(HigherThanPrevious5, BarIndex(), 0);
_Trace("BarIndexFutCondition1="+BarIndexFutCondition1);

PlotShapes( IIF( HigherThanPrevious5, shapeSmallCircle, shapeNone ), colorYellow, 0, L - ATR(5)/3);

Filter=1;

AddColumn(BarIndex(), "BarIndex()");
AddColumn(HigherThanPrevious5, "HigherThanPrevious5");
AddColumn(BarIndexFutCondition, "BarIndexFutCondition");
AddColumn(BarIndexFutCondition1, "BarIndexFutCondition1");