<?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; Custom Backtest</title>
	<atom:link href="http://www.amibroker.com/kb/category/afl/custom-backtest/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>Historical portfolio backtest metrics</title>
		<link>http://www.amibroker.com/kb/2008/05/19/historical-portfolio-backtest-metrics/</link>
		<comments>http://www.amibroker.com/kb/2008/05/19/historical-portfolio-backtest-metrics/#comments</comments>
		<pubDate>Mon, 19 May 2008 09:22:54 +0000</pubDate>
		<dc:creator>Tomasz Janeczko</dc:creator>
				<category><![CDATA[AFL]]></category>
		<category><![CDATA[Custom Backtest]]></category>

		<guid isPermaLink="false">http://www.amibroker.com/kb/?p=73</guid>
		<description><![CDATA[Recently on the AmiBroker mailing list some users expressed wish to have access to some of portfolio backtest metrics available in &#8220;historical&#8221; form (i.e. as date series, as opposed to scalars), so they can be plotted as an indicator.
Implementing such functionality is actually easy with existing tools and does not require any OLE scripts. Everything [...]]]></description>
			<content:encoded><![CDATA[<p>Recently on the AmiBroker mailing list some users expressed wish to have access to some of portfolio backtest metrics available in &#8220;historical&#8221; form (i.e. as date series, as opposed to scalars), so they can be plotted as an indicator.</p>
<p>Implementing such functionality is actually easy with existing tools and does not require any OLE scripts. Everything you need is small custom-backtester procedure that just reads built-in stats every bar and puts them into composite ticker.<br />
In the accompanying indicator code all you need to do is simply use Foreign() function to access the historical metrics data generated during backtest. </p>
<p>The code below shows the BACKTEST formula with custom backtester part:</p>
<p><code><span></span><span style="color: #007000">//&nbsp;Replace&nbsp;lines&nbsp;below&nbsp;with&nbsp;YOUR&nbsp;TRADING&nbsp;SYSTEM<br />
</span><span style="color: #0000BB">EnableRotationalTrading</span><span style="color: #000000">();&nbsp;<br />
</span><span style="color: #0000BB">PositionScore&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #000000">/</span><span style="color: #0000BB">RSI</span><span style="color: #000000">(</span><span style="color: #0000BB">14</span><span style="color: #000000">);&nbsp;<br />
</span><span style="color: #0000BB">PositionSize&nbsp;</span><span style="color: #000000">=&nbsp;-</span><span style="color: #0000BB">25</span><span style="color: #000000">;<br />
</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">7&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">4&nbsp;</span><span style="color: #000000">);&nbsp;</p>
<p></span><span style="color: #007000">////////////////////////////////////////<br />
//&nbsp;BELOW&nbsp;IS&nbsp;ACTUAL&nbsp;CUSTOM&nbsp;BACKTESTER&nbsp;PART<br />
//&nbsp;that&nbsp;can&nbsp;read&nbsp;any&nbsp;built-in&nbsp;metric&nbsp;(in&nbsp;this&nbsp;example&nbsp;UlcerIndex)<br />
//&nbsp;and&nbsp;store&nbsp;it&nbsp;into&nbsp;composite&nbsp;ticker&nbsp;for&nbsp;further<br />
//&nbsp;retrieval&nbsp;as&nbsp;data&nbsp;series</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;//&nbsp;initialize&nbsp;with&nbsp;null&nbsp;<br />
&nbsp;&nbsp;//&nbsp;you&nbsp;can&nbsp;have&nbsp;as&nbsp;many&nbsp;historical&nbsp;metrics&nbsp;as&nbsp;you&nbsp;want&nbsp;<br />
&nbsp;&nbsp;//&nbsp;(just&nbsp;duplicate&nbsp;line&nbsp;below&nbsp;for&nbsp;many&nbsp;metrics&nbsp;you&nbsp;want)<br />
&nbsp;&nbsp;</span><span style="color: #0000BB">MyHistStat1&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">Null</span><span style="color: #000000">;&nbsp;<br />
&nbsp;&nbsp;</span><span style="color: #0000BB">MyHistStat2&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">Null</span><span style="color: #000000">;&nbsp;</span><span style="color: #007000">//&nbsp;add&nbsp;your&nbsp;own&nbsp;</p>
<p>&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: #007000">//&nbsp;recalculate&nbsp;built-in&nbsp;stats&nbsp;on&nbsp;EACH&nbsp;BAR<br />
&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">stats&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">bo</span><span style="color: #000000">.</span><span style="color: #0000BB">GetPerformanceStats</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">0&nbsp;</span><span style="color: #000000">);&nbsp;<br />
&nbsp;<br />
&nbsp;&nbsp;&nbsp;</span><span style="color: #007000">//&nbsp;the&nbsp;line&nbsp;below&nbsp;reads&nbsp;the&nbsp;metric&nbsp;and&nbsp;stores&nbsp;it&nbsp;as&nbsp;array&nbsp;element<br />
&nbsp;&nbsp;&nbsp;//&nbsp;you&nbsp;can&nbsp;add&nbsp;many&nbsp;lines&nbsp;for&nbsp;each&nbsp;metric&nbsp;of&nbsp;your&nbsp;choice<br />
&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">MyHistStat1</span><span style="color: #000000">[&nbsp;</span><span style="color: #0000BB">bar&nbsp;</span><span style="color: #000000">]&nbsp;=&nbsp;</span><span style="color: #0000BB">stats</span><span style="color: #000000">.</span><span style="color: #0000BB">GetValue</span><span style="color: #000000">(</span><span style="color: #DD0000">&quot;UlcerIndex&quot;</span><span style="color: #000000">);&nbsp;</span><span style="color: #007000">//&nbsp;get&nbsp;ulcer&nbsp;index&nbsp;value&nbsp;calculated&nbsp;this&nbsp;bar<br />
&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">MyHistStat2</span><span style="color: #000000">[&nbsp;</span><span style="color: #0000BB">bar&nbsp;</span><span style="color: #000000">]&nbsp;=&nbsp;</span><span style="color: #0000BB">stats</span><span style="color: #000000">.</span><span style="color: #0000BB">GetValue</span><span style="color: #000000">(</span><span style="color: #DD0000">&quot;WinnersPercent&quot;</span><span style="color: #000000">);&nbsp;</span><span style="color: #007000">//&nbsp;add&nbsp;your&nbsp;own</p>
<p>&nbsp;&nbsp;</span><span style="color: #000000">}</p>
<p>&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</p>
<p>&nbsp;&nbsp;//&nbsp;now&nbsp;STORE&nbsp;the&nbsp;historical&nbsp;data&nbsp;series&nbsp;representing&nbsp;the&nbsp;metric&nbsp;of&nbsp;your&nbsp;choice<br />
&nbsp;&nbsp;//&nbsp;duplicate&nbsp;the&nbsp;line&nbsp;below&nbsp;for&nbsp;as&nbsp;many&nbsp;metrics&nbsp;as&nbsp;you&nbsp;want<br />
&nbsp;&nbsp;</span><span style="color: #0000BB">AddToComposite</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">MyHistStat1</span><span style="color: #000000">,&nbsp;</span><span style="color: #DD0000">&quot;~~~UI_HISTORICAL&quot;</span><span style="color: #000000">,&nbsp;</span><span style="color: #DD0000">&quot;X&quot;</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">atcFlagEnableInPortfolio&nbsp;</span><span style="color: #000000">|&nbsp;</span><span style="color: #0000BB">atcFlagDefaults&nbsp;</span><span style="color: #000000">);</p>
<p>&nbsp;&nbsp;</span><span style="color: #007000">//&nbsp;you&nbsp;can&nbsp;add&nbsp;your&nbsp;own&nbsp;as&nbsp;shown&nbsp;below<br />
&nbsp;&nbsp;</span><span style="color: #0000BB">AddToComposite</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">MyHistStat2</span><span style="color: #000000">,&nbsp;</span><span style="color: #DD0000">&quot;~~~WP_HISTORICAL&quot;</span><span style="color: #000000">,&nbsp;</span><span style="color: #DD0000">&quot;X&quot;</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">atcFlagEnableInPortfolio&nbsp;</span><span style="color: #000000">|&nbsp;</span><span style="color: #0000BB">atcFlagDefaults&nbsp;</span><span style="color: #000000">);&nbsp;<br />
}<br />
</span></code></p>
<p>In the code above, for illustration purposes, we are exporting UlcerIndex and Winners Percent metrics as data series. They are stored in composite tickers for easy retrieval from indicator level.<br />
You can easily extend code to include ANY number of metrics you want.</p>
<p>Now in order to Plot metrics as indicators, use this simple formula:</p>
<p><code><span>PlotForeign</span><span style="color: #000000">(</span><span style="color: #DD0000">&quot;~~~UI_HISTORICAL&quot;</span><span style="color: #000000">,&nbsp;</span><span style="color: #DD0000">&quot;UlcerIndex&nbsp;Historical&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&nbsp;</span><span style="color: #000000">);<br />
</span><span style="color: #0000BB">PlotForeign</span><span style="color: #000000">(</span><span style="color: #DD0000">&quot;~~~WP_HISTORICAL&quot;</span><span style="color: #000000">,&nbsp;</span><span style="color: #DD0000">&quot;Winners&nbsp;Percent&quot;</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">colorBlue</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">styleLine&nbsp;</span><span style="color: #000000">|&nbsp;</span><span style="color: #0000BB">styleOwnScale&nbsp;</span><span style="color: #000000">);<br />
</span></code></p>
<p>As you can see with one Foreign function call you can read the historical value of any metric generated by the backtester.</p>
<p>NOTE: when running backtest please setup a filter in AA that EXCLUDES composites (group 253) from backtest set.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.amibroker.com/kb/2008/05/19/historical-portfolio-backtest-metrics/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using redundant signals for entries</title>
		<link>http://www.amibroker.com/kb/2006/04/24/using-redundant-signals-for-entries/</link>
		<comments>http://www.amibroker.com/kb/2006/04/24/using-redundant-signals-for-entries/#comments</comments>
		<pubDate>Mon, 24 Apr 2006 10:50:42 +0000</pubDate>
		<dc:creator>Tomasz Janeczko</dc:creator>
				<category><![CDATA[AFL]]></category>
		<category><![CDATA[Custom Backtest]]></category>

		<guid isPermaLink="false">http://www.amibroker.com/kb/2006/04/24/using-redundant-signals-for-entries/</guid>
		<description><![CDATA[NOTE: THIS ARTICLE IS NOW OUTDATED AS AMIBROKER SUPPORTS NEW BACKTEST MODE THAT HANDLES THIS NATIVELY http://www.amibroker.com/f?setbacktestmode
The sample code below shows how to use custom portfolio backtester procedure to change the way backtester works. Normally buy is matched against sell and redundant buy signals between initial buy and matching sell are removed as shown in [...]]]></description>
			<content:encoded><![CDATA[<p><FONT color='ff0000'>NOTE: THIS ARTICLE IS NOW OUTDATED AS AMIBROKER SUPPORTS NEW BACKTEST MODE THAT HANDLES THIS NATIVELY <a href="http://www.amibroker.com/f?setbacktestmode">http://www.amibroker.com/f?setbacktestmode</a></font></p>
<p>The sample code below shows how to use custom portfolio backtester procedure to change the way backtester works. Normally buy is matched against sell and redundant buy signals between initial buy and matching sell are removed as shown in the picture there:<br />
<a href="http://www.amibroker.com/gifs/bt_regular.gif">http://www.amibroker.com/gifs/bt_regular.gif</a></p>
<p>The procedure below changes this behaviour and allows to use redundant signals (they are not removed). </p>
<p><span id="more-28"></span>This is done by changing Buy array values from &#8220;true&#8221; to sigScaleIn (this prevents redundant signals from being removed because scale-in marks are kept untouched) and modifying standard procedure to treat scale-in signals as normal buys (no scaling).</p>
<p>Note that there are many ways to achieve the same effect. The technique presented here was choosen because it is easy-to-use (does not require changes in your core trading system code &#8211; all it needs is to plug-in the custom backtest part). Longer implementation would be required if you do not want to (ab)use scaling signals.  </p>
<p>One thing worth mentioning is the fact that since scaling-in signals do not store position score this example formula does not support ranking of signals according to user-defined scores.</p>
<p><code><span></span><span style="color: #007000">//&nbsp;YOUR&nbsp;TRADING&nbsp;SYSTEM&nbsp;HERE&nbsp;</p>
<p></span><span style="color: #0000BB">Buy</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">H&nbsp;</span><span style="color: #000000">==&nbsp;</span><span style="color: #0000BB">HHV</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">H</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">10&nbsp;</span><span style="color: #000000">);&nbsp;</span><span style="color: #007000">//&nbsp;REPLACE&nbsp;THIS&nbsp;WITH&nbsp;YOUR&nbsp;OWN&nbsp;BUY&nbsp;RULE<br />
</span><span style="color: #0000BB">Sell&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">L&nbsp;</span><span style="color: #000000">==&nbsp;</span><span style="color: #0000BB">LLV</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">L</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">10&nbsp;</span><span style="color: #000000">);&nbsp;</span><span style="color: #007000">//&nbsp;REPLACE&nbsp;THIS&nbsp;WITH&nbsp;YOUR&nbsp;OWN&nbsp;SELL&nbsp;RULE</p>
<p></span><span style="color: #0000BB">PositionSize&nbsp;</span><span style="color: #000000">=&nbsp;-</span><span style="color: #0000BB">20</span><span style="color: #000000">;&nbsp;<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">5&nbsp;</span><span style="color: #000000">);&nbsp;</p>
<p></span><span style="color: #007000">//&nbsp;END&nbsp;OF&nbsp;TRADING&nbsp;SYSTEM&nbsp;HERE&nbsp;</p>
<p>//&nbsp;COMMON&nbsp;CODE&nbsp;PART&nbsp;<br />
//&nbsp;TO&nbsp;BE&nbsp;COPY-PASTED&nbsp;if&nbsp;you&nbsp;want&nbsp;keep&nbsp;redundant&nbsp;signals&nbsp;<br />
//&nbsp;This&nbsp;is&nbsp;long-only&nbsp;version.&nbsp;<br />
//&nbsp;It&nbsp;is&nbsp;easy&nbsp;to&nbsp;extend&nbsp;to&nbsp;handle&nbsp;short&nbsp;trades&nbsp;as&nbsp;well&nbsp;</p>
<p></span><span style="color: #0000BB">Buy&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">IIf</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">Buy</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">sigScaleIn</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">False&nbsp;</span><span style="color: #000000">);&nbsp;</span><span style="color: #007000">//&nbsp;replace&nbsp;regular&nbsp;buy&nbsp;signals&nbsp;by&nbsp;scale&nbsp;in&nbsp;<br />
//&nbsp;so&nbsp;they&nbsp;do&nbsp;not&nbsp;get&nbsp;filtered&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 />
{&nbsp;<br />
&nbsp;&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">();&nbsp;</p>
<p>&nbsp;&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&nbsp;</p>
<p>&nbsp;&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</span><span style="color: #000000">&lt;</span><span style="color: #0000BB">BarCount</span><span style="color: #000000">;&nbsp;</span><span style="color: #0000BB">bar</span><span style="color: #000000">++)&nbsp;<br />
&nbsp;&nbsp;&nbsp;{&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(&nbsp;</span><span style="color: #0000BB">sig</span><span style="color: #000000">=</span><span style="color: #0000BB">bo</span><span style="color: #000000">.</span><span style="color: #0000BB">GetFirstSignal</span><span style="color: #000000">(</span><span style="color: #0000BB">bar</span><span style="color: #000000">);&nbsp;</span><span style="color: #0000BB">sig</span><span style="color: #000000">;&nbsp;</span><span style="color: #0000BB">sig</span><span style="color: #000000">=</span><span style="color: #0000BB">bo</span><span style="color: #000000">.</span><span style="color: #0000BB">GetNextSignal</span><span style="color: #000000">(</span><span style="color: #0000BB">bar</span><span style="color: #000000">)&nbsp;)&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007000">//&nbsp;first&nbsp;handle&nbsp;exit&nbsp;signals&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000">if&nbsp;(</span><span style="color: #0000BB">sig</span><span style="color: #000000">.</span><span style="color: #0000BB">IsExit</span><span style="color: #000000">()&nbsp;AND&nbsp;</span><span style="color: #0000BB">sig</span><span style="color: #000000">.</span><span style="color: #0000BB">Price&nbsp;</span><span style="color: #000000">!=&nbsp;-</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #000000">)&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007000">//&nbsp;Exit&nbsp;Signal&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">bo</span><span style="color: #000000">.</span><span style="color: #0000BB">ExitTrade</span><span style="color: #000000">(</span><span style="color: #0000BB">bar</span><span style="color: #000000">,</span><span style="color: #0000BB">sig</span><span style="color: #000000">.</span><span style="color: #0000BB">symbol</span><span style="color: #000000">,</span><span style="color: #0000BB">sig</span><span style="color: #000000">.</span><span style="color: #0000BB">Price</span><span style="color: #000000">);&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007000">//&nbsp;update&nbsp;stats&nbsp;after&nbsp;closing&nbsp;trades&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">bo</span><span style="color: #000000">.</span><span style="color: #0000BB">UpdateStats</span><span style="color: #000000">(</span><span style="color: #0000BB">bar</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #000000">);&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">bContinue&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">True</span><span style="color: #000000">;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(&nbsp;</span><span style="color: #0000BB">sig</span><span style="color: #000000">=</span><span style="color: #0000BB">bo</span><span style="color: #000000">.</span><span style="color: #0000BB">GetFirstSignal</span><span style="color: #000000">(</span><span style="color: #0000BB">bar</span><span style="color: #000000">);&nbsp;</span><span style="color: #0000BB">sig&nbsp;</span><span style="color: #000000">AND&nbsp;</span><span style="color: #0000BB">bContinue</span><span style="color: #000000">;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">sig</span><span style="color: #000000">=</span><span style="color: #0000BB">bo</span><span style="color: #000000">.</span><span style="color: #0000BB">GetNextSignal</span><span style="color: #000000">(</span><span style="color: #0000BB">bar</span><span style="color: #000000">))&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007000">//&nbsp;enter&nbsp;new&nbsp;trade&nbsp;when&nbsp;scale-in&nbsp;signal&nbsp;is&nbsp;found&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;and&nbsp;we&nbsp;don't&nbsp;have&nbsp;already&nbsp;open&nbsp;position&nbsp;for&nbsp;given&nbsp;symbol&nbsp;</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000">if&nbsp;(</span><span style="color: #0000BB">sig</span><span style="color: #000000">.</span><span style="color: #0000BB">IsScale</span><span style="color: #000000">()&nbsp;AND&nbsp;</span><span style="color: #0000BB">sig</span><span style="color: #000000">.</span><span style="color: #0000BB">Price&nbsp;</span><span style="color: #000000">!=&nbsp;-</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #000000">AND&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">IsNull</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">bo</span><span style="color: #000000">.</span><span style="color: #0000BB">FindOpenPos</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">sig</span><span style="color: #000000">.</span><span style="color: #0000BB">Symbol&nbsp;</span><span style="color: #000000">)&nbsp;)&nbsp;)&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007000">//&nbsp;Entry&nbsp;Signal&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000">if(&nbsp;</span><span style="color: #0000BB">bo</span><span style="color: #000000">.</span><span style="color: #0000BB">EnterTrade</span><span style="color: #000000">(</span><span style="color: #0000BB">bar</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">sig</span><span style="color: #000000">.</span><span style="color: #0000BB">symbol</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">sig</span><span style="color: #000000">.</span><span style="color: #0000BB">IsLong</span><span style="color: #000000">(),&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">sig</span><span style="color: #000000">.</span><span style="color: #0000BB">Price</span><span style="color: #000000">,</span><span style="color: #0000BB">sig</span><span style="color: #000000">.</span><span style="color: #0000BB">PosSize</span><span style="color: #000000">)&nbsp;==&nbsp;</span><span style="color: #0000BB">0&nbsp;</span><span style="color: #000000">)&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007000">//&nbsp;if&nbsp;certain&nbsp;trade&nbsp;can&nbsp;not&nbsp;be&nbsp;entered&nbsp;due&nbsp;to&nbsp;insufficient&nbsp;funds&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;or&nbsp;too&nbsp;small&nbsp;value&nbsp;(less&nbsp;than&nbsp;"MinPositionValue")&nbsp;or&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;to&nbsp;few&nbsp;shares&nbsp;(less&nbsp;than&nbsp;"MinShares&quot;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;then&nbsp;do&nbsp;NOT&nbsp;process&nbsp;any&nbsp;further&nbsp;signals&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">bContinue&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">False</span><span style="color: #000000">;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">bo</span><span style="color: #000000">.</span><span style="color: #0000BB">UpdateStats</span><span style="color: #000000">(</span><span style="color: #0000BB">bar</span><span style="color: #000000">,</span><span style="color: #0000BB">1</span><span style="color: #000000">);&nbsp;</span><span style="color: #007000">//&nbsp;MAE/MFE&nbsp;is&nbsp;updated&nbsp;when&nbsp;timeinbar&nbsp;is&nbsp;set&nbsp;to&nbsp;1.&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">bo</span><span style="color: #000000">.</span><span style="color: #0000BB">UpdateStats</span><span style="color: #000000">(</span><span style="color: #0000BB">bar</span><span style="color: #000000">,</span><span style="color: #0000BB">2</span><span style="color: #000000">);&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;}&nbsp;<br />
&nbsp;&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&nbsp;<br />
&nbsp;&nbsp;&nbsp;</p>
<p></span><span style="color: #000000">}&nbsp;</span></code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.amibroker.com/kb/2006/04/24/using-redundant-signals-for-entries/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Adding custom metric: Average adverse excursion</title>
		<link>http://www.amibroker.com/kb/2006/04/04/adding-custom-metric-average-adverse-excursion/</link>
		<comments>http://www.amibroker.com/kb/2006/04/04/adding-custom-metric-average-adverse-excursion/#comments</comments>
		<pubDate>Tue, 04 Apr 2006 20:20:55 +0000</pubDate>
		<dc:creator>Tomasz Janeczko</dc:creator>
				<category><![CDATA[AFL]]></category>
		<category><![CDATA[Custom Backtest]]></category>

		<guid isPermaLink="false">http://www.amibroker.com/kb/2006/04/04/adding-custom-metric-average-adverse-excursion/</guid>
		<description><![CDATA[Here is a sample that shows how to create custom metric based on per-trade statisitics.
In this example we will calculate the average value of MAE (maximum adverse excursion) from all trades.

SetCustomBacktestProc(&#34;&#34;);&#160;
/*&#160;Now&#160;custom-backtest&#160;procedure&#160;follows&#160;*/&#160;
if(&#160;Status(&#34;action&#34;)&#160;==&#160;actionPortfolio&#160;)&#160;
{&#160;
&#160;&#160;&#160;&#160;bo&#160;=&#160;GetBacktesterObject();&#160;
&#160;&#160;&#160;&#160;bo.Backtest();&#160;//&#160;run&#160;default&#160;backtest&#160;procedure&#160;
&#160;&#160;&#160;SumMAE&#160;=&#160;0;&#160;
&#160;&#160;&#160;NumTrades&#160;=&#160;0;&#160;
&#160;&#160;&#160;//&#160;iterate&#160;through&#160;closed&#160;trades&#160;first&#160;
&#160;&#160;&#160;for(&#160;trade&#160;=&#160;bo.GetFirstTrade();&#160;trade;&#160;trade&#160;=&#160;bo.GetNextTrade()&#160;)&#160;
&#160;&#160;&#160;{&#160;
&#160;&#160;&#160;&#160;&#160;&#160;//&#160;here&#160;we&#160;sum&#160;up&#160;maximum&#160;adverse&#160;excursions
&#160;&#160;&#160;&#160;&#160;&#160;&#160;SumMAE&#160;=&#160;SumMAE&#160;+&#160;trade.GetMAE();
&#160;&#160;&#160;&#160;&#160;&#160;&#160;NumTrades++;&#160;
&#160;&#160;&#160;}&#160;
&#160;&#160;&#160;//&#160;iterate&#160;through&#160;eventually&#160;still&#160;open&#160;positions&#160;
&#160;&#160;&#160;for(&#160;trade&#160;=&#160;bo.GetFirstOpenPos();&#160;trade;&#160;trade&#160;=&#160;bo.GetNextOpenPos()&#160;)&#160;
&#160;&#160;&#160;{&#160;
&#160;&#160;&#160;&#160;&#160;&#160;&#160;SumMAE&#160;=&#160;SumMAE&#160;+&#160;trade.GetMAE();
&#160;&#160;&#160;&#160;&#160;&#160;&#160;NumTrades++;&#160;
&#160;&#160;&#160;}&#160;
&#160;&#160;&#160;averageMAE&#160;=&#160;SumMAE&#160;/&#160;NumTrades;&#160;
&#160;&#160;&#160;bo.AddCustomMetric(&#160;&#34;Avg.&#160;adverse&#160;excursion&#34;,&#160;averageMAE&#160;);&#160;
}&#160;
//&#160;your&#160;trading&#160;system&#160;here&#160;
//...
]]></description>
			<content:encoded><![CDATA[<p>Here is a sample that shows how to create custom metric based on per-trade statisitics.<br />
In this example we will calculate the average value of MAE (maximum adverse excursion) from all trades.<br />
<span id="more-27"></span><br />
<code><span>SetCustomBacktestProc</span><span style="color: #000000">(</span><span style="color: #DD0000">&quot;&quot;</span><span style="color: #000000">);&nbsp;</p>
<p></span><span style="color: #007000">/*&nbsp;Now&nbsp;custom-backtest&nbsp;procedure&nbsp;follows&nbsp;*/&nbsp;</p>
<p></span><span style="color: #000000">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">)&nbsp;<br />
{&nbsp;<br />
&nbsp;&nbsp;&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">();&nbsp;</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">bo</span><span style="color: #000000">.</span><span style="color: #0000BB">Backtest</span><span style="color: #000000">();&nbsp;</span><span style="color: #007000">//&nbsp;run&nbsp;default&nbsp;backtest&nbsp;procedure&nbsp;</p>
<p>&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">SumMAE&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #000000">;&nbsp;<br />
&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">NumTrades&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #000000">;&nbsp;</p>
<p>&nbsp;&nbsp;&nbsp;</span><span style="color: #007000">//&nbsp;iterate&nbsp;through&nbsp;closed&nbsp;trades&nbsp;first&nbsp;<br />
&nbsp;&nbsp;&nbsp;</span><span style="color: #000000">for(&nbsp;</span><span style="color: #0000BB">trade&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">bo</span><span style="color: #000000">.</span><span style="color: #0000BB">GetFirstTrade</span><span style="color: #000000">();&nbsp;</span><span style="color: #0000BB">trade</span><span style="color: #000000">;&nbsp;</span><span style="color: #0000BB">trade&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">bo</span><span style="color: #000000">.</span><span style="color: #0000BB">GetNextTrade</span><span style="color: #000000">()&nbsp;)&nbsp;<br />
&nbsp;&nbsp;&nbsp;{&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007000">//&nbsp;here&nbsp;we&nbsp;sum&nbsp;up&nbsp;maximum&nbsp;adverse&nbsp;excursions<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">SumMAE&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">SumMAE&nbsp;</span><span style="color: #000000">+&nbsp;</span><span style="color: #0000BB">trade</span><span style="color: #000000">.</span><span style="color: #0000BB">GetMAE</span><span style="color: #000000">();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">NumTrades</span><span style="color: #000000">++;&nbsp;<br />
&nbsp;&nbsp;&nbsp;}&nbsp;</p>
<p>&nbsp;&nbsp;&nbsp;</span><span style="color: #007000">//&nbsp;iterate&nbsp;through&nbsp;eventually&nbsp;still&nbsp;open&nbsp;positions&nbsp;<br />
&nbsp;&nbsp;&nbsp;</span><span style="color: #000000">for(&nbsp;</span><span style="color: #0000BB">trade&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">trade</span><span style="color: #000000">;&nbsp;</span><span style="color: #0000BB">trade&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;)&nbsp;<br />
&nbsp;&nbsp;&nbsp;{&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">SumMAE&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">SumMAE&nbsp;</span><span style="color: #000000">+&nbsp;</span><span style="color: #0000BB">trade</span><span style="color: #000000">.</span><span style="color: #0000BB">GetMAE</span><span style="color: #000000">();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">NumTrades</span><span style="color: #000000">++;&nbsp;<br />
&nbsp;&nbsp;&nbsp;}&nbsp;</p>
<p>&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">averageMAE&nbsp;</span><span style="color: #000000">=&nbsp;</span><span style="color: #0000BB">SumMAE&nbsp;</span><span style="color: #000000">/&nbsp;</span><span style="color: #0000BB">NumTrades</span><span style="color: #000000">;&nbsp;</p>
<p>&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">bo</span><span style="color: #000000">.</span><span style="color: #0000BB">AddCustomMetric</span><span style="color: #000000">(&nbsp;</span><span style="color: #DD0000">&quot;Avg.&nbsp;adverse&nbsp;excursion&quot;</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000BB">averageMAE&nbsp;</span><span style="color: #000000">);&nbsp;</p>
<p>}&nbsp;</p>
<p></span><span style="color: #007000">//&nbsp;your&nbsp;trading&nbsp;system&nbsp;here&nbsp;<br />
//...</span></code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.amibroker.com/kb/2006/04/04/adding-custom-metric-average-adverse-excursion/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to create copy of portfolio equity?</title>
		<link>http://www.amibroker.com/kb/2006/03/11/how-to-create-copy-of-portfolio-equity/</link>
		<comments>http://www.amibroker.com/kb/2006/03/11/how-to-create-copy-of-portfolio-equity/#comments</comments>
		<pubDate>Sat, 11 Mar 2006 19:32:51 +0000</pubDate>
		<dc:creator>Tomasz Janeczko</dc:creator>
				<category><![CDATA[AFL]]></category>
		<category><![CDATA[Custom Backtest]]></category>

		<guid isPermaLink="false">http://www.amibroker.com/kb/2006/03/11/how-to-create-copy-of-portfolio-equity/</guid>
		<description><![CDATA[As you know Portfolio backtester creates special ticker &#8220;~~~EQUITY&#8221; which holds portfolio-level equity of the system under test. Some may find it useful to save this equity into another symbol after backtest for future analysis and/or comparison. Good news is that it is possible to do that automatically using custom backtester procedure and AddToComposite function. [...]]]></description>
			<content:encoded><![CDATA[<p>As you know Portfolio backtester creates special ticker &#8220;~~~EQUITY&#8221; which holds portfolio-level equity of the system under test. Some may find it useful to save this equity into another symbol after backtest for future analysis and/or comparison. Good news is that it is possible to do that automatically using <a href="http://www.amibroker.com/guide/h_custombacktest.html">custom backtester procedure</a> and <a href="http://www.amibroker.com/f?addtocomposite">AddToComposite</a> function. The formula below shows how.<br />
<span id="more-13"></span><br />
<code><span></span><span style="color: #007000">//&nbsp;YOUR&nbsp;TRADING&nbsp;SYSTEM&nbsp;HERE</p>
<p></span><span style="color: #0000BB">SetCustomBacktestProc</span><span style="color: #000000">(</span><span style="color: #DD0000">&quot;&quot;</span><span style="color: #000000">);</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;</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;</span><span style="color: #0000BB">bo</span><span style="color: #000000">.</span><span style="color: #0000BB">Backtest</span><span style="color: #000000">();<br />
&nbsp;</span><span style="color: #0000BB">AddToComposite</span><span style="color: #000000">(&nbsp;</span><span style="color: #0000BB">Foreign</span><span style="color: #000000">(</span><span style="color: #DD0000">&quot;~~~EQUITY&quot;</span><span style="color: #000000">,</span><span style="color: #DD0000">&quot;C&quot;</span><span style="color: #000000">),&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">&quot;~~~MY_EQUITY_COPY&quot;</span><span style="color: #000000">,&nbsp;</span><span style="color: #DD0000">&quot;X&quot;</span><span style="color: #000000">,&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">atcFlagDeleteValues&nbsp;</span><span style="color: #000000">|&nbsp;</span><span style="color: #0000BB">atcFlagEnableInPortfolio&nbsp;</span><span style="color: #000000">);<br />
}</span></code></p>
<p>Please note that prior to version 4.78.1 BETA, atcFlagEnableInPortfolio did not work in combination with atcFlagCompositeGroup (or atcFlagDefaults) so we needed to avoid this flag in the code above, to make it work fine with previous versions of AmiBroker. In version 4.78.1 it works with all combinations of flags, so you don&#8217;t need to worry about this any more.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.amibroker.com/kb/2006/03/11/how-to-create-copy-of-portfolio-equity/feed/</wfw:commentRss>
		<slash:comments>4</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>
	</channel>
</rss>

