<?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; Systems</title>
	<atom:link href="http://www.amibroker.com/kb/category/afl/systems/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>Getting started with automatic Walk-Forward optimization</title>
		<link>http://www.amibroker.com/kb/2008/02/12/getting-started-with-automatic-walk-forward-optimization/</link>
		<comments>http://www.amibroker.com/kb/2008/02/12/getting-started-with-automatic-walk-forward-optimization/#comments</comments>
		<pubDate>Tue, 12 Feb 2008 12:54:31 +0000</pubDate>
		<dc:creator>Tomasz Janeczko</dc:creator>
				<category><![CDATA[AFL]]></category>
		<category><![CDATA[Systems]]></category>

		<guid isPermaLink="false">http://www.amibroker.com/kb/2008/02/12/getting-started-with-automatic-walk-forward-optimization/</guid>
		<description><![CDATA[Recently released AmiBroker 5.05 BETA features the automatic Walk-Forward Optimization mode.
The automatic Walk forward optimization is a system design and validation technique in which you optimize the parameter values on a past segment of market data (&#8221;in-sample&#8221;), then test the system forward in time on data following the optimization segment (&#8221;out-of-sample&#8221;). You evaluate the system [...]]]></description>
			<content:encoded><![CDATA[<p>Recently released AmiBroker 5.05 BETA features the automatic Walk-Forward Optimization mode.</p>
<p>The automatic Walk forward optimization is a system design and validation technique in which you optimize the parameter values on a past segment of market data (&#8221;in-sample&#8221;), then test the system forward in time on data following the optimization segment (&#8221;out-of-sample&#8221;). You evaluate the system based on how well it performs on the test data (&#8221;out-of-sample&#8221;), not the data it was optimized on. </p>
<p>To use Walk-Forward optimization please follow these steps:
<ol>
<li>Goto Tools->Automatic Analysis
<li>Click Settings button, then switch to &#8220;Walk-Forward tab&#8221;
<li>Here you can see Walk forward settings for In-sample optimization, out-of-sample backtest<br />
&#8220;Start&#8221; and &#8220;End&#8221; dates mark initial period begin / end<br />
This period will be moved forward by &#8220;Step&#8221; until the &#8220;End&#8221; reaches the &#8220;Last&#8221; date.<br />
The &#8220;Start&#8221; date can move forward by &#8220;step&#8221; too, or can be anchored (constant) if &#8220;Anchored&#8221; check is on.<br />
If you mark &#8220;Use today&#8221; then &#8220;Last&#8221; date entered will be ignored and TODAY (current date) will be used instead</p>
<p>By default an &#8220;EASY MODE&#8221; is selected which simplifies the process of setting up WF parameters.<br />
It assumes that:<br />
a) Out-of-sample segment immediatelly follows in-sample segment<br />
b) the length of out-of-sample segment equals to the walk-forward step</p>
<p>Based on these two assumptions the &#8220;EASY&#8221; mode takes in-sample END date and sets<br />
out-of-sample START date to the following day. Then adds in-sample STEP and this becomes out-of-sample END date.<br />
In-sample and Out-of-sample step values are set to the same values.</p>
<p>The &#8220;EASY&#8221; mode guarantees correctness of WF procedure settings.</p>
<p>In the &#8220;ADVANCED&#8221; mode, the user has complete control over all values, to the extent that<br />
they may not constitute valid WF procedure.<br />
The interface allows to selectivelly disable in-sample and out-of-sample phases using checkboxes at top<br />
(for special things like runnign sequential backtests without optimization). </p>
<p>All settings are immediatelly reflected in the PREVIEW list that shows all generated IS/OOS segments and their dates.</p>
<p>The &#8220;Optimization target&#8221; field defines the optimization raport COLUMN NAME that<br />
will be used for sorting results and finding the BEST one. Any built-in column can be used<br />
(as appears in the optimization output), or you can use any custom metric that you define<br />
in custom backtester. The default is CAR/MDD, you can however select any other built-in metric from the combo.<br />
You can also TYPE-IN any custom metric that you have added via custom backtester interface.</p>
<li>Once you defined Walk-Forward settings, please go to Automatic Analysis and
<li>press the dropdown ARROW on the Optimize button and select &#8220;Walk Forward Optimization&#8221;
</ol>
<p>This will run sequence of optimizaitons and backtest and the results will be displayed in the &#8220;Walk Forward&#8221; document that is open in the main application frame.<br />
When optimization is running you can click &#8220;MINIMIZE&#8221; button on the Progress dialog to minimize it &#8211; this allows to see the Walk Forward output during the optimization steps.</p>
<p><b>IN-SAMPLE and OUT-OF-SAMPLE combined equity</b></p>
<p>Combined in-sample and out-sample equities are available by<br />
~~~ISEQUITY and ~~~OSEQUITY composite tickers (consecutive periods of IS and OOS are concatenated and scaled to<br />
maintain continuity of equity line &#8211; this approach assumes that you generally speaking are compounding profits)<br />
To display IS and OOS equity you may use for example this:</p>
<p><code><span>PlotForeign</span><span style="color: #000000">(</span><span style="color: #DD0000">&quot;~~~ISEQUITY&quot;</span><span style="color: #000000">,</span><span style="color: #DD0000">&quot;In-Sample&nbsp;Equity&quot;</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">colorRed</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">styleLine</span><span style="color: #000000">);&nbsp;<br />
</span><span style="color: #0000BB">PlotForeign</span><span style="color: #000000">(</span><span style="color: #DD0000">&quot;~~~OSEQUITY&quot;</span><span style="color: #000000">,</span><span style="color: #DD0000">&quot;Out-Of-Sample&nbsp;Equity&quot;</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">colorGreen</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">styleLine</span><span style="color: #000000">);&nbsp;<br />
</span><span style="color: #0000BB">Title&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #DD0000">&quot;{{NAME}}&nbsp;-&nbsp;{{INTERVAL}}&nbsp;{{DATE}}&nbsp;{{VALUES}}&quot;</span><span style="color: #000000">;&nbsp;</span></code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.amibroker.com/kb/2008/02/12/getting-started-with-automatic-walk-forward-optimization/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>How to plot a trailing stop in the Price chart</title>
		<link>http://www.amibroker.com/kb/2007/03/24/how-to-plot-a-trailing-stop-in-the-price-chart/</link>
		<comments>http://www.amibroker.com/kb/2007/03/24/how-to-plot-a-trailing-stop-in-the-price-chart/#comments</comments>
		<pubDate>Sat, 24 Mar 2007 16:06:34 +0000</pubDate>
		<dc:creator>Marcin Gorzynski</dc:creator>
				<category><![CDATA[Indicators]]></category>
		<category><![CDATA[Systems]]></category>

		<guid isPermaLink="false">http://www.amibroker.com/kb/2007/03/24/how-to-plot-a-trailing-stop-in-the-price-chart/</guid>
		<description><![CDATA[In this short article we will show how to calculate and plot trailing stop using two different methods. 
First method uses looping and it does not use ApplyStop() function as it does not plot stops &#8211; it only triggers them in backtest mode. The stop % level can be adjusted via PARAMETERS dalog.

StopLevel&#160;=&#160;1&#160;-&#160;Param(&#34;trailing&#160;stop&#160;%&#34;,&#160;3,&#160;0.1,&#160;10,&#160;0.1)/100;
Buy&#160;=&#160;Cross(&#160;MACD(),&#160;Signal()&#160;);
Sell&#160;=&#160;0;
trailARRAY&#160;=&#160;Null;
trailstop&#160;=&#160;0;
for(&#160;i&#160;=&#160;1;&#160;i&#160;&#60;&#160;BarCount;&#160;i++&#160;)
{
&#160;&#160;&#160;if(&#160;trailstop&#160;==&#160;0&#160;AND&#160;Buy[&#160;i&#160;]&#160;)&#160;
&#160;&#160;&#160;{&#160;
&#160;&#160;&#160;&#160;&#160;&#160;trailstop&#160;=&#160;High[&#160;i&#160;]&#160;*&#160;stoplevel;
&#160;&#160;&#160;}
&#160;&#160;&#160;else&#160;Buy[&#160;i&#160;]&#160;=&#160;0;&#160;//&#160;remove&#160;excess&#160;buy&#160;signals
&#160;&#160;&#160;if(&#160;trailstop&#160;&#62;&#160;0&#160;AND&#160;Low[&#160;i&#160;]&#160;&#60;&#160;trailstop&#160;)
&#160;&#160;&#160;{
&#160;&#160;&#160;&#160;&#160;&#160;Sell[&#160;i&#160;]&#160;=&#160;1;
&#160;&#160;&#160;&#160;&#160;&#160;SellPrice[&#160;i&#160;]&#160;=&#160;trailstop;
&#160;&#160;&#160;&#160;&#160;&#160;trailstop&#160;=&#160;0;
&#160;&#160;&#160;}
&#160;&#160;&#160;if(&#160;trailstop&#160;&#62;&#160;0&#160;)
&#160;&#160;&#160;{&#160;&#160;&#160;
&#160;&#160;&#160;&#160;&#160;&#160;trailstop&#160;=&#160;Max(&#160;High[&#160;i&#160;]&#160;*&#160;stoplevel,&#160;trailstop&#160;);
&#160;&#160;&#160;&#160;&#160;&#160;trailARRAY[&#160;i&#160;]&#160;=&#160;trailstop;
&#160;&#160;&#160;}
}
PlotShapes(Buy*shapeUpArrow,colorGreen,0,Low);
PlotShapes(Sell*shapeDownArrow,colorRed,0,High);
Plot(&#160;Close,&#34;Price&#34;,colorBlack,styleBar);
Plot(&#160;trailARRAY,&#34;trailing&#160;stop&#160;level&#34;,&#160;colorRed&#160;);

 

Alternatively you can [...]]]></description>
			<content:encoded><![CDATA[<p>In this short article we will show how to calculate and plot trailing stop using two different methods. <span id="more-52"></span></p>
<p>First method uses looping and it does not use ApplyStop() function as it does not plot stops &#8211; it only triggers them in backtest mode. The stop % level can be adjusted via PARAMETERS dalog.</p>
<p><code><span></p>
<p>StopLevel&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #000000">-&nbsp;</span><span style="color: #0000BB">Param</span><span style="color: #000000">(</span><span style="color: #DD0000">&quot;trailing&nbsp;stop&nbsp;%&quot;</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">0.1</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">10</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">0.1</span><span style="color: #000000">)/</span><span style="color: #0000BB">100</span><span style="color: #000000">;</p>
<p></span><span style="color: #0000BB">Buy&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">Cross</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">MACD</span><span style="color: #000000">(),&nbsp;</span><span style="color: #0000BB">Signal</span><span style="color: #000000">()&nbsp;);<br />
</span><span style="color: #0000BB">Sell&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #000000">;<br />
</span><span style="color: #0000BB">trailARRAY&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">Null</span><span style="color: #000000">;<br />
</span><span style="color: #0000BB">trailstop&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #000000">;</p>
<p>for(&nbsp;</span><span style="color: #0000BB">i&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #000000">;&nbsp;</span><span style="color: #0000BB">i&nbsp;</span><span style="color: #000000">&lt;&nbsp;</span><span style="color: #0000BB">BarCount</span><span style="color: #000000">;&nbsp;</span><span style="color: #0000BB">i</span><span style="color: #000000">++&nbsp;)<br />
{</p>
<p>&nbsp;&nbsp;&nbsp;if(&nbsp;</span><span style="color: #0000BB">trailstop&nbsp;</span><span style="color: #000000">==&nbsp;</span><span style="color: #0000BB">0&nbsp;</span><span style="color: #000000">AND&nbsp;</span><span style="color: #0000BB">Buy</span><span style="color: #000000">[&nbsp;</span><span style="color: #0000BB">i&nbsp;</span><span style="color: #000000">]&nbsp;)&nbsp;<br />
&nbsp;&nbsp;&nbsp;{&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">trailstop&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">High</span><span style="color: #000000">[&nbsp;</span><span style="color: #0000BB">i&nbsp;</span><span style="color: #000000">]&nbsp;*&nbsp;</span><span style="color: #0000BB">stoplevel</span><span style="color: #000000">;<br />
&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;else&nbsp;</span><span style="color: #0000BB">Buy</span><span style="color: #000000">[&nbsp;</span><span style="color: #0000BB">i&nbsp;</span><span style="color: #000000">]&nbsp;=&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #000000">;&nbsp;</span><span style="color: #007000">//&nbsp;remove&nbsp;excess&nbsp;buy&nbsp;signals</p>
<p>&nbsp;&nbsp;&nbsp;</span><span style="color: #000000">if(&nbsp;</span><span style="color: #0000BB">trailstop&nbsp;</span><span style="color: #000000">&gt;&nbsp;</span><span style="color: #0000BB">0&nbsp;</span><span style="color: #000000">AND&nbsp;</span><span style="color: #0000BB">Low</span><span style="color: #000000">[&nbsp;</span><span style="color: #0000BB">i&nbsp;</span><span style="color: #000000">]&nbsp;&lt;&nbsp;</span><span style="color: #0000BB">trailstop&nbsp;</span><span style="color: #000000">)<br />
&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">Sell</span><span style="color: #000000">[&nbsp;</span><span style="color: #0000BB">i&nbsp;</span><span style="color: #000000">]&nbsp;=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #000000">;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">SellPrice</span><span style="color: #000000">[&nbsp;</span><span style="color: #0000BB">i&nbsp;</span><span style="color: #000000">]&nbsp;=&nbsp;</span><span style="color: #0000BB">trailstop</span><span style="color: #000000">;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">trailstop&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #000000">;<br />
&nbsp;&nbsp;&nbsp;}</p>
<p>&nbsp;&nbsp;&nbsp;if(&nbsp;</span><span style="color: #0000BB">trailstop&nbsp;</span><span style="color: #000000">&gt;&nbsp;</span><span style="color: #0000BB">0&nbsp;</span><span style="color: #000000">)<br />
&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">trailstop&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">Max</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">High</span><span style="color: #000000">[&nbsp;</span><span style="color: #0000BB">i&nbsp;</span><span style="color: #000000">]&nbsp;*&nbsp;</span><span style="color: #0000BB">stoplevel</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">trailstop&nbsp;</span><span style="color: #000000">);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">trailARRAY</span><span style="color: #000000">[&nbsp;</span><span style="color: #0000BB">i&nbsp;</span><span style="color: #000000">]&nbsp;=&nbsp;</span><span style="color: #0000BB">trailstop</span><span style="color: #000000">;<br />
&nbsp;&nbsp;&nbsp;}</p>
<p>}</p>
<p></span><span style="color: #0000BB">PlotShapes</span><span style="color: #000000">(</span><span style="color: #0000BB">Buy</span><span style="color: #000000">*</span><span style="color: #0000BB">shapeUpArrow</span><span style="color: #000000">,</span><span style="color: #0000BB">colorGreen</span><span style="color: #000000">,</span><span style="color: #0000BB">0</span><span style="color: #000000">,</span><span style="color: #0000BB">Low</span><span style="color: #000000">);<br />
</span><span style="color: #0000BB">PlotShapes</span><span style="color: #000000">(</span><span style="color: #0000BB">Sell</span><span style="color: #000000">*</span><span style="color: #0000BB">shapeDownArrow</span><span style="color: #000000">,</span><span style="color: #0000BB">colorRed</span><span style="color: #000000">,</span><span style="color: #0000BB">0</span><span style="color: #000000">,</span><span style="color: #0000BB">High</span><span style="color: #000000">);</p>
<p></span><span style="color: #0000BB">Plot</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">Close</span><span style="color: #000000">,</span><span style="color: #DD0000">&quot;Price&quot;</span><span style="color: #000000">,</span><span style="color: #0000BB">colorBlack</span><span style="color: #000000">,</span><span style="color: #0000BB">styleBar</span><span style="color: #000000">);<br />
</span><span style="color: #0000BB">Plot</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">trailARRAY</span><span style="color: #000000">,</span><span style="color: #DD0000">&quot;trailing&nbsp;stop&nbsp;level&quot;</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">colorRed&nbsp;</span><span style="color: #000000">);</p>
<p></span></code><br />
 </p>
<p><a href='http://www.amibroker.com/kb/wp-content/uploads/2007/03/trailing.gif' title='Trailing stop plot'><img src='http://www.amibroker.com/kb/wp-content/uploads/2007/03/trailing.gif' alt='Trailing stop plot' /></a></p>
<p>Alternatively you can use code without looping, but then it requires Equity(1) to evaluate stops as shown in the example code below. Equity( 1 ) is the backtester-in-a-box that runs actual single-security backtest and when parameter 1 is passed it writes back signals (removing excessive ones and writing out all stops to Buy/Sell/Short/Cover arrays). </p>
<p><code><span></p>
<p>StopLevel&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">Param</span><span style="color: #000000">(</span><span style="color: #DD0000">&quot;trailing&nbsp;stop&nbsp;%&quot;</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">0.1</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">10</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">0.1&nbsp;</span><span style="color: #000000">);</p>
<p></span><span style="color: #0000BB">SetTradeDelays</span><span style="color: #000000">(</span><span style="color: #0000BB">0</span><span style="color: #000000">,</span><span style="color: #0000BB">0</span><span style="color: #000000">,</span><span style="color: #0000BB">0</span><span style="color: #000000">,</span><span style="color: #0000BB">0</span><span style="color: #000000">);</p>
<p></span><span style="color: #0000BB">Buy&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">Cross</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">MACD</span><span style="color: #000000">(),&nbsp;</span><span style="color: #0000BB">Signal</span><span style="color: #000000">()&nbsp;);<br />
</span><span style="color: #0000BB">Sell&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #000000">;<br />
</span><span style="color: #0000BB">ApplyStop</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">stopTypeTrailing</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">stopModePercent</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">StopLevel</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">True&nbsp;</span><span style="color: #000000">);<br />
&nbsp;<br />
</span><span style="color: #0000BB">Equity</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">1</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;evaluate&nbsp;stops,&nbsp;all&nbsp;quotes</p>
<p></span><span style="color: #0000BB">InTrade&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">Flip</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">Buy</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">Sell&nbsp;</span><span style="color: #000000">);</p>
<p></span><span style="color: #0000BB">SetOption</span><span style="color: #000000">(</span><span style="color: #DD0000">&quot;EveryBarNullCheck&quot;</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">True&nbsp;</span><span style="color: #000000">);<br />
</span><span style="color: #0000BB">stopline&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">IIf</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">InTrade</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">HighestSince</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">Buy</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">High&nbsp;</span><span style="color: #000000">)&nbsp;*&nbsp;(&nbsp;</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #000000">-&nbsp;</span><span style="color: #0000BB">0.01&nbsp;</span><span style="color: #000000">*&nbsp;</span><span style="color: #0000BB">StopLevel&nbsp;</span><span style="color: #000000">),&nbsp;</span><span style="color: #0000BB">Null&nbsp;</span><span style="color: #000000">);</p>
<p></span><span style="color: #0000BB">PlotShapes</span><span style="color: #000000">(</span><span style="color: #0000BB">Buy</span><span style="color: #000000">*</span><span style="color: #0000BB">shapeUpArrow</span><span style="color: #000000">,</span><span style="color: #0000BB">colorGreen</span><span style="color: #000000">,</span><span style="color: #0000BB">0</span><span style="color: #000000">,</span><span style="color: #0000BB">Low</span><span style="color: #000000">);<br />
</span><span style="color: #0000BB">PlotShapes</span><span style="color: #000000">(</span><span style="color: #0000BB">Sell</span><span style="color: #000000">*</span><span style="color: #0000BB">shapeDownArrow</span><span style="color: #000000">,</span><span style="color: #0000BB">colorRed</span><span style="color: #000000">,</span><span style="color: #0000BB">0</span><span style="color: #000000">,</span><span style="color: #0000BB">High</span><span style="color: #000000">);</p>
<p></span><span style="color: #0000BB">Plot</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">Close</span><span style="color: #000000">,</span><span style="color: #DD0000">&quot;Price&quot;</span><span style="color: #000000">,</span><span style="color: #0000BB">colorBlack</span><span style="color: #000000">,</span><span style="color: #0000BB">styleBar</span><span style="color: #000000">);<br />
</span><span style="color: #0000BB">Plot</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">stopline</span><span style="color: #000000">,&nbsp;</span><span style="color: #DD0000">&quot;trailing&nbsp;stop&nbsp;line&quot;</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">colorRed&nbsp;</span><span style="color: #000000">);<br />
</span></code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.amibroker.com/kb/2007/03/24/how-to-plot-a-trailing-stop-in-the-price-chart/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>How to close open positions at the end of the day (for daytraders)</title>
		<link>http://www.amibroker.com/kb/2006/10/26/how-to-close-open-positions-at-the-end-of-the-day-for-daytraders/</link>
		<comments>http://www.amibroker.com/kb/2006/10/26/how-to-close-open-positions-at-the-end-of-the-day-for-daytraders/#comments</comments>
		<pubDate>Thu, 26 Oct 2006 10:19:08 +0000</pubDate>
		<dc:creator>Marcin Gorzynski</dc:creator>
				<category><![CDATA[AFL]]></category>
		<category><![CDATA[Systems]]></category>

		<guid isPermaLink="false">http://www.amibroker.com/kb/2006/10/26/how-to-close-open-positions-at-the-end-of-the-day-for-daytraders/</guid>
		<description><![CDATA[If we are backtesting the intraday strategy and we do not want to keep open positions overnight &#8211; then it&#8217;s necessary to force closing all the open positions before the session end. In order to code it in AFL &#8211; the easiest is to use TimeNum() function for that purpose. 
The below example shows how [...]]]></description>
			<content:encoded><![CDATA[<p>If we are backtesting the <strong>intraday</strong> strategy and we do not want to keep open positions overnight &#8211; then it&#8217;s necessary to force closing all the open positions before the session end. In order to code it in AFL &#8211; the easiest is to use TimeNum() function for that purpose. <span id="more-53"></span></p>
<p>The below example shows how to close all open positions after 15:50, just before the end of the trading session (when testing on intraday quotes).</p>
<p><code><span>Buy&nbsp;</span><span style="color: #000000">=&nbsp;...</span><span style="color: #0000BB">your&nbsp;regular&nbsp;conditions</span><span style="color: #000000">....<br />
</span><span style="color: #0000BB">Sell&nbsp;</span><span style="color: #000000">=&nbsp;....</span><span style="color: #0000BB">your&nbsp;regular&nbsp;conditions</span><span style="color: #000000">....&nbsp;OR&nbsp;</span><span style="color: #0000BB">TimeNum</span><span style="color: #000000">()&nbsp;&gt;&nbsp;</span><span style="color: #0000BB">155000</span><span style="color: #000000">;</span></code><br />
For example &#8211; in case of MACD crossover &#8211; the code will look like:</p>
<p><code><span>Buy&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">Cross</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">MACD</span><span style="color: #000000">(),&nbsp;</span><span style="color: #0000BB">Signal</span><span style="color: #000000">()&nbsp;);<br />
</span><span style="color: #0000BB">Sell&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">Cross</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">Signal</span><span style="color: #000000">(),&nbsp;</span><span style="color: #0000BB">MACD</span><span style="color: #000000">()&nbsp;)&nbsp;OR&nbsp;</span><span style="color: #0000BB">TimeNum</span><span style="color: #000000">()&nbsp;&gt;&nbsp;</span><span style="color: #0000BB">155000</span><span style="color: #000000">;</span></code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.amibroker.com/kb/2006/10/26/how-to-close-open-positions-at-the-end-of-the-day-for-daytraders/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to set individual trading rules for symbols in the same backtest</title>
		<link>http://www.amibroker.com/kb/2006/09/29/how-to-set-individual-trading-rules-for-symbols-in-the-same-backtest/</link>
		<comments>http://www.amibroker.com/kb/2006/09/29/how-to-set-individual-trading-rules-for-symbols-in-the-same-backtest/#comments</comments>
		<pubDate>Fri, 29 Sep 2006 11:55:36 +0000</pubDate>
		<dc:creator>Marcin Gorzynski</dc:creator>
				<category><![CDATA[AFL]]></category>
		<category><![CDATA[Systems]]></category>

		<guid isPermaLink="false">http://www.amibroker.com/kb/2006/09/29/how-to-set-individual-trading-rules-for-symbols-in-the-same-backtest/</guid>
		<description><![CDATA[The following code shows how to use separate trading rules for several symbols included in the same backtest. 

//&#160;default&#160;trading&#160;rules&#160;if&#160;the&#160;symbol&#160;is&#160;not&#160;specified&#160;below:
Buy&#160;=&#160;Cross(&#160;MA(&#160;Close,&#160;20),&#160;MA(&#160;Close,&#160;50)&#160;);
Sell&#160;=&#160;Cross(&#160;MA(&#160;Close,&#160;50),&#160;MA(&#160;Close,&#160;20)&#160;);
//&#160;individual&#160;trading&#160;rules&#160;for&#160;selected&#160;symbols&#160;
//&#160;that&#160;will&#160;overwrite&#160;the&#160;above&#160;default&#160;rules&#160;
//&#160;if&#160;particular&#160;symbol&#160;is&#160;detected
//&#160;system&#160;for&#160;MSFT
if(&#160;Name()&#160;==&#160;&#34;MSFT&#34;&#160;)
{
&#160;Buy&#160;=&#160;Cross(&#160;MA(&#160;Close,&#160;50),&#160;MA(&#160;Close,&#160;100)&#160;);
&#160;Sell&#160;=&#160;Cross(&#160;MA(&#160;Close,&#160;100),&#160;MA(&#160;Close,&#160;50)&#160;);
}
//&#160;system&#160;for&#160;IBM
if(&#160;Name()&#160;==&#160;&#34;IBM&#34;&#160;)
{
&#160;Buy&#160;=&#160;Cross(&#160;MACD(),&#160;Signal()&#160;);
&#160;Sell&#160;=&#160;Cross(&#160;Signal(),&#160;MACD()&#160;);
}
//&#160;system&#160;for&#160;NVDA
if(&#160;Name()&#160;==&#160;&#34;NVDA&#34;&#160;)
{
&#160;Buy&#160;=&#160;Cross(&#160;RSI(),&#160;30);
&#160;Sell&#160;=&#160;0;
&#160;ApplyStop(&#160;stopTypeNBar,&#160;stopModeBars,&#160;10);
}&#160;
Note that different per-symbol stops (ApplyStop) are possible only in regular (non-rotational) backtest.
]]></description>
			<content:encoded><![CDATA[<p>The following code shows how to use separate trading rules for several symbols included in the same backtest. <span id="more-36"></span></p>
<p><code><span><br />
</span><span style="color: #007000">//&nbsp;default&nbsp;trading&nbsp;rules&nbsp;if&nbsp;the&nbsp;symbol&nbsp;is&nbsp;not&nbsp;specified&nbsp;below:<br />
</span><span style="color: #0000BB">Buy&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">Cross</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">MA</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">Close</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">20</span><span style="color: #000000">),&nbsp;</span><span style="color: #0000BB">MA</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">Close</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">50</span><span style="color: #000000">)&nbsp;);<br />
</span><span style="color: #0000BB">Sell&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">Cross</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">MA</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">Close</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">50</span><span style="color: #000000">),&nbsp;</span><span style="color: #0000BB">MA</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">Close</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">20</span><span style="color: #000000">)&nbsp;);</p>
<p></span><span style="color: #007000">//&nbsp;individual&nbsp;trading&nbsp;rules&nbsp;for&nbsp;selected&nbsp;symbols&nbsp;<br />
//&nbsp;that&nbsp;will&nbsp;overwrite&nbsp;the&nbsp;above&nbsp;default&nbsp;rules&nbsp;<br />
//&nbsp;if&nbsp;particular&nbsp;symbol&nbsp;is&nbsp;detected</p>
<p>//&nbsp;system&nbsp;for&nbsp;MSFT<br />
</span><span style="color: #000000">if(&nbsp;</span><span style="color: #0000BB">Name</span><span style="color: #000000">()&nbsp;==&nbsp;</span><span style="color: #DD0000">&quot;MSFT&quot;&nbsp;</span><span style="color: #000000">)<br />
{<br />
&nbsp;</span><span style="color: #0000BB">Buy&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">Cross</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">MA</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">Close</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">50</span><span style="color: #000000">),&nbsp;</span><span style="color: #0000BB">MA</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">Close</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">100</span><span style="color: #000000">)&nbsp;);<br />
&nbsp;</span><span style="color: #0000BB">Sell&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">Cross</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">MA</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">Close</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">100</span><span style="color: #000000">),&nbsp;</span><span style="color: #0000BB">MA</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">Close</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">50</span><span style="color: #000000">)&nbsp;);<br />
}</p>
<p></span><span style="color: #007000">//&nbsp;system&nbsp;for&nbsp;IBM<br />
</span><span style="color: #000000">if(&nbsp;</span><span style="color: #0000BB">Name</span><span style="color: #000000">()&nbsp;==&nbsp;</span><span style="color: #DD0000">&quot;IBM&quot;&nbsp;</span><span style="color: #000000">)<br />
{<br />
&nbsp;</span><span style="color: #0000BB">Buy&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">Cross</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">MACD</span><span style="color: #000000">(),&nbsp;</span><span style="color: #0000BB">Signal</span><span style="color: #000000">()&nbsp;);<br />
&nbsp;</span><span style="color: #0000BB">Sell&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">Cross</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">Signal</span><span style="color: #000000">(),&nbsp;</span><span style="color: #0000BB">MACD</span><span style="color: #000000">()&nbsp;);<br />
}</p>
<p></span><span style="color: #007000">//&nbsp;system&nbsp;for&nbsp;NVDA<br />
</span><span style="color: #000000">if(&nbsp;</span><span style="color: #0000BB">Name</span><span style="color: #000000">()&nbsp;==&nbsp;</span><span style="color: #DD0000">&quot;NVDA&quot;&nbsp;</span><span style="color: #000000">)<br />
{<br />
&nbsp;</span><span style="color: #0000BB">Buy&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">Cross</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">RSI</span><span style="color: #000000">(),&nbsp;</span><span style="color: #0000BB">30</span><span style="color: #000000">);<br />
&nbsp;</span><span style="color: #0000BB">Sell&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #000000">;<br />
&nbsp;</span><span style="color: #0000BB">ApplyStop</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">stopTypeNBar</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">stopModeBars</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">10</span><span style="color: #000000">);<br />
}&nbsp;</span></code></p>
<p>Note that different per-symbol stops (ApplyStop) are possible only in regular (non-rotational) backtest.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.amibroker.com/kb/2006/09/29/how-to-set-individual-trading-rules-for-symbols-in-the-same-backtest/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Stops priority in the default backtest procedure in AmiBroker</title>
		<link>http://www.amibroker.com/kb/2006/09/29/stops-priority-in-the-default-backtest-procedure-in-amibroker/</link>
		<comments>http://www.amibroker.com/kb/2006/09/29/stops-priority-in-the-default-backtest-procedure-in-amibroker/#comments</comments>
		<pubDate>Fri, 29 Sep 2006 11:48:44 +0000</pubDate>
		<dc:creator>Marcin Gorzynski</dc:creator>
				<category><![CDATA[AFL]]></category>
		<category><![CDATA[Systems]]></category>

		<guid isPermaLink="false">http://www.amibroker.com/kb/2006/09/29/stops-priority-in-the-default-backtest-procedure-in-amibroker/</guid>
		<description><![CDATA[The order stops are triggered in the backtest is the following:
 - Fixed Ruin stop (loosing 99.96% of the starting capital)
 - Max. loss stop
 - Profit target stop
 - Trailing stop
 - N-bar stop* (see below)
In versions 4.61 and higher: you can decide if N-BAR stop has the lowest or the highest priority. 
ExitAtStop parameter in ApplyStop function is [...]]]></description>
			<content:encoded><![CDATA[<p>The order stops are triggered in the backtest is the following:<br />
 - Fixed Ruin stop (loosing 99.96% of the starting capital)<br />
 - Max. loss stop<br />
 - Profit target stop<br />
 - Trailing stop<br />
 - N-bar stop* (see below)</p>
<p>In versions 4.61 and higher: you can decide if N-BAR stop has the lowest or the highest priority. <span id="more-35"></span></p>
<p>ExitAtStop parameter in ApplyStop function is used for that purpose.<br />
 - If ExitAtStop = 0 then N-bar stop has the lowest priority (so if for example profit target stop is hit on the same bar then profit target is evaluated first)<br />
 - If ExitAtStop = 1 then N-bar stop has highest priority and it is evaluated before all other stops.</p>
<p>The same effect is obtained by checking &#8220;Has priority&#8221; box in AA Settings window.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.amibroker.com/kb/2006/09/29/stops-priority-in-the-default-backtest-procedure-in-amibroker/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>AmiBroker for FOREX</title>
		<link>http://www.amibroker.com/kb/2006/08/09/amibroker-for-forex/</link>
		<comments>http://www.amibroker.com/kb/2006/08/09/amibroker-for-forex/#comments</comments>
		<pubDate>Wed, 09 Aug 2006 07:56:17 +0000</pubDate>
		<dc:creator>Marcin Gorzynski</dc:creator>
				<category><![CDATA[AFL]]></category>
		<category><![CDATA[AmiQuote]]></category>
		<category><![CDATA[Data]]></category>
		<category><![CDATA[Indicators]]></category>
		<category><![CDATA[Systems]]></category>

		<guid isPermaLink="false">http://www.amibroker.com/kb/2006/08/09/amibroker-for-forex/</guid>
		<description><![CDATA[Here is an article that tells you everything you need to know about using AmiBroker for trading FOREX markets. 
I. DATA
AmiBroker is very flexible as regards the datasources that can be used to feed data to the program.
1) Realtime data
Forex traders usually require a realtime datasource and with AB you have a variety of choices.
The exact [...]]]></description>
			<content:encoded><![CDATA[<p>Here is an article that tells you everything you need to know about using AmiBroker for trading FOREX markets. <span id="more-31"></span></p>
<p><strong>I. DATA</strong></p>
<p>AmiBroker is very flexible as regards the datasources that can be used to feed data to the program.</p>
<p>1) Realtime data</p>
<p>Forex traders usually require a realtime datasource and with AB you have a variety of choices.<br />
The exact configuration process depends on the particular source &#8211; click on the appropriate link to learn how to configure the source of your choice:</p>
<p>- eSignal &#8211; <a href="http://www.amibroker.com/video/esignal.html">http://www.amibroker.com/video/esignal.html</a> <br />
- IQFeed &#8211; <a href="http://www.amibroker.com/iqfeed.html">http://www.amibroker.com/iqfeed.html</a><br />
- Interactive Brokers &#8211; <a href="http://www.amibroker.com/video/ib.html">http://www.amibroker.com/video/ib.html</a><br />
- any source that supports DDE standard (this is a generic communication interface, check if your broker’s application supports DDE) &#8211; <a href="http://www.amibroker.com/dde.html">http://www.amibroker.com/dde.html</a><br />
- any source that delivers the data in MetaStock format &#8211; see:<br />
<a href="http://www.amibroker.com/guide/h_extsources.html">http://www.amibroker.com/guide/h_extsources.html</a></p>
<p> </p>
<p>2) AmiQuote downloader</p>
<p>If you do not require realtime quotations, but it&#8217;s enough for you to have the historical data (e.g. for backtesting your strategies) &#8211; then you can also use AmiQuote downloader program (a companion program that is installed with AmiBroker) and it will allow you to get FREE forex data (both EOD and intraday: 1-, 3-, 5-, 15-, 30-, 60- and 120-minute intervals).</p>
<p>AmiQuote can download the quotations for the following currency pairs:<br />
EURCHF,EURGBP,EURJPY,EURUSD,GBPUSD,USDCHF,USDJPY</p>
<p>The download process is shown in the video:<br />
<a href="http://www.amibroker.net/video/forex.html">http://www.amibroker.net/video/forex.html</a></p>
<p>You need to do the following:<br />
- set up database in AmiBroker (File -&gt; New Database, local database, base<br />
time interval, e.g. EOD)<br />
- run AmiQuote (START -&gt; Programs -&gt; AmiBroker -&gt; AmiQuote )<br />
- add forex symbols in AQ: (Edit -&gt; Add tickers )<br />
- select FOREX as a datasource<br />
- select time range<br />
- check &#8220;Automatic Import&#8221; field<br />
- choose: File -&gt; Start download<br />
The intraday forex quotes are available in the Registered version of AmiQuote only.<br />
Although the entire data range is very long, you must remember that in case of intraday quotes the saefst way is to get data in small parts, few weeks at a time.<br />
Otherwise the request may be too large for the data server to handle it and as a result it will reject the request.<br />
The other important thing to remember is that the data is not avalable for downloads between 13:00 &#8211; 22:00 GMT time (7:00 &#8211; 16:00 EST) &#8211; in these hours the data  vendor&#8217;s  server just rejects all the requests for intraday quotes.</p>
<p> </p>
<p>3) Text files.</p>
<p>You can also use any data that comes in the text files. The ASCII Importer available in AmiBroker is very flexible and accepts practically any standard of data.<br />
To import quotations &#8211; the most convenient is to use File -&gt; Import Wizard.</p>
<p>To learn more about importing the data from ASCII (text) files &#8211; please read the following tutorial:<br />
<a href="http://www.amibroker.com/guide/w_impwizard.html">http://www.amibroker.com/guide/w_impwizard.html</a><br />
<strong> </strong></p>
<p><strong>II.  SYMBOL GUIDE</strong></p>
<p>Once you configure the database (to read realtime data), then all you need to do is to add the symbol via: Symbol -&gt; New menu and AmiBroker will automatically read the data for the selected symbol. Please note that various datasources have different symbology, so please always refer to the data vendor&#8217;s Symbol guide to learn about the required symbol format.</p>
<p>Here you will find the links to the most popular vendors guidlines:</p>
<p>- eSignal:<br />
<a href="http://www.esignalcentral.com/support/symbol/symbol_format.asp#forexGarban">http://www.esignalcentral.com/support/symbol/symbol_format.asp#forexGarban</a><br />
<a href="http://www.esignalcentral.com/support/symbol/forex.asp">http://www.esignalcentral.com/support/symbol/forex.asp</a></p>
<p>- IQFeed:<br />
<a href="http://www.iqfeed.net/symbolguide/index.cfm?symbolguide=guide&amp;displayaction=support&amp;section=guide&amp;web=iqfeed">http://www.iqfeed.net/symbolguide/index&#8230;</a></p>
<p>- Interactive Brokers:<br />
<a href="http://www.amibroker.com/ib.html">http://www.amibroker.com/ib.html</a><br />
In case of Interactive Brokers &#8211; if you have any doubt what format to use -<br />
you can easily check any symbol in IB.</p>
<p>Just enter the symbol in Interactive<br />
Brokers TWS, then change the view to Symbol mode<br />
(View -&gt; Symbol mode). Now you can compose the actual symbol out of three<br />
fields:</p>
<p>SYMBOL-EXCHANGE-TYPE<br />
where:<br />
SYMBOL is the same as the symbol column as displayed in TWS while under<br />
symbol mode<br />
EXCHANGE is the exchange d in TWS while under symbol mode<br />
TYPE  is one the following: STK &#8211; stocks, FUT &#8211; futures, FOP &#8211; options on<br />
futures, OPT &#8211; options, IND &#8211; indexes, CASH -cash (ideal FX)</p>
<p> </p>
<p><strong>III. CHARTING</strong></p>
<p>Since most currency pairs requires 4 decimals to display the rates properly, it&#8217;s necessary to set-up AmiBroker accordingly. The number of decimal places can be defined  in Preferences dialog in:</p>
<p>Tools -&gt; Preferences -&gt; Miscellaneous</p>
<p><img src="http://www.amibroker.com/kb/wp-content/uploads/2006/08/fxprefs.gif" /></p>
<p>The changes will also affect such tools as Fibonacci Extension or Retracement drawing tools.</p>
<p> </p>
<p> </p>
<p><strong>IV. SCANNING and DATA EXPLORATIONS </strong></p>
<p>AmiBroker allows you to perform sophisticated scanning and data explorations (both in realtime and with use of historical quotes). To perform data analysis and display the values of chosen indicators in the customized table &#8211; we can use Automatic Analysis window. The detailed description on how to perform explorations is available at:<br />
<a href="http://www.amibroker.com/guide/h_exploration.html">http://www.amibroker.com/guide/h_exploration.html</a></p>
<p>As a short example &#8211; we will find the crossovers of MACD and its Signal line and additionally &#8211; display values of the symbol we test. The 3rd parameter of <strong><a title="AddColumn()" href="http://www.amibroker.com/f?addcolumn">AddColumn()</a></strong> function allows to customize the number of places after decimal point, so it&#8217;s possible to specify if we need 2 or 4 decimal places. If we use:</p>
<p><strong>AddColumn( Close, &#8220;Close&#8221;, 1.4);</strong><br />
then &#8211; 4 decimal places will be displayed. On the other hand &#8211; if we use:</p>
<p><strong>AddColumn( Close, &#8220;Close&#8221;, 1.2);</strong><br />
then AB will display only 2 decimals.</p>
<p>To perform the test - it&#8217;s necessary to do the following:<br />
- open the Formula Editor (Analysis -&gt; Formula Editor)<br />
- enter the formula:<br />
<code><span>Buy&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">Cross</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">MACD</span><span style="color: #000000">(),&nbsp;</span><span style="color: #0000BB">Signal</span><span style="color: #000000">()&nbsp;);<br />
</span><span style="color: #0000BB">Sell&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">Cross</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">Signal</span><span style="color: #000000">(),&nbsp;</span><span style="color: #0000BB">MACD</span><span style="color: #000000">()&nbsp;);<br />
</span><span style="color: #0000BB">Filter&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">Buy&nbsp;</span><span style="color: #000000">OR&nbsp;</span><span style="color: #0000BB">Sell</span><span style="color: #000000">;<br />
</span><span style="color: #0000BB">AddColumn</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">Close</span><span style="color: #000000">,&nbsp;</span><span style="color: #DD0000">&quot;Close&quot;</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">1.4</span><span style="color: #000000">);</span></code><br />
- Tools -&gt; Send to Auto-analysis<br />
- select the time-range of the exploration<br />
- press EXPLORE</p>
<p>As a result &#8211; we will get a list of MACD/Signal crossover points and the value of the chosen symbol on that bar.</p>
<p><strong>V. BACKTESTING</strong></p>
<p>First of all, it&#8217;s necessary to enter the symbol-specific information into Symbol -&gt; Information page (individually for each ticker). In case of currencies denominated in USD (like EURUSD) the following settings should be used:<br />
<img src="http://www.amibroker.com/kb/wp-content/uploads/2006/08/symbolinfo.gif" /></p>
<p>- <strong>Round lot size</strong> should be equal to 1 </p>
<p>- <strong>Tick size</strong> should be set to <strong>pip value</strong> equal 0.0001 for currencies with four decimal digits and to 0.01 for currencies with two decimal digits (so in case of EURUSD it&#8217;s 0.0001).</p>
<p>- <strong>Point value</strong> should be set to to the dollar value of a single pip divided by pip so for EURUSD it will be:<br />
             10$ / 0.0001  =  100000</p>
<p>- <strong>Margin Deposit</strong> in most cases should be set to 1000 (1% margin from $100&#8242;000)</p>
<p> </p>
<p>1) Currencies denominated in USD</p>
<p>Let&#8217;s analyse the results generated by a simple formula (a crossover of 12- and 24-day Moving Averages of Closing price, trading 3 contracts at a time). To perform a backtest &#8211; it&#8217;s necessary to do the following:</p>
<p>- open the Formula Editor (Analysis -&gt; Formula Editor)<br />
- enter the formula:<br />
<code><span>Buy&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">Cross</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">MA</span><span style="color: #000000">(</span><span style="color: #0000BB">Close</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">12</span><span style="color: #000000">)&nbsp;,&nbsp;</span><span style="color: #0000BB">MA</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">Close</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">24</span><span style="color: #000000">)&nbsp;);<br />
</span><span style="color: #0000BB">Sell&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">Cross</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">MA</span><span style="color: #000000">(</span><span style="color: #0000BB">Close</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">24</span><span style="color: #000000">)&nbsp;,&nbsp;</span><span style="color: #0000BB">MA</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">Close</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">12</span><span style="color: #000000">)&nbsp;);<br />
</span><span style="color: #0000BB">SetPositionSize</span><span style="color: #000000">(</span><span style="color: #0000BB">3</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">spsShares&nbsp;</span><span style="color: #000000">);</span></code></p>
<p> - choose: Tools -&gt; Send to Auto-analysis</p>
<p>As a result &#8211; the Automatic Analysis window will open. In the settings dialog (SETTNGS button) it&#8217;s necessary to turn on the FUTURES MODE (in order to use the information entered into the Information dialog) and define the Initial Equity.</p>
<p><img src="http://www.amibroker.com/kb/wp-content/uploads/2006/08/AAsettings.gif" /></p>
<p> </p>
<p>then &#8211; press OK. In the AA window main screen it&#8217;s necessary to define the time range of the backtest and the symbols included in the test. For our example that will be: <strong>Current Symbol, All quotations</strong></p>
<p>Then &#8211; once everything is configured &#8211; press <strong>BACKTEST</strong> button. Now let&#8217;s have a look at the results list.<br />
The profit is calculated as follows:</p>
<p><strong>NumContracts * (SellPrice &#8211; BuyPrice) * PointValue</strong><br />
 </p>
<p><img src="http://www.amibroker.com/kb/wp-content/uploads/2006/08/AAresults1.gif" /> </p>
<p>In the first transaction:<br />
- the <strong>Entry Price</strong> is equal to <strong>1.2154</strong><br />
- the <strong>Exit Price</strong> is equal to <strong>1.2304</strong><br />
- <strong>NumContracts</strong> = <strong>3</strong> (since we trade 3 contracts).<br />
- we trade on <strong>1% margin</strong> so deposit is $1,000 x 3 = <strong>$3,000</strong> (that&#8217;s expressed in <strong>Position Value</strong>)</p>
<p><strong>Profit</strong> =   3 * (1.2304 &#8211; 1.2154) * 100&#8242;000<strong> = 4&#8242;500</strong></p>
<p>So &#8211; the profit matches the results we&#8217;re getting by manual calculation.</p>
<p> </p>
<p>2) Currencies denominated in a different currency from USD (assuming that your account is in USD)</p>
<p>AmiBroker allows you to define a base currency and exchange rates (fixed or dynamic) for different currencies, and as a result - to get correct backtest results when testing securities denominated in different currency than your base portfolio currency.</p>
<p>These settings can be defined in: <strong>Tools -&gt; Preferences -&gt; Currencies </strong>dialog.</p>
<p><img src="http://www.amibroker.com/kb/wp-content/uploads/2006/08/pref13.gif" /></p>
<p> </p>
<p>AmiBroker allows to use both fixed and dynamic (historical) quotes for backtesting purposes (using dynamic quotes will allow you to check the real influence of the currency rates changes for your trades denominated in different currencies). </p>
<p>There are following requirements to use currency adjustements:<br />
a) Symbol-&gt;Information, &#8220;<strong>Currency</strong>&#8221; field shows currency different than BASE currency<br />
b) Appropriate currency (defined in Symbol-&gt; Information) has matching entry in Preferences-&gt;Currencies page<br />
c) the dynamic rate &#8220;FX SYMBOL&#8221; defined in the preferences EXISTS in your database and HAS QUOTES for each day under analysis range.</p>
<p> </p>
<p>&#8220;INVERSE&#8221; check box for in the preferences should be checked, when testing the FX rates like USDJPY or USDCHF &#8211; not denominated in the base currency of the portfolio.</p>
<p>For the same reason - if we look at the example of EURUSD &#8211; when &#8220;USD&#8221; is your BASE currency then EUR exchange rate would be &#8220;straight&#8221; EURUSD fx (e.g. ~1.25). But when &#8220;EUR&#8221; is your BASE currency then USD exchange rate would be INVERSE of EURUSD (i.e. ~1/1.25).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.amibroker.com/kb/2006/08/09/amibroker-for-forex/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>How to display arrows for trades generated in backtest?</title>
		<link>http://www.amibroker.com/kb/2006/08/09/how-to-display-arrows-for-trades-generated-in-backtest/</link>
		<comments>http://www.amibroker.com/kb/2006/08/09/how-to-display-arrows-for-trades-generated-in-backtest/#comments</comments>
		<pubDate>Wed, 09 Aug 2006 07:54:37 +0000</pubDate>
		<dc:creator>Marcin Gorzynski</dc:creator>
				<category><![CDATA[Indicators]]></category>
		<category><![CDATA[Systems]]></category>

		<guid isPermaLink="false">http://www.amibroker.com/kb/2006/08/09/how-to-display-arrows-for-trades-generated-in-backtest/</guid>
		<description><![CDATA[In order to display trade arrows, first of all &#8211; it&#8217;s necessary to enable the arrows for particular chart. To do so &#8211; right-click on the chart pane of your choice, choose: Parameters -&#62; Axes&#38;Grid, then set: Show trading arrows to YES, as shown in the screenshot. 

 
Then &#8211; perform backtest, click on the results [...]]]></description>
			<content:encoded><![CDATA[<p>In order to display trade arrows, first of all &#8211; it&#8217;s necessary to enable the arrows for particular chart. To do so &#8211; right-click on the chart pane of your choice, choose: <strong>Parameters -&gt; Axes&amp;Grid</strong>, then set: <strong>Show trading arrows</strong> to <strong>YES</strong>, as shown in the screenshot. <span id="more-32"></span></p>
<p><img src="http://www.amibroker.com/kb/wp-content/uploads/2006/08/arrows.gif" /></p>
<p> </p>
<p>Then &#8211; perform backtest, click on the results with the right mouse button and choose one of the &#8220;<strong>Show arrows&#8230;.</strong>&#8221; options.</p>
<p><img src="http://www.amibroker.com/kb/wp-content/uploads/2006/08/m_aaresult.gif" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.amibroker.com/kb/2006/08/09/how-to-display-arrows-for-trades-generated-in-backtest/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Re-balancing open positions</title>
		<link>http://www.amibroker.com/kb/2006/03/06/re-balancing-open-positions/</link>
		<comments>http://www.amibroker.com/kb/2006/03/06/re-balancing-open-positions/#comments</comments>
		<pubDate>Mon, 06 Mar 2006 20:50:38 +0000</pubDate>
		<dc:creator>Tomasz Janeczko</dc:creator>
				<category><![CDATA[AFL]]></category>
		<category><![CDATA[Custom Backtest]]></category>
		<category><![CDATA[Systems]]></category>

		<guid isPermaLink="false">http://www.amibroker.com/kb/2006/03/06/re-balancing-open-positions/</guid>
		<description><![CDATA[Here is an example that shows how to code rotational trading system with rebalancing. The system buys and shorts top 20 securities according to absolute value of positionscore (user definable &#8211; in this example we used 20 day rate-of-change) &#8211; each at 5% of equity then each day it rebalances existing positions to 5% if [...]]]></description>
			<content:encoded><![CDATA[<p>Here is an example that shows how to code rotational trading system with rebalancing. The system buys and shorts top 20 securities according to absolute value of positionscore (user definable &#8211; in this example we used 20 day rate-of-change) &#8211; each at 5% of equity then each day it rebalances existing positions to 5% if only the difference between current position value and &#8220;ideal&#8221; value is greater than 0.5% and bigger than one share.<br />
<span id="more-7"></span><br />
Note that this code sample uses <a href="http://www.amibroker.com/guide/a_custombacktest.html">Custom Backtester interface</a> that is documented <a href="http://www.amibroker.com/guide/a_custombacktest.html">here</a>.</p>
<p><code><span>EnableRotationalTrading</span><span style="color: #000000">();&nbsp;</p>
<p></span><span style="color: #0000BB">EachPosPercent&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #000000">;&nbsp;</p>
<p></span><span style="color: #0000BB">PositionScore&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">ROC</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">C</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">20&nbsp;</span><span style="color: #000000">);&nbsp;</p>
<p></span><span style="color: #0000BB">PositionSize&nbsp;</span><span style="color: #000000">=&nbsp;-</span><span style="color: #0000BB">EachPosPercent</span><span style="color: #000000">;&nbsp;</p>
<p></span><span style="color: #0000BB">SetOption</span><span style="color: #000000">(</span><span style="color: #DD0000">&quot;WorstRankHeld&quot;</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">40&nbsp;</span><span style="color: #000000">);<br />
</span><span style="color: #0000BB">SetOption</span><span style="color: #000000">(</span><span style="color: #DD0000">&quot;MaxOpenPositions&quot;</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">20&nbsp;</span><span style="color: #000000">);&nbsp;</p>
<p></span><span style="color: #0000BB">SetOption</span><span style="color: #000000">(</span><span style="color: #DD0000">&quot;UseCustomBacktestProc&quot;</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">True&nbsp;</span><span style="color: #000000">);&nbsp;</p>
<p>if(&nbsp;</span><span style="color: #0000BB">Status</span><span style="color: #000000">(</span><span style="color: #DD0000">&quot;action&quot;</span><span style="color: #000000">)&nbsp;==&nbsp;</span><span style="color: #0000BB">actionPortfolio&nbsp;</span><span style="color: #000000">)<br />
{<br />
&nbsp;&nbsp;</span><span style="color: #0000BB">bo&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">GetBacktesterObject</span><span style="color: #000000">();<br />
&nbsp;<br />
&nbsp;&nbsp;</span><span style="color: #0000BB">bo</span><span style="color: #000000">.</span><span style="color: #0000BB">PreProcess</span><span style="color: #000000">();&nbsp;</span><span style="color: #007000">//&nbsp;Initialize&nbsp;backtester<br />
&nbsp;<br />
&nbsp;&nbsp;</span><span style="color: #000000">for(</span><span style="color: #0000BB">bar</span><span style="color: #000000">=</span><span style="color: #0000BB">0</span><span style="color: #000000">;&nbsp;</span><span style="color: #0000BB">bar&nbsp;</span><span style="color: #000000">&lt;&nbsp;</span><span style="color: #0000BB">BarCount</span><span style="color: #000000">;&nbsp;</span><span style="color: #0000BB">bar</span><span style="color: #000000">++)<br />
&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">bo</span><span style="color: #000000">.</span><span style="color: #0000BB">ProcessTradeSignals</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">bar&nbsp;</span><span style="color: #000000">);<br />
&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">CurEquity&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">bo</span><span style="color: #000000">.</span><span style="color: #0000BB">Equity</span><span style="color: #000000">;<br />
&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;for(&nbsp;</span><span style="color: #0000BB">pos&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">bo</span><span style="color: #000000">.</span><span style="color: #0000BB">GetFirstOpenPos</span><span style="color: #000000">();&nbsp;</span><span style="color: #0000BB">pos</span><span style="color: #000000">;&nbsp;</span><span style="color: #0000BB">pos&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">bo</span><span style="color: #000000">.</span><span style="color: #0000BB">GetNextOpenPos</span><span style="color: #000000">()&nbsp;)<br />
&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">posval&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">pos</span><span style="color: #000000">.</span><span style="color: #0000BB">GetPositionValue</span><span style="color: #000000">();<br />
&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">diff&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">posval&nbsp;</span><span style="color: #000000">-&nbsp;</span><span style="color: #0000BB">0.01&nbsp;</span><span style="color: #000000">*&nbsp;</span><span style="color: #0000BB">EachPosPercent&nbsp;</span><span style="color: #000000">*&nbsp;</span><span style="color: #0000BB">CurEquity</span><span style="color: #000000">;<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">price&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">pos</span><span style="color: #000000">.</span><span style="color: #0000BB">GetPrice</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">bar</span><span style="color: #000000">,&nbsp;</span><span style="color: #DD0000">&quot;O&quot;&nbsp;</span><span style="color: #000000">);<br />
&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007000">//&nbsp;rebalance&nbsp;only&nbsp;if&nbsp;difference&nbsp;between&nbsp;desired&nbsp;and<br />
&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;current&nbsp;position&nbsp;value&nbsp;is&nbsp;greater&nbsp;than&nbsp;0.5%&nbsp;of&nbsp;equity<br />
&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;and&nbsp;greater&nbsp;than&nbsp;price&nbsp;of&nbsp;single&nbsp;share<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000">if(&nbsp;</span><span style="color: #0000BB">diff&nbsp;</span><span style="color: #000000">!=&nbsp;</span><span style="color: #0000BB">0&nbsp;</span><span style="color: #000000">AND<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">abs</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">diff&nbsp;</span><span style="color: #000000">)&nbsp;&gt;&nbsp;</span><span style="color: #0000BB">0.005&nbsp;</span><span style="color: #000000">*&nbsp;</span><span style="color: #0000BB">CurEquity&nbsp;</span><span style="color: #000000">AND<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">abs</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">diff&nbsp;</span><span style="color: #000000">)&nbsp;&gt;&nbsp;</span><span style="color: #0000BB">price&nbsp;</span><span style="color: #000000">)<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">bo</span><span style="color: #000000">.</span><span style="color: #0000BB">ScaleTrade</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">bar</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">pos</span><span style="color: #000000">.</span><span style="color: #0000BB">Symbol</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">diff&nbsp;</span><span style="color: #000000">&lt;&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">price</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">abs</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">diff&nbsp;</span><span style="color: #000000">)&nbsp;);<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;}<br />
&nbsp;&nbsp;</span><span style="color: #0000BB">bo</span><span style="color: #000000">.</span><span style="color: #0000BB">PostProcess</span><span style="color: #000000">();&nbsp;</span><span style="color: #007000">//&nbsp;Finalize&nbsp;backtester<br />
</span><span style="color: #000000">}</span></code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.amibroker.com/kb/2006/03/06/re-balancing-open-positions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Preventing exit during first N bars</title>
		<link>http://www.amibroker.com/kb/2006/03/06/preventing-exit-during-first-n-bars/</link>
		<comments>http://www.amibroker.com/kb/2006/03/06/preventing-exit-during-first-n-bars/#comments</comments>
		<pubDate>Mon, 06 Mar 2006 14:15:22 +0000</pubDate>
		<dc:creator>Tomasz Janeczko</dc:creator>
				<category><![CDATA[AFL]]></category>
		<category><![CDATA[Systems]]></category>

		<guid isPermaLink="false">http://www.amibroker.com/kb/2006/03/06/preventing-exit-during-first-n-bars/</guid>
		<description><![CDATA[Here is sample technique that allows to prevent exiting position during first N bars since entry. The implementation uses loops that checks for signals in Buy array and if it finds one it starts counting bars in trade. During first N bars all sell signals are then removed (set to zero) only once counter reaches [...]]]></description>
			<content:encoded><![CDATA[<p>Here is sample technique that allows to prevent exiting position during first N bars since entry. The implementation uses loops that checks for signals in Buy array and if it finds one it starts counting bars in trade. During first N bars all sell signals are then removed (set to zero) only once counter reaches user-defined limit sell signals are accepted.<br />
<span id="more-6"></span><br />
To change the N parameter please modify MinHoldBars variable (in this sample formula it is set to 17). </p>
<p>Note also that code works only for regular trading systems that do not use any stops.<br />
If you use stops or use rotational trading then the only solution would be using custom backtester. On the positive side: next version of AmiBroker (4.78) will have native implementation of MinHoldBars so you will not need to code it for yourself.</p>
<p><code><span></span><span style="color: #007000">//&nbsp;Sample&nbsp;system<br />
//&nbsp;Buy&nbsp;when&nbsp;MACD&nbsp;is&nbsp;greater&nbsp;than&nbsp;zero&nbsp;AND&nbsp;RSI&nbsp;is&nbsp;greater&nbsp;than&nbsp;30<br />
//&nbsp;Sell&nbsp;if&nbsp;either&nbsp;MACD&nbsp;is&nbsp;less&nbsp;than&nbsp;zero<br />
//&nbsp;OR&nbsp;RSI&nbsp;crosses&nbsp;below&nbsp;70</p>
<p></span><span style="color: #0000BB">Buy&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">MACD</span><span style="color: #000000">()&nbsp;&gt;&nbsp;</span><span style="color: #0000BB">0&nbsp;</span><span style="color: #000000">AND&nbsp;</span><span style="color: #0000BB">RSI</span><span style="color: #000000">()&nbsp;&gt;&nbsp;</span><span style="color: #0000BB">30</span><span style="color: #000000">;<br />
</span><span style="color: #0000BB">Sell&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">MACD</span><span style="color: #000000">()&nbsp;&lt;&nbsp;</span><span style="color: #0000BB">0&nbsp;</span><span style="color: #000000">OR&nbsp;</span><span style="color: #0000BB">Cross</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">70</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">RSI</span><span style="color: #000000">()&nbsp;);</p>
<p></span><span style="color: #007000">//&nbsp;now&nbsp;we&nbsp;would&nbsp;like&nbsp;to&nbsp;ensure&nbsp;that&nbsp;position&nbsp;is&nbsp;NOT<br />
//&nbsp;exited&nbsp;during&nbsp;first&nbsp;MinHoldBars&nbsp;</p>
<p></span><span style="color: #0000BB">MinHoldBars&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">17</span><span style="color: #000000">;&nbsp;</span><span style="color: #007000">//&nbsp;say&nbsp;don't&nbsp;want&nbsp;to&nbsp;exit&nbsp;for&nbsp;first&nbsp;17&nbsp;bars&nbsp;since&nbsp;entry</p>
<p>//&nbsp;first&nbsp;filter&nbsp;out&nbsp;buy&nbsp;signals&nbsp;occuring&nbsp;outside&nbsp;testing&nbsp;range<br />
</span><span style="color: #0000BB">Buy&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">Buy&nbsp;</span><span style="color: #000000">AND&nbsp;</span><span style="color: #0000BB">Status</span><span style="color: #000000">(</span><span style="color: #DD0000">&quot;barinrange&quot;</span><span style="color: #000000">);</p>
<p></span><span style="color: #0000BB">BarsInTrade&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #000000">;<br />
for(&nbsp;</span><span style="color: #0000BB">i&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #000000">;&nbsp;</span><span style="color: #0000BB">i&nbsp;</span><span style="color: #000000">&lt;&nbsp;</span><span style="color: #0000BB">BarCount</span><span style="color: #000000">;&nbsp;</span><span style="color: #0000BB">i</span><span style="color: #000000">++&nbsp;)<br />
{<br />
&nbsp;&nbsp;</span><span style="color: #007000">//&nbsp;if&nbsp;in-trade,&nbsp;then&nbsp;increase&nbsp;bar&nbsp;counter<br />
&nbsp;&nbsp;</span><span style="color: #000000">if(&nbsp;</span><span style="color: #0000BB">BarsInTrade&nbsp;</span><span style="color: #000000">&gt;&nbsp;</span><span style="color: #0000BB">0&nbsp;</span><span style="color: #000000">)&nbsp;</span><span style="color: #0000BB">BarsInTrade&nbsp;</span><span style="color: #000000">++;<br />
&nbsp;&nbsp;else<br />
&nbsp;&nbsp;if(&nbsp;</span><span style="color: #0000BB">Buy</span><span style="color: #000000">[&nbsp;</span><span style="color: #0000BB">i&nbsp;</span><span style="color: #000000">]&nbsp;)&nbsp;</span><span style="color: #0000BB">BarsInTrade&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #000000">;&nbsp;</span><span style="color: #007000">//&nbsp;on&nbsp;buy&nbsp;signal&nbsp;start&nbsp;counting&nbsp;bars-in-trade<br />
&nbsp;<br />
&nbsp;&nbsp;//&nbsp;if&nbsp;we&nbsp;are&nbsp;in&nbsp;trade&nbsp;-&nbsp;remove&nbsp;sells&nbsp;occurring&nbsp;too&nbsp;soon<br />
&nbsp;&nbsp;</span><span style="color: #000000">if(&nbsp;</span><span style="color: #0000BB">BarsInTrade&nbsp;</span><span style="color: #000000">&lt;&nbsp;</span><span style="color: #0000BB">MinHoldBars&nbsp;</span><span style="color: #000000">)&nbsp;</span><span style="color: #0000BB">Sell</span><span style="color: #000000">[&nbsp;</span><span style="color: #0000BB">i&nbsp;</span><span style="color: #000000">]&nbsp;=&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #000000">;<br />
&nbsp;&nbsp;else<br />
&nbsp;&nbsp;if(&nbsp;</span><span style="color: #0000BB">Sell</span><span style="color: #000000">[&nbsp;</span><span style="color: #0000BB">i&nbsp;</span><span style="color: #000000">]&nbsp;)&nbsp;</span><span style="color: #0000BB">BarsInTrade&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #000000">;&nbsp;</span><span style="color: #007000">//&nbsp;on&nbsp;sell&nbsp;reset&nbsp;barsintrade&nbsp;flag<br />
</span><span style="color: #000000">}</span></code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.amibroker.com/kb/2006/03/06/preventing-exit-during-first-n-bars/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

