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

#11
It's always a challenge to work with AFL if one comes from a procedural programming (C, C++, Java etc.) background as AFL is a vector programming language (another e.g. being R). In AFL, data can be readily used as an array of OHLC, V and OI rather than one data point at a time. Although it is also possible to access it one-by-one by looping through as you are attempting to do. If you want to keep your AFL code simple, learn and use the data array directly as far as possible. You may not find it easy to begin with but once you get a hang of it you will start appreciating it. Lookup for AFL code samples in the AFL code library or AFL help itself.

Hope the above information helps you becoming an expert in AFL soon.

Cheers
Curious_Trader
 

trash

Well-Known Member
#12
Also, for loops do not work when it comes to analysing data. It says array elements cannot be used in for loops.
Your first sentence conclusion is wrong.
Would you actually care reading the help of AmiBroker before coming up with such nonsense? See coding mistakes for example.

And of course array elements can be accessed within loops.

You just have to do it correctly. You not not being able to do it correctly is far away from being equal to "not working".


Example

This one is wrong

Code:
for( i = 0; i < BarCount; i++ )
{
  if( MACD() > 0 )
	// then do something
}
because you try to check entire array but not elements of array.

This one is correct
Code:
aMACD = MACD();

for( i = 0; i < BarCount; i++ )
{
  if( aMACD[i] > 0 )
	// then do something
}
because now you iterate bar by bar through MACD array.
 
Last edited:

trash

Well-Known Member
#13
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.


Code:
currentCandle = IIF(Close<Ref(Close,-1), 1, 0);
currentCandle = IIF(Close>Ref(Close,-1), 2, 0);
currentCandle = IIF(Close == Ref(Close,-1), 3, 0);
Code:
currentCandle = IIF(Close<Ref(Close,-1), 1, Null);
currentCandle = IIF(Close>Ref(Close,-1), 2, Null);
currentCandle = IIF(Close == Ref(Close,-1), 3, Null);
Code:
currentCandle = ValueWhen(Close<Ref(Close,-1), 1);
currentCandle = ValueWhen(Close>Ref(Close,-1), 2);
currentCandle = ValueWhen(Close == Ref(Close,-1), 3);
Plot those ones to see the differences
 

mittens

Active Member
#14
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.
You just need one line of code for all that :
currentCandle = iif( close > ref(close,-1), 1, iif( close < ref(close,-1), 2, 3));
 
#15
Code:
currentCandle = IIF(Close<Ref(Close,-1), 1, 0);
currentCandle = IIF(Close>Ref(Close,-1), 2, 0);
currentCandle = IIF(Close == Ref(Close,-1), 3, 0);
Code:
currentCandle = IIF(Close<Ref(Close,-1), 1, Null);
currentCandle = IIF(Close>Ref(Close,-1), 2, Null);
currentCandle = IIF(Close == Ref(Close,-1), 3, Null);
Code:
currentCandle = ValueWhen(Close<Ref(Close,-1), 1);
currentCandle = ValueWhen(Close>Ref(Close,-1), 2);
currentCandle = ValueWhen(Close == Ref(Close,-1), 3);
Plot those ones to see the differences
that's neat and clean ..+1 button disabled
 

Similar threads