<?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"
	>

<channel>
	<title>AmiBroker Users' Knowledge Base</title>
	<atom:link href="http://www.amibroker.org/userkb/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.amibroker.org/userkb</link>
	<description>Share your experience, code and everything with other AmiBroker Users'.</description>
	<pubDate>Fri, 02 May 2008 13:22:20 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
	<language>en</language>
			<item>
		<title>Wordpress upgrade</title>
		<link>http://www.amibroker.org/userkb/2008/04/15/wordpress-upgrade/</link>
		<comments>http://www.amibroker.org/userkb/2008/04/15/wordpress-upgrade/#comments</comments>
		<pubDate>Tue, 15 Apr 2008 10:56:18 +0000</pubDate>
		<dc:creator>Tomasz Janeczko</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.amibroker.org/userkb/?p=1774</guid>
		<description><![CDATA[Due to vulnerability found in previous version of Wordpress (2.2) and attack attempts this site experienced and the compatibility issues with Windows Live Writer that surfaced after quick security fix was applied, I was forced to perform upgrade to newest version of Wordpress.
This results in old Table-of-content plugin not working anymore and the need for [...]]]></description>
			<content:encoded><![CDATA[<p>Due to vulnerability found in previous version of Wordpress (2.2) and attack attempts this site experienced and the compatibility issues with Windows Live Writer that surfaced after quick security fix was applied, I was forced to perform upgrade to newest version of Wordpress.<br />
This results in old Table-of-content plugin not working anymore and the need for rewrite. The new version produces ill-formatted output. Please be patient when TOC plugin is under rewrite.</p>
<p>Also note that although read experience does not change (the site looks identical from reader perspective), the author control panel has changed and looks different. It requires some time to adjust to new design, but after a while I think it is better. I am sorry about this mess but it was not caused by me.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.amibroker.org/userkb/2008/04/15/wordpress-upgrade/feed/</wfw:commentRss>
		</item>
		<item>
		<title>US-Stocks Database (v2)</title>
		<link>http://www.amibroker.org/userkb/2008/03/27/us-stocks-database/</link>
		<comments>http://www.amibroker.org/userkb/2008/03/27/us-stocks-database/#comments</comments>
		<pubDate>Thu, 27 Mar 2008 22:03:03 +0000</pubDate>
		<dc:creator>brian_z</dc:creator>
		
		<category><![CDATA[Yahoo]]></category>

		<guid isPermaLink="false">http://www.amibroker.org/userkb/2007/11/10/us-stocks-database/</guid>
		<description><![CDATA[Jim, who is a long term AmiBroker user, regularly provides an updated template that automatically sets up a database of all US stocks available from Yahoo!Finance.
The stocks are initially categorized by Market and then as Stocks, Indexes or Funds at the Group level. They are also organized into Sectors, using the Hemscott classification system.
A link [...]]]></description>
			<content:encoded><![CDATA[<p>Jim, who is a long term <em>AmiBroker</em> user, regularly provides an updated template that automatically sets up a database of all US stocks available from <em>Yahoo!Finance</em>.</p>
<p>The stocks are initially categorized by <em>Market</em> and then as <em>Stocks, Indexes</em> or <em>Funds</em> at the <em>Group</em> level. They are also organized into <em>Sectors</em>, using the <em>Hemscott </em>classification system.</p>
<p>A link to the latest message from Jim is available at AmiBrokerYahooGroup message # 119057 - &quot;AmiBroker/Yahoo Database File&quot;:&#160; <a title="http://finance.groups.yahoo.com/group/amibroker/message/119057" href="http://finance.groups.yahoo.com/group/amibroker/message/119057">http://finance.groups.yahoo.com/group/amibroker/message/119057</a></p>
<p>Or link directly to the site to obtain the download:</p>
<p><a href="http://www.icc-az.com">http://www.icc-az.com</a></p>
<p>Go to Links &gt;&gt; AmiBroker at that site (instructions are available at the download page).</p>
<p>After the database is setup, use AmiQuote to download data from the <em>Yahoo Historical</em> server (allow approximately 1/2 - 1 hour download time, using a mid speed broadband connection, per year of history).</p>
<p>The length of back history available varies with each symbol (the author has downloaded up to 10 years history using this template).</p>
<p>Typically the ticker list, as setup, will contain a couple of hundred redundant symbols (symbols that return an error message when attempting to download historical data for them). Most of the redundant symbols are .OB (over the counter stock) and are not relevant to the majority of traders. Users can elect to delete them from the database or go to the Yahoo site, or other sites, to find the cause of the &#8216;download error&#8217; e.g. the stock may have been de-listed or undergone a name change .</p>
<p>Instructions on how to find and delete the redundant (NoQuotes) symbols can be found in the UKB post &gt;&gt; <a href="http://www.amibroker.org/userkb/2007/11/09/setup-a-custom-database-nasdaq/">Setup A Custom Database - Nasdaq</a></p>
<p>Information, or discussion, on the pros and cons of using <em>Yahoo</em> data is included in articles in the <em>Yahoo</em> sub-category at the <em>Users&#8217; Knowledge Base:</em></p>
<p><a title="http://www.amibroker.org/userkb/?cat=99" href="http://www.amibroker.org/userkb/?cat=99">http://www.amibroker.org/userkb/?cat=99</a></p>
<p>Further information, about using the US-Stocks database, can be obtained by searching the <em>AmiBrokerYahooGroup</em> message board (use an <em>Advanced Search</em> and enter S<em>ubject contains &quot;US-Stocks&quot;</em> OR &quot;<em>AmiBroker/Yahoo Database</em>&quot; as the search criteria . </p>
<p>&#160;</p>
<p><sub>Version 2 - Feb 02/2008 - by Brian_z - written using Windows Live Writer - updated links to current message and new download site</sub></p>
]]></content:encoded>
			<wfw:commentRss>http://www.amibroker.org/userkb/2008/03/27/us-stocks-database/feed/</wfw:commentRss>
		</item>
		<item>
		<title>ASCII Import - Standard and Poor&#8217;s Global 1200</title>
		<link>http://www.amibroker.org/userkb/2008/03/25/ascii-import-standard-and-poors-global-1200/</link>
		<comments>http://www.amibroker.org/userkb/2008/03/25/ascii-import-standard-and-poors-global-1200/#comments</comments>
		<pubDate>Tue, 25 Mar 2008 09:44:08 +0000</pubDate>
		<dc:creator>brian_z</dc:creator>
		
		<category><![CDATA[Database Management]]></category>

		<guid isPermaLink="false">http://www.amibroker.org/userkb/2008/03/25/ascii-import-standard-and-poors-global-1200/</guid>
		<description><![CDATA[Objective&#160;
The objective, for this post, is to outline a basic method for setting up a custom database, using ASCII importation to install the categorized structure.
A Global 1200 database, suitable for use with Yahoo data, is used as the example.
Note: it is an outline only and the procedure is not taken to completion i.e. it is [...]]]></description>
			<content:encoded><![CDATA[<p><strong><u>Objective</u></strong>&#160;</p>
<p>The objective, for this post, is to outline a basic method for setting up a custom database, using ASCII importation to install the categorized structure.</p>
<p>A Global 1200 database, suitable for use with <em>Yahoo</em> data, is used as the example.</p>
<p>Note: it is an outline only and the procedure is not taken to completion i.e. it is not set-up as a working database (interested readers can complete that task for themselves).</p>
<p>&#160;<u><strong>Pre-Requisites</strong></u></p>
<p>It is assumed that readers are familiar with earlier posts in the <em>UKB &gt;&gt; Database Management</em> series (the basic skills introduced there are not recapped in this post) and also the <em>ASCII Importer Reference</em> (from the <em>Users Guide</em>).</p>
<p><a title="http://www.amibroker.com/guide/d_ascii.html" href="http://www.amibroker.com/guide/d_ascii.html">http://www.amibroker.com/guide/d_ascii.html</a></p>
<p>&#160;<strong><u>Introduction</u></strong></p>
<blockquote><p>&quot;American Standard Code for Information Interchange (ASCII)<strong> </strong>allows digital devices to communicate with each other and to process, store, and communicate character-oriented information&quot; <sup>1</sup>.</p>
</blockquote>
<p>It is the format used to import data into <em>AmiBroker</em>, via <em>AmiQuote</em>, or manually, via the <em>ASCII Import</em> function.</p>
<p>In this example, a list of the <em>Global 1200</em> constituents, including <em>Market</em> and <em>Sector</em> categorization, is downloaded from the <em>Standard and Poors</em> (US) site and pre-conditioned into an ASCII compliant format, before importing it into <em>AmiBroker</em>.</p>
<p>Note: the example is a basic one, but, users can easily extend the method to more complex databases.</p>
<p><strong><u>Preparation</u></strong></p>
<p><strong><u></u></strong></p>
<p><strong><u>Step1</u></strong></p>
<p>A constituent list, in Comma Separated Value (CSV) format, can be downloaded from the <em>Standard and Poors Homesite</em> (US):</p>
<p><a title="http://www2.standardandpoors.com/portal/site/sp/en/us/page.home/home/0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0.html" href="http://www2.standardandpoors.com/portal/site/sp/en/us/page.home/home/0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0.html">http://www2.standardandpoors.com/portal/site/sp/en/us/page.home/home/0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0.html</a></p>
<p>Go to <strong>Indices &gt;&gt;&#160; + Equity Indices &gt;&gt; S and P Global Indices &gt;&gt; S and P Global 1200</strong></p>
<p>OR follow the link below to go directly to the page:</p>
<p><a title="http://www2.standardandpoors.com/portal/site/sp/en/us/page.topic/indices_gbl1200/2,3,2,1,0,0,0,0,0,0,3,0,0,0,0,0.html" href="http://www2.standardandpoors.com/portal/site/sp/en/us/page.topic/indices_gbl1200/2,3,2,1,0,0,0,0,0,0,3,0,0,0,0,0.html">http://www2.standardandpoors.com/portal/site/sp/en/us/page.topic/indices_gbl1200/2,3,2,1,0,0,0,0,0,0,3,0,0,0,0,0.html</a></p>
<p>&#160;</p>
<p><strong><u>Step 2</u></strong></p>
<p>Click on <strong>Download Table, </strong>at the top of the page, to save a local copy of the <em>Constituent List</em> (save it in a spreadsheet compliant format)</p>
<p><a href="http://www.amibroker.org/userkb/wp-content/uploads/2008/03/asc001.png"><img height="523" alt="ASC001" src="http://www.amibroker.org/userkb/wp-content/uploads/2008/03/asc001-thumb.png" width="595" border="0" /></a></p>
<p>Note: not all of the Countries, with stocks in the <em>Global 1200</em>, are supported by <em>Yahoo!Finance</em> (refer to the following link for a list of <em>Yahoo&#8217;s</em> international exchanges&#160; - <a title="http://finance.yahoo.com/exchanges" href="http://finance.yahoo.com/exchanges">http://finance.yahoo.com/exchanges</a>)</p>
<p>&#160;</p>
<p><strong><u>Step 3</u></strong></p>
<p>Prepare the file for ASCII Import, using the spreadsheet functions:</p>
<p>a) Trim excess &#8216;whitespace&#8217; from the symbols, manually enter <em>Yahoo</em> exchange suffixes and append them to the symbols, sort by <strong>Market</strong> (Country) and manually add a column for the <strong>Market ID</strong> (0-23).</p>
<p>b) Delete symbols that are from unsupported exchanges, sort by <strong>Sector</strong> and manually add a column for the <strong>Sector ID&#8217;s</strong> (0-9).</p>
<p>c) Copy the <strong>Fullname</strong>, <strong>Ticker</strong>, <strong>Market ID</strong> and <strong>Sector ID</strong> columns to a separate <strong>Global1200.CSV</strong> file, ready for importing.</p>
<p>Note: Refer to the attached files <strong>24-March-2008_GBL1200.xls</strong> and <strong>Global1200.xls </strong>(the Global1200 file is attached in .xls format as the UKB does not permit uploading of .csv files).</p>
<p>&#160;</p>
<p><strong><u>Step 4</u></strong></p>
<p>Prepare a <strong>broker.industries</strong> and <strong>broker.sector file</strong>, using the template in the <strong>24-March-2008_GBL1200.xls</strong> file, and save them in the root of the <strong>Programs/AmiBroker</strong><em> </em>directory (overwrite the existing broker.industries and broker.sector files).</p>
<p>&#160;</p>
<p><strong><u>Step 5</u></strong></p>
<p>Prepare an ASCII formats file and save it in the <strong>Programs/AmiBroker/Formats</strong> folder.</p>
<p>The file, for this example, needs to be in the following format (the file can be written in a plain text editor, like <em>NotePad</em>, and saved as <strong>Global1200.format</strong>):</p>
<p><a href="http://www.amibroker.org/userkb/wp-content/uploads/2008/03/asc002.png"><img height="445" alt="ASC002" src="http://www.amibroker.org/userkb/wp-content/uploads/2008/03/asc002-thumb.png" width="736" border="0" /></a></p>
<p>Note: The file name will be automatically added to the <em>import.types</em> list, which is also in the <em>Programs/AmiBroker/Formats</em> folder.</p>
<p>&#160;</p>
<p><strong><u>Step 6</u></strong></p>
<p>Create a new EOD database, called <strong>Data_Global1200</strong></p>
<p>Go to <strong>File &gt;&gt; Import ASCII</strong> and elect to <strong>Open</strong> the <strong>Global1200.csv</strong> file as file type <strong>Global1200(*.*)</strong></p>
<p>Note: the <em>import.types</em> (formats file) list will be the default <em>Files of type</em> list in the file browser window that opens.</p>
<p><a href="http://www.amibroker.org/userkb/wp-content/uploads/2008/03/asc003.png"><img height="478" alt="ASC003" src="http://www.amibroker.org/userkb/wp-content/uploads/2008/03/asc003-thumb.png" width="563" border="0" /></a></p>
<p>The symbol list and database structure, as defined in the Global1200.csv file, will now be set-up in the current database.</p>
<p>Note: the Markets and Groups categories will remain as the default settings (they can only be renamed manually via <strong>Symbol &gt;&gt; Categories).</strong></p>
<p><a href="http://www.amibroker.org/userkb/wp-content/uploads/2008/03/asc005.png"><img height="369" alt="ASC005" src="http://www.amibroker.org/userkb/wp-content/uploads/2008/03/asc005-thumb.png" width="485" border="0" /></a></p>
<p><strong><u>Step 7</u></strong></p>
<p>Data can now be downloaded, for the database, from the Yahoo historical server.</p>
<p>On downloading some symbols will return a 404 error.</p>
<p>In some cases the errors can be repaired by &#8216;researching&#8217; via the Yahoo sites e.g. Hong Kong stocks use four digits and require leading zeros, which are not included in the symbols used by Standard and Poors.</p>
<p>In other cases there are multiple exchanges, supported by Yahoo, for a given country, so the ticker may need an alternative suffix (for this example the author assumed all tickers included in the Global 1200 were listed on the constituent countries major exchange).</p>
<p>&#160;</p>
<p>ATTACHED FILES:</p>
<p><a title="24-mar-2008_gbl1200.xls" href="http://www.amibroker.org/userkb/wp-content/uploads/2008/03/24-mar-2008_gbl1200.xls">24-mar-2008_gbl1200.xls</a>     <br /><a title="global1200.xls" href="http://www.amibroker.org/userkb/wp-content/uploads/2008/03/global1200.xls">global1200.xls</a></p>
<p><em><sub>Browse the spreadsheet files on line, or, right click and select </sub><sub>Save As </sub><sub>to download them as an editable version.</sub></em></p>
<p>REFERENCES:</p>
<p>1 - <a title="http://en.wikipedia.org/wiki/ASCII" href="http://en.wikipedia.org/wiki/ASCII">http://en.wikipedia.org/wiki/ASCII</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.amibroker.org/userkb/2008/03/25/ascii-import-standard-and-poors-global-1200/feed/</wfw:commentRss>
		</item>
		<item>
		<title>AmiBroker Supported Sites</title>
		<link>http://www.amibroker.org/userkb/2008/03/23/amibroker-supported-sites/</link>
		<comments>http://www.amibroker.org/userkb/2008/03/23/amibroker-supported-sites/#comments</comments>
		<pubDate>Sun, 23 Mar 2008 06:44:40 +0000</pubDate>
		<dc:creator>brian_z</dc:creator>
		
		<category><![CDATA[Blogroll]]></category>

		<guid isPermaLink="false">http://www.amibroker.org/userkb/2008/03/23/amibroker-supported-sites/</guid>
		<description><![CDATA[&#160;
This post contains a list of AmiBroker &#8217;supported&#8217; sites (most of the sites are officially supported by AmiBroker but some are not).
Search the main sites by using Google&#8217;s &#34;Advanced Search&#34; feature at http://www.google.com (search within a site, or domain, by using the URL address).

AmiBroker&#8217;s Homesite - http://www.amibroker.com/ 
AmiBrokers third party site - http://www.amibroker.org 

Alternatively visit [...]]]></description>
			<content:encoded><![CDATA[<p>&#160;</p>
<p>This post contains a list of <em>AmiBroker</em> &#8217;supported&#8217; sites (most of the sites are officially supported by <em>AmiBroker</em> but some are not).</p>
<p>Search the main sites by using <em>Google&#8217;s</em> &quot;<em>Advanced Search&quot;</em> feature at <a title="http://www.google.com" href="http://www.google.com">http://www.google.com</a> (search within a site, or domain, by using the URL address).</p>
<ul>
<li><em>AmiBroker&#8217;s</em> Homesite - <a title="http://www.amibroker.com/" href="http://www.amibroker.com/">http://www.amibroker.com/</a> </li>
<li><em>AmiBrokers</em> third party site - <a href="http://www.amibroker.org">http://www.amibroker.org</a> </li>
</ul>
<p>Alternatively visit any of the &#8217;supported&#8217; sites for categorized information on <em>AmiBroker</em> features, purchase, installation, testimonials, news, FAQ&#8217;s, downloads, documents, knowledge bases, tips, code and mailing lists.</p>
<p>For personal support please email - support [at] AmiBroker.com</p>
<p><strong>1) <u>AmiBroker&#8217;s Homesite</u></strong></p>
<p><strong><u></u></strong></p>
<p>This site is searchable using the &quot;<em>Search</em>&quot; box in the top left corner of the <strong>&quot;</strong><em>Welcome</em><strong>&quot;</strong> page.</p>
<ul>
<li>Welcome - <a title="http://www.amibroker.com/index.html" href="http://www.amibroker.com/index.html">http://www.amibroker.com/index.html</a> </li>
<li>Features - <a title="http://www.amibroker.com/features.html" href="http://www.amibroker.com/features.html">http://www.amibroker.com/features.html</a> </li>
<li>News - <a title="http://www.amibroker.com/news.html" href="http://www.amibroker.com/news.html">http://www.amibroker.com/news.html</a> </li>
<li>Download - <a title="http://www.amibroker.com/download.html" href="http://www.amibroker.com/download.html">http://www.amibroker.com/download.html</a> </li>
<li>Order - <a title="http://www.amibroker.com/order.php" href="http://www.amibroker.com/order.php">http://www.amibroker.com/order.php</a> </li>
<li>Support - <a title="http://www.amibroker.com/support.html" href="http://www.amibroker.com/support.html">http://www.amibroker.com/support.html</a> </li>
<li>FAQ - <a title="http://www.amibroker.com/faq.html" href="http://www.amibroker.com/faq.html">http://www.amibroker.com/faq.html</a> </li>
<li>Wishlist - <a title="http://www.amibroker.com/todo.html" href="http://www.amibroker.com/todo.html">http://www.amibroker.com/todo.html</a> </li>
<li>Links - <a title="http://www.amibroker.com/links.html" href="http://www.amibroker.com/links.html">http://www.amibroker.com/links.html</a> </li>
</ul>
<p>&#160;</p>
<p><strong><u>Links to specific pages at the AmiBroker Homesite</u></strong></p>
<p><strong></strong></p>
<p><strong>a) A downloadable PDF version of the <em>Users&#8217; Guide</em> (Help Manual).</strong></p>
<p>Note: the PDF version has superior search features compared to the other available versions.</p>
<p><a title="http://www.amibroker.com/" href="http://www.amibroker.com/bin/UsersGuide.pdf">http://www.amibroker.com/bin/UsersGuide.pdf</a></p>
<p><strong>b) An online version of the <em>Users&#8217; Guide</em> (Help Manual).</strong></p>
<p><a title="http://www.amibroker.com/guide/" href="http://www.amibroker.com/guide/">http://www.amibroker.com/guide/</a></p>
<p><strong>c) FAQ&#8217;s.</strong></p>
<p><a title="http://www.amibroker.com/faq.html" href="http://www.amibroker.com/faq.html">http://www.amibroker.com/faq.html</a></p>
<p><strong>d) AmiBroker Knowledge Base (KB).</strong></p>
<p>The &#8216;official&#8217; Knowledge Base site with tips &amp; usage hints from AmiBroker.com</p>
<p><a title="http://www.amibroker.com/kb/" href="http://www.amibroker.com/kb/">http://www.amibroker.com/kb/</a></p>
<p><strong>e) AmiBroker Tips Newsletter.</strong></p>
<p><a title="http://www.amibroker.com/newsletter/" href="http://www.amibroker.com/newsletter/">http://www.amibroker.com/newsletter/</a></p>
<p>NOTE: The Newsletter has been now replaced by the <em>AmiBroker Knowledge Base</em> at:<b>&#160;<a href="http://www.amibroker.com/kb/">http://www.amibroker.com/kb/</a></b></p>
<p><strong>f) AmiBroker support page.</strong></p>
<p>The support page features additional documentation and videos.</p>
<p><strong><a title="http://www.amibroker.com/support.html" href="http://www.amibroker.com/support.html">http://www.amibroker.com/support.html</a></strong></p>
<p><strong>g) AFL Library.</strong></p>
<p>The AFL on-line library provides a very convenient way to share formulas, including descriptions and comments, between AmiBroker users. You will not only&#160; be able to get the formula you need here but also contribute your own ideas.</p>
<p><a title="http://www.amibroker.com/library/" href="http://www.amibroker.com/library/">http://www.amibroker.com/library/</a></p>
<p><strong>h) Members Area.</strong></p>
<p>This provides additional material for registered <em>AmiBroker</em> users, including the newest issues of <em>Stocks &amp; Commodities Traders</em> tips for <em>AmiBroker</em> </p>
<p><a title="http://www.amibroker.com/login.html" href="http://www.amibroker.com/login.html">http://www.amibroker.com/login.html</a></p>
<p><strong>i) The online version of the <em>AFL Function Reference</em> (searchable).</strong></p>
<p><a title="http://www.amibroker.com/guide/afl/" href="http://www.amibroker.com/guide/afl/">http://www.amibroker.com/guide/afl/</a></p>
<p><strong>j) AmiBroker Development Log.</strong></p>
<p><a title="http://www.amibroker.com/devlog/" href="http://www.amibroker.com/devlog/">http://www.amibroker.com/devlog/</a></p>
<p><strong>k) Feedback Centre.</strong></p>
<p>You can submit your ideas and suggestions and see the list of features planned for the next few releases (registered users only).</p>
<p><a title="http://www.amibroker.com/todo.html" href="http://www.amibroker.com/todo.html">http://www.amibroker.com/todo.html</a></p>
<p>&#160;</p>
<p><strong>2) <u>AmiBroker User&#8217;s Knowledge Base (UKB)</u></strong></p>
<p><strong><u></u></strong></p>
<p>This site is owned and operated by <em>AmiBroker</em> but its contents are entirely contributed by users.</p>
<p>It was was created to save you time, raise your productivity, aid you in your <em>Amibroker Formula Language</em> (AFL) learning experience, and help you become a more successful trader.</p>
<p><a title="http://www.amibroker.org/userkb/" href="http://www.amibroker.org/userkb/">http://www.amibroker.org/userkb/</a></p>
<p>UKB features:</p>
<p><em>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <font color="#0080ff"></font></em><font color="#0080ff"><a href="http://www.amibroker.org/userkb/?cat=135">Automated Trading, by Herman van den Bergen</a></font></p>
<p><font color="#0080ff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font><a href="http://www.amibroker.org/userkb/?cat=115">Intelligent Optimization, by Fred Tonetti.</a></p>
<p>&#160;</p>
<p><strong>3) <u>AmiBroker third party site</u></strong></p>
<p>An unstructured FTP site, sponsored by AmiBroker.com, to facilitate&#160; file sharing between Amibroker users.</p>
<p><a title="http://www.amibroker.org/3rdparty/" href="http://www.amibroker.org/3rdparty/">http://www.amibroker.org/3rdparty/</a></p>
<p>&#160;</p>
<p><strong>4) <u>The AmiBroker mailing list</u></strong></p>
<p><strong><u></u></strong></p>
<p>The list is provided to help <em>Amibroker</em> users to share ideas, tips and other related information. This is the place where you can meet other <em>AmiBroker</em> users, ask questions and share ideas (searchable archive and file uploading).</p>
<p><a title="http://finance.groups.yahoo.com/group/amibroker/" href="http://finance.groups.yahoo.com/group/amibroker/">http://finance.groups.yahoo.com/group/amibroker/</a></p>
<p>&#160;</p>
<p><strong>5) <u>The AmiBroker Trading Systems mailing list.</u></strong></p>
<p><strong><u></u></strong></p>
<p>This group is for discussion on developing and testing trading systems, using <em>AmiBroker</em> (searchable archive and file uploading).</p>
<p><a title="http://finance.groups.yahoo.com/group/amibroker-ts/" href="http://finance.groups.yahoo.com/group/amibroker-ts/">http://finance.groups.yahoo.com/group/amibroker-ts/</a></p>
<p>&#160;</p>
<p><strong>6)</strong> <u><strong>The AmiBroker Automated Trading mailing list.</strong></u></p>
<p><strong><u></u></strong></p>
<p>This group is for discussion on Automated Trading, using <em>AmiBroker</em> (searchable archive and file uploading).</p>
<p><a title="http://finance.groups.yahoo.com/group/AmiBroker-at/" href="http://finance.groups.yahoo.com/group/AmiBroker-at/">http://finance.groups.yahoo.com/group/AmiBroker-at/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.amibroker.org/userkb/2008/03/23/amibroker-supported-sites/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Using a GFX Include file</title>
		<link>http://www.amibroker.org/userkb/2008/03/21/using-a-gfx-include-file/</link>
		<comments>http://www.amibroker.org/userkb/2008/03/21/using-a-gfx-include-file/#comments</comments>
		<pubDate>Fri, 21 Mar 2008 13:07:03 +0000</pubDate>
		<dc:creator>Herman</dc:creator>
		
		<category><![CDATA[GFX Programming]]></category>

		<guid isPermaLink="false">http://www.amibroker.org/userkb/2008/03/21/using-a-gfx-include-file/</guid>
		<description><![CDATA[Important note: The AmiBroker 5.09.0 Beta introduced the following new GFX functions:
Status(&#8221;pxchartleft&#8221;) - returns x-coordinate of top-left corner of chart area
Status(&#8221;pxcharttop&#8221;) - returns y-coordinate of top-left corner of chart area
Status(&#8221;pxchartright&#8221;) - returns x-coordinate of bottom-right corner of chart area
Status(&#8221;pxchartbottom&#8221;) - returns y-coordinate of bottom-right corner of chart area
Status(&#8221;pxchartwidth&#8221;) - returns width chart area (right-left)
Status(&#8221;pxchartheight&#8221;) - [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Important note</strong>: The <a href="http://www.amibroker.com/members/bin/ab5090beta.exe">AmiBroker 5.09.0 Beta</a> introduced the following new GFX functions:</p>
<p>Status(&#8221;pxchartleft&#8221;) - returns x-coordinate of top-left corner of chart area<br />
Status(&#8221;pxcharttop&#8221;) - returns y-coordinate of top-left corner of chart area<br />
Status(&#8221;pxchartright&#8221;) - returns x-coordinate of bottom-right corner of chart area<br />
Status(&#8221;pxchartbottom&#8221;) - returns y-coordinate of bottom-right corner of chart area<br />
Status(&#8221;pxchartwidth&#8221;) - returns width chart area (right-left)<br />
Status(&#8221;pxchartheight&#8221;) - returns width chart area (bottom-top)</p>
<p>Since this release appeared after this post was published these functions are not used in the examples below. This post has been left unchanged for educational purposes. For examples using the new functions please see the <a href="http://www.amibroker.com/devlog/wp-content/uploads/2008/04/readme5090.html">5.09.0 Read Me</a> file.</p>
<p align="center"> =====</p>
<p>While the post <a href="http://www.amibroker.org/userkb/2008/03/20/creating-gfx-chart-overlays/">Creating GFX Chart-Overlays (v2)</a> may have clarified a few of the more important aspects of using GFX functions, it doesn’t really give you a “quick Start” template to get started. Using a GFXInclude file can remove some of the burden of having to define pixel and charting parameters. The Include file at the bottom of this post contains most definitions as well as these common functions that you may want to call from your GFX application:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="afl">GetVisualBarIndex<span style="color: #003f00;">&#40;</span> <span style="color: #003f00;">&#41;</span>; <span style="color: #007f00; ">// Returns array containing index for visible bars</span>
gfxPlotHLine<span style="color: #003f00;">&#40;</span> YPixels, Color <span style="color: #003f00;">&#41;</span>; <span style="color: #007f00; ">// Plots horizontal line at level YPixels</span>
gfxPlotVLine<span style="color: #003f00;">&#40;</span> XPixels, Color <span style="color: #003f00;">&#41;</span>; <span style="color: #007f00; ">// plots vertical line at level XPixels</span>
GetYPixels<span style="color: #003f00;">&#40;</span> Y <span style="color: #003f00;">&#41;</span>; <span style="color: #007f00; ">// Convert a vertical price number to the pixel equivalent</span>
GetXPixels<span style="color: #003f00;">&#40;</span> X <span style="color: #003f00;">&#41;</span>; <span style="color: #007f00; ">// Convert a horizontal DateTime number value to the pixel equivalent</span></pre></td></tr></table></div>

<p>Of course you can, and should, add additional functions of your own. Here is an example of how to call the above functions to draw a GFX cross-hair cursor (Red in the capture):</p>
<p align="center">
<a href='http://www.amibroker.org/userkb/wp-content/uploads/2008/03/gfxcrosshair.jpg' title='gfxcrosshair.jpg'><img src='http://www.amibroker.org/userkb/wp-content/uploads/2008/03/gfxcrosshair.jpg' alt='gfxcrosshair.jpg' /></a></p>
<p>Here is the code that produced the above image:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="afl">GraphXSpace = <span style="color: #7f0000;">5</span>; <span style="color: #007f00; ">// See the AmiBroker help on how to init these variables</span>
GfxSetBkMode<span style="color: #003f00;">&#40;</span> bkmode = <span style="color: #7f0000;">2</span> <span style="color: #003f00;">&#41;</span>;
GfxSetOverlayMode<span style="color: #003f00;">&#40;</span> mode = <span style="color: #7f0000;">0</span> <span style="color: #003f00;">&#41;</span>;
GfxSelectPen<span style="color: #003f00;">&#40;</span> colorRed <span style="color: #003f00;">&#41;</span>;
&nbsp;
<span style="color: #0000ff;">Plot</span><span style="color: #003f00;">&#40;</span> C, <span style="color: #7f007f;">&quot;&quot;</span>, colorBlack, styleLine <span style="color: #003f00;">&#41;</span>; <span style="color: #007f00; ">// To define miny and maxy</span>
<span style="color: #007f00;">#include &lt;GFXInclude-001.afl&gt; // Located in your default Include folder</span>
&nbsp;
<span style="color: #007f00; ">// Example to draw cross-hair cursor</span>
Yprice = GetCursorYPosition<span style="color: #003f00;">&#40;</span><span style="color: #7f0000;">0</span><span style="color: #003f00;">&#41;</span>;
XIndex = SelectedValue<span style="color: #003f00;">&#40;</span>GetVisualBarIndex<span style="color: #003f00;">&#40;</span> <span style="color: #003f00;">&#41;</span><span style="color: #003f00;">&#41;</span>;
gfxPlotHLine<span style="color: #003f00;">&#40;</span> GetYPixels<span style="color: #003f00;">&#40;</span> YPrice <span style="color: #003f00;">&#41;</span>, colorRed <span style="color: #003f00;">&#41;</span>;
gfxPlotVLine<span style="color: #003f00;">&#40;</span> GetXPixels<span style="color: #003f00;">&#40;</span> XIndex <span style="color: #003f00;">&#41;</span>, colorRed <span style="color: #003f00;">&#41;</span>;</pre></td></tr></table></div>

<p>The include file listed below defines the following variables:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="afl"><span style="color: #007f00; ">// pxwidth, pxheight, Miny, MinX, YRange, VisBarIndex, NumBarsVisible, pxPaneWidth, pxPaneheight, PixelsPerBar, PixelsPerPrice</span></pre></td></tr></table></div>

<p>You may want to copy the above comment line below the #include statement in your code to refresh your memory. You should copy the Include file to your default AmiBroker Include folder.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
</pre></td><td class="code"><pre class="afl"><span style="color: #007f00; ">// GFXInclude-001.afl copy to default include folder</span>
&nbsp;
<span style="color: #7f0000; ">function</span> gfxPlotHLine<span style="color: #003f00;">&#40;</span> YPixels, Color <span style="color: #003f00;">&#41;</span>
<span style="color: #003f00;">&#123;</span>
    <span style="color: #7f0000;">global</span> pxwidth;
    GfxSelectPen<span style="color: #003f00;">&#40;</span> Color <span style="color: #003f00;">&#41;</span> ;
    GfxMoveTo<span style="color: #003f00;">&#40;</span> <span style="color: #7f0000;">0</span>, YPixels <span style="color: #003f00;">&#41;</span>;
    GfxLineTo<span style="color: #003f00;">&#40;</span> pxwidth, YPixels <span style="color: #003f00;">&#41;</span>;
<span style="color: #003f00;">&#125;</span>
&nbsp;
<span style="color: #7f0000; ">function</span> gfxPlotVLine<span style="color: #003f00;">&#40;</span> XPixels, Color <span style="color: #003f00;">&#41;</span>
<span style="color: #003f00;">&#123;</span>
    <span style="color: #7f0000;">global</span> pxheight;
    GfxSelectPen<span style="color: #003f00;">&#40;</span> Color <span style="color: #003f00;">&#41;</span> ;
    GfxMoveTo<span style="color: #003f00;">&#40;</span> XPixels, <span style="color: #7f0000;">0</span> <span style="color: #003f00;">&#41;</span>;
    GfxLineTo<span style="color: #003f00;">&#40;</span> XPixels, pxheight <span style="color: #003f00;">&#41;</span>;
<span style="color: #003f00;">&#125;</span>
&nbsp;
<span style="color: #7f0000; ">function</span> GetVisualBarIndex<span style="color: #003f00;">&#40;</span> <span style="color: #003f00;">&#41;</span>
<span style="color: #003f00;">&#123;</span>
    lvb = <span style="color: #0000ff;">Status</span><span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;lastvisiblebar&quot;</span> <span style="color: #003f00;">&#41;</span>;
    fvb = <span style="color: #0000ff;">Status</span><span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;firstvisiblebar&quot;</span> <span style="color: #003f00;">&#41;</span>;
    bi = BarIndex<span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</span>;
    StaticVarSet<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;NumberbarsVisible&quot;</span>, Lvb - fvb + <span style="color: #7f0000;">1</span> <span style="color: #003f00;">&#41;</span>;
    <span style="color: #7f0000; ">return</span> bi - bi<span style="color: #003f00;">&#91;</span> <span style="color: #7f0000;">0</span> <span style="color: #003f00;">&#93;</span> - fvb;
<span style="color: #003f00;">&#125;</span>
&nbsp;
<span style="color: #7f0000; ">function</span> GetYPixels<span style="color: #003f00;">&#40;</span> Y <span style="color: #003f00;">&#41;</span>
	<span style="color: #003f00;">&#123;</span>
	<span style="color: #7f0000;">global</span> PixelsPerPrice, pxTopArea, MaxY; 
	<span style="color: #7f0000; ">return</span> <span style="color: #003f00;">&#40;</span>MaxY - Y<span style="color: #003f00;">&#41;</span> * PixelsPerPrice + pxTopArea;
	<span style="color: #003f00;">&#125;</span>
&nbsp;
<span style="color: #7f0000; ">function</span> GetXPixels<span style="color: #003f00;">&#40;</span> X <span style="color: #003f00;">&#41;</span>
	<span style="color: #003f00;">&#123;</span>
	<span style="color: #7f0000;">global</span> PixelsPerBar, pxLeftArea;
	<span style="color: #7f0000; ">return</span> X * PixelsPerBar + pxLeftArea;
	<span style="color: #003f00;">&#125;</span>
&nbsp;
_SECTION_BEGIN<span style="color: #003f00;">&#40;</span><span style="color: #7f007f;">&quot;GFX INITIALIZATION&quot;</span><span style="color: #003f00;">&#41;</span>;
<span style="color: #007f00; ">// These Parameters will change depending on screen layout/fonts</span>
pxRightArea = Param<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Right Axis Area&quot;</span>, <span style="color: #7f0000;">93</span>, <span style="color: #7f0000;">0</span>, <span style="color: #7f0000;">200</span>, <span style="color: #7f0000;">1</span> <span style="color: #003f00;">&#41;</span>; <span style="color: #007f00; ">// Depends on font</span>
pxDateArea = Param<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Date Axis Area&quot;</span>, <span style="color: #7f0000;">11</span>, <span style="color: #7f0000;">0</span>, <span style="color: #7f0000;">100</span>, <span style="color: #7f0000;">1</span> <span style="color: #003f00;">&#41;</span>; <span style="color: #007f00; ">// Depends on font</span>
DateaxisOn = ParamToggle<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Date Axis&quot;</span>, <span style="color: #7f007f;">&quot;HIDE|SHOW&quot;</span>, <span style="color: #7f0000;">1</span> <span style="color: #003f00;">&#41;</span>;
&nbsp;
pxLeftArea = <span style="color: #7f0000;">5</span>; 
pxTopArea = <span style="color: #7f0000;">5</span>; 
pxBottomArea = <span style="color: #7f0000;">5</span>; 
<span style="color: #7f0000; ">if</span> <span style="color: #003f00;">&#40;</span> DateaxisOn <span style="color: #003f00;">&#41;</span>
<span style="color: #003f00;">&#123;</span>
    pxBottomArea = pxDateArea + pxBottomArea;
    SetChartOptions<span style="color: #003f00;">&#40;</span> <span style="color: #7f0000;">2</span>, chartShowDates <span style="color: #003f00;">&#41;</span>;
<span style="color: #003f00;">&#125;</span>
<span style="color: #7f0000; ">else</span>
    SetChartOptions<span style="color: #003f00;">&#40;</span> <span style="color: #7f0000;">3</span>, chartShowDates <span style="color: #003f00;">&#41;</span>;
&nbsp;
pxwidth = <span style="color: #0000ff;">Status</span><span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;pxwidth&quot;</span> <span style="color: #003f00;">&#41;</span>;
pxheight = <span style="color: #0000ff;">Status</span><span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;pxheight&quot;</span> <span style="color: #003f00;">&#41;</span>;
&nbsp;
<span style="color: #007f00; ">// clalculate charting area width and height</span>
Miny = <span style="color: #0000ff;">Status</span><span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;axisminy&quot;</span> <span style="color: #003f00;">&#41;</span>;
Maxy = <span style="color: #0000ff;">Status</span><span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;axismaxy&quot;</span> <span style="color: #003f00;">&#41;</span>;
YRange = MaxY - MinY;
VisBarIndex =  GetVisualBarIndex<span style="color: #003f00;">&#40;</span> <span style="color: #003f00;">&#41;</span>;
NumBarsVisible = StaticVarGet<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;NumberbarsVisible&quot;</span> <span style="color: #003f00;">&#41;</span>;
&nbsp;
<span style="color: #007f00; ">// Calculate Pane width and height</span>
pxPaneWidth = pxwidth - pxLeftArea - pxRightArea;
pxPaneHeight = pxHeight - pxTopArea - pxBottomArea;
&nbsp;
<span style="color: #007f00; ">// calculate conversion factors</span>
PixelsPerBar 	= pxPaneWidth / NumBarsVisible;
PixelsPerPrice = pxPaneHeight / YRange;
_SECTION_END<span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</span>;</pre></td></tr></table></div>

<p>Edited by Al Venosa.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.amibroker.org/userkb/2008/03/21/using-a-gfx-include-file/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Creating GFX Chart-Overlays (v3)</title>
		<link>http://www.amibroker.org/userkb/2008/03/20/creating-gfx-chart-overlays/</link>
		<comments>http://www.amibroker.org/userkb/2008/03/20/creating-gfx-chart-overlays/#comments</comments>
		<pubDate>Thu, 20 Mar 2008 20:10:00 +0000</pubDate>
		<dc:creator>Herman</dc:creator>
		
		<category><![CDATA[GFX Programming]]></category>

		<guid isPermaLink="false">http://www.amibroker.org/userkb/2008/03/20/creating-gfx-chart-overlays/</guid>
		<description><![CDATA[Important note: The AmiBroker 5.09.0 Beta introduced the following new GFX functions:
Status(&#8221;pxchartleft&#8221;) - returns x-coordinate of top-left corner of chart area
Status(&#8221;pxcharttop&#8221;) - returns y-coordinate of top-left corner of chart area
Status(&#8221;pxchartright&#8221;) - returns x-coordinate of bottom-right corner of chart area
Status(&#8221;pxchartbottom&#8221;) - returns y-coordinate of bottom-right corner of chart area
Status(&#8221;pxchartwidth&#8221;) - returns width chart area (right-left)
Status(&#8221;pxchartheight&#8221;) - [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Important note</strong>: The <a href="http://www.amibroker.com/members/bin/ab5090beta.exe">AmiBroker 5.09.0 Beta</a> introduced the following new GFX functions:</p>
<p>Status(&#8221;pxchartleft&#8221;) - returns x-coordinate of top-left corner of chart area<br />
Status(&#8221;pxcharttop&#8221;) - returns y-coordinate of top-left corner of chart area<br />
Status(&#8221;pxchartright&#8221;) - returns x-coordinate of bottom-right corner of chart area<br />
Status(&#8221;pxchartbottom&#8221;) - returns y-coordinate of bottom-right corner of chart area<br />
Status(&#8221;pxchartwidth&#8221;) - returns width chart area (right-left)<br />
Status(&#8221;pxchartheight&#8221;) - returns width chart area (bottom-top)</p>
<p>Since this release appeared after this post was published these functions are not used in the examples below. This post has been left unchanged for educational purposes. For examples using the new functions please see the <a href="http://www.amibroker.com/devlog/wp-content/uploads/2008/04/readme5090.html">5.09.0 Read Me</a> file.</p>
<p align="center"> =====</p>
<p>Creating an exact chart overlay using GFX functions can be a daunting task for the non-professional programmer. The solutions presented here were derived through experimentation; if there is a better way, please make a comment. Once the pixel layout is fully understood, GFX becomes an extremely powerful tool and may just give you an additional trading edge. The first and most important step in using GFX functions is to understand how pixels make up your display. In AmiBroker the width and height of your charting pane can be retrieved using the following two functions:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="afl">pxwidth = <span style="color: #0000ff;">Status</span><span style="color: #003f00;">&#40;</span><span style="color: #7f007f;">&quot;pxwidth&quot;</span><span style="color: #003f00;">&#41;</span>;
pxheight = <span style="color: #0000ff;">Status</span><span style="color: #003f00;">&#40;</span><span style="color: #7f007f;">&quot;pxheight&quot;</span><span style="color: #003f00;">&#41;</span>;</pre></td></tr></table></div>

<p>Horizontal pixels count left to right, 1 to pxwidth; vertical pixels count top to bottom, 1 to pxheight. The area covered by these two numbers is shown in Yellow below. For a high resolution monitor this area may cover about 2000 (H) x 1000 (V) pixels. This pixel area includes the areas used by the X and Y axis, and the blank top and bottom margins.</p>
<p align="center">
<a href='http://www.amibroker.org/userkb/wp-content/uploads/2008/03/pixelarea.png' title='pixelarea.png'><img src='http://www.amibroker.org/userkb/wp-content/uploads/2008/03/pixelarea.png' alt='pixelarea.png' /></a></p>
<p>Next is the standard charting area, which is the area where your price charts are located. This area excludes the surrounding blank margins and the areas used for axis labeling. If you want to keep your overlay within the standard chart boundaries, you have to place your images within the above boundaries. This area is highlighted in Blue in the image below:</p>
<p align="center">
<a href='http://www.amibroker.org/userkb/wp-content/uploads/2008/03/chartingarea.png' title='chartingarea.png'><img src='http://www.amibroker.org/userkb/wp-content/uploads/2008/03/chartingarea.png' alt='chartingarea.png' /></a></p>
<p>The boundaries for this area can be determined by running the example code listed in <a href="http://www.amibroker.org/userkb/2008/03/14/finding-pixel-boundaries/">Finding Pixel Boundaries</a>. Eight parameters must be known to create pixel overlays:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="afl">pxwidth = <span style="color: #0000ff;">Status</span><span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;pxwidth&quot;</span> <span style="color: #003f00;">&#41;</span>;
pxheight = <span style="color: #0000ff;">Status</span><span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;pxheight&quot;</span> <span style="color: #003f00;">&#41;</span>;
pxLeftArea = Param<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Left Blank Margin&quot;</span>, <span style="color: #7f0000;">5</span>, <span style="color: #7f0000;">0</span>, <span style="color: #7f0000;">100</span>, <span style="color: #7f0000;">1</span> <span style="color: #003f00;">&#41;</span>; <span style="color: #007f00; ">// Constant</span>
pxRightArea = Param<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Right Axis Area&quot;</span>, <span style="color: #7f0000;">93</span>, <span style="color: #7f0000;">0</span>, <span style="color: #7f0000;">200</span>, <span style="color: #7f0000;">1</span> <span style="color: #003f00;">&#41;</span>; <span style="color: #007f00; ">// Depends on font</span>
pxTopArea = Param<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Top Blank Margin&quot;</span>, <span style="color: #7f0000;">5</span>, <span style="color: #7f0000;">0</span>, <span style="color: #7f0000;">100</span>, <span style="color: #7f0000;">1</span> <span style="color: #003f00;">&#41;</span>; <span style="color: #007f00; ">// Constant</span>
pxDateArea = Param<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Date Axis Area&quot;</span>, <span style="color: #7f0000;">11</span>, <span style="color: #7f0000;">0</span>, <span style="color: #7f0000;">100</span>, <span style="color: #7f0000;">1</span> <span style="color: #003f00;">&#41;</span>; <span style="color: #007f00; ">// Depends on font</span>
pxBottomArea = Param<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Bottom Blank Margin&quot;</span>, <span style="color: #7f0000;">5</span>, <span style="color: #7f0000;">0</span>, <span style="color: #7f0000;">100</span>, <span style="color: #7f0000;">1</span> <span style="color: #003f00;">&#41;</span>; <span style="color: #007f00; ">// Constant</span>
DateaxisOn = ParamToggle<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Date Axis&quot;</span>, <span style="color: #7f007f;">&quot;HIDE|SHOW&quot;</span>, <span style="color: #7f0000;">1</span> <span style="color: #003f00;">&#41;</span>;</pre></td></tr></table></div>

<p>Since adding date labels to your DateTime axis changes the size of your pixel plotting area, you need to compensate for this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="afl"><span style="color: #7f0000; ">if</span><span style="color: #003f00;">&#40;</span> DateaxisOn <span style="color: #003f00;">&#41;</span> 
	<span style="color: #003f00;">&#123;</span>
	pxBottomArea = pxDateArea + pxBottomArea;
	SetChartOptions<span style="color: #003f00;">&#40;</span><span style="color: #7f0000;">2</span>,chartShowDates<span style="color: #003f00;">&#41;</span>;
	<span style="color: #003f00;">&#125;</span>
<span style="color: #7f0000; ">else</span> 
	<span style="color: #003f00;">&#123;</span>
	SetChartOptions<span style="color: #003f00;">&#40;</span><span style="color: #7f0000;">3</span>,chartShowDates<span style="color: #003f00;">&#41;</span>;
	<span style="color: #003f00;">&#125;</span></pre></td></tr></table></div>

<p>The pixel plotting area&#8217;s width and height can now be calculated:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="afl">pxPaneWidth = pxwidth - pxLeftArea- pxRightArea;
pxPaneHeight = pxHeight - pxTopArea- pxBottomArea;</pre></td></tr></table></div>

<p>The dimension of the Blue area shown earlier changes when you resize AmiBroker, open additional windows or panes, change the fonts in your axis, or turn On/Off date labels. When this happens you will have to recalibrate the boundaries. To convert prices to pixels, so that you can create an exact overlay, you also need to define the width and height of your regular chart pane. These are expressed in DateTime and Price units. They will change when you zoom your chart. When you have at least one price plot displayed, so that the values for miny and maxy are defined, you can calculate these boundaries as follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="afl">Miny = <span style="color: #0000ff;">Status</span><span style="color: #003f00;">&#40;</span><span style="color: #7f007f;">&quot;axisminy&quot;</span><span style="color: #003f00;">&#41;</span>;
Maxy = <span style="color: #0000ff;">Status</span><span style="color: #003f00;">&#40;</span><span style="color: #7f007f;">&quot;axismaxy&quot;</span><span style="color: #003f00;">&#41;</span>;
YRange = MaxY - MinY
BarsVisible = <span style="color: #0000ff;">Status</span><span style="color: #003f00;">&#40;</span><span style="color: #7f007f;">&quot;BarVisible&quot;</span><span style="color: #003f00;">&#41;</span>;
NumBarsVisible = Cum<span style="color: #003f00;">&#40;</span>BarsVisible<span style="color: #003f00;">&#41;</span>;</pre></td></tr></table></div>

<p>You now have all the information needed to calculate the Pixels/Price and Pixels/Bar conversion factors:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="afl">PixelsPerBar 	= pxPaneWidth / NumBarsVisible;
PixelsPerPrice = pxPaneHeight / YRange;</pre></td></tr></table></div>

<p>Putting it all together in a demo program (listed at the end of this post) produces the price-chart overlay shown below. The regular price plot is plotted using dots, so that the overlay is clearly visible. When you plot both traces in lines, you will see minor deviations that are probably due to rounding to the nearest pixel. The pixel price plot is shown in Red. The purpose of this exercise is to learn to work with pixels and be able to produce an exact overlay on the price chart. The Param window below the charts shows typical parameters; they will likely be different for your screen layout.</p>
<p align="center">
<a href='http://www.amibroker.org/userkb/wp-content/uploads/2008/03/pricechartoverlay1.png' title='pricechartoverlay1.png'><img src='http://www.amibroker.org/userkb/wp-content/uploads/2008/03/pricechartoverlay1.png' alt='pricechartoverlay1.png' /></a></p>
<p align="center">
<a href='http://www.amibroker.org/userkb/wp-content/uploads/2008/03/pricechartoverlayparam.png' title='pricechartoverlayparam.png'><img src='http://www.amibroker.org/userkb/wp-content/uploads/2008/03/pricechartoverlayparam.png' alt='pricechartoverlayparam.png' /></a></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
</pre></td><td class="code"><pre class="afl"><span style="color: #7f0000; ">function</span> gfxPlotHLine<span style="color: #003f00;">&#40;</span> YPixels, Color <span style="color: #003f00;">&#41;</span>
<span style="color: #003f00;">&#123;</span>
    <span style="color: #7f0000;">global</span> pxwidth;
    GfxSelectPen<span style="color: #003f00;">&#40;</span> Color <span style="color: #003f00;">&#41;</span> ;
    GfxMoveTo<span style="color: #003f00;">&#40;</span> <span style="color: #7f0000;">0</span>, YPixels <span style="color: #003f00;">&#41;</span>;
    GfxLineTo<span style="color: #003f00;">&#40;</span> pxwidth, YPixels <span style="color: #003f00;">&#41;</span>;
<span style="color: #003f00;">&#125;</span>
&nbsp;
<span style="color: #7f0000; ">function</span> gfxPlotVLine<span style="color: #003f00;">&#40;</span> XPixels, Color <span style="color: #003f00;">&#41;</span>
<span style="color: #003f00;">&#123;</span>
    <span style="color: #7f0000;">global</span> pxheight;
    GfxSelectPen<span style="color: #003f00;">&#40;</span> Color <span style="color: #003f00;">&#41;</span> ;
    GfxMoveTo<span style="color: #003f00;">&#40;</span> XPixels, <span style="color: #7f0000;">0</span> <span style="color: #003f00;">&#41;</span>;
    GfxLineTo<span style="color: #003f00;">&#40;</span> XPixels, pxheight <span style="color: #003f00;">&#41;</span>;
<span style="color: #003f00;">&#125;</span>
&nbsp;
<span style="color: #7f0000; ">function</span> GetVisualBarIndex<span style="color: #003f00;">&#40;</span> <span style="color: #003f00;">&#41;</span>
<span style="color: #003f00;">&#123;</span>
    lvb = <span style="color: #0000ff;">Status</span><span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;lastvisiblebar&quot;</span> <span style="color: #003f00;">&#41;</span>;
    fvb = <span style="color: #0000ff;">Status</span><span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;firstvisiblebar&quot;</span> <span style="color: #003f00;">&#41;</span>;
    bi = BarIndex<span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</span>;
    StaticVarSet<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;NumberbarsVisible&quot;</span>, Lvb - fvb + <span style="color: #7f0000;">1</span> <span style="color: #003f00;">&#41;</span>;
    <span style="color: #7f0000; ">return</span> bi - bi<span style="color: #003f00;">&#91;</span> <span style="color: #7f0000;">0</span> <span style="color: #003f00;">&#93;</span> - fvb;
<span style="color: #003f00;">&#125;</span>
&nbsp;
<span style="color: #7f0000; ">function</span> GetYPixels<span style="color: #003f00;">&#40;</span> Y <span style="color: #003f00;">&#41;</span>
	<span style="color: #003f00;">&#123;</span>
	<span style="color: #7f0000;">global</span> PixelsPerPrice, pxTopArea, MaxY; 
	<span style="color: #7f0000; ">return</span> <span style="color: #003f00;">&#40;</span>MaxY - Y<span style="color: #003f00;">&#41;</span> * PixelsPerPrice + pxTopArea;
	<span style="color: #003f00;">&#125;</span>
&nbsp;
<span style="color: #7f0000; ">function</span> GetXPixels<span style="color: #003f00;">&#40;</span> X <span style="color: #003f00;">&#41;</span>
	<span style="color: #003f00;">&#123;</span>
	<span style="color: #7f0000;">global</span> PixelsPerBar, pxLeftArea;
	<span style="color: #7f0000; ">return</span> X * PixelsPerBar + pxLeftArea;
	<span style="color: #003f00;">&#125;</span>
&nbsp;
GraphXSpace = <span style="color: #7f0000;">5</span>;
SetChartOptions<span style="color: #003f00;">&#40;</span> <span style="color: #7f0000;">0</span>, chartHideQuoteMarker <span style="color: #003f00;">&#41;</span>;
&nbsp;
pxwidth = <span style="color: #0000ff;">Status</span><span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;pxwidth&quot;</span> <span style="color: #003f00;">&#41;</span>;
pxheight = <span style="color: #0000ff;">Status</span><span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;pxheight&quot;</span> <span style="color: #003f00;">&#41;</span>;
&nbsp;
<span style="color: #007f00; ">// These Parameters will change depending on screen layout/fonts</span>
pxRightArea = Param<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Right Axis Area&quot;</span>, <span style="color: #7f0000;">93</span>, <span style="color: #7f0000;">0</span>, <span style="color: #7f0000;">200</span>, <span style="color: #7f0000;">1</span> <span style="color: #003f00;">&#41;</span>; <span style="color: #007f00; ">// Depends on font</span>
pxDateArea = Param<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Date Axis Area&quot;</span>, <span style="color: #7f0000;">11</span>, <span style="color: #7f0000;">0</span>, <span style="color: #7f0000;">100</span>, <span style="color: #7f0000;">1</span> <span style="color: #003f00;">&#41;</span>; <span style="color: #007f00; ">// Depends on font</span>
DateaxisOn = ParamToggle<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Date Axis&quot;</span>, <span style="color: #7f007f;">&quot;HIDE|SHOW&quot;</span>, <span style="color: #7f0000;">1</span> <span style="color: #003f00;">&#41;</span>;
&nbsp;
<span style="color: #007f00; ">// These Parameters appear constant and can probably be hardcoded</span>
pxLeftArea = Param<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Left Blank Margin&quot;</span>, <span style="color: #7f0000;">5</span>, <span style="color: #7f0000;">0</span>, <span style="color: #7f0000;">100</span>, <span style="color: #7f0000;">1</span> <span style="color: #003f00;">&#41;</span>; <span style="color: #007f00; ">// Constant</span>
pxTopArea = Param<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Top Blank Margin&quot;</span>, <span style="color: #7f0000;">5</span>, <span style="color: #7f0000;">0</span>, <span style="color: #7f0000;">100</span>, <span style="color: #7f0000;">1</span> <span style="color: #003f00;">&#41;</span>; <span style="color: #007f00; ">// Constant</span>
pxBottomArea = Param<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Bottom Blank Margin&quot;</span>, <span style="color: #7f0000;">5</span>, <span style="color: #7f0000;">0</span>, <span style="color: #7f0000;">100</span>, <span style="color: #7f0000;">1</span> <span style="color: #003f00;">&#41;</span>; <span style="color: #007f00; ">// Constant</span>
&nbsp;
<span style="color: #7f0000; ">if</span> <span style="color: #003f00;">&#40;</span> DateaxisOn <span style="color: #003f00;">&#41;</span> <span style="color: #007f00; ">// Size of bottom boundary depends on whether dates are shown</span>
<span style="color: #003f00;">&#123;</span>
    pxBottomArea = pxDateArea + pxBottomArea;
    SetChartOptions<span style="color: #003f00;">&#40;</span> <span style="color: #7f0000;">2</span>, chartShowDates <span style="color: #003f00;">&#41;</span>;
<span style="color: #003f00;">&#125;</span>
<span style="color: #7f0000; ">else</span>
    SetChartOptions<span style="color: #003f00;">&#40;</span> <span style="color: #7f0000;">3</span>, chartShowDates <span style="color: #003f00;">&#41;</span>;
&nbsp;
<span style="color: #007f00; ">// Test Plots to help line up boundary lines</span>
Color = colorWhite;
gfxPlotVLine<span style="color: #003f00;">&#40;</span> pxLeftArea, color <span style="color: #003f00;">&#41;</span>;
gfxPlotVLine<span style="color: #003f00;">&#40;</span> pxwidth - pxRightArea, color <span style="color: #003f00;">&#41;</span>;
gfxPlotHLine<span style="color: #003f00;">&#40;</span> pxTopArea, color <span style="color: #003f00;">&#41;</span>;
gfxPlotHLine<span style="color: #003f00;">&#40;</span> pxHeight - pxBottomArea, color <span style="color: #003f00;">&#41;</span>;
&nbsp;
<span style="color: #007f00; ">// Calculate Pane width and height</span>
pxPaneWidth = pxwidth - pxLeftArea - pxRightArea;
pxPaneHeight = pxHeight - pxTopArea - pxBottomArea;
&nbsp;
<span style="color: #007f00; ">// clalculate charting area width and height</span>
<span style="color: #0000ff;">Plot</span><span style="color: #003f00;">&#40;</span> C, <span style="color: #7f007f;">&quot;&quot;</span>, <span style="color: #7f0000;">1</span>, styleDots <span style="color: #003f00;">&#41;</span>;
Miny = <span style="color: #0000ff;">Status</span><span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;axisminy&quot;</span> <span style="color: #003f00;">&#41;</span>;
Maxy = <span style="color: #0000ff;">Status</span><span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;axismaxy&quot;</span> <span style="color: #003f00;">&#41;</span>;
YRange = MaxY - MinY;
VisBarIndex =  GetVisualBarIndex<span style="color: #003f00;">&#40;</span> <span style="color: #003f00;">&#41;</span>;
NumBarsVisible = StaticVarGet<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;NumberbarsVisible&quot;</span> <span style="color: #003f00;">&#41;</span>;
&nbsp;
<span style="color: #007f00; ">// calculate conversion factors</span>
PixelsPerBar 	= pxPaneWidth / NumBarsVisible;
PixelsPerPrice = pxPaneHeight / YRange;
&nbsp;
<span style="color: #007f00; ">// For verification: Overlay pixel on price plot</span>
FVB = <span style="color: #0000ff;">Status</span><span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;firstvisiblebar&quot;</span> <span style="color: #003f00;">&#41;</span>;
LVB = <span style="color: #0000ff;">Status</span><span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;lastvisiblebar&quot;</span> <span style="color: #003f00;">&#41;</span>;
GfxSelectPen<span style="color: #003f00;">&#40;</span> colorRed <span style="color: #003f00;">&#41;</span>;
<span style="color: #7f0000; ">for</span> <span style="color: #003f00;">&#40;</span> b = FVB + <span style="color: #7f0000;">1</span>; b &lt;= LVB AND b &lt; BarCount; b++ <span style="color: #003f00;">&#41;</span>
<span style="color: #003f00;">&#123;</span>
    PrevPixelY = GetYPixels<span style="color: #003f00;">&#40;</span> C<span style="color: #003f00;">&#91;</span>b<span style="color: #7f0000;">-1</span><span style="color: #003f00;">&#93;</span> <span style="color: #003f00;">&#41;</span>;
    PixelY = GetYPixels<span style="color: #003f00;">&#40;</span> C<span style="color: #003f00;">&#91;</span>b<span style="color: #003f00;">&#93;</span> <span style="color: #003f00;">&#41;</span>;
&nbsp;
    PrevPixelX = GetXPixels<span style="color: #003f00;">&#40;</span> VisBarIndex<span style="color: #003f00;">&#91;</span>b<span style="color: #7f0000;">-1</span><span style="color: #003f00;">&#93;</span> <span style="color: #003f00;">&#41;</span>;
    PixelX = GetXPixels<span style="color: #003f00;">&#40;</span> VisBarIndex<span style="color: #003f00;">&#91;</span>b<span style="color: #003f00;">&#93;</span> <span style="color: #003f00;">&#41;</span>;
&nbsp;
    GfxMoveTo<span style="color: #003f00;">&#40;</span> PrevPixelX, PrevPixelY <span style="color: #003f00;">&#41;</span>;
    GfxLineTo<span style="color: #003f00;">&#40;</span> PixelX, PixelY <span style="color: #003f00;">&#41;</span>;
<span style="color: #003f00;">&#125;</span></pre></td></tr></table></div>

<p>Edited by Al Venosa.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.amibroker.org/userkb/2008/03/20/creating-gfx-chart-overlays/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Setting prices with your cursor</title>
		<link>http://www.amibroker.org/userkb/2008/03/16/setting-prices-with-your-cursor/</link>
		<comments>http://www.amibroker.org/userkb/2008/03/16/setting-prices-with-your-cursor/#comments</comments>
		<pubDate>Sun, 16 Mar 2008 23:20:52 +0000</pubDate>
		<dc:creator>Herman</dc:creator>
		
		<category><![CDATA[Real-Time Chart-Trading]]></category>

		<guid isPermaLink="false">http://www.amibroker.org/userkb/2008/03/16/setting-prices-with-your-cursor/</guid>
		<description><![CDATA[To place LMT or STP orders quickly is easiest done by moving the horizontal cursor-line over the desired price and making a left mouse click. The demo code below shows how you can lock in prices this way. To Chart-Trade additional code must be added to make the click perform one task of many, and [...]]]></description>
			<content:encoded><![CDATA[<p>To place LMT or STP orders quickly is easiest done by moving the horizontal cursor-line over the desired price and making a left mouse click. The demo code below shows how you can lock in prices this way. To Chart-Trade additional code must be added to make the click perform one task of many, and only do so when required. When you Apply the code below to an indicator you will see a dashed line at the cursor price. This dashed line refreshes only once per second with a local database, however, it will speed up when your chart is refreshed more frequently when working with live data. </p>
<p>Suppose you want to place a LMT order at $60.00 on the chart below. To do this you move the dashed line over the $60.00 price and click the Left mouse button. This will place a stationary solid line. You can now move your cursor to another price and click again, the solid line will move to this new location.  In actual trading you can follow the price in real time and adjust your Limit prices so that they stay exactly where you want them. The chart produced by this demo code looks like this:</p>
<p align="center"><a href='http://www.amibroker.org/userkb/wp-content/uploads/2008/03/placingline.png' title='placingline.png'><img src='http://www.amibroker.org/userkb/wp-content/uploads/2008/03/placingline.png' alt='placingline.png' /></a></p>
<p>In Chart-Trading the first click that places the solid line would also place your order on the TWS. Each subsequent click would modify the order to the new price. This way you can adjust several prices on your chart, setting and moving around Entries, Targets, Stops, etc.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code"><pre class="afl">RequestTimedRefresh<span style="color: #003f00;">&#40;</span> <span style="color: #7f0000;">1</span> <span style="color: #003f00;">&#41;</span>;
SetChartOptions<span style="color: #003f00;">&#40;</span> <span style="color: #7f0000;">2</span>, chartHideQuoteMarker <span style="color: #003f00;">&#41;</span>;
LButtonDown = GetCursorMouseButtons<span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</span> == <span style="color: #7f0000;">9</span>;
MousePrice = GetCursorYPosition<span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</span>;
&nbsp;
<span style="color: #7f0000; ">if</span> <span style="color: #003f00;">&#40;</span> MousePrice <span style="color: #003f00;">&#41;</span>
<span style="color: #003f00;">&#123;</span>
    StaticVarSet<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;MousePrice&quot;</span>, MousePrice <span style="color: #003f00;">&#41;</span>;
    <span style="color: #7f0000; ">if</span> <span style="color: #003f00;">&#40;</span> LButtonDown <span style="color: #003f00;">&#41;</span>
        StaticVarSet<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;ClickedMousePrice&quot;</span>, MousePrice <span style="color: #003f00;">&#41;</span>;
<span style="color: #003f00;">&#125;</span>
&nbsp;
LB = BarCount - <span style="color: #7f0000;">1</span>;
MousePrice = Nz<span style="color: #003f00;">&#40;</span> StaticVarGet<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Mouseprice&quot;</span> <span style="color: #003f00;">&#41;</span>, <span style="color: #000000; font-weight: bold;">Null</span> <span style="color: #003f00;">&#41;</span>;
ClickedMousePrice = StaticVarGet<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;ClickedMousePrice&quot;</span> <span style="color: #003f00;">&#41;</span>;
&nbsp;
<span style="color: #0000ff;">Plot</span><span style="color: #003f00;">&#40;</span> C, <span style="color: #7f007f;">&quot;&quot;</span>, <span style="color: #7f0000;">1</span>, <span style="color: #7f0000;">128</span> <span style="color: #003f00;">&#41;</span>;
<span style="color: #0000ff;">Plot</span><span style="color: #003f00;">&#40;</span> MousePrice, <span style="color: #7f007f;">&quot;&quot;</span>, colorWhite, <span style="color: #7f0000;">1</span> | styleNoRescale | styleDashed | styleNoLabel, <span style="color: #7f0000;">0</span>, <span style="color: #7f0000;">0</span>, <span style="color: #7f0000;">2</span> <span style="color: #003f00;">&#41;</span>;
PlotText<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;CURSOR &quot;</span> + NumToStr<span style="color: #003f00;">&#40;</span> MousePrice<span style="color: #003f00;">&#91;</span>LB<span style="color: #003f00;">&#93;</span>, <span style="color: #7f0000;">1.2</span> <span style="color: #003f00;">&#41;</span>, LB - <span style="color: #7f0000;">5</span>, MousePrice<span style="color: #003f00;">&#91;</span>LB<span style="color: #003f00;">&#93;</span>, colorBlack, colorWhite <span style="color: #003f00;">&#41;</span>;
<span style="color: #0000ff;">Plot</span><span style="color: #003f00;">&#40;</span> ClickedMousePrice, <span style="color: #7f007f;">&quot;&quot;</span>, colorBlack, <span style="color: #7f0000;">1</span> | styleNoLabel | styleNoRescale, <span style="color: #7f0000;">0</span>, <span style="color: #7f0000;">0</span>, <span style="color: #7f0000;">2</span> <span style="color: #003f00;">&#41;</span>;
PlotText<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;ORDER PLACED $&quot;</span> + NumToStr<span style="color: #003f00;">&#40;</span> ClickedMousePrice<span style="color: #003f00;">&#91;</span>LB<span style="color: #003f00;">&#93;</span>, <span style="color: #7f0000;">1.2</span> <span style="color: #003f00;">&#41;</span>, LB - <span style="color: #7f0000;">5</span>, ClickedMousePrice<span style="color: #003f00;">&#91;</span>LB<span style="color: #003f00;">&#93;</span>, colorBlack, colorWhite <span style="color: #003f00;">&#41;</span>;</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.amibroker.org/userkb/2008/03/16/setting-prices-with-your-cursor/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Parsing TWS Error messages</title>
		<link>http://www.amibroker.org/userkb/2008/03/16/parsing-tws-error-messages/</link>
		<comments>http://www.amibroker.org/userkb/2008/03/16/parsing-tws-error-messages/#comments</comments>
		<pubDate>Sun, 16 Mar 2008 17:05:37 +0000</pubDate>
		<dc:creator>Herman</dc:creator>
		
		<category><![CDATA[Real-Time Auto-Trading]]></category>

		<guid isPermaLink="false">http://www.amibroker.org/userkb/2008/03/16/parsing-tws-error-messages/</guid>
		<description><![CDATA[Here is some simple code you can use to enable you to start developing your own error-parsing functions. In this example, error messages are hard-coded to facilitate code development, i.e. you don’t have to be live with IB to test it. I suggest that you inspect the  IB list of error messages, copy the [...]]]></description>
			<content:encoded><![CDATA[<p>Here is some simple code you can use to enable you to start developing your own error-parsing functions. In this example, error messages are hard-coded to facilitate code development, i.e. you don’t have to be live with IB to test it. I suggest that you inspect the  <a href="http://individuals.interactivebrokers.com/php/webhelp/Interoperability/Socket_Client_C++/errors.htm">IB list of error messages</a>, copy the errors that are important to you, and add them to the hard-coded list. Since IB doesn’t list the error messages in the exact format you will receive them, you may have to place some orders with errors to see their exact format. When Applied to an Indicator, the code can be exercised by selecting the Error Message with the slider in the Param window. It will display the current error and parsing results in the Chart Title. Be sure to set Parameter properties to wrap the Title.</p>
<p align="center"><a href='http://www.amibroker.org/userkb/wp-content/uploads/2008/03/parsingerrors.png' title='parsingerrors.png'><img src='http://www.amibroker.org/userkb/wp-content/uploads/2008/03/parsingerrors.png' alt='parsingerrors.png' /></a></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
</pre></td><td class="code"><pre class="afl">S = Param<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Example Error Msg&quot;</span>, <span style="color: #7f0000;">0</span>, <span style="color: #7f0000;">0</span>, <span style="color: #7f0000;">10</span>, <span style="color: #7f0000;">1</span> <span style="color: #003f00;">&#41;</span>;
<span style="color: #7f0000; ">switch</span> <span style="color: #003f00;">&#40;</span> S <span style="color: #003f00;">&#41;</span>
<span style="color: #003f00;">&#123;</span>
<span style="color: #7f0000; ">case</span> <span style="color: #7f0000;">0</span>:
    TWSLastErrorMsg = <span style="color: #7f007f;">&quot;ID=15071. Error 135. Can't find order with id =:15071&quot;</span>;
    <span style="color: #7f0000; ">break</span>;
<span style="color: #7f0000; ">case</span> <span style="color: #7f0000;">1</span>:
    TWSLastErrorMsg = <span style="color: #7f007f;">&quot;ID=2094, Error 103, Duplicate ORder id&quot;</span>;
    <span style="color: #7f0000; ">break</span>;
<span style="color: #7f0000; ">case</span> <span style="color: #7f0000;">2</span>:
    TWSLastErrorMsg = <span style="color: #7f007f;">&quot;ID=-1, Error 1102, Connectivity between IB AND TWS Has been restored - data MAintained&quot;</span>;
    <span style="color: #7f0000; ">break</span>;
<span style="color: #7f0000; ">case</span> <span style="color: #7f0000;">3</span>:
    TWSLastErrorMsg = <span style="color: #7f007f;">&quot;ID=-1, Error 2100, New account data requested&quot;</span>;
    <span style="color: #7f0000; ">break</span>;
<span style="color: #7f0000; ">case</span> <span style="color: #7f0000;">4</span>:
    TWSLastErrorMsg = <span style="color: #7f007f;">&quot;ID=-1, Error 1100, Connectivity between IB AND TWS has been lost&quot;</span>;
    <span style="color: #7f0000; ">break</span>;
<span style="color: #7f0000; ">case</span> <span style="color: #7f0000;">5</span>:
    TWSLastErrorMsg = <span style="color: #7f007f;">&quot;Connection established OK, Next Order Id=2080&quot;</span>;
    <span style="color: #7f0000; ">break</span>;
<span style="color: #7f0000; ">case</span> <span style="color: #7f0000;">6</span>:
    TWSLastErrorMsg = <span style="color: #7f007f;">&quot;ID=6125. Error 202. Order Canceled - reason:&quot;</span>;
    <span style="color: #7f0000; ">break</span>;
<span style="color: #7f0000; ">default</span>:
    TWSLastErrorMsg = <span style="color: #7f007f;">&quot;&quot;</span>;
    <span style="color: #7f0000; ">break</span>;
<span style="color: #003f00;">&#125;</span>
&nbsp;
<span style="color: #7f0000; ">function</span> GetErrorCode<span style="color: #003f00;">&#40;</span> TWSLastErrorMsg <span style="color: #003f00;">&#41;</span>
<span style="color: #003f00;">&#123;</span>
    p = StrFind<span style="color: #003f00;">&#40;</span> TWSLastErrorMsg, <span style="color: #7f007f;">&quot; Error &quot;</span> <span style="color: #003f00;">&#41;</span>;
    ErrStr = StrMid<span style="color: #003f00;">&#40;</span> TWSLastErrorMsg, p + <span style="color: #7f0000;">6</span>, <span style="color: #7f0000;">4</span> <span style="color: #003f00;">&#41;</span>;
    ErrNum	= StrToNum<span style="color: #003f00;">&#40;</span> ErrStr <span style="color: #003f00;">&#41;</span>;
    ErrStr	= NumToStr<span style="color: #003f00;">&#40;</span> ErrNum, <span style="color: #7f0000;">1.0</span>, <span style="color: #000000; font-weight: bold;">False</span> <span style="color: #003f00;">&#41;</span>;
    <span style="color: #7f0000; ">return</span> ErrStr;
<span style="color: #003f00;">&#125;</span>
&nbsp;
<span style="color: #7f0000; ">function</span> ExtractErrorID<span style="color: #003f00;">&#40;</span> TWSLastErrorMsg <span style="color: #003f00;">&#41;</span>
<span style="color: #003f00;">&#123;</span>
    p = StrFind<span style="color: #003f00;">&#40;</span> TWSLastErrorMsg, <span style="color: #7f007f;">&quot;ID=&quot;</span> <span style="color: #003f00;">&#41;</span>;
    IDStr = StrMid<span style="color: #003f00;">&#40;</span> TWSLastErrorMsg, p + <span style="color: #7f0000;">2</span>, <span style="color: #7f0000;">5</span> <span style="color: #003f00;">&#41;</span>;
    IDNum	= StrToNum<span style="color: #003f00;">&#40;</span> IDStr <span style="color: #003f00;">&#41;</span>;
    IDStr	= NumToStr<span style="color: #003f00;">&#40;</span> IDNum, <span style="color: #7f0000;">1.0</span>, <span style="color: #000000; font-weight: bold;">False</span> <span style="color: #003f00;">&#41;</span>;
    <span style="color: #7f0000; ">return</span> IDStr;
<span style="color: #003f00;">&#125;</span>
&nbsp;
TWSErrorCode = GetErrorCode<span style="color: #003f00;">&#40;</span> TWSLastErrorMsg <span style="color: #003f00;">&#41;</span>;
TWSErrorOrderID = ExtractErrorID<span style="color: #003f00;">&#40;</span> TWSLastErrorMsg <span style="color: #003f00;">&#41;</span>;
Title = <span style="color: #7f007f;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> +
        <span style="color: #7f007f;">&quot;Extracting OrderIDs and Error codes from TWS Error Messages&quot;</span> + <span style="color: #7f007f;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> +
        <span style="color: #7f007f;">&quot;Example Error Msg: &quot;</span> + TWSLastErrorMsg + <span style="color: #7f007f;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> +
        <span style="color: #7f007f;">&quot;        Error Code: &quot;</span> + TWSErrorCode + <span style="color: #7f007f;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> +
        <span style="color: #7f007f;">&quot; TWS Error OrderID: &quot;</span> + TWSErrorOrderID;</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.amibroker.org/userkb/2008/03/16/parsing-tws-error-messages/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Introduction to Real-Time Chart-Trading</title>
		<link>http://www.amibroker.org/userkb/2008/03/16/introduction-to-real-time-chart-trading/</link>
		<comments>http://www.amibroker.org/userkb/2008/03/16/introduction-to-real-time-chart-trading/#comments</comments>
		<pubDate>Sun, 16 Mar 2008 15:03:13 +0000</pubDate>
		<dc:creator>Herman</dc:creator>
		
		<category><![CDATA[Real-Time Chart-Trading]]></category>

		<guid isPermaLink="false">http://www.amibroker.org/userkb/2008/03/16/introduction-to-real-time-chart-trading/</guid>
		<description><![CDATA[IMPORTANT NOTE: DO NOT USE ANY OF THE PROGRAMS PRESENTED HERE TO TRADE REAL MONEY. Most of the programs presented will only work with live data, execution reports, and market conditions. They will NOT work using the AmiBroker Bar Replay or the IB eDemo account.
Traditional Day-Trading by looking at TWS prices may still work for [...]]]></description>
			<content:encoded><![CDATA[<p>IMPORTANT NOTE: DO NOT USE ANY OF THE PROGRAMS PRESENTED HERE TO TRADE REAL MONEY. Most of the programs presented will only work with live data, execution reports, and market conditions. They will NOT work using the AmiBroker Bar Replay or the IB eDemo account.</p>
<p>Traditional Day-Trading by looking at TWS prices may still work for experienced traders who, probably over a period of many years, developed rapid interpretation skills. However, for less experienced traders graphical representations are much easier to interpret than spreadsheets or tables. Setting prices by clicking on your chart is much faster than entering prices in little windows. Trailing the price with your cursor is much faster than entering and modifying parameters in a GUI. All this is especially true when you are trading in real-time, where every second counts.</p>
<p>Posts in this category will introduce techniques that you can use to create your own custom designed Chart-Trading GUI. If you <a href="http://www.google.ca/search?num=100&#038;hl=en&#038;client=firefox-a&#038;channel=s&#038;rls=org.mozilla%3Aen-US%3Aofficial&#038;q=chart-trading&#038;btnG=Search&#038;meta=">Google Chart-Trading </a> you will find many examples of the many ways you can layout your personal interface. If you aim for maximum simplicity and automation you will end up with the most efficient GUI. Tentative topics that will be covered are:</p>
<p>1) Laying out buttons and controls (Param() Or Button())<br />
2) House keeping functions (AutoTrading, CancelAll, CloseAll, etc.)<br />
3) Automating setup for different trading methodologies (minimizing setup procedure)<br />
4) Auto placement of orders<br />
5) Auto Modification of Orders<br />
6) Displaying order and system status<br />
7) Performance reporting (Profits, Wins, etc.)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.amibroker.org/userkb/2008/03/16/introduction-to-real-time-chart-trading/feed/</wfw:commentRss>
		</item>
		<item>
		<title>AmiBroker Custom Backtester Interface</title>
		<link>http://www.amibroker.org/userkb/2008/03/16/amibroker-custom-backtester-interface-2/</link>
		<comments>http://www.amibroker.org/userkb/2008/03/16/amibroker-custom-backtester-interface-2/#comments</comments>
		<pubDate>Sun, 16 Mar 2008 11:52:08 +0000</pubDate>
		<dc:creator>Herman</dc:creator>
		
		<category><![CDATA[Custom Backtester]]></category>

		<guid isPermaLink="false">http://www.amibroker.org/userkb/2008/03/16/amibroker-custom-backtester-interface-2/</guid>
		<description><![CDATA[by Wayne (GP)
Introduction	
From version 4.67.0, AmiBroker provides a custom backtester interface to allow customising the operation of the backtester&#8217;s second phase which processes the trading signals. This allows a range of special situations to be achieved that aren&#8217;t natively supported by the backtester. AmiBroker tends to refer to this as the Advanced Portfolio Backtester Interface, [...]]]></description>
			<content:encoded><![CDATA[<p><em>by Wayne (GP)</em></p>
<p><strong>Introduction</strong>	</p>
<p>From version 4.67.0, AmiBroker provides a custom backtester interface to allow customising the operation of the backtester&#8217;s second phase which processes the trading signals. This allows a range of special situations to be achieved that aren&#8217;t natively supported by the backtester. AmiBroker tends to refer to this as the Advanced Portfolio Backtester Interface, but as it seems to be more widely referred to as the Custom Backtester Interface, I will use this latter terminology.</p>
<p>Due to the object model used by the backtester interface, a higher level of programming knowledge is required than for simple AFL or looping. This document starts by discussing that model, so is aimed at AFL programmers who are already proficient and comfortable with basic AFL use, array indexing, and looping. If you don&#8217;t understanding looping, then you almost certainly won&#8217;t understand the custom backtester interface.</p>
<p><strong>The Object Model</strong></p>
<p>The modern programming paradigm is called object-oriented programming, with the system being developed modelled as a set of objects that interact. The custom backtester interface follows that model.</p>
<p>An object can be thought of as a self-contained black-box that has certain properties and can perform certain functions. Internally it&#8217;s a combination of code and variables, where both can be made either private to the internals of the object only or accessible from outside for the benefit of users of the object. The private code and variables are totally hidden from the outside world and are of no interest to users of the object. Only developers working on the object itself care about them. Users of the object are only interested in the code and variables made accessible for their use.</p>
<p>Any variable made accessible to an object&#8217;s user is called a property of the object. For example, the Backtester object has a property (variable) called &#8220;Equity&#8221;, which is the current value of the portfolio equity during a backtest. Properties can be read and written much the same as any other variable, just by using them in expressions and assigning values to them (although some properties may be read-only). However, the syntax is a little different due to the fact they&#8217;re properties of an object, not ordinary variables.</p>
<p>An object&#8217;s code is made accessible to its users by providing a set of functions that can be called in relation to the object. These functions are called methods of the object. They are essentially identical to ordinary functions, but perform operations that are relevant to the purpose of the object. For example, the Backtester object has methods (functions) that perform operations related to backtesting. Methods are called in much the same way as other functions, but again the syntax is a little different due to them being methods of an object rather than ordinary functions.</p>
<p>The aim of the object model is to view the application as a set of self-contained and reusable objects that can manage their own functionality and provide interfaces for other objects and code to use. Imagine it as being similar to a home entertainment system, where you buy a number of components (objects) like a TV, DVD player, surround-sound system, and karaoke unit (if you&#8217;re that way inclined!). Each of those components manages its own functionality and provides you with a set of connectors and cables to join them all together to create the final application: the home entertainment system. The beauty of that arrangement is that each component provides a standard interface (if you&#8217;re lucky) that will allow any brands of the other components to be connected, without those components having to know the details of how all the other components work internally, and considerable choice in the structure of the final entertainment system constructed. Similarly, software objects have standard interfaces in the form of methods and properties that allow them to be used and reused in any software.</p>
<p><strong>Accessing Oject Properties And Methods</strong></p>
<p>To access the properties and methods of an object, you need to know not only the name of the property or method, but also the name of the object. In AmiBroker AFL, you cannot define or create your own objects, only use objects already provided by AmiBroker. AmiBroker help details all its objects, methods, and properties in the section &#8220;Advanced portfolio backtester interface&#8221;.<br />
To use real AFL examples, the first object detailed in the help is the Backtester object. AmiBroker provides a single Backtester object to perform backtests. To use the Backtester object, you first have to get a copy of it and assign that to your own variable:</p>

<div class="wp_syntax"><div class="code"><pre class="afl">bo = GetBacktesterObject<span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</span>;</pre></div></div>

<p>The variable &#8220;bo&#8221; is your own variable, and you can call it whatever you like within the naming rules of AFL. However, to avoid a lot of verbose statements, it&#8217;s good to keep it nice and short. Previously you&#8217;ve only dealt with variables that are either single numbers, arrays of numbers, or strings. The variable &#8220;bo&#8221; is none of those, instead being a new type of variable called an object variable. In this case it holds the Backtester object (or really a reference to the Backtester object, but I don&#8217;t want to get into the complicated topic of references here). Now that you have the Backtester object in your own variable, you can access its properties and methods.</p>
<p>The syntax for referencing an object&#8217;s property is <font color ='0000ff'>objectName.objectProperty</font>, for example <font color ='0000ff'>bo.InitialEquity</font>,. That can then be used the same as any other variable (assuming it&#8217;s not a read-only property, which InitialEquity is not):</p>

<div class="wp_syntax"><div class="code"><pre class="afl">bo.<span style="color: #202020;">InitialEquity</span> = <span style="color: #7f0000;">10000</span>;
capital = bo.<span style="color: #202020;">InitialEquity</span>;
gain = <span style="color: #003f00;">&#40;</span>capital - bo.<span style="color: #202020;">InitialEquity</span><span style="color: #003f00;">&#41;</span> / bo.<span style="color: #202020;">InitialEquity</span> * <span style="color: #7f0000;">100</span>;</pre></div></div>

<p>From this you can see the advantage of keeping object variable names short. If you called the variable &#8220;myBacktesterObject&#8221;, then for the last example above you&#8217;d end up with:</p>

<div class="wp_syntax"><div class="code"><pre class="afl">gain = <span style="color: #003f00;">&#40;</span>capital - myBacktesterObject.<span style="color: #202020;">InitialEquity</span><span style="color: #003f00;">&#41;</span> / myBacktesterObject.<span style="color: #202020;">InitialEquity</span> * <span style="color: #7f0000;">100</span>;</pre></div></div>

<p>===============<br />
Here I&#8217;ve had to reduce the font size just to fit it all on a single line.<br />
If a property is read-only, then you cannot perform any operation that would change its value. So, using the Equity property which is read-only:</p>

<div class="wp_syntax"><div class="code"><pre class="afl">currentEquity = bo.<span style="color: #202020;">Equity</span>;	<span style="color: #007f00; ">//  This is fine</span></pre></div></div>

<p>but:</p>

<div class="wp_syntax"><div class="code"><pre class="afl">bo.<span style="color: #202020;">Equity</span> = <span style="color: #7f0000;">50000</span>;	<span style="color: #007f00; ">//  This is an error!</span></pre></div></div>

<p>The same syntax is used to access the methods of an object. The method name is preceded by the object name with a decimal point: objectName.objectMethod(). Any parameters are passed to the method in the same manner as to ordinary functions:</p>

<div class="wp_syntax"><div class="code"><pre class="afl">objectName.<span style="color: #202020;">objectMethod</span><span style="color: #003f00;">&#40;</span>parm1, parm2, parm3<span style="color: #003f00;">&#41;</span>.</pre></div></div>

<p>For example, to call the Backtester object&#8217;s AddCustomMetric method and pass the two compulsory parameters Title and Value, a statement like this would be used:</p>

<div class="wp_syntax"><div class="code"><pre class="afl">bo.<span style="color: #202020;">AddCustomMetric</span><span style="color: #003f00;">&#40;</span><span style="color: #7f007f;">&quot;myMetric&quot;</span>, <span style="color: #7f0000;">1000</span><span style="color: #003f00;">&#41;</span>;</pre></div></div>

<p>AmiBroker help indicates that this method returns a value of type &#8220;bool&#8221;, which means boolean and thus can only take the values True and False. However, it doesn&#8217;t detail what this return value means. A good guess would be that it returns True if the custom metric was successfully added and False if for some reason it failed to be added. However, that&#8217;s only a guess, but a common reason for returning boolean values. For some of the other methods that return values of type &#8220;long&#8221;, it&#8217;s more difficult to guess what they might contain.<br />
Another example with a return parameter:</p>

<div class="wp_syntax"><div class="code"><pre class="afl">sig = bo.<span style="color: #202020;">GetFirstSignal</span><span style="color: #003f00;">&#40;</span>i<span style="color: #003f00;">&#41;</span>;</pre></div></div>

<p>Here the variable &#8220;sig&#8221; is another object variable, but this time of type Signal rather than Backtester. In other words, it holds a Signal object rather than a Backtester object. Unlike the single Backtester object, AmiBroker can have many different Signal objects created at the same time (one for each trading signal). As a Signal object holds the signal data for a particular symbol at a particular bar, the method needs to know the bar number, which would typically be specified using a loop index variable (&#8217;i&#8217; above) inside a loop:</p>

<div class="wp_syntax"><div class="code"><pre class="afl"><span style="color: #7f0000; ">for</span> <span style="color: #003f00;">&#40;</span>i = <span style="color: #7f0000;">0</span>; i &lt; BarCount; i++<span style="color: #003f00;">&#41;</span>
<span style="color: #003f00;">&#123;</span>
    . . . .
    <span style="color: #202020;">sig</span> = bo.<span style="color: #202020;">GetFirstSignal</span><span style="color: #003f00;">&#40;</span>i<span style="color: #003f00;">&#41;</span>;
    . . . .
<span style="color: #003f00;">&#125;</span></pre></div></div>

<p>Once a Signal object has been obtained, its properties and methods can be referenced:</p>

<div class="wp_syntax"><div class="code"><pre class="afl">sig.<span style="color: #202020;">PosScore</span> = <span style="color: #7f0000;">0</span>;	<span style="color: #007f00; ">//  Set position score to zero for this bar</span>
<span style="color: #7f0000; ">if</span> <span style="color: #003f00;">&#40;</span>sig.<span style="color: #202020;">IsEntry</span><span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</span><span style="color: #003f00;">&#41;</span>	<span style="color: #007f00; ">//  If this bar's signal is entry (buy/short)</span>
<span style="color: #003f00;">&#123;</span>
    . . . .
<span style="color: #003f00;">&#125;</span></pre></div></div>

<p>Note that the property sig.PosScore is a single number, not an array. While the AFL variable PositionScore is an array, the &#8220;sig&#8221; object only holds data for a single bar, so the property sig.PosScore is the position score value for that bar only, thus a single number.</p>
<p>Also note that AmiBroker help is not very clear on some topics. For example, the Signal object only has a few methods that indicate whether the current bar contains an entry, exit, long, or short signal, or has a scale in or out signal. However, it doesn&#8217;t indicate how you combine these to get the exact details. For example, how do you tell the difference between a scale-in and a scale-out? Is scaling in to a long position a combination of IsScale, IsEntry, and IsLong, or perhaps just IsScale and IsLong, or neither of those? In some cases you need to use trial and error and see what actually works (learn how to use the DebugView program with _TRACE statements: see Appendix B). Fortunately for this specific example, the Signal object also has a property called Type that indicates exactly what type the signal is.</p>
<p><strong>Using The Custom Backtester Interface</strong></p>
<p>To use your own custom backtest procedure, you first need to tell AmiBroker that you will be doing so. There are a few ways of doing this:</p>
<ol>
<li>By setting a path to the file holding the procedure in the Automatic Analysis Settings Portfolio page. This procedure will then be used with all backtests, if the &#8220;Enable custom backtest procedure&#8221; checkbox is checked.
<li>By specifying these same two settings in your AFL code using the functions SetOption(&#8221;UseCustomBacktestProc&#8221;, True) and SetCustomBacktestProc(&#8221;
<path to procedure AFL file>&#8220;). Note that path separators inside strings need to use two backslashes, for example &#8220;c:\\AmiBroker\\Formulas\\Custom\\Backtests\\MyProc.afl&#8221;. Although why is not important here, it&#8217;s because a single backslash is what&#8217;s called an escape character, meaning the character(s) after it can have special meaning rather than just being printable characters, so to actually have a printable backslash, you have to put two in a row.</p>
<li>By putting the procedure in the same file as the other AFL code and using the statement SetCustomBacktestProc(&#8221;"). This tells AmiBroker that there is a custom backtest procedure but there&#8217;s no path for it, because it&#8217;s in the current file. This option will be used throughout the rest of this document.
</ol>
<p>The next thing that&#8217;s required in all backtest procedures is to ensure the procedure only runs during the second phase of the backtest. That&#8217;s achieved with the following conditional statement:</p>

<div class="wp_syntax"><div class="code"><pre class="afl"><span style="color: #7f0000; ">if</span> <span style="color: #003f00;">&#40;</span><span style="color: #0000ff;">Status</span><span style="color: #003f00;">&#40;</span><span style="color: #7f007f;">&quot;action&quot;</span><span style="color: #003f00;">&#41;</span> == actionPortfolio<span style="color: #003f00;">&#41;</span>
<span style="color: #003f00;">&#123;</span>
    . . . . 
<span style="color: #003f00;">&#125;</span></pre></div></div>

<p>And finally, before anything else can be done, a copy of the Backtester object is needed:</p>

<div class="wp_syntax"><div class="code"><pre class="afl">bo = GetBacktesterObject<span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</span>;</pre></div></div>

<p>So all custom backtest procedures, where they&#8217;re in the same file as the other AFL code, will have a template like this:</p>

<div class="wp_syntax"><div class="code"><pre class="afl">SetCustomBacktestProc<span style="color: #003f00;">&#40;</span><span style="color: #7f007f;">&quot;&quot;</span><span style="color: #003f00;">&#41;</span>;
<span style="color: #7f0000; ">if</span> <span style="color: #003f00;">&#40;</span><span style="color: #0000ff;">Status</span><span style="color: #003f00;">&#40;</span><span style="color: #7f007f;">&quot;action&quot;</span><span style="color: #003f00;">&#41;</span> == actionPortfolio<span style="color: #003f00;">&#41;</span>
<span style="color: #003f00;">&#123;</span>
    bo = GetBacktesterObject<span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</span>;
&nbsp;
	<span style="color: #007f00; ">//  Rest of procedure goes here</span>
&nbsp;
<span style="color: #003f00;">&#125;</span></pre></div></div>

<p>If the backtests were using a procedure in the file:</p>
<p>c:\AmiBroker\Formulas\Custom\Backtests\MyBacktest.afl</p>
<p>then the first line above in your system AFL code would be replaced with:</p>

<div class="wp_syntax"><div class="code"><pre class="afl">SetOption<span style="color: #003f00;">&#40;</span><span style="color: #7f007f;">&quot;UseCustomBacktestProc&quot;</span>, <span style="color: #000000; font-weight: bold;">True</span><span style="color: #003f00;">&#41;</span>;
SetCustomBacktestProc<span style="color: #003f00;">&#40;</span><span style="color: #7f007f;">&quot;c:<span style="color: #000099; font-weight: bold;">\\</span>AmiBroker<span style="color: #000099; font-weight: bold;">\\</span>Formulas<span style="color: #000099; font-weight: bold;">\\</span>Custom<span style="color: #000099; font-weight: bold;">\\</span>Backtests<span style="color: #000099; font-weight: bold;">\\</span>MyBacktest.afl&quot;</span><span style="color: #003f00;">&#41;</span>;</pre></div></div>

<p>and the rest of the procedure would be in the specified file. Or, if the same values were specified in the Automatic Analysis settings, the two lines above would not be needed in your AFL code at all, and the procedure would be in the specified file.</p>
<p><strong>Custom Backtester Levels</strong></p>
<p>The AmiBroker custom backtester interface provides three levels of user customisation, simply called high-level, mid-level, and low-level. The high-level approach requires the least programming knowledge, and the low-level approach the most. These levels are just a convenient way of grouping together methods that can and need to be called for a customisation to work, and conversely indicate which methods cannot be called in the same customisation because their functionality conflicts. Some methods can be called at all levels, others only at higher levels, and others only at lower levels. AmiBroker help details which levels each method can be used with. Naturally, the higher the level and the simpler the programming, the less flexibility that&#8217;s available.</p>
<p>This document will not detail every single method and property available, so the rest of this document should be read in conjunction with the AmiBroker help sections &#8220;Advanced portfolio backtester interface&#8221; and &#8220;Adding custom backtest metrics&#8221;.</p>
<p><strong>High-Level Interface</strong></p>
<p>The high-level interface doesn&#8217;t allow any customising of the backtest procedure itself. It simply allows custom metrics to be defined for the backtester results display, and trade statistics and metrics to be calculated and examined. A single method call runs the whole backtest in one hit, the same as when the custom backtester interface isn&#8217;t used at all.</p>
<p>AmiBroker help has an example of using the high level interface to add a custom metric. See the section called &#8220;Adding custom backtest metrics&#8221;. In essence, the steps are:</p>
<ol>
<li>Start with the custom backtest template above
<li>Run the backtest
<li>Get the performance statistics or trade details
<li>Calculate your new metric
<li>Add your new metric to the results display
</ol>
<p>That would look something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="afl">SetCustomBacktestProc<span style="color: #003f00;">&#40;</span><span style="color: #7f007f;">&quot;&quot;</span><span style="color: #003f00;">&#41;</span>;
<span style="color: #7f0000; ">if</span> <span style="color: #003f00;">&#40;</span><span style="color: #0000ff;">Status</span><span style="color: #003f00;">&#40;</span><span style="color: #7f007f;">&quot;action&quot;</span><span style="color: #003f00;">&#41;</span> == actionPortfolio<span style="color: #003f00;">&#41;</span>
<span style="color: #003f00;">&#123;</span>
    bo = GetBacktesterObject<span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</span>;	<span style="color: #007f00; ">//  Get backtester object</span>
    bo.<span style="color: #202020;">Backtest</span><span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</span>;	<span style="color: #007f00; ">//  Run backtests</span>
    stats = bo.<span style="color: #202020;">GetPerformanceStats</span><span style="color: #003f00;">&#40;</span><span style="color: #7f0000;">0</span><span style="color: #003f00;">&#41;</span>;	<span style="color: #007f00; ">//  Get Stats object for all trades</span>
    myMetric = &lt;calculation using stats&gt;;	<span style="color: #007f00; ">//  Calculate new metric</span>
    bo.<span style="color: #202020;">AddCustomMetric</span><span style="color: #003f00;">&#40;</span><span style="color: #7f007f;">&quot;MyMetric&quot;</span>, myMetric<span style="color: #003f00;">&#41;</span>;	<span style="color: #007f00; ">//  Add metric to display</span>
<span style="color: #003f00;">&#125;</span></pre></div></div>

<p>As well as just using the built-in statistics and metrics, obtained from the Stats object after the backtest has been run, it&#8217;s also possible to calculate your metric by examining all the trades using the Trade object. As some positions may still be open at the end of the backtest, you may need to iterate through both the closed trade and open position lists:</p>

<div class="wp_syntax"><div class="code"><pre class="afl"><span style="color: #7f0000; ">for</span> <span style="color: #003f00;">&#40;</span>trade = bo.<span style="color: #202020;">GetFirstTrade</span><span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</span>; trade; trade = bo.<span style="color: #202020;">GetNextTrade</span><span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</span><span style="color: #003f00;">&#41;</span>
<span style="color: #003f00;">&#123;</span>
    . . . .
<span style="color: #003f00;">&#125;</span>
<span style="color: #7f0000; ">for</span> <span style="color: #003f00;">&#40;</span>trade = bo.<span style="color: #202020;">GetFirstOpenPos</span><span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</span>; trade; trade = bo.<span style="color: #202020;">GetNextOpenPos</span><span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</span><span style="color: #003f00;">&#41;</span>
<span style="color: #003f00;">&#123;</span>
    . . . .
<span style="color: #003f00;">&#125;</span></pre></div></div>

<p>In this example, &#8220;trade&#8221; is an object variable of type Trade, meaning it holds a Trade object. As with the Signal object, AmiBroker can have many Trade objects created at the same time, one for each closed or open trade. The first for loop iterates through the closed trade list, and the second through the open position trade list. The continuation condition &#8220;trade&#8221; theoretically means while the trade object is not zero, but in fact &#8220;trade&#8221; will be Null when the end of the list is reached. However, any conditional involving a null value is always false, so this will still work. The five Backtester object methods GetFirstTrade, GetNextTrade, GetFirstOpenPos, GetNextOpenPos, and FindOpenPos all return Null when the end of the list is reached or if no trade or open position is found.</p>
<p>The for loops are a little different to normal for loops in that they don&#8217;t have a standard loop index variable like &#8216;i&#8217; that gets incremented at the end of each pass. Instead they call a Backtester object method to get the initial value (the first Trade object) and then another member to get the next value (the next Trade object). So the for loop conditions here are just saying start from the first Trade object, at the end of each pass get the next Trade object, and keep doing that until there are no more Trade objects (ie. &#8220;trade&#8221; is Null). The loops are iterating through the list of trades, not the bars on a chart. Each Trade object holds the details for a single trade.</p>
<p>Putting that code inside the custom backtest template looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="afl">SetCustomBacktestProc<span style="color: #003f00;">&#40;</span><span style="color: #7f007f;">&quot;&quot;</span><span style="color: #003f00;">&#41;</span>;
<span style="color: #7f0000; ">if</span> <span style="color: #003f00;">&#40;</span><span style="color: #0000ff;">Status</span><span style="color: #003f00;">&#40;</span><span style="color: #7f007f;">&quot;action&quot;</span><span style="color: #003f00;">&#41;</span> == actionPortfolio<span style="color: #003f00;">&#41;</span>
<span style="color: #003f00;">&#123;</span>
    bo = GetBacktesterObject<span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</span>;	<span style="color: #007f00; ">//  Get backtester object</span>
    bo.<span style="color: #202020;">Backtest</span><span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</span>;	<span style="color: #007f00; ">//  Run backtests</span>
    <span style="color: #7f0000; ">for</span> <span style="color: #003f00;">&#40;</span>trade = bo.<span style="color: #202020;">GetFirstTrade</span><span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</span>; trade; trade = bo.<span style="color: #202020;">GetNextTrade</span><span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</span><span style="color: #003f00;">&#41;</span>
    <span style="color: #003f00;">&#123;</span>
        . . . .	<span style="color: #007f00; ">//  Use Trade object here</span>
    <span style="color: #003f00;">&#125;</span>
    <span style="color: #7f0000; ">for</span> <span style="color: #003f00;">&#40;</span>trade = bo.<span style="color: #202020;">GetFirstOpenPos</span><span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</span>; trade; trade = bo.<span style="color: #202020;">GetNextOpenPos</span><span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</span><span style="color: #003f00;">&#41;</span>
    <span style="color: #003f00;">&#123;</span>
        . . . .	<span style="color: #007f00; ">//  Use Trade object here</span>
    <span style="color: #003f00;">&#125;</span>
    myMetric = &lt;some result from Trade object calculations&gt;;
    bo.<span style="color: #202020;">AddCustomMetric</span><span style="color: #003f00;">&#40;</span><span style="color: #7f007f;">&quot;MyMetric&quot;</span>, myMetric<span style="color: #003f00;">&#41;</span>;	<span style="color: #007f00; ">//  Add metric to display</span>
<span style="color: #003f00;">&#125;</span></pre></div></div>

<p>As an example, say we want to calculate the average number of calendar days that winning trades were held for (there&#8217;s already a built-in Stats object value for number of bars, but we want number of calendar days). For that we&#8217;ll need a function that can calculate the number of calendar days between two dates. Let&#8217;s call it &#8220;DayCount&#8221;, a function that takes two parameters: the entry date and the exit date, both in AmiBroker date/time format. Since this document is about the custom backtester interface, I don&#8217;t want to go into how that function works right now. Let&#8217;s just assume it does, but the code for such a function is given in Appendix A if you&#8217;re interested. Then, for each trade we&#8217;ll need to know:</p>
<ol>
<li>If it was a winning or losing trade
<li>The entry date
<li>The exit date
</ol>
<p>And to calculate the average, we&#8217;ll need a total figure for the number of winning trade days and another total figure for the number of trades. The average is the total number of days winning trades were held divided by the total number of winning trades.<br />
For the trade details, the Trade object has the following properties:</p>
<ol>
<li>EntryDateTime	The entry date &#038; time
<li>ExitDateTime	The exit date &#038; time<br />
and the following method:</p>
<li>GetProfit()	The profit for the trade
</ol>
<p>Before trying this example, the first time we&#8217;ve used this Trade object method, we make the assumption that the profit will be negative for losing trades and positive for winning trades, as AmiBroker help doesn&#8217;t clarify that detail (it could be some undefined value for losing trades). If trial and error proves that not to be the case, then we could alternatively try using the Trade object properties EntryPrice, ExitPrice, and IsLong to determine if it was a winning or losing trade. As it turns out upon testing, GetProfit does in fact work as expected.</p>
<p>Note that the Trade object also has a property called BarsInTrade, which looks like it could potentially be used instead of the dates, but that only gives the number of bars, not the number of calendar days.<br />
So, to get the number of calendar days spent in a trade, we call our DayCount function passing the entry and exit dates:<br />
DayCount(trade.EntryDateTime, trade.ExitDateTime);<br />
and to determine if it was a winning trade, where break-even doesn&#8217;t count as winning:<br />
trade.GetProfit() > 0;<br />
The whole procedure would then be:</p>

<div class="wp_syntax"><div class="code"><pre class="afl">SetCustomBacktestProc<span style="color: #003f00;">&#40;</span><span style="color: #7f007f;">&quot;&quot;</span><span style="color: #003f00;">&#41;</span>;
<span style="color: #7f0000; ">if</span> <span style="color: #003f00;">&#40;</span><span style="color: #0000ff;">Status</span><span style="color: #003f00;">&#40;</span><span style="color: #7f007f;">&quot;action&quot;</span><span style="color: #003f00;">&#41;</span> == actionPortfolio<span style="color: #003f00;">&#41;</span>
<span style="color: #003f00;">&#123;</span>
    bo = GetBacktesterObject<span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</span>;	<span style="color: #007f00; ">//  Get backtester object</span>
    bo.<span style="color: #202020;">Backtest</span><span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</span>;	<span style="color: #007f00; ">//  Run backtests</span>
    totalDays = <span style="color: #7f0000;">0</span>;	<span style="color: #007f00; ">//  Total number of winning days</span>
    totalTrades = <span style="color: #7f0000;">0</span>;	<span style="color: #007f00; ">//  Total number of winning trades</span>
    <span style="color: #7f0000; ">for</span> <span style="color: #003f00;">&#40;</span>trade = bo.<span style="color: #202020;">GetFirstTrade</span><span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</span>; trade; trade = bo.<span style="color: #202020;">GetNextTrade</span><span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</span><span style="color: #003f00;">&#41;</span>
    <span style="color: #003f00;">&#123;</span>	<span style="color: #007f00; ">//  Loop through all closed trades</span>
        <span style="color: #7f0000; ">if</span> <span style="color: #003f00;">&#40;</span>trade.<span style="color: #202020;">GetProfit</span><span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</span> &gt; <span style="color: #7f0000;">0</span><span style="color: #003f00;">&#41;</span>	<span style="color: #007f00; ">//  If this was a winning trade</span>
        <span style="color: #003f00;">&#123;</span>
            totalDays = totalDays + DayCount<span style="color: #003f00;">&#40;</span>trade.<span style="color: #202020;">EntryDateTime</span>, trade.<span style="color: #202020;">ExitDateTime</span><span style="color: #003f00;">&#41;</span>;
            totalTrades++;  
        <span style="color: #003f00;">&#125;</span>
    <span style="color: #003f00;">&#125;</span>	<span style="color: #007f00; ">//  End of for loop over all trades</span>
    avgWinDays = totalDays / totalTrades;	<span style="color: #007f00; ">//  Calculate average win days</span>
    bo.<span style="color: #202020;">AddCustomMetric</span><span style="color: #003f00;">&#40;</span><span style="color: #7f007f;">&quot;AvgWinDays&quot;</span>, avgWinDays<span style="color: #003f00;">&#41;</span>;	<span style="color: #007f00; ">//  Add to results display</span>
<span style="color: #003f00;">&#125;</span></pre></div></div>

<p>Note that we only need to consider closed trades in this example, as counting open positions would not accurately reflect the number of days trades were typically held for. Also, the &#8220;totalTrades&#8221; variable only counts winning trades, not all trades, since we&#8217;re only averaging over winning trades.<br />
When a backtest is run with this custom interface and a report generated, our new metric &#8220;avgWinDays&#8221; will be printed at the bottom of the report:<br />
<img src='http://www.amibroker.org/userkb/wp-content/uploads/2008/03/cbt1.GIF' alt='cbt1.GIF' /></p>
<p>And if we run an optimisation (using a different backtest to above), it will have a column near the right-hand end of the results:</p>
<p><img src='http://www.amibroker.org/userkb/wp-content/uploads/2008/03/cbt2.GIF' alt='cbt2.GIF' /> </p>
<p>Note that the reason the &#8220;W. Avg Bars Held&#8221; column doesn&#8217;t seem to agree with the &#8220;AvgWinDays&#8221; column (ie. the former goes down while the latter goes up) is because the average bars figure includes open positions at the end of the backtest whereas we specifically excluded them.<br />
As well as overall metrics per backtest, it&#8217;s also possible to include individual trade metrics in the backtester results. For this, the metric is added to each Trade object rather than the Backtester object and the trades are listed at the end of the procedure.<br />
For example, to display the entry position score value against each trade in the backtester results, the following code could be used:</p>

<div class="wp_syntax"><div class="code"><pre class="afl">SetCustomBacktestProc<span style="color: #003f00;">&#40;</span><span style="color: #7f007f;">&quot;&quot;</span><span style="color: #003f00;">&#41;</span>;
<span style="color: #7f0000; ">if</span> <span style="color: #003f00;">&#40;</span><span style="color: #0000ff;">Status</span><span style="color: #003f00;">&#40;</span><span style="color: #7f007f;">&quot;action&quot;</span><span style="color: #003f00;">&#41;</span> == actionPortfolio<span style="color: #003f00;">&#41;</span>
<span style="color: #003f00;">&#123;</span>
    bo = GetBacktesterObject<span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</span>;	<span style="color: #007f00; ">//  Get backtester object</span>
    bo.<span style="color: #202020;">Backtest</span><span style="color: #003f00;">&#40;</span><span style="color: #000000; font-weight: bold;">True</span><span style="color: #003f00;">&#41;</span>;			<span style="color: #007f00; ">//  Run backtests with no trade listing</span>
    <span style="color: #7f0000; ">for</span> <span style="color: #003f00;">&#40;</span>trade = bo.<span style="color: #202020;">GetFirstTrade</span><span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</span>; trade; trade = bo.<span style="color: #202020;">GetNextTrade</span><span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</span><span style="color: #003f00;">&#41;</span>
        trade.<span style="color: #202020;">AddCustomMetric</span><span style="color: #003f00;">&#40;</span><span style="color: #7f007f;">&quot;Score&quot;</span>, trade.<span style="color: #202020;">Score</span><span style="color: #003f00;">&#41;</span>;	<span style="color: #007f00; ">//  Add closed trade score</span>
    <span style="color: #7f0000; ">for</span> <span style="color: #003f00;">&#40;</span>trade = bo.<span style="color: #202020;">GetFirstOpenPos</span><span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</span>; trade; trade = bo.<span style="color: #202020;">GetNextOpenPos</span><span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</span><span style="color: #003f00;">&#41;</span>
        trade.<span style="color: #202020;">AddCustomMetric</span><span style="color: #003f00;">&#40;</span><span style="color: #7f007f;">&quot;Score&quot;</span>, trade.<span style="color: #202020;">Score</span><span style="color: #003f00;">&#41;</span>;	<span style="color: #007f00; ">//  Add open pos score</span>
    bo.<span style="color: #202020;">ListTrades</span><span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</span>;			<span style="color: #007f00; ">//  Generate trades list</span>
<span style="color: #003f00;">&#125;</span></pre></div></div>

<p>The first for loop iterates through the closed trade list and the second through the open position list to get the entry score value for every trade listed in the results. Note that the bo.BackTest call is passed the value &#8220;True&#8221; in this case to prevent the list of trades being generated automatically by the backtester. Instead, they&#8217;re generated by the subsequent call to the bo.ListTrades method.<br />
As another example, say we want to list for each winning trade how far above or below the average winning profit it was as a percentage, and similarly for each losing trade, how far above or below the average loss it was as a percentage. For this we need the &#8220;WinnersAvgProfit&#8221; and &#8220;LosersAvgLoss&#8221; values from the Stats object, and the profit from the Trade objects for each closed trade (for this example we&#8217;ll ignore open positions). Relative loss percentages are displayed as negative numbers.</p>

<div class="wp_syntax"><div class="code"><pre class="afl">SetCustomBacktestProc<span style="color: #003f00;">&#40;</span><span style="color: #7f007f;">&quot;&quot;</span><span style="color: #003f00;">&#41;</span>;
<span style="color: #7f0000; ">if</span> <span style="color: #003f00;">&#40;</span><span style="color: #0000ff;">Status</span><span style="color: #003f00;">&#40;</span><span style="color: #7f007f;">&quot;action&quot;</span><span style="color: #003f00;">&#41;</span> == actionPortfolio<span style="color: #003f00;">&#41;</span>
<span style="color: #003f00;">&#123;</span>
    bo = GetBacktesterObject<span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</span>;	<span style="color: #007f00; ">//  Get backtester object</span>
    bo.<span style="color: #202020;">Backtest</span><span style="color: #003f00;">&#40;</span><span style="color: #000000; font-weight: bold;">True</span><span style="color: #003f00;">&#41;</span>;			<span style="color: #007f00; ">//  Run backtests with no trade listing</span>
    stat = bo.<span style="color: #202020;">GetPerformanceStats</span><span style="color: #003f00;">&#40;</span><span style="color: #7f0000;">0</span><span style="color: #003f00;">&#41;</span>;	<span style="color: #007f00; ">//  Get Stats object for all trades</span>
    winAvgProfit = stat.<span style="color: #202020;">GetValue</span><span style="color: #003f00;">&#40;</span><span style="color: #7f007f;">&quot;WinnersAvgProfit&quot;</span><span style="color: #003f00;">&#41;</span>;
    loseAvgLoss = stat.<span style="color: #202020;">GetValue</span><span style="color: #003f00;">&#40;</span><span style="color: #7f007f;">&quot;LosersAvgLoss&quot;</span><span style="color: #003f00;">&#41;</span>;
    <span style="color: #7f0000; ">for</span> <span style="color: #003f00;">&#40;</span>trade = bo.<span style="color: #202020;">GetFirstTrade</span><span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</span>; trade; trade = bo.<span style="color: #202020;">GetNextTrade</span><span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</span><span style="color: #003f00;">&#41;</span>
    <span style="color: #003f00;">&#123;</span>					<span style="color: #007f00; ">//  Loop through all closed trades</span>
        prof = trade.<span style="color: #202020;">GetProfit</span><span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</span>;		<span style="color: #007f00; ">//  Get trade profit in dollars</span>
        relProf = <span style="color: #7f0000;">0</span>;			<span style="color: #007f00; ">//  This will be profit/avgProfit as %</span>
        <span style="color: #7f0000; ">if</span> <span style="color: #003f00;">&#40;</span>prof &gt; <span style="color: #7f0000;">0</span><span style="color: #003f00;">&#41;</span>			<span style="color: #007f00; ">//  If a winner (profit &gt; 0)</span>
            relProf = prof / winAvgProfit * <span style="color: #7f0000;">100</span>;	<span style="color: #007f00; ">//  Profit relative to average</span>
        <span style="color: #7f0000; ">else</span>				<span style="color: #007f00; ">//  Else if a loser (profit &lt;= 0)</span>
            relProf = -prof / loseAvgLoss * <span style="color: #7f0000;">100</span>;	<span style="color: #007f00; ">//  Loss relative to average</span>
        trade.<span style="color: #202020;">AddCustomMetric</span><span style="color: #003f00;">&#40;</span><span style="color: #7f007f;">&quot;Rel Avg Profit%&quot;</span>, relProf<span style="color: #003f00;">&#41;</span>;	<span style="color: #007f00; ">//  Add metric</span>
    <span style="color: #003f00;">&#125;</span>					<span style="color: #007f00; ">//  End of for loop over all trades</span>
    bo.<span style="color: #202020;">ListTrades</span><span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</span>;			<span style="color: #007f00; ">//  Generate list of trades</span>
<span style="color: #003f00;">&#125;</span></pre></div></div>

<p><strong>Mid-Level Interface</strong></p>
<p>To be able to modify actual backtest behaviour, the mid-level or low-level interfaces must be used. New metrics can also be calculated at these levels, but since that&#8217;s already covered above, this section will only look at what backtest behaviour can be modified at this level. Essentially this means using Signal objects as well as the Backtester object.<br />
With the mid-level interface, each trading signal at each bar can be examined and the properties of the signals changed, based on the value of other Signal or Backtester object properties, before any trades are executed for that bar. For example, one Backtester object property is &#8220;Equity&#8221;, which gives the current portfolio equity, and one Signal object property is &#8220;PosSize&#8221;, the position size specified in the main AFL code, so the mid-level interface can allow, for example, position size to be modified based on current portfolio equity.<br />
The custom backtester interface template for a mid-level approach, where all the signals at each bar need to be examined, is:</p>

<div class="wp_syntax"><div class="code"><pre class="afl">SetCustomBacktestProc<span style="color: #003f00;">&#40;</span><span style="color: #7f007f;">&quot;&quot;</span><span style="color: #003f00;">&#41;</span>;
<span style="color: #7f0000; ">if</span> <span style="color: #003f00;">&#40;</span><span style="color: #0000ff;">Status</span><span style="color: #003f00;">&#40;</span><span style="color: #7f007f;">&quot;action&quot;</span><span style="color: #003f00;">&#41;</span> == actionPortfolio<span style="color: #003f00;">&#41;</span> <span style="color: #003f00;">&#123;</span>
    bo = GetBacktesterObject<span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</span>;	<span style="color: #007f00; ">//  Get backtester object</span>
    bo.<span style="color: #202020;">PreProcess</span><span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</span>;	<span style="color: #007f00; ">//  Do pre-processing (always required)</span>
    <span style="color: #7f0000; ">for</span> <span style="color: #003f00;">&#40;</span>i = <span style="color: #7f0000;">0</span>; i &lt; BarCount; i++<span style="color: #003f00;">&#41;</span>	<span style="color: #007f00; ">//  Loop through all bars</span>
    <span style="color: #003f00;">&#123;</span>
        <span style="color: #7f0000; ">for</span> <span style="color: #003f00;">&#40;</span>sig = bo.<span style="color: #202020;">GetFirstSignal</span><span style="color: #003f00;">&#40;</span>i<span style="color: #003f00;">&#41;</span>; sig; sig = bo.<span style="color: #202020;">GetNextSignal</span><span style="color: #003f00;">&#40;</span>i<span style="color: #003f00;">&#41;</span><span style="color: #003f00;">&#41;</span>
        <span style="color: #003f00;">&#123;</span>	<span style="color: #007f00; ">//  Loop through all signals at this bar </span>
                . . . .
        <span style="color: #003f00;">&#125;</span>	<span style="color: #007f00; ">//  End of for loop over signals at this bar</span>
        bo.<span style="color: #202020;">ProcessTradeSignals</span><span style="color: #003f00;">&#40;</span>i<span style="color: #003f00;">&#41;</span>;	<span style="color: #007f00; ">//  Process trades at bar (always required)</span>
    <span style="color: #003f00;">&#125;</span>	<span style="color: #007f00; ">//  End of for loop over bars</span>
    bo.<span style="color: #202020;">PostProcess</span><span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</span>;	<span style="color: #007f00; ">//  Do post-processing (always required)</span>
<span style="color: #003f00;">&#125;</span></pre></div></div>

<p>In this example, the variable &#8220;sig&#8221; is an object variable of type Signal, meaning it holds a Signal object. As with the Trade object in the earlier example, the inner for loop iterates through the list of signals at each bar, not through all bars on a chart. The for loop conditions are effectively saying start from the first Signal object for the current bar, at the end of each pass get the next Signal object for the same bar, and keep doing that until there are no more Signal objects for the bar (ie. &#8220;sig&#8221; is Null). Each Signal object holds the details of one signal at the current bar (ie. a buy, sell, short, cover or scale indication for one symbol).<br />
The main differences between the mid-level and high-level approaches are:</p>
<ol>
<li>The Backtester object&#8217;s Backtest method is not called.
<li>The Backtester object&#8217;s ProcessTradeSignals method is called instead at each bar, after examining and possibly modifying some of the Signal object properties and/or closed or open Trade object properties.
<li>A loop is required to iterate through all bars of the chart.
<li>A nested loop is required inside that one to iterate through all the signals at each of those bars.
</ol>
<p>If a trading decision needs to be based on some other property of a particular stock, like