{"id":2210,"date":"2011-04-22T10:20:26","date_gmt":"2011-04-22T10:20:26","guid":{"rendered":"http:\/\/www.amibroker.org\/userkb\/?p=2210"},"modified":"2018-09-02T19:49:43","modified_gmt":"2018-09-02T19:49:43","slug":"using-iif-if-and-switch-functions","status":"publish","type":"post","link":"http:\/\/www.amibroker.org\/editable_userkb\/2011\/04\/22\/using-iif-if-and-switch-functions\/","title":{"rendered":"Using IIF(), IF() and Switch() functions"},"content":{"rendered":"
Many newcomers to AFL are confused by the IF(), IIF() and Switch(). This post gives a few simple examples of their use. The IF() and Switch() are program flow control statements, the IIF() is a function that acts on all elements of an input array and returns an output array. <\/p>\n
In all but the simplest applications the Switch() is the preferred method to the IF() to change program flow. It can be used to code complex decision trees<\/a> and state machines<\/a>, for example as these are often needed in automated trading systems.<\/p>\n For more detailed explanations click IF()<\/a>, IIF()<\/a>, or Switch()<\/a>. A search of the afl library<\/a> will also get you many more examples.<\/p>\n It is possible to use if()s to individually test and modify each bar in an array for a condition. An example on how this would be done is shown in the function below (copied from the AmiBroker help). This function is an AFL equivalent for the IIF() function.<\/p>\n While the above approach works, using the IIF() function always provides a better and faster solution. The IIF() is very powerful and should be used whenever possible. Below are a few simple examples to get started. btw, It is highly unlikely that you will be able to improve on execution time by using a loop or writing a DLL. <\/p>\n To color all bars that fall on a Monday White:<\/p>\n IIF()s Can be nested. This example colors Monday bars White, Wednesday bars Blue and Friday bars Yellow:<\/p>\n One of the most common applications for the if() is to select what you want to see on your chart:<\/p>\n In the above example the IF() basically selects one of two sections of code.\u00a0 To select one of many options you could the use the else-if extension:<\/p>\n When there are many conditions, the lengthy If() expressions can become confusing, difficult to compose, and difficult to modify. In such cases it is often better to use the Switch() statement. Using a simple Switch() the last example looks much cleaner:<\/p>\nThe IIF() function<\/h3>\n
<\/span>function <\/span>IIF_AFL<\/span>( <\/span>condition<\/span>, <\/span>inputA<\/span>, <\/span>inputB <\/span>)\r{\r <\/span>result <\/span>= <\/span>Null<\/span>;\r for( <\/span>bar <\/span>= <\/span>0<\/span>; <\/span>bar <\/span>< <\/span>BarCount<\/span>; <\/span>bar<\/span>++ )\r {\r if( <\/span>condition<\/span>[ <\/span>bar <\/span>] )\r <\/span>result<\/span>[ <\/span>bar <\/span>] = <\/span>inputA<\/span>[ <\/span>bar <\/span>];\r else\r <\/span>result<\/span>[ <\/span>bar <\/span>] = <\/span>inputB<\/span>[ <\/span>bar <\/span>];\r }\r return <\/span>result<\/span>;\r}<\/span><\/pre>\n
Color <\/span>= <\/span>IIf<\/span>( <\/span>DayOfWeek<\/span>()==<\/span>1<\/span>, <\/span>colorWhite<\/span>, <\/span>colorBlack<\/span>);\r<\/span>Plot<\/span>( <\/span>C<\/span>, <\/span>"Close"<\/span>, <\/span>color<\/span>, <\/span>styleBar <\/span>);<\/span><\/pre>\n
D <\/span>= <\/span>DayOfWeek<\/span>();\r<\/span>Color <\/span>= <\/span>IIf<\/span>(<\/span>D<\/span>==<\/span>1<\/span>, <\/span>colorWhite<\/span>, <\/span>IIf<\/span>(<\/span>D<\/span>==<\/span>3<\/span>, <\/span>colorBlue<\/span>, <\/span>IIf<\/span>(<\/span>D<\/span>==<\/span>5<\/span>, <\/span>colorYellow<\/span>, <\/span>colorBlack<\/span>)));\r<\/span>Plot<\/span>( <\/span>C<\/span>, <\/span>"Close"<\/span>, <\/span>color<\/span>, <\/span>styleBar <\/span>);<\/span><\/pre>\n
The IF() Statement<\/h3>\n
\rPlot<\/span>( <\/span>C<\/span>, <\/span>"Close"<\/span>, <\/span>colorBlack<\/span>, <\/span>styleBar <\/span>);\r<\/span>ShowMA10 <\/span>= <\/span>ParamToggle<\/span>( <\/span>"Moving Average"<\/span>, <\/span>"MA|EMA"<\/span>, <\/span>0 <\/span>);\r\rif ( <\/span>ShowMA10 <\/span>)\r{\r<\/span>Plot<\/span>( <\/span>MA<\/span>( <\/span>C<\/span>, <\/span>10 <\/span>), <\/span>"MA10"<\/span>, <\/span>colorWhite<\/span>, <\/span>styleLine <\/span>);\r}\relse\r{\r<\/span>Plot<\/span>( <\/span>EMA<\/span>( <\/span>C<\/span>, <\/span>10 <\/span>), <\/span>"MA10"<\/span>, <\/span>colorWhite<\/span>, <\/span>styleLine <\/span>);\r}\r<\/span><\/pre>\n
\rSelectedIndicator <\/span>= <\/span>ParamList<\/span>( <\/span>"Show"<\/span>, <\/span>"MA10,MA50,MA100"<\/span>, <\/span>1 <\/span>);\r\rif ( <\/span>SelectedIndicator <\/span>== <\/span>"MA10" <\/span>)\r{\r<\/span>Plot<\/span>( <\/span>MA<\/span>( <\/span>C<\/span>, <\/span>10 <\/span>), <\/span>"MA10"<\/span>, <\/span>colorBlue<\/span>, <\/span>styleLine <\/span>);\r}\relse\rif ( <\/span>SelectedIndicator <\/span>== <\/span>"MA10" <\/span>)\r{\r<\/span>Plot<\/span>( <\/span>EMA<\/span>( <\/span>C<\/span>, <\/span>50 <\/span>), <\/span>"MA10"<\/span>, <\/span>colorRed<\/span>, <\/span>styleLine <\/span>);\r}\relse\rif ( <\/span>SelectedIndicator <\/span>== <\/span>"MA100" <\/span>)\r{\r<\/span>Plot<\/span>( <\/span>EMA<\/span>( <\/span>C<\/span>, <\/span>100 <\/span>), <\/span>"MA100"<\/span>, <\/span>colorYellow<\/span>, <\/span>styleLine <\/span>);\r}\r<\/span><\/pre>\n
The Switch() Statement<\/h3>\n
\rSelectedIndicator <\/span>= <\/span>ParamList<\/span>( <\/span>"Show"<\/span>, <\/span>"MA10,MA50,MA100"<\/span>, <\/span>1 <\/span>);\rswitch ( <\/span>SelectedIndicator <\/span>)\r{\r\rcase <\/span>"MA10"<\/span>:\r<\/span>Plot<\/span>( <\/span>MA<\/span>( <\/span>C<\/span>, <\/span>10 <\/span>