{"id":497,"date":"2014-10-05T17:59:57","date_gmt":"2014-10-05T22:59:57","guid":{"rendered":"http:\/\/www.amibroker.com\/kb\/?p=497"},"modified":"2014-12-03T08:30:05","modified_gmt":"2014-12-03T13:30:05","slug":"what-are-constants-in-afl-and-how-they-work","status":"publish","type":"post","link":"https:\/\/www.amibroker.com\/wordpress\/kb\/2014\/10\/05\/what-are-constants-in-afl-and-how-they-work\/","title":{"rendered":"What are constants in AFL and how they work"},"content":{"rendered":"

The AFL language contains many pre-defined words like: shapeUpArrow, stopTypeTrailing, colorRed, styleThick, inDaily and many more. These are examples of constants. As written in AFL language specification (http:\/\/www.amibroker.com\/guide\/a_language.html<\/a>): Constants are tokens representing fixed numeric or character values<\/em>.<\/p>

To better explain what this means, let us consider example of PI constant, which equals 3.14159265358979….. PI is the name of constant we use this name in mathematical equations, because it is easier and more practical to use than using the numerical value each time. Constants in AFL serve the same purpose, each of these words represents certain value properly interpreted by the program in the context they are used.<\/p>

That is why using the following statement in backtesting code:<\/p>ApplyStop<\/span>( <\/span>stopTypeTrailing<\/span>, <\/span>stopModePercent<\/span>, <\/span>10 <\/span>)<\/code>

is much better to use than cryptic statement like:<\/p>ApplyStop<\/span>( <\/span>2<\/span>, <\/span>1<\/span>, <\/span>10 <\/span>)<\/code>

Both commands are equivalent, because value of stopTypeTrailing constant equals 2 and value of stopModePercent constant equals 1, yet the first version is much more understandable.<\/p>

There is also another reason to use pre-defined constants rather than hard-coded numbers in the code. If for any reason the internal value of given constant changes due to development needs – all formulas using constants will continue to work properly (because new version would interpret them properly), while hard-coded numbers may change the code execution. For example – inWeekly and inMonthly constants have changed with introduction of N*inDaily timeframes, however if we always used:
TimeFrameSet( in Weekly ); in the code, then such internal change does not really affect our formulas at all.<\/p>

There is one more example worth discussing – in the documentation of PlotShapes function we can find:<\/p>Plot<\/span>( <\/span>Close<\/span>, <\/span>"Price"<\/span>, <\/span>colorBlack<\/span>, <\/span>styleCandle <\/span>);
<\/span>\/\/
<\/span>Buy <\/span>= <\/span>Cross<\/span>( <\/span>MACD<\/span>(), <\/span>Signal<\/span>() );
<\/span>Sell <\/span>= <\/span>Cross<\/span>( <\/span>Signal<\/span>(), <\/span>MACD<\/span>() );
<\/span>\/\/
<\/span>shape <\/span>= <\/span>Buy <\/span>* <\/span>shapeUpArrow <\/span>+ <\/span>Sell <\/span>* <\/span>shapeDownArrow<\/span>;
<\/span>\/\/
<\/span>PlotShapes<\/span>( <\/span>shape<\/span>, <\/span>IIf<\/span>( <\/span>Buy<\/span>, <\/span>colorGreen<\/span>, <\/span>colorRed <\/span>), <\/span>0<\/span>, <\/span>IIf<\/span>( <\/span>Buy<\/span>, <\/span>Low<\/span>, <\/span>High <\/span>) )<\/code>

So – what does the multiplication mean in the above context? If we remember that constants are in fact just numbers, and boolean True<\/strong> in AFL has numeric value of 1, while boolean False<\/strong> has numeric value of 0, then:<\/p>

– if Buy<\/strong> is True<\/strong> (equals 1) and Sell<\/strong> is False<\/strong> (equals 0), then the result of such calculation will be<\/p>

shape = 1 * shapeUpArrow + 0 * shapeDownArrow = shapeUpArrow<\/code><\/p>

– if Buy<\/strong> is False<\/strong> (equals 0) and Sell<\/strong> is True<\/strong> (equals 1), then the result of such calculation will be:<\/p>

shape = 0 * shapeUpArrow + 1 * shapeDownArrow = shapeDownArrow<\/code> <\/p>

The above approach is kind of shortcut that saves using conditional statements. It would work correctly only if Buy<\/strong> and Sell<\/strong> signals never occur on the same bar and only if we assign just 0 or 1 (False<\/strong> \/ True<\/strong>) to Buy<\/strong> and Sell<\/strong> arrays. Otherwise the result of calculations would be different. The internal value of shapeUpArrow<\/strong> is 1 and ShapeDownArrow<\/strong> is 2, so in situation, where both Buy<\/strong> and Sell<\/strong> signals were true, we would get<\/p>

shape = 1 * shapeUpArrow + 1 * shapeDownArrow = shapeUpArrow + shapeDownArrow = 1 + 2 = 3<\/code><\/p>

So – we would then pass number 3 to PlotShapes function and this is neither shapeUpArrow<\/strong> nor ShapeDownArrow<\/strong>, but a different shape. That is why in general case it is better to use conditional function IIf, like shown below:<\/p>shape <\/span>= <\/span>IIf<\/span>( <\/span>Buy<\/span>, <\/span>shapeUpArrow<\/span>, <\/span>IIf<\/span>( <\/span>Sell<\/span>, <\/span>shapeDownArrow<\/span>, <\/span>shapeNone <\/span>) )<\/code>

This way we are always sure that returned value will be shapeUpArrow<\/strong> or shapeDownArrow<\/strong> or shapeNone<\/strong>.<\/p>","protected":false},"excerpt":{"rendered":"

The AFL language contains many pre-defined words like: shapeUpArrow, stopTypeTrailing, colorRed, styleThick, inDaily and many more. These are examples of constants. As written in AFL language specification (http:\/\/www.amibroker.com\/guide\/a_language.html): Constants are tokens representing fixed numeric or character values.To better explain what this means, let us consider example of PI constant, which equals 3.14159265358979….. PI is the […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[53],"_links":{"self":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/497"}],"collection":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/comments?post=497"}],"version-history":[{"count":2,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/497\/revisions"}],"predecessor-version":[{"id":504,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/497\/revisions\/504"}],"wp:attachment":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/media?parent=497"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/categories?post=497"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/tags?post=497"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}