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.
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 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.
@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.
}
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
}
@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);
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
@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.