AFL help required .. How do I select first bar from available data?

#1
Let's say I need to check the high of the very first candlestick of the chart i have opened. Which function should i use for it? Can someone post the code for it? Your help would be appreciated.
 

mittens

Active Member
#2
// first bar of the database
firstBar = BarIndex() == 0;
firstbarHigh = ValueWhen( firstBar, High);
firstbarLow = ValueWhen( firstBar, Low );

// First visible bar on the screen
firstVisibleHigh = FirstVisibleValue( High );
firstVisibleLow = FirstVisibleValue( Low );

// Last visible bar on screen
lastVisibleHigh = LastVisibleValue( High );
lastVisibleLow = LastVisibleValue( Low );

Thanks
 
#3
Thanks Mittens. Ive one more question. Let's say I was looking at a random candlestick (n). How do i select the previous of the n'th candlestock or the next one in afl? i.e (n+1) candlestick, (n+2)candlestick, (n-1) candlestick and so on?
 

mittens

Active Member
#4
Thanks Mittens. Ive one more question. Let's say I was looking at a random candlestick (n). How do i select the previous of the n'th candlestock or the next one in afl? i.e (n+1) candlestick, (n+2)candlestick, (n-1) candlestick and so on?
You should use "Ref()" function.

Say you are at 'n', you want the previous value
closeNow = Close
closeNbarsAge = Ref(close, -1 * nBar ); // -ve number
closeNbarInFuture = Ref( close, 1 * nBar ); // future +ve number

But the future value under no circumstance can be more than 'Barcount' value. If it is more than 'Barcount' it will give errors.

variable 'Barcount' is system defined as the total number of bars in database.


Amibroker help :
SYNTAX Ref( ARRAY, period )
RETURNS ARRAY
FUNCTION References a previous or subsequent element in a ARRAY. A positive period references "n" periods in the future; a negative period references "n" periods ago. The function accepts periods parameter that can be constant as well as time-variant (array).
EXAMPLE The formula "ref( CLOSE, -14 )" returns the closing price 14 periods ago. Thus, you could write the 14-day price rate-of-change (expressed in points) as "C - ref( C, -14 )." The formula "ref( C, 12 )" returns the closing price 12 periods ahead (this means looking up the future)
 
#5
Thanks a ton. It really helped me.

Let's say i used firstBar = BarIndex() == 0; to select the first bar. If i need to go to the last bar(Barcount[] - 1) by selecting and looking at the high of each bar from 0 to n, how do i do that?

I need to stop at each bar starting from bar1, look at the high, then go to the next bar, look at the high, and so on till i get to the last bar in the DataBase.
 

mittens

Active Member
#6
Thanks a ton. It really helped me.

Let's say i used firstBar = BarIndex() == 0; to select the first bar. If i need to go to the last bar(Barcount[] - 1) by selecting and looking at the high of each bar from 0 to n, how do i do that?

I need to stop at each bar starting from bar1, look at the high, then go to the next bar, look at the high, and so on till i get to the last bar in the DataBase.
By using a loop, something like this :

start = 0; // Starting from first bar
for ( barNumber = start;barNumber < BarCount;barNumber++ ) // till last bar Barcount
{
currentBarHigh[barNumber] = High[barNumber]; // you can get each bar here
}

But if you can use Amibroker array then dont use loops, as loops are slow.
Depends on what you want to code, sometimes we can not avoid loops.
 
#7
hi .. actually i was looking for the answer of the same. can you tell me this will give me day's first bar or the first bar of the complete chart ..??

i am a intraday trader and i want to calculate open high low and close of the first bar of the intraday chart for a given time frame.

can u please help me in this ..??
 

mittens

Active Member
#8
For first bar of the day values :

newday = Day() != Ref(Day(), -1);
firstbarOfDayOpen = ValueWhen( newday, Open);
firstbarOfDayHigh = ValueWhen( newday, High);
firstbarOfDayLow = ValueWhen( newday, Low);
firstbarOfDayClose = ValueWhen( newday, Close);
 
#9
Let us say i want to test 4 conditions simultaneously and assign each result for a different variable. How do i do that?

For example, I want to look at the close of the currentCandle and then compare it to the previous candle. If it is higher, assign the digit 1 to the variable currentCandle. If its lower, assign 2, if its same, assign 3.

When I use,

currentCandle = IIF(Close<(Close,-1) , 1 , "Im forced to give an else value here");

I dont want to give an else value.

I want it to say this

currentCandle = IIF(Close<(Close,-1), 1, "do nothing, just store the value of 1);
currentCandle = IIF(Close>(Close,-1), 2, "do nothing, just store the value of 2);
currentCandle = IIF(Close=(Close,-1), 3, "do nothing, just store the value of 3);


Thanks,
Pranav.
 

Similar threads