How can I get current bar?

@phb - really you need to read this a couple of times:
http://www.amibroker.com/guide/h_understandafl.html

The fundamental thing is:
There is NO CURRENT BAR.
AmiBroker processes ENTIRE ARRAY at once. ALL bars in single operation.

If you write

X = H + L;

It internally does this:

// add every bar's high to every bar's low
for( i = 0; i < BarCount; i++ )
{
   X[ i ] = H[ i ] + L[ i ]; 
}

So bar by bar it adds high to low and result is the ARRAY. You do not see any “current” bar because it goes thru ALL bars.

If you want to process things bar by bar, you need to write explicit loop and then loop counter gives you current bar INSIDE THE LOOP.

for( i = 0; i < BarCount; i++ )
{
   if( Close[ i ] > .... )  // bar-by-bar processing 
   {

    }
}

Looping is much slower than array processing and if array solution exist, you should use array.

Your use of WriteIf is wrong.
Instead you should use either SelectedValue function (to get bar selected by vertical line) - that would give exactly the same result of what you would achieve with WriteIf

if( SelectedValue( Close ) > AlertPrice )
{
 ...
}

or use LastPrice function if you are interested always in most recent value (i.e. the LAST value), ignoring what is selected with vertical line on chart

if( LastValue( Close ) > AlertPrice )
{
....
}
1 Like