{"id":739,"date":"2014-11-26T13:44:06","date_gmt":"2014-11-26T18:44:06","guid":{"rendered":"http:\/\/www.amibroker.com\/kb\/?p=739"},"modified":"2014-12-04T08:18:36","modified_gmt":"2014-12-04T13:18:36","slug":"handling-limit-orders-in-the-backtester","status":"publish","type":"post","link":"https:\/\/www.amibroker.com\/wordpress\/kb\/2014\/11\/26\/handling-limit-orders-in-the-backtester\/","title":{"rendered":"Handling limit orders in the backtester"},"content":{"rendered":"

In order to simulate limit orders in backtesting it is necessary to check in the code if Low<\/strong> price of the entry bar is below the limit price we want to use. The following example shows an entry signal based on Close<\/strong> price crossing over 100-period simple moving average. The position is opened on the next bar if price drops 1% below the Close<\/strong> of signal bar.<\/p>BuySignal <\/span>= <\/span>Cross<\/span>( <\/span>Close<\/span>, <\/span>MA<\/span>(<\/span>Close<\/span>, <\/span>100 <\/span>) );

<\/span>\/\/ buy on the next bar
<\/span>Buy <\/span>= <\/span>Ref<\/span>( <\/span>BuySignal<\/span>, -<\/span>1<\/span>);
<\/span>BuyLimitPrice <\/span>= <\/span>Ref<\/span>( <\/span>Close<\/span>, -<\/span>1<\/span>) * <\/span>0.99<\/span>;

<\/span>\/\/ now we check if limit was hit
<\/span>Buy <\/span>= <\/span>Buy <\/span>AND <\/span>L <\/span>< <\/span>BuyLimitPrice<\/span>;

<\/span>\/\/ if Open price is below the limit, then we use Open for entry
<\/span>BuyPrice <\/span>= <\/span>Min<\/span>( <\/span>Open<\/span>, <\/span>BuyLimitPrice <\/span>)<\/code>

If we want the order to be valid for more than one bar, then we can use Hold<\/strong> function for this purpose:<\/p>BuySignal <\/span>= <\/span>Cross<\/span>( <\/span>Close<\/span>, <\/span>MA<\/span>(<\/span>Close<\/span>, <\/span>100 <\/span>) );

<\/span>\/\/ buy on the next bar
<\/span>Buy <\/span>= <\/span>Ref<\/span>( <\/span>BuySignal<\/span>, -<\/span>1<\/span>);
<\/span>BuyLimitPrice <\/span>= <\/span>ValueWhen<\/span>(<\/span>BuySignal<\/span>, <\/span>Close<\/span>) * <\/span>0.99<\/span>;

<\/span>\/\/ now we check if limit was hit
<\/span>Buy <\/span>= <\/span>Hold<\/span>( <\/span>Buy<\/span>, <\/span>3 <\/span>) AND <\/span>L <\/span>< <\/span>BuyLimitPrice<\/span>;

<\/span>\/\/ if Open price is below the limit, then we use Open for entry
<\/span>BuyPrice <\/span>= <\/span>Min<\/span>( <\/span>Open<\/span>, <\/span>BuyLimitPrice <\/span>)<\/code>

In a portfolio-level backtest we usually advocate against using limit orders. Why? Simply because we may not have enough cash in your account to place limit orders for all possible entry candidates. If your trading system generates 100 possible entries, you would need to place 100 limit orders only to find out that eventually only few of them fired. With limited buying power, we may need to place limit orders only for the top N-scored tickers that have generated BuySignal and skip the others. To simulate the situation when we only place small set of limit orders for top ranked stocks we can use new ranking functionalities introduced in AmiBroker 5.70. Knowing the rank at this stage is required if we only want to allow orders for top-scored tickers. Let us say that we prefer symbols with smallest RSI values.<\/p>

The code would look the following way: Formula first generates a ranking for all tickers included in the test (below example uses Watchlist 0), then when testing individual symbols – checks the pre-calculated rank and generates Buy signal based on that reading.<\/p><\/span>\/\/ we run the code on WatchList 0
<\/span>List = <\/span>CategoryGetSymbols<\/span>( <\/span>categoryWatchlist<\/span>, <\/span>0 <\/span>);
<\/span>SetOption<\/span>(<\/span>"MaxOpenPositions"<\/span>, <\/span>3<\/span>);

if ( <\/span>Status<\/span>(<\/span>"stocknum"<\/span>) == <\/span>0 <\/span>) <\/span>\/\/ Generate ranking when we are on the very first symbol
<\/span>{
     <\/span>StaticVarRemove<\/span>( <\/span>"values*" <\/span>);

     for ( <\/span>n <\/span>= <\/span>0<\/span>; ( <\/span>Symbol <\/span>= <\/span>StrExtract<\/span>( List, <\/span>n <\/span>) )  != <\/span>""<\/span>;  <\/span>n<\/span>++    )
     {
         <\/span>SetForeign <\/span>( <\/span>symbol <\/span>);

         <\/span>\/\/ value used for scoring
         <\/span>values <\/span>= <\/span>100 <\/span>- <\/span>RSI<\/span>();
         <\/span>RestorePriceArrays<\/span>();
         <\/span>StaticVarSet <\/span>(  <\/span>"values"  <\/span>+  <\/span>symbol<\/span>, <\/span>values <\/span>);
         <\/span>_TRACE<\/span>( <\/span>symbol <\/span>);
     }

     <\/span>StaticVarGenerateRanks<\/span>( <\/span>"rank"<\/span>, <\/span>"values"<\/span>, <\/span>0<\/span>, <\/span>1224 <\/span>);
}

<\/span>symbol <\/span>= <\/span>Name<\/span>();
<\/span>values <\/span>= <\/span>StaticVarGet <\/span>( <\/span>"values" <\/span>+  <\/span>symbol <\/span>);
<\/span>rank <\/span>= <\/span>StaticVarGet <\/span>( <\/span>"rankvalues" <\/span>+  <\/span>symbol <\/span>);

<\/span>PositionScore <\/span>= <\/span>values<\/span>;

<\/span>BuySignal <\/span>= <\/span>Cross<\/span>( <\/span>Close<\/span>, <\/span>MA<\/span>(<\/span>Close<\/span>, <\/span>100 <\/span>) );

<\/span>\/\/ buy on the next bar
<\/span>Buy <\/span>= <\/span>Ref<\/span>( <\/span>BuySignal<\/span>, -<\/span>1<\/span>);
<\/span>BuyLimitPrice <\/span>= <\/span>Ref<\/span>( <\/span>Close<\/span>, -<\/span>1<\/span>) * <\/span>0.999<\/span>;

<\/span>\/\/ now we check if limit was hit for the symbols ranked as top 3
<\/span>Buy <\/span>= <\/span>Buy <\/span>AND <\/span>L <\/span>< <\/span>BuyLimitPrice <\/span>AND <\/span>rank <\/span><= <\/span>3<\/span>;
<\/span>BuyPrice <\/span>= <\/span>Min<\/span>( <\/span>Open<\/span>, <\/span>BuyLimitPrice <\/span>);

<\/span>\/\/ sample exit rules - 5 - bar stop
<\/span>Sell <\/span>= <\/span>0<\/span>;
<\/span>ApplyStop<\/span>( <\/span>stopTypeNBar<\/span>, <\/span>stopModeBars<\/span>, <\/span>5<\/span>, <\/span>1<\/span>)<\/code>

Detailed description of the ranking functionality used above is available in the manual at: http:\/\/www.amibroker.com\/guide\/h_ranking.html<\/a><\/p>","protected":false},"excerpt":{"rendered":"

In order to simulate limit orders in backtesting it is necessary to check in the code if Low price of the entry bar is below the limit price we want to use. The following example shows an entry signal based on Close price crossing over 100-period simple moving average. The position is opened on the […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[53,14,15],"_links":{"self":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/739"}],"collection":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/comments?post=739"}],"version-history":[{"count":2,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/739\/revisions"}],"predecessor-version":[{"id":741,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/739\/revisions\/741"}],"wp:attachment":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/media?parent=739"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/categories?post=739"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/tags?post=739"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}