{"id":1349,"date":"2016-04-20T21:02:24","date_gmt":"2016-04-20T20:02:24","guid":{"rendered":"http:\/\/www.amibroker.com\/kb\/?p=1349"},"modified":"2020-02-04T14:07:12","modified_gmt":"2020-02-04T13:07:12","slug":"calling-custom-user-functions-in-our-code","status":"publish","type":"post","link":"https:\/\/www.amibroker.com\/wordpress\/kb\/2016\/04\/20\/calling-custom-user-functions-in-our-code\/","title":{"rendered":"Calling custom user functions in our code"},"content":{"rendered":"

AFL language allows us to define reusable functions that can be used in our formulas. The following chapter of the manual explains the procedure in details: http:\/\/amibroker.com\/guide\/a_userfunctions.html<\/a><\/p>

When we want to call such function in our formula, we should add function definition into our code, so AmiBroker could identify and interpret custom keyword properly. Consequently, if we use the function in multiple chart panes, each of the formulas should contain the function definition first.<\/p><\/span>\/\/ custom function definition
<\/span>function <\/span>myMACD<\/span>( array, <\/span>fast<\/span>, <\/span>slow <\/span>)
{
   return <\/span>EMA<\/span>( array, <\/span>fast <\/span>) - <\/span>EMA<\/span>( array, <\/span>slow <\/span>);
}

<\/span>\/\/ use of function
<\/span>Plot<\/span>( <\/span>myMACD<\/span>( <\/span>High<\/span>, <\/span>12<\/span>, <\/span>26 <\/span>), <\/span>"Open MACD"<\/span>, <\/span>colorRed <\/span>)<\/code>

Since we may potentially define a large group of our own functions, pasting the definitions manually may not be very convenient. To avoid that, we can use #include statement and group our definitions in a separate AFL file which will be called with a single statement from our main code.<\/p>

To create such file we should do the following:<\/p>

  1. Create a new formula. The preferred location is in Include folder in chart windows, we can in fact choose any custom location of the file.
    include<\/p>
  2. We can also rename the file to a descriptive name, for example myfunctions.afl:
    include<\/p>
  3. Now we can edit the file and paste our function definitions, then save the file:
    <\/span>function <\/span>myMACD<\/span>( array, <\/span>fast<\/span>, <\/span>slow <\/span>)
    {
       return <\/span>EMA<\/span>( array, <\/span>fast <\/span>) - <\/span>EMA<\/span>( array, <\/span>slow <\/span>);
    <\/code>
  4. Now in our main file we can use only a reference to myfunctions.afl file:<\/span>\/\/ include our definitions
    #include <myfunctions.afl>

    \/\/ use of function
    <\/span>Plot<\/span>( <\/span>myMACD<\/span>( <\/span>High<\/span>, <\/span>12<\/span>, <\/span>26 <\/span>), <\/span>"Open MACD"<\/span>, <\/span>colorRed <\/span><\/code><\/ol>

    We don’t have to specify the path, because we saved our formula in the folder, which is specified as a ‘default include path’ in Tools\u2013>Preferences\u2013>AFL<\/strong>:<\/p>

    include<\/p>

    In other cases we should provide full path to the file – #include is a pre-processor command, therefore this time we use single backslashes in the path:<\/p>

    #include “C:\\Program Files\\AmiBroker\\AFL\\common.afl”<\/p>

    More information about include command can be found at:<\/p>

    AFL language allows us to define reusable functions that can be used in our formulas. The following chapter of the manual explains the procedure in details: http:\/\/amibroker.com\/guide\/a_userfunctions.htmlWhen we want to call such function in our formula, we should add function definition into our code, so AmiBroker could identify and interpret custom keyword properly. Consequently, if […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"_links":{"self":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/1349"}],"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=1349"}],"version-history":[{"count":2,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/1349\/revisions"}],"predecessor-version":[{"id":1561,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/1349\/revisions\/1561"}],"wp:attachment":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/media?parent=1349"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/categories?post=1349"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/tags?post=1349"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}

    http:\/\/www.amibroker.com\/guide\/afl\/_include.html<\/a><\/p>","protected":false},"excerpt":{"rendered":"