[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[amibroker] AFL IIF() function


  • To: "AmiBroker Mailing List" <amibroker@xxxx>
  • Subject: [amibroker] AFL IIF() function
  • From: "Tomasz Janeczko" <tjaneczk@xxxx>
  • Date: 18 Jun 1999 19:37:21 -0000

PureBytes Links

Trading Reference Links

Hi Everyone,

I just wanted to explain the IIF() function which seems to
be difficult to understand and use properly.

The most important thing is that IIF is a FUNCTION and it is NOT
a conditional statement.
That's because I given the name IIF to it (oposite to simple IF)

In a "normal" programming language you may write something like that

IF A > 5
THEN DoThing1
ELSE DoThing2.

And the behaviour of this statement is that if A is greater then 5
"DoThing1" will be executed elsewhere "DoThing2"
will be executed. Note that this statement does not return any value!
So if you want to assign a value to B you may write something like that:
IF A>5
THEN B=0
ELSE B=1;

In current implementation there is no "normal" IF statement. There is
however IIF function. But how it works?

IIF() function is somewhat different. This is a function so IT RETURNS
VALUE!!!
If you want to assign a value to B using IIF function you can write

B = IIF( A>5, 0, 1 );

This works almost identically as C-language conditional assignment ( ? : )
operator
b = a > 5 ? 0 : 1;

But please keep in mind that AFL is ARRAY-oriented so IIF() function in
almost all cases will return
an array.
Let's say that A holds the following array:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10

If you write
B = IIF( A > 5, 0, 1 );

AFL interpreter will give you following values in resulting B array:
1, 1, 1, 1, 1, 0, 0, 0, 0, 0

===========================
Now practical example.

Donald Dalley asked me how to plot following indicator (here comes his
original
description):

/* Price-Volume Rank Indicator
Use a 1 if price and volume are up
Use a 2 if price is up and volume is down
Use a 3 if price and volume are down
Use a 4 if price is down and volume is up

Plot a 5-day and a 10-day MA of these values.
Buy/Sell at 5/10-day crossovers.
Buy when fast line crosses below 2.5.
Sell when fast line crosses above 2.5.
*/
================================
The solution:

P1 = REF( CLOSE, -1 ); // previous closing price
V1 = REF( VOLUME, -1); // previous volume

/* following IIFs generate an ARRAY which will be used for
moving average smoothing later. The logic is as follows:
Use a 1 if price and volume are up
Use a 2 if price is up and volume is down
Use a 3 if price and volume are down
Use a 4 if price is down and volume is up
*/
PVR = IIF( CLOSE > P1 AND VOLUME > V1, 1,
IIF( CLOSE > P1 AND VOLUME <= V1, 2,
IIF( CLOSE <= P1 AND VOLUME <= V1, 3, 4 )));

/* now comes MA-smoothing */
GRAPH0 = MA( PVR, 5 );
GRAPH1 = MA( PVR, 10 ) ;

=================

And that's all.

I wish to thank Donald Dalley for his decription of Price-Volume Rank.

If anyone else has questions regarding AFL please don't hesitate to ask.

Best regards,

Tomasz Janeczko
===============
Visit AmiBroker WWW Site at:
http://www.polbox.com/a/amibrok/



------------------------------------------------------------------------

eGroups.com home: http://www.egroups.com/group/amibroker
http://www.egroups.com - Simplifying group communications