Amibroker Lesson…The IIF function

If you’re an Amibroker user with no programming background or knowledge, you might be a little confused by the IIF function.

In this article I hope to resolve any confusion and illustrate to you how useful the IIF function can be when developing your strategy.

The following excerpt is taken for the Amibroker User Guide page which can be accessed here.

IIf( EXPRESSION, TRUE_PART, FALSE_PART )

“Immediate-IF” – a conditional function that returns the value of the second parameter (TRUE_PART) if the conditional expression defined by the first parameter (EXPRESSION) is true; otherwise, the value of third parameter is returned (FALSE_PART).

Please note that IIF is a function – so the result of evaluation is returned by that function and should be assigned to some variable.

IIf always evaluates both TRUE_PART and FALSE_PART, even though it returns only one of them.

Because of this, you should watch for undesirable side effects. The following example shows one common error made with IIF function:

IIF( condition, result = 7, result = 9 ); // THIS IS WRONG Correct usage is: result = IIF( condition, 7, 9 ); /* 7 or 9 is *returned* and assigned to a variable depending on condition */

The IIF function step-by-step

Let’s begin by creating a strategy…

We want to buy the SPY when the Close is lower than lowest low of the past 5 days.

Buy = C < Ref(LLV(L,5),-1) ;

We will exit open positions when the Close is higher than the previous day high.

Sell = C > Ref(H,-1) ;

We will enter and exit our positions on the open of the day which follows the buy or sell signal.

We can simulate this by using the SetTradeDelays() Function and the Buyprice and Sellprice variables.

So our strategy AFL looks like this…

//////////////////////////////

SetTradeDelays (1,1,1,1) ;

BuyPrice = Open;

SellPrice = Open;

Buy = C < Ref(LLV(L,5),-1) ;

Sell = C > Ref(H,-1) ;

//////////////////////////////

The strategy produced the following results between 01/01/2005 and today….

  • No. of trades = 164
  • % of Winners  = 71.95%
  • Average P/L% per trade = 0.45%
  • Average hold time = 4 days

The strategy is O.K. but we can probably do better.

To improve our strategy, we decide to use a different Sell rule depending upon the predominant trend of the market.

Let’s keep things simple;

If the Close is above the 200 day MA, we don’t want to exit our positions too soon. We can therefore introduce a rule which states that if the Close is above the 200 day MA, we will exit positions if the Close is higher than the previous 5 day high.

Sell =  C > Ref(HHV(H,5),-1) ;

If the Close is below the 200 day MA, we’ll use our original exit strategy…

Sell = C > Ref(H,-1) ;

To test our modified strategy, we can use the IIF function to control our conditional sell rule.

To remind you,the IIF function is written as:

IIf( EXPRESSION, TRUE_PART, FALSE_PART )

In this example, expression is our conditional statement which is either true or false. I.E. “the Close is greater than the 200 day MA”.

If true, assign True_Part to our variable. If false, assign False_Part to our variable.

The variable in this example is the Sell variable.

Sell = IIF (C > MA(C,200) , TRUE_PART, FALSE_PART )

We’ve already established that if the Close is greater than the 200 day MA, I.E. our expression is true…. We want our sell rule to be a Close higher than the previous 5 day high.

Sell = IIF (C > MA(C,200) , C > Ref(HHV(H,5),-1)  , FALSE_PART )

If our expression is false, we want our sell rule to be a Close that is higher than the previous day high.

Sell = IIF (C > MA(C,200) , C > Ref(HHV(H,5),-1)  , C > Ref(H,-1)  ) ;

The above statement says that if the close is greater than the 200 day MA, we will Sell our position if the close is higher than the previous 5 day high.

If the close isn’t greater than the 200 day MA, we will sell our position if the close is higher than the previous day high.

The complete formula is now:

//////////////////////////////

SetTradeDelays (1,1,1,1) ;

BuyPrice = Open;

SellPrice = Open;

Buy = C < Ref(LLV(L,5),-1) ;

Sell = IIF (C > MA(C,200) , C > Ref(HHV(H,5),-1)  , C > Ref(H,-1)  ) ;

///////////////////////////////

If you’re curious, the results of the modified strategy are below:

  • No. of trades = 135
  • % of Winners  = 73.33%
  • Average P/L% per trade = 0.71%
  • Average hold time = 8 days

Of course, you can be as creative with the IIF function as you like. If you have any of your own ideas that you’d like to share, please use the comments box below.

 

Leave A Response

* Denotes Required Field