How can I get current bar?

Hi ! Is amibroker have function to get current bar in terms of number ?
Because of Funcion BarIndex() return in terms of array. So If I want to know current bar how can I get it ?
or is it have some coding technique that return current bars.

Thank you very much. ^^

Tried the function

LastValue() ?

Thank you Mr.NowToLook for your helping. :slight_smile:

I’ve already tried the function LastValue().
It’s only return the last value. So I want to get current bar in each bars.
My problem is BarIndex() is return in array. but i want to get BarIndex() in each bars in terms of Number.

You need to read How do I learn AFL post

I read this http://www.amibroker.com/guide/h_understandafl.html

still unable to understand simple way to get current bar values as numbers instead of Array.

I want to get barindex, O,H,L,C values as numbers instead of arrays.
this is the biggest confusion for new AFL programmers.

I tried using Lastvalue, SelectedValue. they are not giving desired results.

I simply wanted to compare current values as numbers in if, for, while conditons how to do that ?

any simple method ?

You might have read “How do I learn AFL” but you clearly haven’t UNDERSTOOD it. Re-read it again and again until you understand it.

You are still thinking in individual bars, which is NOT REQUIRED and the WRONG way to approach things. I suspect you want individual bars so that you can do loops. This is the WRONG way to approach things. Arrays and AFL approach is MUCH faster and clearer.

Re-read “How do I learn AFL” until you understand it.

Until then you are wasting your (and our) time.

3 Likes

@phb I think you need to give a clear example of what you have coded that is not giving you the desired result? And be clear on what your trying to accomplish so that forum members can be accurate with their advice (and post your code properly using the code tags).

I am using this method to overcome the issue of getting current bar values

if(WriteIf(LongV,"True","False") == "True")
{
	IntraTarget =  BuyPrice * 1.01;
	PosTarget = BuyPrice * 1.03;
           // . and do more code here like displaying those values.
}

instead I wanted to do this

if(LongV)
{
	IntraTarget =  BuyPrice * 1.01;
	PosTarget = BuyPrice * 1.03;
}

another example

AlertPrice = Param("AlertPrice",1, 1,99999);

If(Close > AlertPrice)
{
     	if(SoundAlert)
		PlaySound("C:\\Windows\\Media\\Windows Ding"); 		

	if(Popalert)
		PopupWindow(text,"Alert", 10);

        // write alert time and symbol name  to a text CSV file.
}

SigH = Valuewhen(BuySig, H);

if(Close > SigH)
{

// do something here 
}

@phb I only understood one of your questions (I think) so here is an example that may help.

// Example trading system rules

Buy = Cross( MACD(), Signal() );
Sell = Cross( Signal(), MACD() );

SigH = Valuewhen( Buy, H, 1 );
DoSomethingHere = IIf( Close > SigH, 100, Null ); // for "do something", I assigned number 100 


Filter = 1;
// just to add colours to the EXPLORE
Buy_color = IIf( Buy, colorLime, colorDefault );
Sell_color = IIf( Sell, colorRed, colorDefault );
AddColumn( IIf( Buy, 'B', IIf( Sell, 'S', Null ) ), "Signal", formatChar,
           colorDefault, IIf( Buy, Buy_color, IIf( Sell, Sell_color, colorDefault ) ) );
AddColumn( Close, "Close" );
AddColumn( High, "High" );
AddColumn( SigH, "SigH" );
AddColumn( DoSomethingHere, "DoSomethingHere" );

Producing values the the variables SigH and DoSomethingHere that are constant numbers until conditions change.
image

You had written earlier,

unable to understand simple way to get current bar values as numbers instead of Array.

So I hope this example helped.

For the other example I do not understand what "LongV" is ? And it is past my bedtime so hope some other forum members can help. Good luck!

1 Like

@portfoliobuilder Thank for you time. Once again you proved posting code will help resolve.
I learned about imroved usage of ValueWhen() with your answer.

LongV is also a signal condition based on volume.

in the below code I am trying to find out L or H of the candle met condition ShortSig
I need to get First signal after sarsigs and that bar values.

I used selected value. after clicking on any bar it is changing the marks placed at the signals.

sar1 = SAR(0.01);
sarsigs = C < sar1 AND Ref(C,-1) > Ref(sar1,-1);

ShortSig1 = Close < Open AND Close < superTrend AND C < sar1 AND C < ema100; 
ShortSig = ShortSig1 AND Open==High;
bi = BarIndex();

sigL = ValueWhen(ShortSig,L,1); // it gets latest signal value, but  I want first value after  _**sarsigs**_

for(i=SelectedValue(ValueWhen(sarsigs,bi)); i< BarCount-1; i++)
{
	if(ShortSig[i] == True)
	{
		sigL =L[i];
		i = BarCount;
	}
}

Short = (Ref(ShortSig,-1) AND L < Ref(L,-1)) OR  ShortSig1 AND biy >= sy AND bi >= biy AND  L < sigL;
Cover = H > sar1;
soff = H > sar1;
Short=ExRem(Short,Cover);
Cover=ExRem(Cover,Short);

ShortV=Flip(ShortSig,soff);
ssig = ShortSig AND (Ref(ShortV,-1) == 0);

PlotShapes(IIf(ssig, shapeDownTriangle, shapeNone),colorRed, 0, H, Offset=-20);
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);

@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

@Tomasz Thank you. I understood about the Calclulations.

But my requirement is when I am marking on the chart based on certain condiitons those marks getting vanished if I click on any bar or scroll this happenning when I use SelectionValue or Lastvalue.

LastValue(Close) is giving Close[barcount-1]. I need to get individual bar values so that I can do marking and draw trend lines etc.

if not selected any bar

image

after clicking any bar

image

I now understood how to use LastValue after seeing some examples here.

LastValue(valueWhen(condtion,array)) is solving my problem.

for(i=LastValue(ValueWhen(sarsigs,bi)); i< BarCount-1; i++)
{
	if(ShortSig[i] == True)
	{
		sigL =L[i];
		i = BarCount;
	}
}

above code replaced with

sigL = LastValue(ValueWhen(ShortSig,L));

Thank you @Tomasz

1 Like