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

Re: [Amibroker] Creating a toggle switch



PureBytes Links

Trading Reference Links

Hello,
 
 
And of course you can use IIF inside the loop but you should always think if you are operating on arrays or on numbers.
The rules are that you can not use array where just number is expected. For example if you are assigning value
to the array element then a number is needed.
 
a[ 0 ] = IIF( x[ 0 ] > y[ 0 ], C[ 0 ], H[ 0 ] ); // all parameters are numbers -> result is also a number and can be assigned to single array element
 
 
Best regards,
Tomasz Janeczko
amibroker.com
----- Original Message -----
Sent: Monday, June 27, 2005 11:18 PM
Subject: Re: [Amibroker] Creating a toggle switch

Hello,
 
Explained in the User's Guide, "Common coding mistakes"
 

if-else statement needs boolean (or single numeric expression), not array

The if keyword executes statement1 if expression is true (nonzero); if else is present and expression is false (zero), it executes statement2. After executing statement1 or statement2, control passes to the next statement. Expression must be boolean ( True/False) type (so it CANNOT be ARRAY because there would be no way do decide whether to execute statement1 or not, if for example array was: [True,True,False,.....,False,True] )

if( expression )
   statement1
else
   statement2

EXAMPLE

if( Close > Open// WRONG
   Color =
colorGreen; //statement 1
else
   Color =
colorRed; //statement 2

Plot(Close,"Colored Price",Color,styleCandle);

The above example is wrong, as both Open and Close are arrays and such expression as Close > Open is also an ARRAY. The solution depends on the statement. It’s either possible to implement it on bar-by-bar basis, with use of FOR loop:

for( i = 0; i < BarCount; i++ )
{
  
if( Close[ i ] > Open[ i ] ) // CORRECT
       Color[ i ] =
colorGreen;
  
else
       Color[ i ] =
colorRed;
}

Plot( Close, "Colored Price", Color, styleCandle );

It is also possible in this case to use IIf( ) function:

Color = IIf( Close > Open, colorGreen, colorRed ); // ALSO CORRECT - working directly on arrays
Plot( Close, "Colored Price", Color, styleCandle );


Best regards,
Tomasz Janeczko
amibroker.com
----- Original Message -----
From: Terry
Sent: Monday, June 27, 2005 5:31 PM
Subject: RE: [Amibroker] Creating a toggle switch

Tomasz will have to explain the circumstances in which this works or not.

I just know I have gotten errors trying to use the array IIF inside a loop, which doesn’t even make sense since which part of the array would you be referencing when looping through bar by bar?

 

Did you try my version? Does it work?

--

Terry

-----Original Message-----
From:
amibroker@xxxxxxxxxxxxxxx [mailto:amibroker@xxxxxxxxxxxxxxx] On Behalf Of wavemechanic
Sent: Monday, June 27, 2005 09:05
To:
amibroker@xxxxxxxxxxxxxxx
Subject: Re: [Amibroker] Creating a toggle switch

 

 

----- Original Message -----

From: Terry

Sent: Monday, June 27, 2005 9:24 AM

Subject: RE: [Amibroker] Creating a toggle switch

 

You can't use IIF inside a loop. I'm surprised you aren't getting
compiler errors.

 

I must not be following something here.  Look at the following loop from the Users Manual modified with an IIF which works OK.  What am I missing?

 

Len = Param("Len", 20, 10, 50, 10);

factor = 2 / (Len + 1);

e1 = e2 = Close[0];

for(i = 0; i < BarCount; i++)

{

e1 = factor * Close[ i ] + ( 1 - factor ) * e1;

e2 = factor * e1 + (1 - factor) * e2;

myDemaIIF[i] = IIf(len == 20, 2 * e1 - e2, 0);

}

Plot(myDemaIIF, "Dema in iif loop", colorRed);

Plot(DEMA(C, Len), "Built-in DEMA", colorWhite);



Please note that this group is for discussion between users only.

To get support from AmiBroker please send an e-mail directly to
SUPPORT {at} amibroker.com

For other support material please check also:
http://www.amibroker.com/support.html






Please note that this group is for discussion between users only.

To get support from AmiBroker please send an e-mail directly to
SUPPORT {at} amibroker.com

For other support material please check also:
http://www.amibroker.com/support.html





Please note that this group is for discussion between users only.

To get support from AmiBroker please send an e-mail directly to
SUPPORT {at} amibroker.com

For other support material please check also:
http://www.amibroker.com/support.html





Please note that this group is for discussion between users only.

To get support from AmiBroker please send an e-mail directly to
SUPPORT {at} amibroker.com

For other support material please check also:
http://www.amibroker.com/support.html





YAHOO! GROUPS LINKS