{"id":389,"date":"2014-09-26T15:48:18","date_gmt":"2014-09-26T20:48:18","guid":{"rendered":"http:\/\/www.amibroker.com\/kb\/?p=389"},"modified":"2014-12-04T08:21:10","modified_gmt":"2014-12-04T13:21:10","slug":"closing-trades-in-delisted-symbols","status":"publish","type":"post","link":"https:\/\/www.amibroker.com\/wordpress\/kb\/2014\/09\/26\/closing-trades-in-delisted-symbols\/","title":{"rendered":"Closing trades in delisted symbols"},"content":{"rendered":"

When we perform historical tests on databases that contain delisted symbols – we may encounter a situation, where there are open positions in those tickers remaining till the very end of the backtest, distorting the results (as these open positions will reduce remaining maximum open positions limit for the other symbols).<\/p>

Here is an easy technique which allows to force closing positions in those symbols on the very last bar traded for given symbol. The code below just adds an additional Sell<\/strong> signal on the last available bar in the database for this symbol:<\/p>bi <\/span>= <\/span>BarIndex<\/span>();
<\/span>exitLastBar <\/span>= <\/span>bi <\/span>== <\/span>LastValue<\/span>( <\/span>bi <\/span>);
<\/span>Sell <\/span>= <\/span>\/*your regular sell rules*\/ <\/span>OR <\/span>exitLastBar<\/span><\/code>

If we are using 1-bar trade delays in our backtesting settings, then the exit signal would need to be triggered one bar in advance (so the delayed signal could still be traded on the last bar) and the code would look like this:<\/p>SetTradeDelays<\/span>(<\/span>1<\/span>,<\/span>1<\/span>,<\/span>1<\/span>,<\/span>1<\/span>);
<\/span>bi <\/span>= <\/span>BarIndex<\/span>();
<\/span>exitLastBar <\/span>= <\/span>bi <\/span>== <\/span>LastValue<\/span>( <\/span>bi <\/span>- <\/span>1 <\/span>);
<\/span>Sell <\/span>= <\/span>\/*your regular sell rules*\/ <\/span>OR <\/span>exitLastBar<\/span><\/code>

There is also a dedicated field in Symbol->Information<\/strong> window which allows to store the delisting date directly in the database. AmiBroker allows to read that field from AFL code using GetFnData<\/strong>() function. If we have this field populated for delisted symbols for our symbols, then the code forcing exits on delisting date would be:<\/p>exitLastBar <\/span>= <\/span>datetime<\/span>() >= <\/span>GetFnData<\/span>(<\/span>"DelistingDate"<\/span>);
<\/span>Sell <\/span>= <\/span>\/*your regular sell rules*\/ <\/span>OR <\/span>exitLastBar<\/span><\/code>

What is important, this approach would work also, when Pad and Align to reference symbol<\/strong> feature is used in Analysis<\/strong> window settings.<\/p>

In order to populate Delisting Date<\/strong> field in the database, we can enter the dates manually through Symbol->Information<\/strong> window or use ASCII importer to import the information from the input text files. More details about ASCII importing can be found at:
http:\/\/www.amibroker.com\/guide\/d_ascii.html<\/a><\/p>","protected":false},"excerpt":{"rendered":"

When we perform historical tests on databases that contain delisted symbols – we may encounter a situation, where there are open positions in those tickers remaining till the very end of the backtest, distorting the results (as these open positions will reduce remaining maximum open positions limit for the other symbols).Here is an easy technique […]<\/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\/389"}],"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=389"}],"version-history":[{"count":1,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/389\/revisions"}],"predecessor-version":[{"id":791,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/389\/revisions\/791"}],"wp:attachment":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/media?parent=389"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/categories?post=389"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/tags?post=389"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}