<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>AmiBroker Knowledge Base &#187; Explorations</title>
	<atom:link href="http://www.amibroker.com/kb/category/afl/explorations/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.amibroker.com/kb</link>
	<description>Providing you with tips &#038; tricks for everyday AmiBroker use</description>
	<lastBuildDate>Sat, 17 Dec 2011 10:18:10 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>QuickAFL facts</title>
		<link>http://www.amibroker.com/kb/2008/07/03/quickafl/</link>
		<comments>http://www.amibroker.com/kb/2008/07/03/quickafl/#comments</comments>
		<pubDate>Thu, 03 Jul 2008 10:33:15 +0000</pubDate>
		<dc:creator>Tomasz Janeczko</dc:creator>
				<category><![CDATA[AFL]]></category>
		<category><![CDATA[Custom Backtest]]></category>
		<category><![CDATA[Explorations]]></category>
		<category><![CDATA[Indicators]]></category>
		<category><![CDATA[Systems]]></category>

		<guid isPermaLink="false">http://www.amibroker.com/kb/?p=74</guid>
		<description><![CDATA[QuickAFL(tm) is a feature that allows faster AFL calculation under certain conditions. Initially (since 2003) it was available for indicators only, as of version 5.14+ it is available in Automatic Analysis too.
Initially the idea was to allow faster chart redraws through calculating AFL formula only for that part which is visible on the chart. In [...]]]></description>
			<content:encoded><![CDATA[<p>QuickAFL(tm) is a feature that allows faster AFL calculation under certain conditions. Initially (since 2003) it was available for indicators only, as of version 5.14+ it is available in Automatic Analysis too.</p>
<p>Initially the idea was to allow faster chart redraws through calculating AFL formula only for that part which is visible on the chart. In a similar manner, automatic analysis window can use subset of available quotations to calculate AFL, if selected &#8220;range&#8221; parameter is less than &#8220;All quotations&#8221;.</p>
<p>So, in short QuickAFL works so it calculates only part of the array that is currently visible (indicator) or within selected range (Automatic Analysis).</p>
<p>Your formulas, under QuickAFL, may or may NOT use all data bars available, but only visible (or &#8220;in-range&#8221;) bars (plus some extra to ensure calculation of used indicators), so when you are using Close[ 0 ] it represents not first bar in entire data set but first bar in array currently used (which is just a bit longer than visible, or &#8216;in-range&#8217; area).</p>
<p>The QuickAFL is designed to be transparent, i.e. do not require any changes to the formulas you are using. To achieve this goal, AmiBroker in the first execution of given formula &#8220;learns&#8221; how many bars are really needed to calculate it correctly.</p>
<p>To find out the number of bars required to calculate formula AmiBroker internally uses two variables &#8216;backward ref&#8217; and &#8216;forward ref&#8217;.</p>
<p>&#8216;backward ref&#8217; describes how many PREVIOUS bars are needed to calculate the value for today, and &#8216;forward ref&#8217; tells how many FUTURE bars are needed to calculate value for today.</p>
<p>If these numbers are known, during execution of given formula AmiBroker takes FIRST visible (or in-range) bar and subtracts &#8216;backward ref&#8221; and takes LAST visible (or in-range) bar and adds &#8216;forward ref&#8217; to calculate first and last bar needed for calculation of the formula.</p>
<p>Now, how does AmiBroker know a correct &#8220;backward ref&#8221; and &#8220;forward ref&#8221; for the entire formula?<br />
Well, every AmiBroker&#8217;s built-in function is written so that it knows its own requirements and adds them to global &#8220;backward ref&#8221; and &#8220;forward ref&#8221; each time given function is called from your formula.</p>
<p>The whole process starts with setting initial BackwardRef to 30 and ForwardRef to zero. These initial values are used to give &#8220;safety margin&#8221; for simple loops/scripts.</p>
<p>Next, when parser scans the formula like this:</p>
<p><code><span>Buy&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">C&nbsp;</span><span style="color: #000000">&gt;&nbsp;</span><span style="color: #0000BB">Ref&nbsp;</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">MA</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">C</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">40&nbsp;</span><span style="color: #000000">),&nbsp;-</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #000000">);</span></code></p>
<p>it analyses it and &#8220;sees&#8221; the MA with parameter 40. It knows that simple moving average of period 40 requires 40 past bars and zero future bars to calculate correctly so it does the following (all internally):</p>
<p><code><span>BackwardRef&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">BackwardRef&nbsp;</span><span style="color: #000000">+&nbsp;</span><span style="color: #0000BB">40</span><span style="color: #000000">;<br />
</span><span style="color: #0000BB">ForwardRef&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">ForwardRef&nbsp;</span><span style="color: #000000">+&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #000000">;</span></code></p>
<p>So now, the value of BackwardRef will be 70 (40+30(initial)), and ForwardRef will be zero.</p>
<p>Next the parser sees Ref( .., -1 );</p>
<p>It knows that Ref with shift of -1 requires 1 past bar and zero future bars so it &#8220;accumulates&#8221; requirements this way:</p>
<p><code><span>BackwardRef&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">BackwardRef&nbsp;</span><span style="color: #000000">+&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #000000">;<br />
</span><span style="color: #0000BB">ForwardRef&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">ForwardRef&nbsp;</span><span style="color: #000000">+&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #000000">;</span></code></p>
<p>So it ends up with:</p>
<p><code><span>BackwardRef&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">71</span><span style="color: #000000">;<br />
</span><span style="color: #0000BB">ForwardRef&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #000000">;</span></code></p>
<p>The BackwardRef  and ForwardRef numbers are displayed by AFL Editor&#8217;s Tools->Check and Profile as well as on charts when &#8220;Display chart timing&#8221; is selected in the preferences.</p>
<p>If you use Check and Profile tool, it will tell you that the formula</p>
<p><code><span>Buy&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">C&nbsp;</span><span style="color: #000000">&gt;&nbsp;</span><span style="color: #0000BB">Ref&nbsp;</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">MA</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">C</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">40&nbsp;</span><span style="color: #000000">),&nbsp;-</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #000000">);</span></code></p>
<p>requires 71 past bars and 0 future bars.</p>
<p>You can modify it to</p>
<p><code><span>Buy&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">C&nbsp;</span><span style="color: #000000">&gt;&nbsp;</span><span style="color: #0000BB">Ref&nbsp;</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">MA</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">C</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">50&nbsp;</span><span style="color: #000000">),&nbsp;-</span><span style="color: #0000BB">2&nbsp;</span><span style="color: #000000">);</span></code></p>
<p>and it will tell you that it requires 82 past bars (30+50+2) and zero future bars.</p>
<p>If you modify it to</p>
<p><code><span>Buy&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">C&nbsp;</span><span style="color: #000000">&gt;&nbsp;</span><span style="color: #0000BB">Ref&nbsp;</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">MA</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">C</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">50&nbsp;</span><span style="color: #000000">),&nbsp;</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #000000">);</span></code></p>
<p>It will tell you that it needs 80 past bars (30+50) and ONE future bar (from Ref).</p>
<p>Thanks to that your formula will use 80 bars prior to first visible (or in-range) bar leading to correct calculation result, while improving the speed of execution by not using bars preceding required ones.</p>
<p><strong>IMPORTANT NOTES</strong></p>
<p>It is very important to understand, that the above estimate requirements while fairly conservative,<br />
and working fine in majority of cases, may NOT give you identical results with QuickAFL enabled, if your formulas use:<br />
a) JScript/VBScript scripting<br />
b) for/while/do loops using more than 30 past bars<br />
c) any functions from external indicator DLLs<br />
d) certain functions that use recursive calculation such as very long exponential averages or TimeFrame functions with much higher intervals than base interval</p>
<p>In these cases, you may need to use SetBarsRequired() function to set initial requirements to value higher than default 30. For example, by placing</p>
<p><code><span>SetBarsRequired</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">1000</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">0&nbsp;</span><span style="color: #000000">);</span></code></p>
<p>at the <strong>TOP</strong> of your formula you are telling AmiBroker to add 1000 bars PRIOR to first visible (or in-range) bar to ensure more data to stabilise indicators.</p>
<p>You can also effectively turn OFF QuickAFL by adding:</p>
<p><code><span>SetBarsRequired</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">sbrAll</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">sbrAll&nbsp;</span><span style="color: #000000">);</span></code></p>
<p>at the top of your formula. It tells AmiBroker to use ALL bars all the time.</p>
<p>It is also worth noting that certain functions like cumulative sum (Cum()) by default request ALL past bars to guarantee the same results when QuickAFL is enabled. But when using such a function, you may or may NOT want to use all bars. So SetBarsRequired() gives you also ability to DECREASE the requirements of formula. This is done by placing SetBarsRequired at the <strong>END</strong> of your formula, as any call to SetBarsRequired effectively overwrites previously calculated estimate. So<br />
if you write</p>
<p><code><span>x&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">Cum</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #000000">);<br />
</span><span style="color: #0000BB">SetBarsRequired</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">1000</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">0&nbsp;</span><span style="color: #000000">);&nbsp;</span><span style="color: #007000">//&nbsp;use&nbsp;1000&nbsp;past&nbsp;bars&nbsp;DESPITE&nbsp;using&nbsp;Cum()</span></code></p>
<p>You may force AmiBroker to use only 1000 bars prior first visible even though Cum() by itself would require all bars.</p>
<p>It is also worth noting that when QuickAFL is used, BarIndex() function does NOT represent elements of the AFL array, but rather the indexes of ENTIRE quotation array. With QuickAFL turned on, an AFL array is usually shorter than quotation array, as illustrated in this picture:</p>
<p><img border=0 src="http://www.amibroker.com/gifs/barindex_small.gif" alt="QuickAFL, BarIndex and BarCount" /></p>
<p><strong>SPECIAL CASE: AddToComposite function</strong></p>
<p>Since AddToComposite creates artificial stock data it is desirable that it works the same regardless of how many &#8216;visible&#8217; bars there are or how many bars are needed by other parts of the formula.</p>
<p>For this reason internally AddToComposite does this:</p>
<p><code><span>SetBarsRequired</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">sbrAll</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">sbrAll&nbsp;</span><span style="color: #000000">);</span></code></p>
<p>which effectivelly means &#8220;use all available bars&#8221; for the formula. AddToComposite function simply tells the AFL engine to use all available bars (from the very first to the very last) regardless of how formula looks like. This is to ensure that AddToComposite updates ALL bars of the composite</p>
<p>The side-effect is that &#8220;Check And Profile&#8221; feature will see that it needs to reference future bars and display a warning even though this is false alert because AddToComposite itself has no impact on trading system at all.</p>
<p>Now why this shows only when flag atcFlagEnableInBacktest is on ??<br />
It is simple: this is so because it means that AddToComposite is ACTIVE in BACKTEST.<br />
<a href="http://www.amibroker.com/guide/afl/afl_view.php?name=ADDTOCOMPOSITE">http://www.amibroker.com/guide/afl/afl_view.php?name=ADDTOCOMPOSITE</a></p>
<p>Since &#8220;Check And Profile&#8221; uses &#8220;BACKTEST&#8221; state you get such result.</p>
<p>If atcFlagEnableInBacktest is not specified AddToComposite is not enabled in Backtest and hence does not affect calculation of BackwardRef and ForwardRef during &#8220;Check And Profile&#8221;.</p>
<p><strong>BACKWARD COMPATIBILITY NOTES</strong></p>
<p>a) QuickAFL is available in Automatic Analysis in version 5.14.0 or higher<br />
b) sbrAll constant is available in Automatic Analysis in version 5.14.0 or higher. If you are using older versions you should use numeric constant of: 1000000 instead.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.amibroker.com/kb/2008/07/03/quickafl/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to detect the study crossover for multiple symbols with use of SCAN</title>
		<link>http://www.amibroker.com/kb/2007/05/14/how-to-detect-the-study-crossover-for-multiple-symbols-with-use-of-scan/</link>
		<comments>http://www.amibroker.com/kb/2007/05/14/how-to-detect-the-study-crossover-for-multiple-symbols-with-use-of-scan/#comments</comments>
		<pubDate>Mon, 14 May 2007 07:35:41 +0000</pubDate>
		<dc:creator>Marcin Gorzynski</dc:creator>
				<category><![CDATA[AFL]]></category>
		<category><![CDATA[Explorations]]></category>

		<guid isPermaLink="false">http://www.amibroker.com/kb/2007/05/14/how-to-detect-the-study-crossover-for-multiple-symbols-with-use-of-scan/</guid>
		<description><![CDATA[It&#8217;s possible to use Automatic Analysis window to search for trendline (or other study) crossovers for multiple symbols at once. It&#8217;s necessary to do the following:
1. Draw trendlines on the chart and assidn them a STUDY ID &#8211; two letter code that allows to recognise the particular study. To do this, go to study properties [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s possible to use Automatic Analysis window to search for trendline (or other study) crossovers for multiple symbols at once. It&#8217;s necessary to do the following:</p>
<p>1. Draw trendlines on the chart and assidn them a STUDY ID &#8211; two letter code that allows to recognise the particular study. To do this, go to study properties (<strong>Alt+Enter</strong>) after you draw the line (in this example &#8211; StudyID = &#8220;RE&#8221;).</p>
<p><a href='http://www.amibroker.com/kb/wp-content/uploads/2007/05/study1.gif' title='study1.gif'><img src='http://www.amibroker.com/kb/wp-content/uploads/2007/05/study1.gif' alt='study1.gif' /></a></p>
<p>2. Repeat the process for other symbols (remember to draw the trendlines in the same chart pane).</p>
<p>3. Check the CHART ID (in order to call this particular chart pane from the SCAN). To check the ChartID &#8211; click on the chart with right mouse button, go to: <strong>PARAMETERS -&gt; Axes&amp;Grid</strong> (in this example &#8211; CHARTID = 1023).</p>
<p><a href='http://www.amibroker.com/kb/wp-content/uploads/2007/05/study2.gif' title='study2.gif'><img src='http://www.amibroker.com/kb/wp-content/uploads/2007/05/study2.gif' alt='study2.gif' /></a></p>
<p>4. Now we can write the formula:<br />
- Analysis -&gt; Formula Editor<br />
- enter:</p>
<p><strong><em>Buy = Cross( Close, Study(&#8221;RE&#8221;, 1023) );</em></strong></p>
<p>(note that we use the same <strong>StudyID</strong> and <strong>ChartID</strong> in the formula)<br />
- Tools -&gt; Send to analysis.<br />
- Apply To: All Symbols, All Quotations<br />
- press SCAN</p>
]]></content:encoded>
			<wfw:commentRss>http://www.amibroker.com/kb/2007/05/14/how-to-detect-the-study-crossover-for-multiple-symbols-with-use-of-scan/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>How to export quotations from AmiBroker to CSV file ?</title>
		<link>http://www.amibroker.com/kb/2006/03/04/how-to-export-quotations-from-amibroker-to-csv-file/</link>
		<comments>http://www.amibroker.com/kb/2006/03/04/how-to-export-quotations-from-amibroker-to-csv-file/#comments</comments>
		<pubDate>Sun, 05 Mar 2006 01:56:07 +0000</pubDate>
		<dc:creator>Tomasz Janeczko</dc:creator>
				<category><![CDATA[AFL]]></category>
		<category><![CDATA[Data]]></category>
		<category><![CDATA[Explorations]]></category>

		<guid isPermaLink="false">http://www.amibroker.com/kb/2006/03/04/how-to-export-quotations-from-amibroker-to-csv-file/</guid>
		<description><![CDATA[The easiest way to export quotes to CSV file is to use the below formula from Automatic Analysis window:
(Analysis -> Automatic Analysis)

Filter=1;&#160;
AddColumn(O,&#34;Open&#34;);&#160;
AddColumn(H,&#34;High&#34;);&#160;
AddColumn(L,&#34;Low&#34;);&#160;
AddColumn(C,&#34;Close&#34;);&#160;
AddColumn(V,&#34;Volume&#34;,1.0);&#160;
AddColumn(OI,&#34;Open&#160;Interest&#34;,1.0);&#160;

Open: Analysis->Formula Editor
Paste the above formula into formula window 
Choose Tools->Send to Auto-Analysis menu in the Formula Editor
In Automatic Analysis window select Apply to: All Stocks, Range: All quotations (or any other time range [...]]]></description>
			<content:encoded><![CDATA[<p>The easiest way to export quotes to CSV file is to use the below formula from Automatic Analysis window:<br />
(Analysis -> Automatic Analysis)<br />
<span id="more-5"></span><br />
<code><span>Filter</span><span style="color: #000000">=</span><span style="color: #0000BB">1</span><span style="color: #000000">;&nbsp;<br />
</span><span style="color: #0000BB">AddColumn</span><span style="color: #000000">(</span><span style="color: #0000BB">O</span><span style="color: #000000">,</span><span style="color: #DD0000">&quot;Open&quot;</span><span style="color: #000000">);&nbsp;<br />
</span><span style="color: #0000BB">AddColumn</span><span style="color: #000000">(</span><span style="color: #0000BB">H</span><span style="color: #000000">,</span><span style="color: #DD0000">&quot;High&quot;</span><span style="color: #000000">);&nbsp;<br />
</span><span style="color: #0000BB">AddColumn</span><span style="color: #000000">(</span><span style="color: #0000BB">L</span><span style="color: #000000">,</span><span style="color: #DD0000">&quot;Low&quot;</span><span style="color: #000000">);&nbsp;<br />
</span><span style="color: #0000BB">AddColumn</span><span style="color: #000000">(</span><span style="color: #0000BB">C</span><span style="color: #000000">,</span><span style="color: #DD0000">&quot;Close&quot;</span><span style="color: #000000">);&nbsp;<br />
</span><span style="color: #0000BB">AddColumn</span><span style="color: #000000">(</span><span style="color: #0000BB">V</span><span style="color: #000000">,</span><span style="color: #DD0000">&quot;Volume&quot;</span><span style="color: #000000">,</span><span style="color: #0000BB">1.0</span><span style="color: #000000">);&nbsp;<br />
</span><span style="color: #0000BB">AddColumn</span><span style="color: #000000">(</span><span style="color: #0000BB">OI</span><span style="color: #000000">,</span><span style="color: #DD0000">&quot;Open&nbsp;Interest&quot;</span><span style="color: #000000">,</span><span style="color: #0000BB">1.0</span><span style="color: #000000">);&nbsp;</span></code></p>
<ol>
<li>Open: <strong>Analysis->Formula Editor</strong></li>
<li>Paste the above formula into formula window </li>
<li>Choose <strong>Tools->Send to Auto-Analysis</strong> menu in the Formula Editor</li>
<li>In Automatic Analysis window select Apply to: <strong>All Stocks</strong>, Range: <strong>All quotations</strong> (or any other time range or filter, depending on what you need to export) </li>
<li>Press <strong>Explore</strong> button </li>
<li>Press <strong>Export</strong> button, specify the name and press OK</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.amibroker.com/kb/2006/03/04/how-to-export-quotations-from-amibroker-to-csv-file/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

