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

<channel>
	<title>AmiBroker 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>
	<lastBuildDate>Sun, 29 Jan 2012 16:55:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>EOD Gap-Trading Portfolio system</title>
		<link>http://www.amibroker.org/userkb/2011/10/14/eod-gap-trading-portfolio-system/</link>
		<comments>http://www.amibroker.org/userkb/2011/10/14/eod-gap-trading-portfolio-system/#comments</comments>
		<pubDate>Fri, 14 Oct 2011 19:03:29 +0000</pubDate>
		<dc:creator>Herman</dc:creator>
				<category><![CDATA[Ideas (Experimental)]]></category>

		<guid isPermaLink="false">http://www.amibroker.org/userkb/?p=3394</guid>
		<description><![CDATA[Added February 29, 2012, additional points to consider: 1) This system depends on getting accurate fills at the Open price. To obtain such fills requires a quality minimum-delay data feed and advanced programming skills to implement trade-automation. 2) When setting the entry price slightly below the Open price (trying to improve performance) the system fails [...]]]></description>
			<content:encoded><![CDATA[<p>Added February 29, 2012, additional points to consider:</p>
<p>1) This system depends on getting accurate fills at the Open price. To obtain such fills requires a quality minimum-delay data feed and advanced programming skills to implement trade-automation.</p>
<p>2) When setting the entry price slightly below the Open price (trying to improve performance) the system fails miserably.  Even improving the price by just one cent kills the system. This suggests that most of the profit comes from days on which the Open price was equal to the daily Low, i.e., the price moved up from the Open and never dropped below it. This, of course, is obvious. To confirm this I added this test condition (it looks ahead) to exclude days on which Open == Low:</p>
<p>Buy = Buy AND NOT O == L; </p>
<p>This kills the system and proves that most of the profit comes from days where O==L. To further confirm this I added the opposite condition:</p>
<p>Buy = Buy AND O == L; </p>
<p>This gives nearly infinite profits and proves that most profits come from days on which the price moves up immediately from the Open and never returns below it. Trying to improve the entry price is a mistake; one should enter on a Stop set 1-2 ct above the Open price, this will eliminate days when the price drops and never turns back. This improves performance significantly.</p>
<p>3) This system trades knee-jerk trader-responses/patterns. Such patterns are usually drowned by large volume trading hence this system works far better when you select tickers with volumes between 500,000 and 5,000,000 shares/day. This also improves performance significantly. </p>
<p>Adding the above two features results in an equity curve much better than that shown below. Sorry, I have no time to document the above in greater detail. Good luck!</p>
<p><strong>The original post:</strong></p>
<p>This post outlines a very simple Long-only trading idea that Buys at a given percentage below yesterday&#8217;s Low, and exits at the next day&#8217;s Open. While sometimes it may be difficult to get the exact Open price, the high profitability of this system makes it a good candidate for further experimentation. The system works well with Watchlists like the N100, SP500, SP1500, Russel 1000, etc. Performance on the Russel 1000, with max. open positions set to 1, for the period 12/10/2003 to 12/10/2011, looks like this:</p>
<div align="center">
<a href="http://www.amibroker.org/userkb/wp-content/uploads/2011/10/EOD2MainStats.png"><img src="http://www.amibroker.org/userkb/wp-content/uploads/2011/10/EOD2MainStats.png" alt="" title="EOD2MainStats" width="387" height="188" class="aligncenter size-full wp-image-3395" /></a></p>
<div align="left">
<div align="center">
<a href="http://www.amibroker.org/userkb/wp-content/uploads/2011/10/EOD2LinearEquity.png"><img src="http://www.amibroker.org/userkb/wp-content/uploads/2011/10/EOD2LinearEquity.png" alt="" title="EOD2LinearEquity" width="769" height="333" class="aligncenter size-full wp-image-3406" /></a></p>
<div align="left">
<p>Some of the other Watchlists give less exposure (profits) but this comes with lower DDs. Commissions were set to $0.005 per share. No margin used.</p>
<p>No explicit ranking is used; tickers are traded based on their alphabetical sort in the Watchlist. This may seem odd but is significant: reversing this sort the system fails. This might mean that, due to real-time scanning problems, symbols listed at the top of this sort may be traded differently than those listed at the bottom. </p>
<p>Pay attention to Liquidity (you might want to trade more than one position) and slippage (Entry is rather risk-free, but exits may be problematic). DDs are significant but may be offset with improved real-time traded entries and exits. When trading automatically it may be possible to place OCA DAY-LMT entry orders for all signals and just wait and see what fills. Since exits are more difficult than entries you may wish to explore other exit strategies.</p>
<p>Parameter default values are just picked out of a hat. Almost certainly you can Optimize them or adjust them dynamically for individual tickers. I briefly tested this system in Walk-Forward mode and the results were profitable for all years tested. Except for the number of stocks traded parameters appear not very critical. Over-optimizing doesn&#8217;t seem a problem in this case.</p>
<p>The code below is very simple and requires few explanations. However it is important to understand that this system enjoys a small edge by trading at the Open, and by calculating the TrendMA using the same Open price. Some might interpret this as future leak, however if you trade this system in real-time, it is not.  Many people do not realize that if you trade at the Open you can also use this price in your calculations &#8212; as long as you perform them in real-time &#8212; this is where AmiBroker and technology can give you an edge. If you Ref() back the TrendMA by one bar the system is still very profitable however DDs increase for some Watchlists. If you use fixed investments the difference is negligible. </p>
<p>The trading procedure would be to start scanning before the market opens and remove tickers that are priced so remote that they are unlikely to meet the OpenThresh. Thus you may start scanning 1000 symbols but very quickly the number scanned will dwindle to just a dozen or so tickers. When you approach 9:30am your real-time scan will be very fast and you will be able to place your LMT order very close to the Open &#8211; you may even be able to improve on the Open price.</p>
<p>Even though a few people looked at the code below and found nothing wrong, the profits seem rather high for such a simple system. Please report errors you may see.</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
</pre></td><td class="code"><pre class="afl"><span style="color: #7f0000; ">function</span> ParamOptimize<span style="color: #003f00;">&#40;</span> description, defaultVal, minv, maxv, step <span style="color: #003f00;">&#41;</span>
<span style="color: #003f00;">&#123;</span>
    <span style="color: #7f0000; ">return</span> Optimize<span style="color: #003f00;">&#40;</span> description, Param<span style="color: #003f00;">&#40;</span> description, defaultVal, minv, maxv, step <span style="color: #003f00;">&#41;</span>, minv, maxv, step <span style="color: #003f00;">&#41;</span>;
<span style="color: #003f00;">&#125;</span>
&nbsp;
PosQty = ParamOptimize<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Position Qty&quot;</span>, <span style="color: #7f0000;">1</span>, <span style="color: #7f0000;">1</span>, <span style="color: #7f0000;">10</span>, <span style="color: #7f0000;">1</span> <span style="color: #003f00;">&#41;</span>;
TrendPeriod = Paramoptimize<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Pd1&quot;</span>, <span style="color: #7f0000;">13</span>, <span style="color: #7f0000;">3</span>, <span style="color: #7f0000;">30</span>, <span style="color: #7f0000;">1</span> <span style="color: #003f00;">&#41;</span>;
OpenOffset = paramOptimize<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;OS&quot;</span>, <span style="color: #7f0000;">0.99</span>, <span style="color: #7f0000;">0.98</span>, <span style="color: #7f0000;">1</span>, <span style="color: #7f0000;">0.001</span> <span style="color: #003f00;">&#41;</span>;
MinVolume = ParamOptimize<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Min. Volume (M)&quot;</span>, <span style="color: #7f0000;">0.5</span>, <span style="color: #7f0000;">0.5</span>, <span style="color: #7f0000;">10</span>, <span style="color: #7f0000;">0.5</span> <span style="color: #003f00;">&#41;</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">Short</span> = <span style="color: #000000; font-weight: bold;">Cover</span> = <span style="color: #7f0000;">0</span>;
SetOption<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;allowsamebarexit&quot;</span>, <span style="color: #000000; font-weight: bold;">False</span> <span style="color: #003f00;">&#41;</span>;
SetOption<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;maxopenpositions&quot;</span>, PosQty <span style="color: #003f00;">&#41;</span>;
&nbsp;
VolumeOK = <span style="color: #0000ff;">MA</span><span style="color: #003f00;">&#40;</span> <span style="color: #0000ff;">Ref</span><span style="color: #003f00;">&#40;</span> V, <span style="color: #7f0000;">-1</span> <span style="color: #003f00;">&#41;</span>, <span style="color: #7f0000;">10</span> <span style="color: #003f00;">&#41;</span> &gt; MinVolume;
TrendMA = <span style="color: #0000ff;">MA</span><span style="color: #003f00;">&#40;</span> O, TrendPeriod <span style="color: #003f00;">&#41;</span>;  
ROCTrend = ROC<span style="color: #003f00;">&#40;</span> TrendMA, <span style="color: #7f0000;">1</span> <span style="color: #003f00;">&#41;</span>;
TrendUp = ROCTrend &gt; <span style="color: #7f0000;">0</span>;
OpenThresh = <span style="color: #0000ff;">Ref</span><span style="color: #003f00;">&#40;</span> L, <span style="color: #7f0000;">-1</span> <span style="color: #003f00;">&#41;</span> * OpenOffset;
&nbsp;
<span style="color: #000000; font-weight: bold;">Buy</span> = O &lt; OpenThresh AND VolumeOK AND TrendUp;
<span style="color: #000000; font-weight: bold;">BuyPrice</span> = O;
&nbsp;
<span style="color: #000000; font-weight: bold;">Sell</span> = <span style="color: #0000ff;">Ref</span><span style="color: #003f00;">&#40;</span> <span style="color: #000000; font-weight: bold;">Buy</span>, <span style="color: #7f0000;">-1</span> <span style="color: #003f00;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">SellPrice</span> = O;
&nbsp;
<span style="color: #000000; font-weight: bold;">PositionSize</span> = <span style="color: #7f0000;">-100</span> / PosQty;  <span style="color: #007f00; ">// Compounded</span>
<span style="color: #007f00; ">//PositionSize = 100000;   // Fixed investment</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> | styleThick <span style="color: #003f00;">&#41;</span>;
<span style="color: #0000ff;">Plot</span><span style="color: #003f00;">&#40;</span> TrendMA, <span style="color: #7f007f;">&quot;&quot;</span>, <span style="color: #7f0000;">2</span>, <span style="color: #7f0000;">1</span> <span style="color: #003f00;">&#41;</span>;
&nbsp;
<span style="color: #7f0000; ">if</span> <span style="color: #003f00;">&#40;</span> ParamToggle<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Triangles&quot;</span>, <span style="color: #7f007f;">&quot;HIDE|SHOW&quot;</span>, <span style="color: #7f0000;">1</span> <span style="color: #003f00;">&#41;</span> <span style="color: #003f00;">&#41;</span>
<span style="color: #003f00;">&#123;</span>
    <span style="color: #0000ff;">PlotShapes</span><span style="color: #003f00;">&#40;</span> <span style="color: #0000ff;">IIf</span><span style="color: #003f00;">&#40;</span> <span style="color: #000000; font-weight: bold;">Buy</span>, 	shapeSmallCircle, shapeNone <span style="color: #003f00;">&#41;</span>, <span style="color: #7f0000;">5</span>, <span style="color: #7f0000;">0</span>, <span style="color: #000000; font-weight: bold;">BuyPrice</span>, <span style="color: #7f0000;">0</span> <span style="color: #003f00;">&#41;</span>;
    <span style="color: #0000ff;">PlotShapes</span><span style="color: #003f00;">&#40;</span> <span style="color: #0000ff;">IIf</span><span style="color: #003f00;">&#40;</span> <span style="color: #000000; font-weight: bold;">Sell</span>,	shapeSmallCircle, shapeNone <span style="color: #003f00;">&#41;</span>, <span style="color: #7f0000;">4</span>, <span style="color: #7f0000;">0</span>, <span style="color: #000000; font-weight: bold;">SellPrice</span>, <span style="color: #7f0000;">0</span> <span style="color: #003f00;">&#41;</span>;
