{"id":1082,"date":"2015-02-03T17:35:59","date_gmt":"2015-02-03T22:35:59","guid":{"rendered":"http:\/\/www.amibroker.com\/kb\/?p=1082"},"modified":"2015-02-11T18:36:21","modified_gmt":"2015-02-11T23:36:21","slug":"when-and-how-often-afl-code-is-executed","status":"publish","type":"post","link":"https:\/\/www.amibroker.com\/wordpress\/kb\/2015\/02\/03\/when-and-how-often-afl-code-is-executed\/","title":{"rendered":"When and how often AFL code is executed?"},"content":{"rendered":"

All analysis in AmiBroker including charting, Analysis window or commentaries is based on underlying AFL code, which is being executed by the program to produce the required output. Therefore – any changes we see in the charts or analysis results (for example – chart updated with new ticks) mean that the program has received some input, then based on this information has recalculated the formula and presented the updated results.<\/p>

These refreshes \/ formula recalculations depend on several factors:<\/p>

  1. In a local database, which is not updated by a realtime plugin, the formula would get refreshed if we perform any actions, such as clicking, scrolling, zooming, changing parameters, choosing another symbol or interval, importing new data etc.
  2. In addition to the actions listed in (1), if we are running a plugin-based realtime feed, then the chart is refreshed based on “Intraday refresh interval” defined in Tools –> Preferences –> Intraday. If we enter 0 into this field, then it will result in chart being refreshed with every new tick (up to 10-times per sec).
  3. It is also possible to force certain refresh rate using dedicated RequestTimedRefresh() function in the AFL code: http:\/\/www.amibroker.com\/f?RequestTimedRefresh<\/a>
  4. It is also possible to manually refresh chart (or all chart and windows) using View->Refresh<\/strong> (or View->Refresh All<\/strong>) menu.<\/ol>

    It is worth noting that chart formulas are refreshed only when they are placed on the active chart sheets. Non-active sheets just ‘don’t exist’, they are only created when you click on a bottom tab (sheet tab) to make them visible and destroyed immediately when other sheet becomes active. This ensures that precious CPU resources are not wasted on invisible chart sheets.<\/p>

    Additionally – by default charts in minimized chart windows or when multiple MDI windows are open and one is maximized, the windows in background that are completely obscured by others and\/or minimized windows are not redrawn\/refreshed during normal RT refresh. We can however call RequestTimedRefresh<\/strong> function with onlyvisible<\/strong> argument set to False<\/strong> and that will force regular refreshes in such windows as well.<\/p>RequestTimedRefresh<\/span>( <\/span>1 <\/span>, <\/span>False <\/span>); <\/span>\/\/ force refresh for minimized MDI window or obscured MDI windo<\/code>

    With regards to Analysis<\/strong> window – in general the formula is executed when we run e.g. Scan, Exploration, Backtest etc. Analysis window executes the formulas in multiple threads running in parallel (this tutorial explains multi-threading aspects: http:\/\/www.amibroker.com\/guide\/h_multithreading.html<\/a>).<\/p>

    Repeated execution (to keep the code running over and over) in Analysis window can be also enabled with “Auto-repeat” option, the following Knowledge Base article explains it in details:<\/p>

    http:\/\/www.amibroker.com\/kb\/2014\/10\/03\/how-to-setup-perodic-scans\/<\/a><\/p>

    Last but definitely not least, we need to remember that AmiBroker may and will perform some executions internally for its own purposes such as:<\/p>

    1. during AFL Syntax Check<\/strong> that happens when applying the chart, or sending the code to Analysis window, or updating existing formula
    2. when it is about to display Parameters<\/strong> window for the first time for given chart or during Parameters’ “Reset All” operation
    3. at the beginning of Optimization when it reads Optimize() statements to configure the optimization process and\/or smart optimization engines
    4. at the beginning of each In-sample walk-forward step again to setup optimization parameters<\/ol>

      Bottom line: we should never assume that certain formula will only be executed e.g. N-times during certain time-frame, because all really depends on the above factors, our actions and changing input.<\/p>","protected":false},"excerpt":{"rendered":"

      All analysis in AmiBroker including charting, Analysis window or commentaries is based on underlying AFL code, which is being executed by the program to produce the required output. Therefore – any changes we see in the charts or analysis results (for example – chart updated with new ticks) mean that the program has received some […]<\/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\/1082"}],"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=1082"}],"version-history":[{"count":2,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/1082\/revisions"}],"predecessor-version":[{"id":1085,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/1082\/revisions\/1085"}],"wp:attachment":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/media?parent=1082"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/categories?post=1082"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/tags?post=1082"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}