<span style="color: #003f00;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.amibroker.org/userkb/2011/10/14/eod-gap-trading-portfolio-system/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Long-only EOD Gap trading idea</title>
		<link>http://www.amibroker.org/userkb/2011/09/01/a-long-only-eod-gap-trading-idea/</link>
		<comments>http://www.amibroker.org/userkb/2011/09/01/a-long-only-eod-gap-trading-idea/#comments</comments>
		<pubDate>Thu, 01 Sep 2011 18:53:49 +0000</pubDate>
		<dc:creator>Herman</dc:creator>
				<category><![CDATA[Ideas (Experimental)]]></category>

		<guid isPermaLink="false">http://www.amibroker.org/userkb/?p=3374</guid>
		<description><![CDATA[This idea was posted ( #161332 ) on the main AmiBroker list on July 3, 2011. There were numerous excellent comments on the list and if you are interested in working on this system you do well to read them all before starting. After posting I found a number of posts on the web discussing [...]]]></description>
			<content:encoded><![CDATA[<p>This idea was posted ( <a href="http://finance.groups.yahoo.com/group/amibroker/message/161332">#161332</a> ) on the main AmiBroker list on July 3, 2011. There were numerous excellent comments on the list and if you are interested in working on this system you do well to read them all before starting. After posting I found a number of posts on the web discussing this trading idea, some claimed to be trading a similar system with good success. </p>
<p>I referred to this system a &#8220;Gap Trading&#8221; system but this may be a bit of a misnomer, &#8220;Mean reversion&#8221; might be a better classification. <a href="http://www.google.ca/#pq=mean+reverse&#038;hl=en&#038;cp=29&#038;gs_id=1e&#038;xhr=t&#038;q=mean+reversion+trading+system&#038;pf=p&#038;sclient=psy&#038;source=hp&#038;pbx=1&#038;oq=mean+reversion+trading+system&#038;aq=0&#038;aqi=g1&#038;aql=&#038;gs_sm=&#038;gs_upl=&#038;bav=on.2,or.r_gc.r_pw.&#038;fp=ae49c4619d154637&#038;biw=1050&#038;bih=1511">Googling for it</a> will get you many more hits to similar systems. Here are a few links:</p>
<p><a href="http://engineering-returns.com/2010/10/19/ndx100-trading-intraday-mean-reversion-using-limit-orders/">NDX100: Trading intraday mean reversion using limit orders</a><br />
<a href="http://engineering-returns.com/2010/10/20/trading-intraday-mean-reversion-using-limit-orders-does-it-work/">Trading intraday mean reversion using limit orders – does it work?</a></p>
<p>It appears to be a fairly widely discussed trading idea and I suggest you&#8217;ll do some Googling on your own to learn the latest. As an Amibroker user you have better tools than most traders and you have a better chance than most to come up with a variation that works. Perhaps with a little less profits, and with a significant amount of additional code &#8212; it won&#8217;t be a &#8220;quicky&#8221; project :-) </p>
<p>Some people commented that this system will not work in real trading, while they may be right others say schemes like this work. I didn&#8217;t finish the system and can&#8217;t claim to know whether it is tradable or not.</p>
<p>The system Buys at a certain percentage below yesterday&#8217;s Low, on a LMT order, and exits in the same day at the Close. </p>
<div align="center">
<a href="http://www.amibroker.org/userkb/wp-content/uploads/2011/09/EODGapSystem.png"><img src="http://www.amibroker.org/userkb/wp-content/uploads/2011/09/EODGapSystem.png" alt="" title="EODGapSystem" width="851" height="552" class="aligncenter size-full wp-image-3380" /></a></p>
<div align="left">

<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
</pre></td><td class="code"><pre class="afl"><span style="color: #000000; font-weight: bold;">Short</span> = <span style="color: #000000; font-weight: bold;">Cover</span> = <span style="color: #7f0000;">0</span>;
Qty = <span style="color: #7f0000;">6</span>;
SetTradeDelays<span style="color: #003f00;">&#40;</span> <span style="color: #7f0000;">0</span>, <span style="color: #7f0000;">0</span>, <span style="color: #7f0000;">0</span>, <span style="color: #7f0000;">0</span> <span style="color: #003f00;">&#41;</span>;
SetOption<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;AllowSameBarExit&quot;</span>, <span style="color: #000000; font-weight: bold;">True</span> <span style="color: #003f00;">&#41;</span>;
SetOption<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;maxopenpositions&quot;</span>, Qty <span style="color: #003f00;">&#41;</span>;
&nbsp;
GapAmount = Param<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Gap (%)&quot;</span>, <span style="color: #7f0000;">1.5</span>, <span style="color: #7f0000;">0</span>, <span style="color: #7f0000;">3</span>, <span style="color: #7f0000;">0.01</span> <span style="color: #003f00;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">BuyPrice</span> = <span style="color: #003f00;">&#40;</span> <span style="color: #7f0000;">1</span> - GapAmount / <span style="color: #7f0000;">100</span> <span style="color: #003f00;">&#41;</span> * <span style="color: #0000ff;">Ref</span><span style="color: #003f00;">&#40;</span> L, <span style="color: #7f0000;">-1</span> <span style="color: #003f00;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">BuyPrice</span> = Min<span style="color: #003f00;">&#40;</span> O, <span style="color: #000000; font-weight: bold;">BuyPrice</span> <span style="color: #003f00;">&#41;</span>; <span style="color: #007f00; ">// Enter immediately at Open if below BuyPrice</span>
<span style="color: #000000; font-weight: bold;">Buy</span> = L &lt; <span style="color: #000000; font-weight: bold;">BuyPrice</span> AND <span style="color: #0000ff;">Ref</span><span style="color: #003f00;">&#40;</span> <span style="color: #0000ff;">MA</span><span style="color: #003f00;">&#40;</span> V, <span style="color: #7f0000;">10</span> <span style="color: #003f00;">&#41;</span>, <span style="color: #7f0000;">-1</span> <span style="color: #003f00;">&#41;</span> &gt; <span style="color: #7f0000;">1000000</span>;
<span style="color: #000000; font-weight: bold;">SellPrice</span> = C;
<span style="color: #000000; font-weight: bold;">Sell</span> = <span style="color: #000000; font-weight: bold;">Buy</span>;
&nbsp;
<span style="color: #007f00; ">// To test for ticker-sensitivity turn on Random Ticker Selection</span>
<span style="color: #007f00; ">// and run this code in the Optimizer to see how performance varies</span>
<span style="color: #007f00; ">// Because it is unknown which ticker will trigger first actual</span>
<span style="color: #007f00; ">// performance will be somewhere between the min/max profits shown</span>
&nbsp;
<span style="color: #7f0000; ">if</span> <span style="color: #003f00;">&#40;</span> ParamToggle<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Random Ticker Selection&quot;</span>, <span style="color: #7f007f;">&quot;OFF|ON&quot;</span>, <span style="color: #7f0000;">0</span> <span style="color: #003f00;">&#41;</span> <span style="color: #003f00;">&#41;</span>
<span style="color: #003f00;">&#123;</span>
    Optimize<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Dummy&quot;</span>, <span style="color: #7f0000;">1</span>, <span style="color: #7f0000;">1</span>, <span style="color: #7f0000;">100</span>, <span style="color: #7f0000;">1</span> <span style="color: #003f00;">&#41;</span>;
    <span style="color: #000000; font-weight: bold;">PositionScore</span> = mtRandom<span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</span>;
<span style="color: #003f00;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">PositionSize</span> = <span style="color: #7f0000;">-100</span> / Qty;
&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>;
&nbsp;
<span style="color: #7f0000; ">if</span> <span style="color: #003f00;">&#40;</span> ParamToggle<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Arrows&quot;</span>, <span style="color: #7f007f;">&quot;HIDE|SHOW&quot;</span>, <span style="color: #7f0000;">0</span> <span style="color: #003f00;">&#41;</span> <span style="color: #003f00;">&#41;</span>
<span style="color: #003f00;">&#123;</span>
    Equity<span style="color: #003f00;">&#40;</span> <span style="color: #7f0000;">1</span> <span style="color: #003f00;">&#41;</span>;
    <span style="color: #0000ff;">PlotShapes</span><span style="color: #003f00;">&#40;</span> <span style="color: #0000ff;">IIf</span><span style="color: #003f00;">&#40;</span> <span style="color: #000000; font-weight: bold;">Buy</span>, shapeUpArrow, shapeNone <span style="color: #003f00;">&#41;</span>, colorGreen, <span style="color: #7f0000;">0</span>, <span style="color: #000000; font-weight: bold;">BuyPrice</span> <span style="color: #003f00;">&#41;</span>;
    <span style="color: #0000ff;">PlotShapes</span><span style="color: #003f00;">&#40;</span> <span style="color: #0000ff;">IIf</span><span style="color: #003f00;">&#40;</span> <span style="color: #000000; font-weight: bold;">Sell</span>, shapeDownArrow, shapeNone <span style="color: #003f00;">&#41;</span>, colorRed, <span style="color: #7f0000;">0</span>, <span style="color: #000000; font-weight: bold;">SellPrice</span> <span style="color: #003f00;">&#41;</span>;
    <span style="color: #0000ff;">PlotShapes</span><span style="color: #003f00;">&#40;</span> <span style="color: #0000ff;">IIf</span><span style="color: #003f00;">&#40;</span> <span style="color: #000000; font-weight: bold;">Buy</span>, shapeSmallCircle, shapeNone <span style="color: #003f00;">&#41;</span>, colorGreen, <span style="color: #7f0000;">0</span>, <span style="color: #000000; font-weight: bold;">BuyPrice</span>, <span style="color: #7f0000;">0</span> <span style="color: #003f00;">&#41;</span>;
    <span style="color: #0000ff;">PlotShapes</span><span style="color: #003f00;">&#40;</span> <span style="color: #0000ff;">IIf</span><span style="color: #003f00;">&#40;</span> <span style="color: #000000; font-weight: bold;">Sell</span>, shapeSmallCircle, shapeNone <span style="color: #003f00;">&#41;</span>, colorRed, <span style="color: #7f0000;">0</span>, <span style="color: #000000; font-weight: bold;">SellPrice</span>, <span style="color: #7f0000;">0</span> <span style="color: #003f00;">&#41;</span>;
<span style="color: #003f00;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.amibroker.org/userkb/2011/09/01/a-long-only-eod-gap-trading-idea/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Bollinger Band ZigZag Indicator</title>
		<link>http://www.amibroker.org/userkb/2011/08/25/bollinger-band-zigzag-indicator/</link>
		<comments>http://www.amibroker.org/userkb/2011/08/25/bollinger-band-zigzag-indicator/#comments</comments>
		<pubDate>Thu, 25 Aug 2011 20:43:46 +0000</pubDate>
		<dc:creator>Herman</dc:creator>
				<category><![CDATA[Indicators]]></category>

		<guid isPermaLink="false">http://www.amibroker.org/userkb/?p=3331</guid>
		<description><![CDATA[IMPORTANT: Do not use the indicator in a real trading system; it looks ahead in time and will make you lose money. It is meant for research only: to show potential profits and display arrows at highly profitable positions to facilitate formulating better trading rules. The indicator presented here is very similar to the ZigZag [...]]]></description>
			<content:encoded><![CDATA[<p>IMPORTANT: Do not use the indicator in a real trading system; it looks ahead in time and will make you lose money. It is meant for research only: to show potential profits and display arrows at highly profitable positions to facilitate formulating better trading rules.</p>
<p>The indicator presented here is very similar to the ZigZag Indicator except that the turning points for this indicator are where the opposite Bollinger Bands are last breached before the next signal.</p>
<p>The formula is written as a trading system. It can be Backtested, and the BB period and width can be optimized. Since this is just an experimental formula no attempt has been made to optimize the code.</p>
<div align="center">
<a href="http://www.amibroker.org/userkb/wp-content/uploads/2011/08/BBZigZag1.png"><img src="http://www.amibroker.org/userkb/wp-content/uploads/2011/08/BBZigZag1.png" alt="" title="BBZigZag" width="744" height="324" class="aligncenter size-full wp-image-3338" /></a></p>
<div align="left">

<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
</pre></td><td class="code"><pre class="afl"><span style="color: #7f0000; ">function</span> ParamOptimize<span style="color: #003f00;">&#40;</span> description, defaultVal, minv, maxv, step <span style="color: #003f00;">&#41;</span>
	<span style="color: #003f00;">&#123;</span> 
	<span style="color: #7f0000; ">return</span> Optimize<span style="color: #003f00;">&#40;</span>description, Param<span style="color: #003f00;">&#40;</span>description,defaultVal, minv, maxv, step <span style="color: #003f00;">&#41;</span>, minv, maxv, step <span style="color: #003f00;">&#41;</span>; 
	<span style="color: #003f00;">&#125;</span>
&nbsp;
BlankBars = <span style="color: #7f0000;">10</span>; <span style="color: #007f00; ">// Set to the number set in preferences</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">Buy</span> = <span style="color: #000000; font-weight: bold;">Sell</span> = <span style="color: #000000; font-weight: bold;">Short</span> = <span style="color: #000000; font-weight: bold;">Cover</span> = Pos = <span style="color: #7f0000;">0</span>;
Periods = ParamOptimize<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Periods&quot;</span>, <span style="color: #7f0000;">3</span>, <span style="color: #7f0000;">3</span>, <span style="color: #7f0000;">30</span>, <span style="color: #7f0000;">1</span> <span style="color: #003f00;">&#41;</span>;
Width = ParamOptimize<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Width&quot;</span>, <span style="color: #7f0000;">1</span>, <span style="color: #7f0000;">0</span>, <span style="color: #7f0000;">5</span>, <span style="color: #7f0000;">0.1</span> <span style="color: #003f00;">&#41;</span>;
BBTOp = BBandTop<span style="color: #003f00;">&#40;</span> H, Periods, Width <span style="color: #003f00;">&#41;</span>; <span style="color: #007f00; ">// Note H is used instead of the tradional C</span>
BBBot = BBandBot<span style="color: #003f00;">&#40;</span> L, Periods, Width <span style="color: #003f00;">&#41;</span>; <span style="color: #007f00; ">// Note L is used instead of the tradional C</span>
&nbsp;
<span style="color: #0000ff;">Plot</span><span style="color: #003f00;">&#40;</span> BBTop, <span style="color: #7f007f;">&quot;BBTop&quot;</span>, colorBlue, styleLine <span style="color: #003f00;">&#41;</span>;
<span style="color: #0000ff;">Plot</span><span style="color: #003f00;">&#40;</span> BBBOt, <span style="color: #7f007f;">&quot;BBBot&quot;</span>, colorBlue, styleLine <span style="color: #003f00;">&#41;</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>, <span style="color: #7f0000;">128</span> <span style="color: #003f00;">&#41;</span>;
&nbsp;
BI = BarIndex<span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</span>;
start = <span style="color: #0000ff;">Status</span><span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;firstvisiblebarindex&quot;</span> <span style="color: #003f00;">&#41;</span> - BI<span style="color: #003f00;">&#91;</span><span style="color: #7f0000;">0</span><span style="color: #003f00;">&#93;</span> - BlankBars;
end 	= <span style="color: #0000ff;">Status</span><span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;lastvisiblebarindex&quot;</span> <span style="color: #003f00;">&#41;</span> - BI<span style="color: #003f00;">&#91;</span><span style="color: #7f0000;">0</span><span style="color: #003f00;">&#93;</span> - BlankBars;
LBI = LastValue<span style="color: #003f00;">&#40;</span> BarIndex<span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</span> <span style="color: #003f00;">&#41;</span>;
&nbsp;
<span style="color: #7f0000; ">for</span> <span style="color: #003f00;">&#40;</span> b = LBI; b &gt; Periods; b-- <span style="color: #003f00;">&#41;</span>
<span style="color: #003f00;">&#123;</span>
    <span style="color: #7f0000; ">if</span> <span style="color: #003f00;">&#40;</span> L<span style="color: #003f00;">&#91;</span>b<span style="color: #003f00;">&#93;</span> &lt;= BBBot<span style="color: #003f00;">&#91;</span>b<span style="color: #003f00;">&#93;</span> AND pos &lt;= <span style="color: #7f0000;">0</span> <span style="color: #003f00;">&#41;</span>
    <span style="color: #003f00;">&#123;</span>
        pos = <span style="color: #7f0000;">1</span>;
        <span style="color: #000000; font-weight: bold;">Buy</span><span style="color: #003f00;">&#91;</span>b<span style="color: #003f00;">&#93;</span> = <span style="color: #000000; font-weight: bold;">True</span>;
        <span style="color: #000000; font-weight: bold;">BuyPrice</span><span style="color: #003f00;">&#91;</span>b<span style="color: #003f00;">&#93;</span> = BBBot<span style="color: #003f00;">&#91;</span>b<span style="color: #003f00;">&#93;</span>;
    <span style="color: #003f00;">&#125;</span>
    <span style="color: #7f0000; ">else</span>
        <span style="color: #7f0000; ">if</span> <span style="color: #003f00;">&#40;</span> H<span style="color: #003f00;">&#91;</span>b<span style="color: #003f00;">&#93;</span> &gt;= BBTop<span style="color: #003f00;">&#91;</span>b<span style="color: #003f00;">&#93;</span> AND Pos &gt;= <span style="color: #7f0000;">0</span> <span style="color: #003f00;">&#41;</span>
        <span style="color: #003f00;">&#123;</span>
            Pos = <span style="color: #7f0000;">-1</span>;
            <span style="color: #000000; font-weight: bold;">Sell</span><span style="color: #003f00;">&#91;</span>b<span style="color: #003f00;">&#93;</span> = <span style="color: #000000; font-weight: bold;">True</span>;
            <span style="color: #000000; font-weight: bold;">SellPrice</span><span style="color: #003f00;">&#91;</span>b<span style="color: #003f00;">&#93;</span> = BBTop<span style="color: #003f00;">&#91;</span>b<span style="color: #003f00;">&#93;</span>;
        <span style="color: #003f00;">&#125;</span>
<span style="color: #003f00;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">Short</span> = <span style="color: #000000; font-weight: bold;">Sell</span>;
ShortPrice = <span style="color: #000000; font-weight: bold;">SellPrice</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">Cover</span> = <span style="color: #000000; font-weight: bold;">Buy</span>;
<span style="color: #000000; font-weight: bold;">CoverPrice</span> = <span style="color: #000000; font-weight: bold;">BuyPrice</span>;
&nbsp;
Eq = Equity<span style="color: #003f00;">&#40;</span> <span style="color: #7f0000;">1</span> <span style="color: #003f00;">&#41;</span>;
&nbsp;
<span style="color: #7f0000; ">if</span> <span style="color: #003f00;">&#40;</span> ParamToggle<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Equity&quot;</span>, <span style="color: #7f007f;">&quot;HIDE|SHOW&quot;</span>, <span style="color: #7f0000;">0</span> <span style="color: #003f00;">&#41;</span> <span style="color: #003f00;">&#41;</span>
    <span style="color: #0000ff;">Plot</span><span style="color: #003f00;">&#40;</span> Eq, <span style="color: #7f007f;">&quot;&quot;</span>, colorYellow, <span style="color: #7f0000;">1</span> | styleOwnScale <span style="color: #003f00;">&#41;</span>;
&nbsp;
ShowTriangles = ParamToggle<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Arrows&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: #7f0000; ">if</span> <span style="color: #003f00;">&#40;</span> showTriangles <span style="color: #003f00;">&#41;</span>
<span style="color: #003f00;">&#123;</span>
    <span style="color: #0000ff;">PlotShapes</span><span style="color: #003f00;">&#40;</span> <span style="color: #0000ff;">IIf</span><span style="color: #003f00;">&#40;</span> <span style="color: #000000; font-weight: bold;">Buy</span>, 	shapeSmallUpTriangle, shapeNone <span style="color: #003f00;">&#41;</span>, <span style="color: #7f0000;">5</span>, <span style="color: #7f0000;">0</span>, <span style="color: #000000; font-weight: bold;">BuyPrice</span>, <span style="color: #7f0000;">0</span> <span style="color: #003f00;">&#41;</span>;
    <span style="color: #0000ff;">PlotShapes</span><span style="color: #003f00;">&#40;</span> <span style="color: #0000ff;">IIf</span><span style="color: #003f00;">&#40;</span> <span style="color: #000000; font-weight: bold;">Sell</span>,	shapeHollowDownTriangle, shapeNone <span style="color: #003f00;">&#41;</span>, <span style="color: #7f0000;">4</span>, <span style="color: #7f0000;">0</span>, <span style="color: #000000; font-weight: bold;">SellPrice</span>, <span style="color: #7f0000;">0</span> <span style="color: #003f00;">&#41;</span>;
    <span style="color: #0000ff;">PlotShapes</span><span style="color: #003f00;">&#40;</span> <span style="color: #0000ff;">IIf</span><span style="color: #003f00;">&#40;</span> <span style="color: #000000; font-weight: bold;">Cover</span>, shapeHollowUpTriangle, shapeNone <span style="color: #003f00;">&#41;</span>, <span style="color: #7f0000;">5</span>, <span style="color: #7f0000;">0</span>, <span style="color: #000000; font-weight: bold;">CoverPrice</span>, <span style="color: #7f0000;">0</span> <span style="color: #003f00;">&#41;</span>;
    <span style="color: #0000ff;">PlotShapes</span><span style="color: #003f00;">&#40;</span> <span style="color: #0000ff;">IIf</span><span style="color: #003f00;">&#40;</span> <span style="color: #000000; font-weight: bold;">Short</span>, shapeSmallDownTriangle, shapeNone <span style="color: #003f00;">&#41;</span>, <span style="color: #7f0000;">4</span>, <span style="color: #7f0000;">0</span>, ShortPrice, <span style="color: #7f0000;">0</span> <span style="color: #003f00;">&#41;</span>;
<span style="color: #003f00;">&#125;</span>
&nbsp;
<span style="color: #7f0000; ">if</span> <span style="color: #003f00;">&#40;</span> ParamToggle<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Trade Lines&quot;</span>, <span style="color: #7f007f;">&quot;HIDE|SHOW&quot;</span>, <span style="color: #7f0000;">0</span> <span style="color: #003f00;">&#41;</span> <span style="color: #003f00;">&#41;</span>
<span style="color: #003f00;">&#123;</span>
    Sig = <span style="color: #000000; font-weight: bold;">Buy</span> OR <span style="color: #000000; font-weight: bold;">Short</span>;
    signum = Cum<span style="color: #003f00;">&#40;</span> Sig <span style="color: #003f00;">&#41;</span>;
    y0 = <span style="color: #7f0000;">0</span>;
    y1 = C<span style="color: #003f00;">&#91;</span><span style="color: #7f0000;">0</span><span style="color: #003f00;">&#93;</span>;
    TPrice = C;
    Shortcolor = LongColor = colorWhite;
&nbsp;
    <span style="color: #7f0000; ">for</span> <span style="color: #003f00;">&#40;</span> b = start ;b &lt; end ;b++ <span style="color: #003f00;">&#41;</span>
    <span style="color: #003f00;">&#123;</span>
        <span style="color: #7f0000; ">if</span> <span style="color: #003f00;">&#40;</span> Sig<span style="color: #003f00;">&#91;</span>b<span style="color: #003f00;">&#93;</span> <span style="color: #003f00;">&#41;</span>
        <span style="color: #003f00;">&#123;</span>
            x0 = y0;
            x1 = y1;
            y0 = b;
&nbsp;
            <span style="color: #7f0000; ">if</span> <span style="color: #003f00;">&#40;</span> <span style="color: #000000; font-weight: bold;">Buy</span><span style="color: #003f00;">&#91;</span>b<span style="color: #003f00;">&#93;</span> <span style="color: #003f00;">&#41;</span>
            <span style="color: #003f00;">&#123;</span>
                y1 = <span style="color: #000000; font-weight: bold;">BuyPrice</span><span style="color: #003f00;">&#91;</span>b<span style="color: #003f00;">&#93;</span>;
                Color = Shortcolor;
            <span style="color: #003f00;">&#125;</span>
            <span style="color: #7f0000; ">else</span>
                <span style="color: #7f0000; ">if</span> <span style="color: #003f00;">&#40;</span> <span style="color: #000000; font-weight: bold;">Short</span><span style="color: #003f00;">&#91;</span>b<span style="color: #003f00;">&#93;</span> <span style="color: #003f00;">&#41;</span>
                <span style="color: #003f00;">&#123;</span>
                    y1 = ShortPrice<span style="color: #003f00;">&#91;</span>b<span style="color: #003f00;">&#93;</span>;
                    Color = Longcolor;
                <span style="color: #003f00;">&#125;</span>
&nbsp;
            <span style="color: #7f0000; ">if</span><span style="color: #003f00;">&#40;</span> SigNum<span style="color: #003f00;">&#91;</span>b<span style="color: #003f00;">&#93;</span> &gt; <span style="color: #7f0000;">1</span> <span style="color: #003f00;">&#41;</span> <span style="color: #0000ff;">Plot</span><span style="color: #003f00;">&#40;</span> <span style="color: #0000ff;">LineArray</span><span style="color: #003f00;">&#40;</span> x0, x1, y0, y1 <span style="color: #003f00;">&#41;</span>, <span style="color: #7f007f;">&quot;&quot;</span>, Color, <span style="color: #7f0000;">1</span> | styleThick <span style="color: #003f00;">&#41;</span>;
        <span style="color: #003f00;">&#125;</span>
    <span style="color: #003f00;">&#125;</span>
<span style="color: #003f00;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.amibroker.org/userkb/2011/08/25/bollinger-band-zigzag-indicator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Subliminal Intraday Trading Tool</title>
		<link>http://www.amibroker.org/userkb/2011/07/22/a-subliminal-intraday-trading-tool/</link>
		<comments>http://www.amibroker.org/userkb/2011/07/22/a-subliminal-intraday-trading-tool/#comments</comments>
		<pubDate>Fri, 22 Jul 2011 23:04:10 +0000</pubDate>
		<dc:creator>Herman</dc:creator>
				<category><![CDATA[Trading Tools]]></category>

		<guid isPermaLink="false">http://www.amibroker.org/userkb/?p=3240</guid>
		<description><![CDATA[Subliminal Messaging Subliminal messaging involves presenting messages below perceptible visual and auditory levels. Visual messages are displayed for durations too short to be perceived consciously, but long enough to be perceived subconsciously. Auditory subliminal messages are presented with a volume level too low to be heard consciously, but high enough to be heard subconsciously. The [...]]]></description>
			<content:encoded><![CDATA[<h3>Subliminal Messaging</h3>
<p>Subliminal messaging involves presenting messages below perceptible visual and auditory levels. Visual messages are displayed for durations too short to be perceived consciously, but long enough to be perceived subconsciously. Auditory subliminal messages are presented with a volume level too low to be heard consciously, but high enough to be heard subconsciously. The assumption is that exposing your subconsciously mind to a repetitive subliminal message, for example &#8220;BE HAPPY&#8221;, will eventually influence your mood and make you a happier person. The program presented here can present perceptible as well as subliminal messages, audibly and visually, with the aim of improving your trading skills.To read more about Subliminal Messaging visit <a href="http://en.wikipedia.org/wiki/Subliminal_stimuli">WikiPedia-Subliminal stimuli</a>.</p>
<h3>The purpose of this tool</h3>
<p>It has happened to all of us that we place an order only to realize too late that we forgot to check some critical indicator or parameter. Haphazard chart inspections and random system checks result in a lack of confidence, is stressful, delays your action, leads to errors, and will eventually cost you money. </p>
<p>If you are always fully aware of all factors that influence your trading decisions you will trade with greater confidence, greater expediency, less stress, and hopefully with greater profitability. </p>
<p>It is hoped that the tool presented here can help you develop such professional trading habits. </p>
<h3>How to use this tool</h3>
<p>First you create a checklist of all the tasks you should remember to do but that you are likely to forget. Messages are entered in csv format using a ParamStr(). Use commas to separate messages. Messages should be as short as possible, make them only as long as is needed to make you remember the tasks to perform. </p>
<p>The program reads messages from this checklist and displays them, or reads them to you, at periodic intervals. Messages can be presented sequentially or randomly. To learn you must comply with each prompt. </p>
<p>You can buy similar programs and they seem to work. It seems logical that if you routinely perform the tasks prompted for, these will eventually become habitual. Good practice is, when you get a prompt, to take note if you are already aware of the condition prompted for. This will give you an idea of how good or bad your trading habits are. Eventually you should already know all conditions you are prompted for.</p>
<p>Try to keep the list moderately short so that there is not too much time between the same message. Place the visual prompts at a location where you most often look at your chart, that way they are less of a distraction and can be read more easily and quickly. Use font properties that lets you read the messages at a glance. You should periodically go over the checklist and remove/replace prompts you no longer need. </p>
<h3>Technical considerations</h3>
<p>To experiment with subliminal sound levels you can set your speaker volume to a barely audible level (at very close range) and then gradually increase their distance until the sound level becomes imperceptible. There are no technical limitation with respect to subliminal audio messaging.</p>
<p>If you have enabled a timed refresh rate of 0.1 second you can obtain a guaranteed minimum message duration of 100 milliseconds. This may be brief but this is not subliminal. If you trade high liquidity instruments and set Preferences -> Intraday -> Real-time chart refresh interval to zero, you might be able to further reduce the message display time and reach subliminal durations. </p>
<p>You can adjust text and background colors in the parameter window. To make messages appear more subliminal, or less obtrusive, you can reduce their contrast with the background. You can do this by setting the text-color to your background-color, clicking &#8216;more&#8217; at the bottom of the color menu, and then use the color slider to shift the color to select the desired contrast. Here is an example of a &#8220;soft&#8221; message.</p>
<div align="center">
<a href="http://www.amibroker.org/userkb/wp-content/uploads/2011/07/SubMessage1.png"><img src="http://www.amibroker.org/userkb/wp-content/uploads/2011/07/SubMessage1.png" alt="" title="SubMessage" width="547" height="264" class="aligncenter size-full wp-image-3250" /></a></p>
<div align="left">
<p>Since I prefer conscious learning, I didn&#8217;t spend much time trying to obtain true subliminal performance.  However, I added adjustable parameters so that you can experiment with both subliminal and perceptible messaging.</p>
<h3>Typical messages</h3>
<p>Below are a few arbitrary conditions you might want to check routinely before placing an order. Messages are in upper case, please substitute you own. </p>
<ol>
Are we in a Squeeze? BB SQUEEZE.<br />
What is the BB range? BB RANGE<br />
Do we have a BB breakout? BB BREAKOUT<br />
Does the price move have volume support? VOLUME<br />
Did we have an opening gap? OPEN GAP<br />
Where are we wrt the Open price? OPEN PRICE<br />
Where are we wrt the previous Close? PREVIOUS CLOSE<br />
Where are we wrt the dail Hi/Lo? DAILY HIGH LOW<br />
What is the daily trend? DAILY TREND<br />
What is the intraday trend? INTRADAY TREND<br />
What is the current trade profit? TRADE PROFIT<br />
What is the current DD? TRADE DRAWDOWN<br />
What is the trading pattern for this time of day? TIME OF DAY<br />
How long are we in the trade? TRADE DURATION
</ol>
<p>Make the message as short as possible, all it needs to do is trigger your memory. You&#8217;ll have to create your own list that meets the requirements of your own trading style and system. When a message appears it reminds you to check a condition: you should always comply with the prompt. </p>
<h3>Features you can add</h3>
<p>This is just a starter program for you to use as a base for further experimentation. Possible features you might like to add:</p>
<ol>
Using a dynamic checklist, i.e., you add and remove items depending on market conditions, or depending on system or position status (short list are more effective).<br />
Use text colors to indicate the importance of checklist items.<br />
A button to remove the last item displayed from the checklist.<br />
Beginner and advanced lists.
</ol>
<h3>Programming notes</h3>
<p>The program selects messages randomly from a file and displays them briefly for 0-2000 milliseconds, at intervals of 5-60 seconds. You can adjust the following settings in the Parameter window;</p>
<div align="center">
<a href="http://www.amibroker.org/userkb/wp-content/uploads/2011/07/SubTrainerParamWindow.png"><img src="http://www.amibroker.org/userkb/wp-content/uploads/2011/07/SubTrainerParamWindow.png" alt="" title="SubTrainerParamWindow" width="342" height="381" class="aligncenter size-full wp-image-3316" /></a></p>
<div align="left">

<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
</pre></td><td class="code"><pre class="afl"><span style="color: #007f00; ">// SubTrainer.afl</span>
<span style="color: #007f00; ">// You may want to save this formula in an include file so that you can </span>
<span style="color: #007f00; ">// easily append it to your system.</span>
RequestTimedRefresh<span style="color: #003f00;">&#40;</span> <span style="color: #7f0000;">0.1</span> <span style="color: #003f00;">&#41;</span>;
pxchartwidth = <span style="color: #0000ff;">Status</span><span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;pxchartwidth&quot;</span> <span style="color: #003f00;">&#41;</span>;
pxchartheight = <span style="color: #0000ff;">Status</span><span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;pxchartheight&quot;</span> <span style="color: #003f00;">&#41;</span>;
&nbsp;
PresMode = ParamList<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Presentation&quot;</span>, <span style="color: #7f007f;">&quot;VISUAL|AUDIBLE|VISUAL AND AUDIBLE&quot;</span>, <span style="color: #7f0000;">0</span><span style="color: #003f00;">&#41;</span>;
MsgDuration = Max<span style="color: #003f00;">&#40;</span> <span style="color: #7f0000;">1</span>, Param<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Display duration (mSec)&quot;</span>, <span style="color: #7f0000;">200</span>, <span style="color: #7f0000;">0</span>, <span style="color: #7f0000;">2000</span>, <span style="color: #7f0000;">10</span> <span style="color: #003f00;">&#41;</span> <span style="color: #003f00;">&#41;</span>;
MsgInterval = Param<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Display Interval (Sec)&quot;</span>, <span style="color: #7f0000;">10</span>, <span style="color: #7f0000;">1</span>, <span style="color: #7f0000;">30</span>, <span style="color: #7f0000;">0.5</span> <span style="color: #003f00;">&#41;</span> * <span style="color: #7f0000;">1000</span>;
RandomMode = ParamToggle<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Display Mode&quot;</span>, <span style="color: #7f007f;">&quot;SEQUENTIAL,RANDOM&quot;</span>, <span style="color: #7f0000;">1</span> <span style="color: #003f00;">&#41;</span>;
FontSize = Param<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Font size&quot;</span>, <span style="color: #7f0000;">36</span>, <span style="color: #7f0000;">10</span>, <span style="color: #7f0000;">112</span>, <span style="color: #7f0000;">1</span> <span style="color: #003f00;">&#41;</span>;
FontName = ParamStr<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Font Name&quot;</span>, <span style="color: #7f007f;">&quot;Lucida Console&quot;</span> <span style="color: #003f00;">&#41;</span>;
TextColor = ParamColor<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Text&quot;</span>, colorBlack <span style="color: #003f00;">&#41;</span>;
BackColor = ParamColor<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Background&quot;</span>, colorWhite <span style="color: #003f00;">&#41;</span>;
OverLayMode = Param<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Overlay Mode&quot;</span>, <span style="color: #7f0000;">1</span>, <span style="color: #7f0000;">0</span>, <span style="color: #7f0000;">2</span>, <span style="color: #7f0000;">1</span> <span style="color: #003f00;">&#41;</span>;
BackMode = Param<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Background Mode&quot;</span>, <span style="color: #7f0000;">1</span>, <span style="color: #7f0000;">1</span>, <span style="color: #7f0000;">2</span>, <span style="color: #7f0000;">1</span> <span style="color: #003f00;">&#41;</span>;
XPosition = Param<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;x-Position&quot;</span>, <span style="color: #7f0000;">500</span>, <span style="color: #7f0000;">0</span>, <span style="color: #7f0000;">3000</span>, <span style="color: #7f0000;">1</span> <span style="color: #003f00;">&#41;</span>;
YPosition = Param<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;y-Position&quot;</span>, <span style="color: #7f0000;">300</span>, <span style="color: #7f0000;">0</span>, <span style="color: #7f0000;">3000</span>, <span style="color: #7f0000;">1</span> <span style="color: #003f00;">&#41;</span>;
&nbsp;
CheckList =
    <span style="color: #7f007f;">&quot;BB SQUEEZE,BB RANGE,BB BREAKOUT,VOLUME,OPEN GAP,OPEN PRICE,PREV CLOSE,&quot;</span> +
    <span style="color: #7f007f;">&quot;DAILY HIGH LOW,DAILY TREND,INTRADAY TREND,TRADE PROFIT,TRADE DRAWDOWN,TIME OF DAY,TRADE DURATION,&quot;</span>; <span style="color: #007f00; ">// end with comma</span>
Msgs = ParamStr<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;CSV Message List&quot;</span>, CheckList <span style="color: #003f00;">&#41;</span>;
NumMsgs = StrCount<span style="color: #003f00;">&#40;</span> CheckList, <span style="color: #7f007f;">&quot;,&quot;</span> <span style="color: #003f00;">&#41;</span>;
&nbsp;
HRCount = GetPerformanceCounter<span style="color: #003f00;">&#40;</span> <span style="color: #000000; font-weight: bold;">False</span> <span style="color: #003f00;">&#41;</span>;
MsgTime = Nz<span style="color: #003f00;">&#40;</span> StaticVarGet<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;MsgTime&quot;</span> <span style="color: #003f00;">&#41;</span><span style="color: #003f00;">&#41;</span>;
<span style="color: #7f0000; ">if</span><span style="color: #003f00;">&#40;</span> HRCount &gt; MsgTime<span style="color: #003f00;">&#41;</span> 
<span style="color: #003f00;">&#123;</span>
    StaticVarSet<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;MsgTime&quot;</span>, HRCount+MsgInterval <span style="color: #003f00;">&#41;</span>;
    StaticVarSet<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;EndMsgTime&quot;</span>, HRCount+MsgDuration <span style="color: #003f00;">&#41;</span>;
    <span style="color: #7f0000; ">if</span><span style="color: #003f00;">&#40;</span> RandomMode <span style="color: #003f00;">&#41;</span> 
    <span style="color: #003f00;">&#123;</span>
        PrevMsgIndex = StaticVarGet<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;PrevMsgIndex&quot;</span> <span style="color: #003f00;">&#41;</span>;
        MsgIndex = mtRandom<span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</span> * NumMsgs;
        <span style="color: #7f0000; ">while</span><span style="color: #003f00;">&#40;</span> PrevMsgIndex == MsgIndex <span style="color: #003f00;">&#41;</span> <span style="color: #007f00; ">// never repeat the same index</span>
            MsgIndex = mtRandom<span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</span> * NumMsgs;
&nbsp;
    <span style="color: #003f00;">&#125;</span>
    <span style="color: #7f0000; ">else</span>
    <span style="color: #003f00;">&#123;</span>
        MsgIndex = Nz<span style="color: #003f00;">&#40;</span>StaticVarGet<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;MsgIndex&quot;</span> <span style="color: #003f00;">&#41;</span><span style="color: #003f00;">&#41;</span> + <span style="color: #7f0000;">1</span>;
        <span style="color: #7f0000; ">if</span><span style="color: #003f00;">&#40;</span> MsgIndex &gt;= NumMsgs <span style="color: #003f00;">&#41;</span> MsgIndex = <span style="color: #7f0000;">0</span>;
    <span style="color: #003f00;">&#125;</span> 
    StaticVarSet<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;MsgIndex&quot;</span>, MsgIndex <span style="color: #003f00;">&#41;</span>;
<span style="color: #003f00;">&#125;</span>
&nbsp;
<span style="color: #7f0000; ">if</span> <span style="color: #003f00;">&#40;</span> HRCount &lt; StaticVarGet<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;EndMsgTime&quot;</span> <span style="color: #003f00;">&#41;</span> <span style="color: #003f00;">&#41;</span>
<span style="color: #003f00;">&#123;</span>
    MsgIndex = StaticVarGet<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;MsgIndex&quot;</span> <span style="color: #003f00;">&#41;</span>;
    Msg = StrExtract<span style="color: #003f00;">&#40;</span> CheckList, MsgIndex <span style="color: #003f00;">&#41;</span>;
&nbsp;
    <span style="color: #7f0000; ">if</span> <span style="color: #003f00;">&#40;</span> StrFind<span style="color: #003f00;">&#40;</span> PresMode, <span style="color: #7f007f;">&quot;VISUAL&quot;</span> <span style="color: #003f00;">&#41;</span> <span style="color: #003f00;">&#41;</span>
    <span style="color: #003f00;">&#123;</span>
        GfxSelectFont<span style="color: #003f00;">&#40;</span> FontName, FontSize <span style="color: #003f00;">&#41;</span>;
        GfxSetTextAlign<span style="color: #003f00;">&#40;</span> <span style="color: #7f0000;">2</span> <span style="color: #003f00;">&#41;</span>;<span style="color: #007f00; ">// center alignment</span>
        GfxSetTextColor<span style="color: #003f00;">&#40;</span> TextColor <span style="color: #003f00;">&#41;</span>;
        GfxSetBkMode<span style="color: #003f00;">&#40;</span> BackMode <span style="color: #003f00;">&#41;</span>; <span style="color: #007f00; ">// 1=transparent, 2=Opaque</span>
        GfxSetOverlayMode<span style="color: #003f00;">&#40;</span> OverlayMode <span style="color: #003f00;">&#41;</span>; <span style="color: #007f00; ">// 0=Top, 1=under, only</span>
        GfxSetBkColor<span style="color: #003f00;">&#40;</span> BackColor <span style="color: #003f00;">&#41;</span>;
        GfxTextOut<span style="color: #003f00;">&#40;</span> Msg, XPosition, YPosition <span style="color: #003f00;">&#41;</span>;
    <span style="color: #003f00;">&#125;</span>
&nbsp;
    <span style="color: #7f0000; ">if</span> <span style="color: #003f00;">&#40;</span> StrFind<span style="color: #003f00;">&#40;</span> PresMode, <span style="color: #7f007f;">&quot;AUDIBLE&quot;</span> <span style="color: #003f00;">&#41;</span> <span style="color: #003f00;">&#41;</span>
    <span style="color: #003f00;">&#123;</span>
        <span style="color: #7f0000; ">if</span> <span style="color: #003f00;">&#40;</span> StaticVarGetText<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;LastMessage&quot;</span> <span style="color: #003f00;">&#41;</span> != Msg <span style="color: #003f00;">&#41;</span> <span style="color: #007f00; ">// Say only once</span>
        <span style="color: #003f00;">&#123;</span>
            Say<span style="color: #003f00;">&#40;</span> Msg, <span style="color: #000000; font-weight: bold;">True</span> <span style="color: #003f00;">&#41;</span>;
            StaticVarSetText<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;LastMessage&quot;</span>, Msg <span style="color: #003f00;">&#41;</span>;
        <span style="color: #003f00;">&#125;</span>
    <span style="color: #003f00;">&#125;</span>
<span style="color: #003f00;">&#125;</span>
&nbsp;
Title = <span style="color: #7f007f;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> + CheckList; <span style="color: #007f00; ">// For debug only</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.amibroker.org/userkb/2011/07/22/a-subliminal-intraday-trading-tool/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Using IIF(), IF() and Switch() functions</title>
		<link>http://www.amibroker.org/userkb/2011/04/22/using-iif-if-and-switch-functions/</link>
		<comments>http://www.amibroker.org/userkb/2011/04/22/using-iif-if-and-switch-functions/#comments</comments>
		<pubDate>Fri, 22 Apr 2011 10:20:26 +0000</pubDate>
		<dc:creator>Herman</dc:creator>
				<category><![CDATA[AFL - The Basics]]></category>

		<guid isPermaLink="false">http://www.amibroker.org/userkb/?p=2210</guid>
		<description><![CDATA[Many newcomers to AFL are confused by the IF(), IIF() and Switch(). This post gives a few simple examples of their use. The IF() and Switch() are program flow control statements, the IIF() is a function that acts on all elements of an input array and returns an output array. In all but the simplest [...]]]></description>
			<content:encoded><![CDATA[<p>Many newcomers to AFL are confused by the IF(), IIF() and Switch(). This post gives a few simple examples of their use. The IF() and Switch() are program flow control statements, the IIF() is a function that acts on all elements of an input array and returns an output array. </p>
<p>In all but the simplest applications the Switch() is the preferred method to the IF() to change program flow. It can be used to code complex <a href="http://en.wikipedia.org/wiki/Decision_tree">decision trees</a> and <a href="http://en.wikipedia.org/wiki/Finite-state_machine">state machines</a>, for example as these are often needed in automated trading systems.</p>
<p>For more detailed explanations click <a href="http://en.wikipedia.org/wiki/If_statement">IF()</a>, <a href="http://amibroker.com/guide/afl/afl_view.php?id=72">IIF()</a>, or <a href="http://en.wikipedia.org/wiki/Switch_statement">Switch()</a>. A search of the <a href="http://www.amibroker.com/members/library/list.php">afl library</a> will also get you many more examples.</p>
<h3>The IIF() function</h3>
<p>It is possible to use if()s to individually test and modify each bar in an array for a condition. An example on how this would be done is shown in the function below (copied from the AmiBroker help). This function is an AFL equivalent for the IIF() function.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="afl"><span style="color: #7f0000; ">function</span> IIF_AFL<span style="color: #003f00;">&#40;</span> condition, inputA, inputB <span style="color: #003f00;">&#41;</span>
<span style="color: #003f00;">&#123;</span>
    result = <span style="color: #000000; font-weight: bold;">Null</span>;
    <span style="color: #7f0000; ">for</span><span style="color: #003f00;">&#40;</span> bar = <span style="color: #7f0000;">0</span>; bar &lt; BarCount; bar++ <span style="color: #003f00;">&#41;</span>
    <span style="color: #003f00;">&#123;</span>
       <span style="color: #7f0000; ">if</span><span style="color: #003f00;">&#40;</span> condition<span style="color: #003f00;">&#91;</span> bar <span style="color: #003f00;">&#93;</span> <span style="color: #003f00;">&#41;</span>
           result<span style="color: #003f00;">&#91;</span> bar <span style="color: #003f00;">&#93;</span> = inputA<span style="color: #003f00;">&#91;</span> bar <span style="color: #003f00;">&#93;</span>;
       <span style="color: #7f0000; ">else</span>
           result<span style="color: #003f00;">&#91;</span> bar <span style="color: #003f00;">&#93;</span> = inputB<span style="color: #003f00;">&#91;</span> bar <span style="color: #003f00;">&#93;</span>;
     <span style="color: #003f00;">&#125;</span>
    <span style="color: #7f0000; ">return</span> result;
<span style="color: #003f00;">&#125;</span></pre></td></tr></table></div>

<p>While the above approach works, using the IIF() function always provides a better and faster solution. The IIF() is very powerful and should be used whenever possible. Below are a few simple examples to get started. btw, It is highly unlikely that you will be able to improve on execution time by using a loop or writing a DLL. </p>
<p>To color all bars that fall on a Monday White:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="afl">Color = <span style="color: #0000ff;">IIf</span><span style="color: #003f00;">&#40;</span> DayOfWeek<span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</span>==<span style="color: #7f0000;">1</span>, colorWhite, colorBlack<span style="color: #003f00;">&#41;</span>;
<span style="color: #0000ff;">Plot</span><span style="color: #003f00;">&#40;</span> C, <span style="color: #7f007f;">&quot;Close&quot;</span>, color, styleBar <span style="color: #003f00;">&#41;</span>;</pre></td></tr></table></div>

<p>IIF()s Can be nested. This example colors Monday bars White, Wednesday bars Blue and Friday bars Yellow:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="afl">D = DayOfWeek<span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</span>;
Color = <span style="color: #0000ff;">IIf</span><span style="color: #003f00;">&#40;</span>D==<span style="color: #7f0000;">1</span>, colorWhite, <span style="color: #0000ff;">IIf</span><span style="color: #003f00;">&#40;</span>D==<span style="color: #7f0000;">3</span>, colorBlue, <span style="color: #0000ff;">IIf</span><span style="color: #003f00;">&#40;</span>D==<span style="color: #7f0000;">5</span>, colorYellow, colorBlack<span style="color: #003f00;">&#41;</span><span style="color: #003f00;">&#41;</span><span style="color: #003f00;">&#41;</span>;
<span style="color: #0000ff;">Plot</span><span style="color: #003f00;">&#40;</span> C, <span style="color: #7f007f;">&quot;Close&quot;</span>, color, styleBar <span style="color: #003f00;">&#41;</span>;</pre></td></tr></table></div>

<h3>The IF() Statement</h3>
<p>One of the most common applications for the if() is to select what you want to see on your chart:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="afl">&nbsp;
<span style="color: #0000ff;">Plot</span><span style="color: #003f00;">&#40;</span> C, <span style="color: #7f007f;">&quot;Close&quot;</span>, colorBlack, styleBar <span style="color: #003f00;">&#41;</span>;
ShowMA10 = ParamToggle<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Moving Average&quot;</span>, <span style="color: #7f007f;">&quot;MA|EMA&quot;</span>, <span style="color: #7f0000;">0</span> <span style="color: #003f00;">&#41;</span>;
&nbsp;
<span style="color: #7f0000; ">if</span> <span style="color: #003f00;">&#40;</span> ShowMA10 <span style="color: #003f00;">&#41;</span>
<span style="color: #003f00;">&#123;</span>
<span style="color: #0000ff;">Plot</span><span style="color: #003f00;">&#40;</span> <span style="color: #0000ff;">MA</span><span style="color: #003f00;">&#40;</span> C, <span style="color: #7f0000;">10</span> <span style="color: #003f00;">&#41;</span>, <span style="color: #7f007f;">&quot;MA10&quot;</span>, colorWhite, styleLine <span style="color: #003f00;">&#41;</span>;
<span style="color: #003f00;">&#125;</span>
<span style="color: #7f0000; ">else</span>
<span style="color: #003f00;">&#123;</span>
<span style="color: #0000ff;">Plot</span><span style="color: #003f00;">&#40;</span> <span style="color: #0000ff;">EMA</span><span style="color: #003f00;">&#40;</span> C, <span style="color: #7f0000;">10</span> <span style="color: #003f00;">&#41;</span>, <span style="color: #7f007f;">&quot;MA10&quot;</span>, colorWhite, styleLine <span style="color: #003f00;">&#41;</span>;
<span style="color: #003f00;">&#125;</span></pre></td></tr></table></div>

<p>In the above example the IF() basically selects one of two sections of code.  To select one of many options you could the use the else-if extension:</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
</pre></td><td class="code"><pre class="afl">&nbsp;
SelectedIndicator = ParamList<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Show&quot;</span>, <span style="color: #7f007f;">&quot;MA10,MA50,MA100&quot;</span>, <span style="color: #7f0000;">1</span> <span style="color: #003f00;">&#41;</span>;
&nbsp;
<span style="color: #7f0000; ">if</span> <span style="color: #003f00;">&#40;</span> SelectedIndicator == <span style="color: #7f007f;">&quot;MA10&quot;</span> <span style="color: #003f00;">&#41;</span>
<span style="color: #003f00;">&#123;</span>
<span style="color: #0000ff;">Plot</span><span style="color: #003f00;">&#40;</span> <span style="color: #0000ff;">MA</span><span style="color: #003f00;">&#40;</span> C, <span style="color: #7f0000;">10</span> <span style="color: #003f00;">&#41;</span>, <span style="color: #7f007f;">&quot;MA10&quot;</span>, colorBlue, styleLine <span style="color: #003f00;">&#41;</span>;
<span style="color: #003f00;">&#125;</span>
<span style="color: #7f0000; ">else</span>
<span style="color: #7f0000; ">if</span> <span style="color: #003f00;">&#40;</span> SelectedIndicator == <span style="color: #7f007f;">&quot;MA10&quot;</span> <span style="color: #003f00;">&#41;</span>
<span style="color: #003f00;">&#123;</span>
<span style="color: #0000ff;">Plot</span><span style="color: #003f00;">&#40;</span> <span style="color: #0000ff;">EMA</span><span style="color: #003f00;">&#40;</span> C, <span style="color: #7f0000;">50</span> <span style="color: #003f00;">&#41;</span>, <span style="color: #7f007f;">&quot;MA10&quot;</span>, colorRed, styleLine <span style="color: #003f00;">&#41;</span>;
<span style="color: #003f00;">&#125;</span>
<span style="color: #7f0000; ">else</span>
<span style="color: #7f0000; ">if</span> <span style="color: #003f00;">&#40;</span> SelectedIndicator == <span style="color: #7f007f;">&quot;MA100&quot;</span> <span style="color: #003f00;">&#41;</span>
<span style="color: #003f00;">&#123;</span>
<span style="color: #0000ff;">Plot</span><span style="color: #003f00;">&#40;</span> <span style="color: #0000ff;">EMA</span><span style="color: #003f00;">&#40;</span> C, <span style="color: #7f0000;">100</span> <span style="color: #003f00;">&#41;</span>, <span style="color: #7f007f;">&quot;MA100&quot;</span>, colorYellow, styleLine <span style="color: #003f00;">&#41;</span>;
<span style="color: #003f00;">&#125;</span></pre></td></tr></table></div>

<h3>The Switch() Statement</h3>
<p>When there are many conditions, the lengthy If() expressions can become confusing, difficult to compose, and difficult to modify.  In such cases it is often better to use the Switch() statement. Using a simple Switch() the last example looks much cleaner:</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
</pre></td><td class="code"><pre class="afl">&nbsp;
SelectedIndicator = ParamList<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Show&quot;</span>, <span style="color: #7f007f;">&quot;MA10,MA50,MA100&quot;</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> SelectedIndicator <span style="color: #003f00;">&#41;</span>
<span style="color: #003f00;">&#123;</span>
&nbsp;
<span style="color: #7f0000; ">case</span> <span style="color: #7f007f;">&quot;MA10&quot;</span>:
<span style="color: #0000ff;">Plot</span><span style="color: #003f00;">&#40;</span> <span style="color: #0000ff;">MA</span><span style="color: #003f00;">&#40;</span> C, <span style="color: #7f0000;">10</span> <span style="color: #003f00;">&#41;</span>, <span style="color: #7f007f;">&quot;MA10&quot;</span>, colorBlue, styleLine <span style="color: #003f00;">&#41;</span>;
<span style="color: #7f0000; ">break</span>;
&nbsp;
<span style="color: #7f0000; ">case</span> <span style="color: #7f007f;">&quot;MA10&quot;</span>:
<span style="color: #0000ff;">Plot</span><span style="color: #003f00;">&#40;</span> <span style="color: #0000ff;">MA</span><span style="color: #003f00;">&#40;</span> C, <span style="color: #7f0000;">50</span> <span style="color: #003f00;">&#41;</span>, <span style="color: #7f007f;">&quot;MA50&quot;</span>, colorBlue, styleLine <span style="color: #003f00;">&#41;</span>;
<span style="color: #7f0000; ">break</span>;
&nbsp;
<span style="color: #7f0000; ">case</span> <span style="color: #7f007f;">&quot;MA10&quot;</span>:
<span style="color: #0000ff;">Plot</span><span style="color: #003f00;">&#40;</span> <span style="color: #0000ff;">MA</span><span style="color: #003f00;">&#40;</span> C, <span style="color: #7f0000;">100</span> <span style="color: #003f00;">&#41;</span>, <span style="color: #7f007f;">&quot;MA100&quot;</span>, colorBlue, styleLine <span style="color: #003f00;">&#41;</span>;
<span style="color: #7f0000; ">break</span>;
<span style="color: #003f00;">&#125;</span></pre></td></tr></table></div>

<p>There are times that you will have many individually named variables that you would like to process in a Switch() statement. Even though the Switch() can only accept a single variable name as argument you can use the method below to work around this limitation:</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
</pre></td><td class="code"><pre class="afl"><span style="color: #7f0000; ">function</span> SayOnce<span style="color: #003f00;">&#40;</span> Text <span style="color: #003f00;">&#41;</span>
<span style="color: #003f00;">&#123;</span>
    <span style="color: #7f0000; ">if</span> <span style="color: #003f00;">&#40;</span> StaticVarGetText<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Lastsay&quot;</span> <span style="color: #003f00;">&#41;</span> != Text <span style="color: #003f00;">&#41;</span>
    <span style="color: #003f00;">&#123;</span>
        Say<span style="color: #003f00;">&#40;</span> Text, <span style="color: #000000; font-weight: bold;">False</span> <span style="color: #003f00;">&#41;</span>;
        StaticVarSetText<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;LastSay&quot;</span>, Text <span style="color: #003f00;">&#41;</span>;
    <span style="color: #003f00;">&#125;</span>
<span style="color: #003f00;">&#125;</span>
&nbsp;
RequestTimedRefresh<span style="color: #003f00;">&#40;</span><span style="color: #7f0000;">1</span><span style="color: #003f00;">&#41;</span>;
Trigger1 = ParamTrigger<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Trigger 1&quot;</span>, <span style="color: #7f007f;">&quot;TRIGGER1&quot;</span> <span style="color: #003f00;">&#41;</span>;
Trigger2 = ParamTrigger<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Trigger 2&quot;</span>, <span style="color: #7f007f;">&quot;TRIGGER2&quot;</span> <span style="color: #003f00;">&#41;</span>;
Trigger3 = ParamTrigger<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Trigger 3&quot;</span>, <span style="color: #7f007f;">&quot;TRIGGER3&quot;</span> <span style="color: #003f00;">&#41;</span>;
Trigger4 = ParamTrigger<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Trigger 4&quot;</span>, <span style="color: #7f007f;">&quot;TRIGGER4&quot;</span> <span style="color: #003f00;">&#41;</span>;
&nbsp;
<span style="color: #7f0000; ">switch</span><span style="color: #003f00;">&#40;</span> <span style="color: #7f0000;">1</span> <span style="color: #003f00;">&#41;</span>
<span style="color: #003f00;">&#123;</span>
    <span style="color: #7f0000; ">case</span> Trigger1:
    SayOnce<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;One&quot;</span> <span style="color: #003f00;">&#41;</span>;
    <span style="color: #7f0000; ">break</span>;
&nbsp;
    <span style="color: #7f0000; ">case</span> Trigger2:
    <span style="color: #7f0000; ">case</span> Trigger4:
    SayOnce<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;2 or 4&quot;</span> <span style="color: #003f00;">&#41;</span>;
    <span style="color: #7f0000; ">break</span>;
&nbsp;
    <span style="color: #7f0000; ">case</span> Trigger3:
    SayOnce<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Three&quot;</span> <span style="color: #003f00;">&#41;</span>;
    <span style="color: #7f0000; ">break</span>;
&nbsp;
    <span style="color: #7f0000; ">default</span>:
    SayOnce<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Default&quot;</span> <span style="color: #003f00;">&#41;</span>;
    <span style="color: #007f00; ">// Here you can place code that will execute</span>
    <span style="color: #007f00; ">// repeatedly while no other case is true</span>
<span style="color: #003f00;">&#125;</span></pre></td></tr></table></div>

<p>The Switch() argument can be a string or number. Using strings makes code easier to read. Another advantage of using the Switch() is that they format nicely using Edit-&gt;Prettify Selection in you editor window, using too many else-if statements tends to run the if()s of the page. As shown above you can stack case statements to have multiple conditions trigger the same task. </p>
<p>To implement a simple State Machine you pass the system &#8220;state&#8221; to the Switch(). This way you can have any event trigger any sequence of tasks, and do so in any desired order. In a real application the SayOnce() functions in the example code below would be replaced by the task you want to be performed in the state. The next state would usually be conditionally set inside each state, for example you only want to proceed to the next state after an order is filled, or a price is crossed. You can use multilevel Switch()s or if()s inside each case section. This use of Switch() statements is very useful in Automated Trading systems. For example to process order status (Pending, Filled, Error, etc) and parsing TWS error messages. </p>
<p>Since states are saved in a Static Variables they remain valid over multiple AFL executions, and can last indefinitely. You can also save states in Persistent Variables.</p>
<p>States are processed in sequential afl executions, i.e., if you change the state in a case statement this next state will be processed in the next AFL refresh. In some applications this delay can cause problems. To ensure responsive code you might want to use a 0.1 second refresh rate. You could remove the delay by using the Switch() inside a loop/while statement, anf loop until a stable state is reached.</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
</pre></td><td class="code"><pre class="afl"><span style="color: #7f0000; ">function</span> SayOnce<span style="color: #003f00;">&#40;</span> Text <span style="color: #003f00;">&#41;</span>
<span style="color: #003f00;">&#123;</span>
    <span style="color: #7f0000; ">if</span> <span style="color: #003f00;">&#40;</span> StaticVarGetText<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Lastsay&quot;</span> <span style="color: #003f00;">&#41;</span> != Text <span style="color: #003f00;">&#41;</span>
    <span style="color: #003f00;">&#123;</span>
        Say<span style="color: #003f00;">&#40;</span> Text, <span style="color: #000000; font-weight: bold;">False</span> <span style="color: #003f00;">&#41;</span>;
        StaticVarSetText<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;LastSay&quot;</span>, Text <span style="color: #003f00;">&#41;</span>;
    <span style="color: #003f00;">&#125;</span>
<span style="color: #003f00;">&#125;</span>
&nbsp;
RequestTimedRefresh<span style="color: #003f00;">&#40;</span><span style="color: #7f0000;">1</span><span style="color: #003f00;">&#41;</span>;
<span style="color: #7f0000; ">if</span><span style="color: #003f00;">&#40;</span> ParamTrigger<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Reset System&quot;</span>, <span style="color: #7f007f;">&quot;RESET&quot;</span> <span style="color: #003f00;">&#41;</span> <span style="color: #003f00;">&#41;</span> StaticVarSetText<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;State&quot;</span>, <span style="color: #7f007f;">&quot;RESET&quot;</span> <span style="color: #003f00;">&#41;</span>;
<span style="color: #7f0000; ">if</span><span style="color: #003f00;">&#40;</span> ParamTrigger<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Task 1&quot;</span>, <span style="color: #7f007f;">&quot;TASK 1&quot;</span> <span style="color: #003f00;">&#41;</span> <span style="color: #003f00;">&#41;</span> StaticVarSetText<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;State&quot;</span>, <span style="color: #7f007f;">&quot;TASK1&quot;</span> <span style="color: #003f00;">&#41;</span>;
<span style="color: #7f0000; ">if</span><span style="color: #003f00;">&#40;</span> ParamTrigger<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Task 2&quot;</span>, <span style="color: #7f007f;">&quot;TASK 2&quot;</span> <span style="color: #003f00;">&#41;</span> <span style="color: #003f00;">&#41;</span> StaticVarSetText<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;State&quot;</span>, <span style="color: #7f007f;">&quot;TASK2&quot;</span> <span style="color: #003f00;">&#41;</span>;
<span style="color: #7f0000; ">if</span><span style="color: #003f00;">&#40;</span> ParamTrigger<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Task 3&quot;</span>, <span style="color: #7f007f;">&quot;TASK 3&quot;</span> <span style="color: #003f00;">&#41;</span> <span style="color: #003f00;">&#41;</span> StaticVarSetText<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;State&quot;</span>, <span style="color: #7f007f;">&quot;TASK3&quot;</span> <span style="color: #003f00;">&#41;</span>;
&nbsp;
State = StaticVarGetText<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;State&quot;</span> <span style="color: #003f00;">&#41;</span>;
<span style="color: #7f0000; ">switch</span><span style="color: #003f00;">&#40;</span> State <span style="color: #003f00;">&#41;</span>
<span style="color: #003f00;">&#123;</span>
    <span style="color: #7f0000; ">case</span> <span style="color: #7f007f;">&quot;RESET&quot;</span>:
    SayOnce<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Reset&quot;</span> <span style="color: #003f00;">&#41;</span>;
    StaticVarSetText<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;State&quot;</span>, <span style="color: #7f007f;">&quot;READY&quot;</span> <span style="color: #003f00;">&#41;</span>;
    <span style="color: #7f0000; ">break</span>;
&nbsp;
    <span style="color: #7f0000; ">case</span> <span style="color: #7f007f;">&quot;READY&quot;</span>:
    SayOnce<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Ready&quot;</span> <span style="color: #003f00;">&#41;</span>;
    StaticVarSetText<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;State&quot;</span>, <span style="color: #7f007f;">&quot;IDLE&quot;</span> <span style="color: #003f00;">&#41;</span>;
    <span style="color: #7f0000; ">break</span>;
&nbsp;
    <span style="color: #7f0000; ">case</span> <span style="color: #7f007f;">&quot;TASK1&quot;</span>:
    SayOnce<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Task 1&quot;</span> <span style="color: #003f00;">&#41;</span>;
    StaticVarSetText<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;State&quot;</span>, <span style="color: #7f007f;">&quot;IDLE&quot;</span> <span style="color: #003f00;">&#41;</span>;
    <span style="color: #7f0000; ">break</span>;
&nbsp;
    <span style="color: #7f0000; ">case</span> <span style="color: #7f007f;">&quot;TASK2&quot;</span>:
    SayOnce<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Task 2&quot;</span> <span style="color: #003f00;">&#41;</span>;
    StaticVarSetText<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;State&quot;</span>, <span style="color: #7f007f;">&quot;TASK1&quot;</span> <span style="color: #003f00;">&#41;</span>;
    <span style="color: #7f0000; ">break</span>;
&nbsp;
    <span style="color: #7f0000; ">case</span> <span style="color: #7f007f;">&quot;TASK3&quot;</span>:
    SayOnce<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Task 3&quot;</span> <span style="color: #003f00;">&#41;</span>;
    StaticVarSetText<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;State&quot;</span>, <span style="color: #7f007f;">&quot;TASK2&quot;</span> <span style="color: #003f00;">&#41;</span>;
    <span style="color: #7f0000; ">break</span>;
&nbsp;
    <span style="color: #7f0000; ">case</span> <span style="color: #7f007f;">&quot;IDLE&quot;</span>:
    SayOnce<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Idle&quot;</span> <span style="color: #003f00;">&#41;</span>;
    <span style="color: #7f0000; ">break</span>;
<span style="color: #003f00;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.amibroker.org/userkb/2011/04/22/using-iif-if-and-switch-functions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The TDash GUI, Bar-Replay version</title>
		<link>http://www.amibroker.org/userkb/2011/04/20/the-tdash-gui-first-look/</link>
		<comments>http://www.amibroker.org/userkb/2011/04/20/the-tdash-gui-first-look/#comments</comments>
		<pubDate>Wed, 20 Apr 2011 23:14:08 +0000</pubDate>
		<dc:creator>Herman</dc:creator>
				<category><![CDATA[Developing a RT Trading Dashboard]]></category>

		<guid isPermaLink="false">http://www.amibroker.org/userkb/?p=2812</guid>
		<description><![CDATA[The TDash code can now be used in Bar-Replay, IBc/TWS interfacing will be the next phase. To be worked on: In Bar-Replay you can only place one trade/bar. I don&#8217;t think it is possible to changes this. The Backtester doesn&#8217;t scale in/out as the TWS would. This may be solved later. The code has not [...]]]></description>
			<content:encoded><![CDATA[<p><!-- 14 spaces --><br />
The TDash code can now be used in Bar-Replay, IBc/TWS interfacing will be the next phase.</p>
<p><H2>To be worked on:</H2></p>
<ol>
<li>In Bar-Replay you can only place one trade/bar. I don&#8217;t think it is possible to changes this.</li>
<li>The Backtester doesn&#8217;t scale in/out as the TWS would. This may be solved later.</li>
<li>The code has not been optimized for speed, this may be a problem for some of you. I am using TDash with a 60-day 5-Sec Local DB &#8212; so far no major problems.</li>
<li>Buttons for functions yet to be implemented are grayed-out.</li>
</ol>
<p><H2>Coding level:</H2><br />
This is an advanced afl project. While this code can be used as is, it is intended for the experienced programmer who may just borrow some ideas, functions, or snippets of the code, to create their own trading GUI.<br />
Sorry, I have no time to provide detailed help in using this program.</p>
<p><H2>Demo Video:</H2></p>
<div align="center">
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0" width="856" height="590"><param name=movie value="http://www.amibroker.org/video/TDashProto1/TDashProto1.swf"><param name=quality value=high><embed src="http://www.amibroker.org/video/TDashProto1/TDashProto1.swf" quality=high pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="950" height="600"></embed></object>
</div>
<p><H2>TDash Features:</H2></p>
<ol>
<li>Modify Pending orders by dragging their Markers</li>
<li>Increment/Decrement price, and Cancel pending orders from the markers</li>
<li>Place multiple orders to scale in/out (But Backtester code is not ready for scaling yet)</li>
<li>A floating order bar (QBar) to place MKT, LMT, and STP orders</li>
<li>When placing a LMT order at the wrong side of the price the order changes to a MKT. This allows you to place MKT and LMT orders without having to change type each time.
<li>The use of composites to log multiple trading sessions for long term Backtesting</li>
<li>3D buttons for Trigger, Toggle, and Rotate functions</li>
<li>Flexibility in custom sizing all objects</li>
<li>Auto-sizing of button layout</li>
<li>Ability to dynamically hide/show buttons and button groups</li>
<li>Use button to control indicators on the main chart</li>
<li>Optional Auto-Backtests when an order is filled to create real-time tradelists and statistics</li>
</ol>
<p><H2>Misc. Notes:</H2></p>
<p>For the arrows to display properly you need to set Preferences -> Intraday -> &#8220;Time of first tick inside bar&#8221; or &#8220;START time of interval&#8221;. Be sure to select REPLAY data by setting the DATA selector in the SETUP button group to REPLAY. </p>
<p>The look of buttons and their layout can be changed in the Parameter window. These parameters are for experimenting, they can be hard-coded when you have decided what you want. To prevent very large buttons when you are re-sizing the TDash pane you can set their maximum height and width in the Parameter window. Button functions have arguments to place buttons groups anywhere in the window/pane.</p>
<p>You can change many other features, like button shape and shadow width, group header height, colors etc. You can make similar adjustments for the QBar and markers. In addition to the settings in the Parameter window the Button3D() function has size, offset, and color arguments you can change from afl. Using these functions you can create dynamically configured button panels, i.e., show/hide buttons or groups, change dimensions and color, to meet virtually any requirement.</p>
<p>At this time there are only three button functions: Trigger, Toggle, and Rotate. The Rotate button is used to select items from a short list. Buttons states are persistent. Grayed-out buttons are not functional yet, or are deactivated from afl because they are not required in the current button layout.</p>
<p>To learn more about buttons, toggle TIPS in the SETUP group to ON, and hover your cursor over the buttons. Except for MKT orders the QBar tracks your vertical cursor position. When MKT type is selected (in the ORDERS button group) the QBar position is fixed at the vertical center of the TDash window and orders will be placed at the last price. LMT@LastPrice or a STP order. For the moment the number of shares traded is set in the parameter window.</p>
<p>STP and LMT prices are monitored during Bar-Replay and fill when the price crosses the threshold. MKT orders will fill immediately. Bracket orders and Pattern orders haven&#8217;t been implemented yet. Pattern orders you probably haven&#8217;t heard of yet: a pattern order can contain a complex order pattern and are very handy when trading fast, it allows you to place any number of orders with a single click. </p>
<p>Markers for pending orders have a point to the left, a Position Marker is pointed at both ends. Order markers can be dragged; incremented, decremented, and canceled from pop-up controls that appear when you hover your cursor to the right of the marker.</p>
<p>Position Markers have a colored line extending onto the main chart that ends at the entry arrow. Inside the TDash window there is a small profit histogram that shows you current trade profit. If this histogram doesn&#8217;t show adjust the X-Offset for Markers to make space. </p>
<p>To run this code you need to apply the <a href='http://www.amibroker.org/userkb/wp-content/uploads/2011/04/TDashMain1041.afl'>TDashMain104.afl</a> to the left top window, the <a href='http://www.amibroker.org/userkb/wp-content/uploads/2011/04/TDash104.afl'>TDash104.afl</a> to the right top window, and <a href='http://www.amibroker.org/userkb/wp-content/uploads/2011/04/TDashInclude104.afl'>TDashInclude104</a> to the indicator pane below the TDash window (the include is only shown for easy access). The left bottom window can be used for other indicators. To run Auto/Manual Backtests you need to copy <a href='http://www.amibroker.org/userkb/wp-content/uploads/2011/04/TDashBacktest.js'>TDashBacktest.js</a> to the \TDash\JS folder in your AmiBroker folder</p>
<p>Eventually the TDash window will be extended to the right (perhaps place or extend it on another monitor) to make room for a collection of custom gfx trading indicators. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.amibroker.org/userkb/2011/04/20/the-tdash-gui-first-look/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Logging and displaying trades</title>
		<link>http://www.amibroker.org/userkb/2011/03/29/using-a-trade-composite/</link>
		<comments>http://www.amibroker.org/userkb/2011/03/29/using-a-trade-composite/#comments</comments>
		<pubDate>Tue, 29 Mar 2011 19:26:34 +0000</pubDate>
		<dc:creator>Herman</dc:creator>
				<category><![CDATA[Developing a RT Trading Dashboard]]></category>

		<guid isPermaLink="false">http://www.amibroker.org/userkb/?p=2842</guid>
		<description><![CDATA[In real trading it is desirable to use a persistent trade record so that trades for multiple sessions be analyzed using the Backtester. The easiest way to do this is to save the trades in a composite. The code shown below is a simplified version of what will be used in the TDash system, however, [...]]]></description>
			<content:encoded><![CDATA[<p>In real trading it is desirable to use a persistent trade record so that trades for multiple sessions be analyzed using the Backtester. The easiest way to do this is to save the trades in a composite. The code shown below is a simplified version of what will be used in the TDash system, however, it can be used with any trading system. This code requires you set Preferences -> Intraday -> &#8220;Time of first tick inside bar&#8221; or &#8220;START time of interval&#8221;. Apply the code to an empty pane, start Bar-Replay, and open the Parameter window. After placing some trades you can send the code to the Backtester for analysis. You can perform the following actions:</p>
<p style="text-align: center;">
<a href="http://www.amibroker.org/userkb/wp-content/uploads/2011/03/TradeComposite.png"><img src="http://www.amibroker.org/userkb/wp-content/uploads/2011/03/TradeComposite.png" alt="" title="TradeComposite" width="337" height="263" class="aligncenter size-full wp-image-2844" /></a>
</p>
<p>I use small solid triangles for Buy and Sell signals, and hollow triangles for Short and Cover signals. This allows me to overlay the Buy and Cover signals, and the Sell and Short signals, to display trade reversals. The triangles are placed at the actual trading price (the Close in this example). This is what the arrows look like:</p>
<p style="text-align: center;">
<a href="http://www.amibroker.org/userkb/wp-content/uploads/2011/03/Arrows.png"><img src="http://www.amibroker.org/userkb/wp-content/uploads/2011/03/Arrows.png" alt="" title="Arrows" width="491" height="193" class="aligncenter size-full wp-image-2850" /></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
100
101
102
</pre></td><td class="code"><pre class="afl">_SECTION_BEGIN<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;USING A TRADE COMPOSITE&quot;</span> <span style="color: #003f00;">&#41;</span>;
<span style="color: #7f0000; ">function</span> DeleteComposite<span style="color: #003f00;">&#40;</span> CompositeName <span style="color: #003f00;">&#41;</span>
<span style="color: #003f00;">&#123;</span>
    <span style="color: #7f0000;">global</span> Ticker;
    oAB = CreateObject<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Broker.Application&quot;</span> <span style="color: #003f00;">&#41;</span>;
    oStocks = oAB.<span style="color: #202020;">Stocks</span><span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</span>;
    oStocks.<span style="color: #202020;">Remove</span><span style="color: #003f00;">&#40;</span> CompositeName <span style="color: #003f00;">&#41;</span>;
    oAB.<span style="color: #202020;">RefreshAll</span><span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</span>;
<span style="color: #003f00;">&#125;</span>
&nbsp;
<span style="color: #7f0000; ">function</span>  AddTradeToComposite<span style="color: #003f00;">&#40;</span> Ticker, Action, LMTPrice <span style="color: #003f00;">&#41;</span>
<span style="color: #003f00;">&#123;</span>
    <span style="color: #7f0000;">global</span> Ticker;
    BI  = BarIndex<span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</span>;
    LBI = LastValue<span style="color: #003f00;">&#40;</span> BI <span style="color: #003f00;">&#41;</span>;
&nbsp;
    <span style="color: #7f0000; ">if</span> <span style="color: #003f00;">&#40;</span> Action != <span style="color: #7f007f;">&quot;&quot;</span> <span style="color: #003f00;">&#41;</span>
    <span style="color: #003f00;">&#123;</span>
        SignalArray = Nz<span style="color: #003f00;">&#40;</span> Foreign<span style="color: #003f00;">&#40;</span> Ticker + <span style="color: #7f007f;">&quot;~SignalArrays&quot;</span>, <span style="color: #7f007f;">&quot;V&quot;</span>, <span style="color: #000000; font-weight: bold;">False</span> <span style="color: #003f00;">&#41;</span> <span style="color: #003f00;">&#41;</span>;
        BuyPriceArray = Nz<span style="color: #003f00;">&#40;</span> Foreign<span style="color: #003f00;">&#40;</span> Ticker + <span style="color: #7f007f;">&quot;~SignalArrays&quot;</span>, <span style="color: #7f007f;">&quot;O&quot;</span>, <span style="color: #000000; font-weight: bold;">False</span> <span style="color: #003f00;">&#41;</span> <span style="color: #003f00;">&#41;</span>;
        SellPriceArray = Nz<span style="color: #003f00;">&#40;</span> Foreign<span style="color: #003f00;">&#40;</span> Ticker + <span style="color: #7f007f;">&quot;~SignalArrays&quot;</span>, <span style="color: #7f007f;">&quot;H&quot;</span>, <span style="color: #000000; font-weight: bold;">False</span> <span style="color: #003f00;">&#41;</span> <span style="color: #003f00;">&#41;</span>;
        ShortPriceArray = Nz<span style="color: #003f00;">&#40;</span> Foreign<span style="color: #003f00;">&#40;</span> Ticker + <span style="color: #7f007f;">&quot;~SignalArrays&quot;</span>, <span style="color: #7f007f;">&quot;L&quot;</span>, <span style="color: #000000; font-weight: bold;">False</span> <span style="color: #003f00;">&#41;</span> <span style="color: #003f00;">&#41;</span>;
        CoverPriceArray = Nz<span style="color: #003f00;">&#40;</span> Foreign<span style="color: #003f00;">&#40;</span> Ticker + <span style="color: #7f007f;">&quot;~SignalArrays&quot;</span>, <span style="color: #7f007f;">&quot;C&quot;</span>, <span style="color: #000000; font-weight: bold;">False</span> <span style="color: #003f00;">&#41;</span> <span style="color: #003f00;">&#41;</span>;
&nbsp;
        <span style="color: #7f0000; ">switch</span> <span style="color: #003f00;">&#40;</span> Action <span style="color: #003f00;">&#41;</span>
        <span style="color: #003f00;">&#123;</span>
&nbsp;
            <span style="color: #7f0000; ">case</span> <span style="color: #7f007f;">&quot;BUY&quot;</span>:
                SignalArray<span style="color: #003f00;">&#91;</span>LBI<span style="color: #003f00;">&#93;</span> = SignalArray<span style="color: #003f00;">&#91;</span>LBI<span style="color: #003f00;">&#93;</span> | <span style="color: #7f0000;">1</span>;
                BuyPriceArray<span style="color: #003f00;">&#91;</span>LBI<span style="color: #003f00;">&#93;</span> = LMTPrice;
                <span style="color: #7f0000; ">break</span>;
&nbsp;
            <span style="color: #7f0000; ">case</span> <span style="color: #7f007f;">&quot;SELL&quot;</span>:
                SignalArray<span style="color: #003f00;">&#91;</span>LBI<span style="color: #003f00;">&#93;</span> = SignalArray<span style="color: #003f00;">&#91;</span>LBI<span style="color: #003f00;">&#93;</span> | <span style="color: #7f0000;">2</span>;
                SellPriceArray<span style="color: #003f00;">&#91;</span>LBI<span style="color: #003f00;">&#93;</span> = LMTPrice;
                <span style="color: #7f0000; ">break</span>;
&nbsp;
            <span style="color: #7f0000; ">case</span> <span style="color: #7f007f;">&quot;SHORT&quot;</span>:
                SignalArray<span style="color: #003f00;">&#91;</span>LBI<span style="color: #003f00;">&#93;</span> = SignalArray<span style="color: #003f00;">&#91;</span>LBI<span style="color: #003f00;">&#93;</span> | <span style="color: #7f0000;">4</span>;
                ShortPriceArray<span style="color: #003f00;">&#91;</span>LBI<span style="color: #003f00;">&#93;</span> = LMTPrice;
                <span style="color: #7f0000; ">break</span>;
&nbsp;
            <span style="color: #7f0000; ">case</span> <span style="color: #7f007f;">&quot;COVER&quot;</span>:
                SignalArray<span style="color: #003f00;">&#91;</span>LBI<span style="color: #003f00;">&#93;</span> = SignalArray<span style="color: #003f00;">&#91;</span>LBI<span style="color: #003f00;">&#93;</span> | <span style="color: #7f0000;">8</span>;
                CoverPriceArray<span style="color: #003f00;">&#91;</span>LBI<span style="color: #003f00;">&#93;</span> = LMTPrice;
                <span style="color: #7f0000; ">break</span>;
&nbsp;
            <span style="color: #7f0000; ">case</span> <span style="color: #7f007f;">&quot;REVLONG&quot;</span>:
                SignalArray<span style="color: #003f00;">&#91;</span>LBI<span style="color: #003f00;">&#93;</span> = SignalArray<span style="color: #003f00;">&#91;</span>LBI<span style="color: #003f00;">&#93;</span> | <span style="color: #7f0000;">8</span> | <span style="color: #7f0000;">1</span>;
                CoverPriceArray<span style="color: #003f00;">&#91;</span>LBI<span style="color: #003f00;">&#93;</span> = BuyPriceArray<span style="color: #003f00;">&#91;</span>LBI<span style="color: #003f00;">&#93;</span> = LMTPrice;
                <span style="color: #7f0000; ">break</span>;
&nbsp;
            <span style="color: #7f0000; ">case</span> <span style="color: #7f007f;">&quot;REVSHORT&quot;</span>:
                SignalArray<span style="color: #003f00;">&#91;</span>LBI<span style="color: #003f00;">&#93;</span> = SignalArray<span style="color: #003f00;">&#91;</span>LBI<span style="color: #003f00;">&#93;</span> | <span style="color: #7f0000;">2</span> | <span style="color: #7f0000;">4</span>;
                SellPriceArray<span style="color: #003f00;">&#91;</span>LBI<span style="color: #003f00;">&#93;</span> = ShortPriceArray<span style="color: #003f00;">&#91;</span>LBI<span style="color: #003f00;">&#93;</span> = LMTPrice;
                <span style="color: #7f0000; ">break</span>;
        <span style="color: #003f00;">&#125;</span>
&nbsp;
        AddToComposite<span style="color: #003f00;">&#40;</span> SignalArray, Ticker + <span style="color: #7f007f;">&quot;~SignalArrays&quot;</span>, <span style="color: #7f007f;">&quot;V&quot;</span>, <span style="color: #7f0000;">7</span> | atcFlagEnableInIndicator <span style="color: #003f00;">&#41;</span>;
        AddToComposite<span style="color: #003f00;">&#40;</span> BuyPriceArray, Ticker + <span style="color: #7f007f;">&quot;~SignalArrays&quot;</span>, <span style="color: #7f007f;">&quot;O&quot;</span>, <span style="color: #7f0000;">7</span> | atcFlagEnableInIndicator <span style="color: #003f00;">&#41;</span>;
        AddToComposite<span style="color: #003f00;">&#40;</span> SellPriceArray, Ticker + <span style="color: #7f007f;">&quot;~SignalArrays&quot;</span>, <span style="color: #7f007f;">&quot;H&quot;</span>, <span style="color: #7f0000;">7</span> | atcFlagEnableInIndicator <span style="color: #003f00;">&#41;</span>;
        AddToComposite<span style="color: #003f00;">&#40;</span> ShortPriceArray, Ticker + <span style="color: #7f007f;">&quot;~SignalArrays&quot;</span>, <span style="color: #7f007f;">&quot;L&quot;</span>, <span style="color: #7f0000;">7</span> | atcFlagEnableInIndicator <span style="color: #003f00;">&#41;</span>;
        AddToComposite<span style="color: #003f00;">&#40;</span> CoverPriceArray, Ticker + <span style="color: #7f007f;">&quot;~SignalArrays&quot;</span>, <span style="color: #7f007f;">&quot;C&quot;</span>, <span style="color: #7f0000;">7</span> | atcFlagEnableInIndicator <span style="color: #003f00;">&#41;</span>;
    <span style="color: #003f00;">&#125;</span>
<span style="color: #003f00;">&#125;</span>
&nbsp;
Ticker = Name<span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</span>;
Action = <span style="color: #7f007f;">&quot;&quot;</span>;
&nbsp;
<span style="color: #7f0000; ">if</span> <span style="color: #003f00;">&#40;</span> ParamTrigger<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Buy&quot;</span>, <span style="color: #7f007f;">&quot;BUY&quot;</span> <span style="color: #003f00;">&#41;</span> <span style="color: #003f00;">&#41;</span> Action = <span style="color: #7f007f;">&quot;BUY&quot;</span>;
<span style="color: #7f0000; ">if</span> <span style="color: #003f00;">&#40;</span> ParamTrigger<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Sell&quot;</span>, <span style="color: #7f007f;">&quot;SELL&quot;</span> <span style="color: #003f00;">&#41;</span> <span style="color: #003f00;">&#41;</span> Action = <span style="color: #7f007f;">&quot;SELL&quot;</span>;
<span style="color: #7f0000; ">if</span> <span style="color: #003f00;">&#40;</span> ParamTrigger<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Short&quot;</span>, <span style="color: #7f007f;">&quot;SHORT&quot;</span> <span style="color: #003f00;">&#41;</span> <span style="color: #003f00;">&#41;</span> Action = <span style="color: #7f007f;">&quot;SHORT&quot;</span>;
<span style="color: #7f0000; ">if</span> <span style="color: #003f00;">&#40;</span> ParamTrigger<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Cover&quot;</span>, <span style="color: #7f007f;">&quot;COVER&quot;</span> <span style="color: #003f00;">&#41;</span> <span style="color: #003f00;">&#41;</span> Action = <span style="color: #7f007f;">&quot;COVER&quot;</span>;
<span style="color: #7f0000; ">if</span> <span style="color: #003f00;">&#40;</span> ParamTrigger<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Reverse to Long&quot;</span>, <span style="color: #7f007f;">&quot;REVLONG&quot;</span> <span style="color: #003f00;">&#41;</span> <span style="color: #003f00;">&#41;</span> Action = <span style="color: #7f007f;">&quot;REVLONG&quot;</span>;
<span style="color: #7f0000; ">if</span> <span style="color: #003f00;">&#40;</span> ParamTrigger<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Reverse to Short&quot;</span>, <span style="color: #7f007f;">&quot;REVSHORT&quot;</span> <span style="color: #003f00;">&#41;</span> <span style="color: #003f00;">&#41;</span> Action = <span style="color: #7f007f;">&quot;REVSHORT&quot;</span>;
<span style="color: #7f0000; ">if</span> <span style="color: #003f00;">&#40;</span> ParamTrigger<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Delete Signal Compoiste&quot;</span>, <span style="color: #7f007f;">&quot;DELETE&quot;</span> <span style="color: #003f00;">&#41;</span> <span style="color: #003f00;">&#41;</span> DeleteComposite<span style="color: #003f00;">&#40;</span> Ticker + <span style="color: #7f007f;">&quot;~SignalArrays&quot;</span> <span style="color: #003f00;">&#41;</span>;
AddTradeToComposite<span style="color: #003f00;">&#40;</span> Ticker, Action, LastValue<span style="color: #003f00;">&#40;</span> Close <span style="color: #003f00;">&#41;</span> <span style="color: #003f00;">&#41;</span>;
&nbsp;
RequestTimedRefresh<span style="color: #003f00;">&#40;</span> <span style="color: #7f0000;">0.1</span> <span style="color: #003f00;">&#41;</span>;
<span style="color: #7f0000; ">if</span> <span style="color: #003f00;">&#40;</span> SetForeign<span style="color: #003f00;">&#40;</span> Ticker + <span style="color: #7f007f;">&quot;~SignalArrays&quot;</span> <span style="color: #003f00;">&#41;</span> <span style="color: #003f00;">&#41;</span>
<span style="color: #003f00;">&#123;</span>
    SignalArray	= Nz<span style="color: #003f00;">&#40;</span> Foreign<span style="color: #003f00;">&#40;</span> Ticker + <span style="color: #7f007f;">&quot;~SignalArrays&quot;</span>, <span style="color: #7f007f;">&quot;V&quot;</span>, <span style="color: #000000; font-weight: bold;">False</span> <span style="color: #003f00;">&#41;</span> <span style="color: #003f00;">&#41;</span>;
    <span style="color: #000000; font-weight: bold;">Buy</span> 		= <span style="color: #0000ff;">IIf</span><span style="color: #003f00;">&#40;</span> SignalArray &amp; <span style="color: #7f0000;">1</span>, <span style="color: #7f0000;">1</span>, <span style="color: #7f0000;">0</span> <span style="color: #003f00;">&#41;</span>;
    <span style="color: #000000; font-weight: bold;">Sell</span> 		= <span style="color: #0000ff;">IIf</span><span style="color: #003f00;">&#40;</span> SignalArray &amp; <span style="color: #7f0000;">2</span>, <span style="color: #7f0000;">1</span>, <span style="color: #7f0000;">0</span> <span style="color: #003f00;">&#41;</span>;
    <span style="color: #000000; font-weight: bold;">Short</span> 		= <span style="color: #0000ff;">IIf</span><span style="color: #003f00;">&#40;</span> SignalArray &amp; <span style="color: #7f0000;">4</span>, <span style="color: #7f0000;">1</span>, <span style="color: #7f0000;">0</span> <span style="color: #003f00;">&#41;</span>;
    <span style="color: #000000; font-weight: bold;">Cover</span> 		= <span style="color: #0000ff;">IIf</span><span style="color: #003f00;">&#40;</span> SignalArray &amp; <span style="color: #7f0000;">8</span>, <span style="color: #7f0000;">1</span>, <span style="color: #7f0000;">0</span> <span style="color: #003f00;">&#41;</span>;
&nbsp;
    <span style="color: #000000; font-weight: bold;">BuyPrice</span> 	= Nz<span style="color: #003f00;">&#40;</span> Foreign<span style="color: #003f00;">&#40;</span> Ticker + <span style="color: #7f007f;">&quot;~SignalArrays&quot;</span>, <span style="color: #7f007f;">&quot;O&quot;</span> <span style="color: #003f00;">&#41;</span> <span style="color: #003f00;">&#41;</span>;
    <span style="color: #000000; font-weight: bold;">SellPrice</span> 	= Nz<span style="color: #003f00;">&#40;</span> Foreign<span style="color: #003f00;">&#40;</span> Ticker + <span style="color: #7f007f;">&quot;~SignalArrays&quot;</span>, <span style="color: #7f007f;">&quot;H&quot;</span> <span style="color: #003f00;">&#41;</span> <span style="color: #003f00;">&#41;</span>;
    ShortPrice	= Nz<span style="color: #003f00;">&#40;</span> Foreign<span style="color: #003f00;">&#40;</span> Ticker + <span style="color: #7f007f;">&quot;~SignalArrays&quot;</span>, <span style="color: #7f007f;">&quot;L&quot;</span> <span style="color: #003f00;">&#41;</span> <span style="color: #003f00;">&#41;</span>;
    <span style="color: #000000; font-weight: bold;">CoverPrice</span> 	= Nz<span style="color: #003f00;">&#40;</span> Foreign<span style="color: #003f00;">&#40;</span> Ticker + <span style="color: #7f007f;">&quot;~SignalArrays&quot;</span>, <span style="color: #7f007f;">&quot;C&quot;</span> <span style="color: #003f00;">&#41;</span> <span style="color: #003f00;">&#41;</span>;
&nbsp;
    <span style="color: #0000ff;">PlotShapes</span><span style="color: #003f00;">&#40;</span> <span style="color: #0000ff;">IIf</span><span style="color: #003f00;">&#40;</span> <span style="color: #000000; font-weight: bold;">Buy</span>, shapeSmallUpTriangle, shapeNone <span style="color: #003f00;">&#41;</span>, <span style="color: #7f0000;">5</span>, <span style="color: #7f0000;">0</span>, <span style="color: #000000; font-weight: bold;">BuyPrice</span>, <span style="color: #7f0000;">0</span> <span style="color: #003f00;">&#41;</span>;
    <span style="color: #0000ff;">PlotShapes</span><span style="color: #003f00;">&#40;</span> <span style="color: #0000ff;">IIf</span><span style="color: #003f00;">&#40;</span> <span style="color: #000000; font-weight: bold;">Sell</span>, shapeSmallDownTriangle, shapeNone <span style="color: #003f00;">&#41;</span>, <span style="color: #7f0000;">4</span>, <span style="color: #7f0000;">0</span>, <span style="color: #000000; font-weight: bold;">SellPrice</span>, <span style="color: #7f0000;">0</span> <span style="color: #003f00;">&#41;</span>;
    <span style="color: #0000ff;">PlotShapes</span><span style="color: #003f00;">&#40;</span> <span style="color: #0000ff;">IIf</span><span style="color: #003f00;">&#40;</span> <span style="color: #000000; font-weight: bold;">Short</span>, shapeHollowDownTriangle, shapeNone <span style="color: #003f00;">&#41;</span>, <span style="color: #7f0000;">4</span>, <span style="color: #7f0000;">0</span>, ShortPrice, <span style="color: #7f0000;">0</span> <span style="color: #003f00;">&#41;</span>;
    <span style="color: #0000ff;">PlotShapes</span><span style="color: #003f00;">&#40;</span> <span style="color: #0000ff;">IIf</span><span style="color: #003f00;">&#40;</span> <span style="color: #000000; font-weight: bold;">Cover</span>, shapeHollowUpTriangle, shapeNone <span style="color: #003f00;">&#41;</span>, <span style="color: #7f0000;">5</span>, <span style="color: #7f0000;">0</span>, <span style="color: #000000; font-weight: bold;">CoverPrice</span>, <span style="color: #7f0000;">0</span> <span style="color: #003f00;">&#41;</span>;
&nbsp;
<span style="color: #003f00;">&#125;</span>
RestorePriceArrays<span style="color: #003f00;">&#40;</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>;
_SECTION_END<span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</span>;</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.amibroker.org/userkb/2011/03/29/using-a-trade-composite/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Quick-Order Bar (QBar)</title>
		<link>http://www.amibroker.org/userkb/2011/03/11/quick-order-bar-qbar/</link>
		<comments>http://www.amibroker.org/userkb/2011/03/11/quick-order-bar-qbar/#comments</comments>
		<pubDate>Fri, 11 Mar 2011 10:46:18 +0000</pubDate>
		<dc:creator>Herman</dc:creator>
				<category><![CDATA[Developing a RT Trading Dashboard]]></category>

		<guid isPermaLink="false">http://www.amibroker.org/userkb/?p=2304</guid>
		<description><![CDATA[Because the project has now reached a stage where it requires the Control panel to function, I have not included demo code at this time. The Control panel will be covered in one of the next posts and, when that is up and running, it will be easier to test most of the features of [...]]]></description>
			<content:encoded><![CDATA[<p>Because the project has now reached a stage where it requires the Control panel to function, I have not included demo code at this time. The Control panel will be covered in one of the next posts and, when that is up and running, it will be easier to test most of the features of the TDash Graphical Interface. </p>
<p>The QBar is a small tool bar that tracks the Y-position of the cursor. It is designed for fast real-time trading and has only three buttons: Buy, Sell, and Reverse. You can add/delete buttons, for example, you could add a button to Cancel the last order placed. Order parameters are set in the Control panel.</p>
<p>The main advantage of the QBar is that you can place an order with a single-click. You do not have to move your eyes away from the last bar on your chart to place the order, you simply move your mouse cursor to the required price and click. </p>
<p>The QBar and the Order Markers share some functionality. The main difference is that the QBar is designed for placing new orders, and that the Order Markers are designed to display and modify pending orders.</p>
<p>The QBar has two display modes: </p>
<ol>
<li> Auto-Hide Off. The QBar displays continuously. When the cursor moves out of the QBar area the QBar remains in its last position.</li>
<li>Auto-Hide ON. The QBar displays only when the cursor is in the Qbar area, i.e., in the vertical column space reserved for the QBar. When the cursor leaves the QBar area, it remains positioned at the last price selected. This mode allows the display of trade and system information in the QBar area when the QBar is auto-hidden.
</li>
</ol>
<p>The QBar also has an &#8220;Auto Modify&#8221; mode. When this is turned on you can place an order and, holding down the left mouse button, drag the QBar to a new price. As long as you keep the mouse button down the order on the TWS will be modified, in real-time, each time the price changes.</p>
<p>Below is a short video that shows what the QBar looks like. I removed the Order markers while working on the QBar. </p>
<div align="center">
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0" width="856" height="590"><param name=movie value="QBarDemo.swf"><param name=quality value=high><embed src="http://www.amibroker.org/video/QBarDemo/QBarDemo.swf" quality=high pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="856" height="590"></embed></object>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.amibroker.org/userkb/2011/03/11/quick-order-bar-qbar/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Processing TDash Commands</title>
		<link>http://www.amibroker.org/userkb/2011/03/09/processing-tdash-commands/</link>
		<comments>http://www.amibroker.org/userkb/2011/03/09/processing-tdash-commands/#comments</comments>
		<pubDate>Wed, 09 Mar 2011 00:37:14 +0000</pubDate>
		<dc:creator>Herman</dc:creator>
				<category><![CDATA[Developing a RT Trading Dashboard]]></category>

		<guid isPermaLink="false">http://www.amibroker.org/userkb/?p=2530</guid>
		<description><![CDATA[If you watched the small video in earlier post on Order Markers you may have noticed that whenever the mouse cursor hovered over, or a mouse button was clicked while over an Order Marker, the &#8220;TDashAction&#8221; variable shown at the right top of the video changed. This is a unique variable that is solely responsible [...]]]></description>
			<content:encoded><![CDATA[<p>If you watched the small video in earlier post on Order Markers you may have noticed that whenever the mouse cursor hovered over, or a mouse button was clicked while over an Order Marker, the &#8220;TDashAction&#8221; variable shown at the right top of the video changed. This is a unique variable that is solely responsible for communicating TDash Action to the Auto-Trading module. This method allows TDash to be linked to your current trade automation module. This post explains how this works. </p>
<p>Because the cursor can only be in one location at a time, all TDash commands are exclusive. Hence we do not need to assign individual names to each parameter, instead we can use a single string variable to tell us the name of the button and action performed. Compared to using traditional Param()s, where each Param() assigns a value to a unique variable, this eliminates about 50-100 variable names, and allows us to use a single Switch() command to process the commands. It also makes code easier to read, modify, and maintain.</p>
<p>To make this work requires that buttons are organized as follows:</p>
<ol>
<li>Buttons are assigned to uniquely named groups like &#8220;Markers&#8221;, &#8220;Menus&#8221;, &#8220;Trading&#8221;, &#8220;Setup&#8221;, &#8220;Account&#8221;, &#8220;Status&#8221;, etc. For example, the Order Markers covered in the previous post would fall in the &#8220;Marker&#8221; group, the forthcoming QBar controls will fall in the &#8220;QBar&#8221; group, etc. </li>
<p></p>
<li>All buttons must have an upper &#8220;Header&#8221;, and may have an optional lower &#8220;Label&#8221;. The visible header and label is unique to the group it belongs to. A typical example would be a Buy @ LMT button, &#8220;Buy&#8221; would be the Header, and &#8220;LMT&#8221; would be the lower Label. In this case the lower label may take on different values, such as &#8220;MKT&#8221;, &#8220;STP&#8221;, and &#8220;STPLMT&#8221;. Header-only buttons are typically used for trigger functions which have no changing state to display, for example &#8220;Reset&#8221; or &#8220;Abort&#8221; buttons.</li>
<p></p>
<li>Since all buttons respond to mouse movement and button clicks, buttons can have four possible possible action states:
<ol>
Hover: the cursor hovers over the button<br />
LeftClick: the left button is clicked while the cursor is over the button<br />
LeftDown: the left button is held down while the cursor is over the button (Dragging)<br />
LeftRelease: The left button is released while over the button
</ol>
</ol>
<p>These four action states are encoded by prefixing a &#8220;~&#8221; for a Leftclick, &#8220;~~&#8221; for a LeftDown condition, and &#8220;~~~&#8221; for the LeftRelease.  Concatenating the action prefix, group name, visible header string, and visible Label string into a csv string provides us with a &#8220;TDashAction&#8221; string that uniquely defines the button and its transient states. Because for multi-state buttons the lower label describes the state of the button, this string also tells us the state of the button. For additional clarity we can also change the button-color to indicate its state.</p>
<p>For example, dragging the Buy Order Marker shown in the previous post would, over consecutive executions, generate the following sequence of changes in the TDashAction variable:</p>
<ol>
Marker,Buy       // cursor hovers over the button<br />
~Marker,Buy     // left-click on the button is detected<br />
~~Marker,Buy   // the left mouse button is down on the button (dragging)<br />
~~~Marker,Buy // the left mouse button is released while over the button
</ol>
<p>This type of encoding is used for all TDash buttons and controls. While this method may be a little esoteric it makes reading the code very easy, and allows us to add controls without having to dream up a new set of variable names. We just look at the displayed TDashAction string and we know the name for the action to be decoded in our Switch().  </p>
<p>This method makes decoding TDash action using a switch() simple and easy to debug. Note that in most cases not all possible states need a case statement. A major advantage of using a Switch() statement is that one can, at any time, easily add and/or remove buttons. </p>
<p>Below is a simple Switch(0 to decode the &#8220;Marker Buy&#8221; command used in the previous post:</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
</pre></td><td class="code"><pre class="afl">TDashAction =  VarGetText<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;TDashAction&quot;</span> <span style="color: #003f00;">&#41;</span>;
&nbsp;
<span style="color: #7f0000; ">switch</span> <span style="color: #003f00;">&#40;</span> TDashAction <span style="color: #003f00;">&#41;</span>
<span style="color: #003f00;">&#123;</span>
&nbsp;
<span style="color: #7f0000; ">case</span> <span style="color: #7f007f;">&quot;Marker,Buy&quot;</span>:
    <span style="color: #007f00; ">// perform tasks in response to hovering over the Marker</span>
    _TRACE<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;TDashAction: &quot;</span> + TDashAction <span style="color: #003f00;">&#41;</span>;
    <span style="color: #7f0000; ">break</span>;
&nbsp;
<span style="color: #7f0000; ">case</span> <span style="color: #7f007f;">&quot;~Marker,Buy&quot;</span>:
    <span style="color: #007f00; ">// Perform action in response to a left click on the Marker</span>
    _TRACE<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;TDashAction: &quot;</span> + TDashAction <span style="color: #003f00;">&#41;</span>;
    <span style="color: #7f0000; ">break</span>;
&nbsp;
<span style="color: #7f0000; ">case</span> <span style="color: #7f007f;">&quot;~~Marker,Buy&quot;</span>:
    <span style="color: #007f00; ">// React to dragging the marker</span>
    _TRACE<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;TDashAction: &quot;</span> + TDashAction <span style="color: #003f00;">&#41;</span>;
    <span style="color: #7f0000; ">break</span>;
&nbsp;
<span style="color: #7f0000; ">case</span> <span style="color: #7f007f;">&quot;~~~Marker,Buy&quot;</span>:
    <span style="color: #007f00; ">// Perform action in response to releasing the mouse button</span>
    _TRACE<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;TDashAction: &quot;</span> + TDashAction <span style="color: #003f00;">&#41;</span>;
    <span style="color: #7f0000; ">break</span>;
<span style="color: #003f00;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.amibroker.org/userkb/2011/03/09/processing-tdash-commands/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Equalize X-Range for all windows</title>
		<link>http://www.amibroker.org/userkb/2011/03/07/equalize-x-range-for-all-windows/</link>
		<comments>http://www.amibroker.org/userkb/2011/03/07/equalize-x-range-for-all-windows/#comments</comments>
		<pubDate>Mon, 07 Mar 2011 07:17:58 +0000</pubDate>
		<dc:creator>Herman</dc:creator>
				<category><![CDATA[AFL - Utilities and Functions]]></category>

		<guid isPermaLink="false">http://www.amibroker.org/userkb/?p=2616</guid>
		<description><![CDATA[This function was requested on the main list, and was solved with the help of several expert programmers from the list. Thanks guys! This function can be copied to an include file that is included in each program from which you might want to synchronize the datetime range for all visible windows. The function places [...]]]></description>
			<content:encoded><![CDATA[<p>This function was requested on the main list, and was solved with the help of several expert programmers from the list. Thanks guys!</p>
<p>This function can be copied to an include file that is included in each program from which you might want to synchronize the datetime range for all visible windows. The function places a small [R] button at the right top of your chart. Clicking this button will set the datetime ranges of all windows equal to the one you click the button in. Note that this window has to be active for the button to work, i.e., if the window was not active (selected) it will require two clicks for the button to respond.</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
</pre></td><td class="code"><pre class="afl"><span style="color: #7f0000; ">function</span> RangeAllWindows<span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</span>
<span style="color: #003f00;">&#123;</span>
    MX = GetCursorXPosition<span style="color: #003f00;">&#40;</span> <span style="color: #7f0000;">1</span> <span style="color: #003f00;">&#41;</span>;
    MY = GetCursorYPosition<span style="color: #003f00;">&#40;</span> <span style="color: #7f0000;">1</span> <span style="color: #003f00;">&#41;</span>;
    LeftClick = GetCursorMouseButtons<span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</span> == <span style="color: #7f0000;">9</span>;
&nbsp;
    <span style="color: #007f00; ">// Place Ranging Button</span>
    ButtonSize=<span style="color: #7f0000;">20</span>;
    X2 = <span style="color: #0000ff;">Status</span><span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;pxchartright&quot;</span> <span style="color: #003f00;">&#41;</span><span style="color: #7f0000;">+1</span>;
    X1 = X2 - ButtonSize;
    Y1 = <span style="color: #7f0000;">0</span>;
    Y2 = Y1 + ButtonSize;
    GfxSelectFont<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Tahoma&quot;</span>, ButtonSize / <span style="color: #7f0000;">1.5</span>, <span style="color: #7f0000;">800</span> <span style="color: #003f00;">&#41;</span>;
    GfxSelectPen<span style="color: #003f00;">&#40;</span> colorBlack , <span style="color: #7f0000;">1</span> <span style="color: #003f00;">&#41;</span>;
    GfxSelectSolidBrush<span style="color: #003f00;">&#40;</span> colorYellow <span style="color: #003f00;">&#41;</span>;
    GfxRectangle<span style="color: #003f00;">&#40;</span> X1, Y1, X2, Y2 <span style="color: #003f00;">&#41;</span>;
    GfxSetTextColor<span style="color: #003f00;">&#40;</span> colorBlack <span style="color: #003f00;">&#41;</span>;
    GfxSetBkMode<span style="color: #003f00;">&#40;</span> <span style="color: #7f0000;">1</span> <span style="color: #003f00;">&#41;</span>;
    GfxDrawText<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;R&quot;</span>, X1, Y1, X2, Y2, <span style="color: #7f0000;">41</span> <span style="color: #003f00;">&#41;</span>;
    OnButton = MX &gt;= X1 AND MY &gt;= Y1 AND MX &lt;= X2 AND MY &lt;= Y2;
&nbsp;
    <span style="color: #7f0000; ">if</span> <span style="color: #003f00;">&#40;</span> OnButton AND LeftClick <span style="color: #003f00;">&#41;</span>
    <span style="color: #003f00;">&#123;</span>
        DT = DateTime<span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</span>;
        BI = BarIndex<span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</span>;
        FirstBarIndex = <span style="color: #0000ff;">Status</span><span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;firstvisiblebarindex&quot;</span> <span style="color: #003f00;">&#41;</span>;
        LastBarIndex = <span style="color: #0000ff;">Status</span><span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;lastvisiblebarindex&quot;</span> <span style="color: #003f00;">&#41;</span>;
        FirstDateTime = LastValue<span style="color: #003f00;">&#40;</span> ValueWhen<span style="color: #003f00;">&#40;</span> FirstBarIndex == BI, DT <span style="color: #003f00;">&#41;</span> <span style="color: #003f00;">&#41;</span>;
        LastDateTime = LastValue<span style="color: #003f00;">&#40;</span> ValueWhen<span style="color: #003f00;">&#40;</span> LastBarIndex == BI, DT <span style="color: #003f00;">&#41;</span> <span style="color: #003f00;">&#41;</span>;
        FirstDateTimestr = DateTimeToStr<span style="color: #003f00;">&#40;</span> FirstDateTime <span style="color: #003f00;">&#41;</span>;
        LastDateTimestr = DateTimeToStr<span style="color: #003f00;">&#40;</span> LastDateTime <span style="color: #003f00;">&#41;</span>;
        AB = CreateObject<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;Broker.Application&quot;</span> <span style="color: #003f00;">&#41;</span>;
        docs = AB.<span style="color: #202020;">Documents</span>;
        Qty = docs.<span style="color: #202020;">Count</span>;
&nbsp;
        <span style="color: #7f0000; ">for</span> <span style="color: #003f00;">&#40;</span> i = <span style="color: #7f0000;">0</span>; i &lt; Qty; i++ <span style="color: #003f00;">&#41;</span> <span style="color: #007f00; ">// Range all windows</span>
        <span style="color: #003f00;">&#123;</span>
            doc = docs.<span style="color: #202020;">Item</span><span style="color: #003f00;">&#40;</span> i <span style="color: #003f00;">&#41;</span>;
            AW = doc.<span style="color: #202020;">ActiveWindow</span>;
            AW.<span style="color: #202020;">Activate</span><span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</span>;
            AW.<span style="color: #202020;">ZoomToRange</span><span style="color: #003f00;">&#40;</span> FirstDateTimestr, LastDateTimestr <span style="color: #003f00;">&#41;</span>;
            <span style="color: #007f00; ">// correct shift due to blank bars</span>
            WSHShell = CreateObject<span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;WScript.Shell&quot;</span> <span style="color: #003f00;">&#41;</span>;
            WSHShell.<span style="color: #202020;">AppActivate</span><span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;AmiBroker&quot;</span> <span style="color: #003f00;">&#41;</span>;
            WSHShell.<span style="color: #202020;">Sendkeys</span><span style="color: #003f00;">&#40;</span> <span style="color: #7f007f;">&quot;{PGDN}&quot;</span> <span style="color: #003f00;">&#41;</span>;
        <span style="color: #003f00;">&#125;</span>
    <span style="color: #003f00;">&#125;</span>
<span style="color: #003f00;">&#125;</span>
&nbsp;
<span style="color: #007f00; ">//Demo Code</span>
RangeAllWindows<span style="color: #003f00;">&#40;</span><span style="color: #003f00;">&#41;</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>,<span style="color: #7f0000;">128</span><span style="color: #003f00;">&#41;</span>;</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.amibroker.org/userkb/2011/03/07/equalize-x-range-for-all-windows/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

