{"id":62,"date":"2007-06-01T02:53:14","date_gmt":"2007-06-01T02:53:14","guid":{"rendered":"http:\/\/www.amibroker.org\/userkb\/2007\/04\/30\/detecting-look-ahead-by-nulling-bars\/"},"modified":"2007-07-07T19:36:32","modified_gmt":"2007-07-07T19:36:32","slug":"detecting-look-ahead-by-nulling-bars","status":"publish","type":"post","link":"http:\/\/www.amibroker.org\/editable_userkb\/2007\/06\/01\/detecting-look-ahead-by-nulling-bars\/","title":{"rendered":"Detecting Look-Ahead Problems"},"content":{"rendered":"
Look-ahead problems occur when, during Backtesting, your code uses future prices to calculate signals. When this happens, your system’s performance may be heavily inflated, but obviously since you are looking into the future, the system cannot be traded.<\/p>\n
While the Indicator formula editor menu has a Check<\/em> function, it often fails when debugging complex systems, especially if they use Plug-Ins or DLLs. Also, when it discovers a problem, it does not show you at which bar the problem occurred. The simple code presented here detects all look-ahead problems that might occur during a Backtest, even if your system uses Plugins or DLLs, and it can be appended to any system.<\/p>\n The code makes use of the fact that setting a future bar to NULL will cause dependent variables and signals to change. To use this technique your Arrows must be placed using the PlotShapes() function so that they will change when you NULL future bars. This would not happen if the arrows are placed by the Backtester. Thus, you should turn Off Arrows in your Param() menu.<\/p>\n To test for Look-Ahead problems, you should copy the code to your system formula and Apply it to an Indicator pane. Make sure your chart is fully right justified so that you can see the last bar, and zoom so that you can see some signal arrows. Next, open the Param window and move the slider to change the Remove Bars count. As you do this you will see bars disappearing, last bar first. When the disappearance of a bar causes a preceding arrow to disappear, your system looks ahead.<\/p>\n Note that this code can also be used to detect Indicator dependence on future bars. If your indicator changes ahead of your NULL bar, your Indicator is dependent on future data. When changes are very small, this may be hard to see. It would be an interesting coding challenge to save a number of bars and compare so that even small changes can be detected. Perhaps someone will write a DLL to do all this for us.<\/p>\n A minor drawback of this code is that you have to scan your chart manually, which may take a few minutes. You could add an automated scan and run the code using an exploration, generating an error table for an entire Watchlist. It wouldn’t be very fast, but it would increase the chance of detecting all possible errors.<\/p>\n The demo code below uses the Zig() function to place signals and arrows on the chart. Since Zig() looks ahead by varying amounts, it is perfect to demonstrate how this code works.<\/p>\n Edited by Al Venosa<\/p>\n","protected":false},"excerpt":{"rendered":" Look-ahead problems occur when, during Backtesting, your code uses future prices to calculate signals. When this happens, your system’s performance may be heavily inflated, but obviously since you are looking into the future, the system cannot be traded. While the Indicator formula editor menu has a Check function, it often fails when debugging complex systems, […]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[63],"tags":[],"_links":{"self":[{"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/posts\/62"}],"collection":[{"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/comments?post=62"}],"version-history":[{"count":0,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/posts\/62\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/media?parent=62"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/categories?post=62"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/tags?post=62"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}EnableNulling = ParamToggle(\"NULLing of Data\",\"DISABLED|ENABLED\",0);
\nRM = Param(\"NULL Bars L<-R\",0,0,1000,1);
\nif( EnableNulling )
\n{
\nZ = Null;
\nLB = LastValue(BarIndex());
\nO = IIf(BarIndex()>(LB-RM),Z,O);
\nH = IIf(BarIndex()>(LB-RM),Z,H);
\nL = IIf(BarIndex()>(LB-RM),Z,L);
\nC = IIf(BarIndex()>(LB-RM),Z,C);
\n}
\nZChange = Param(\"%Zig\",0.1,0,1,0.05);
\nZ = Zig(C,ZChange);
\nBuy = Z < Ref(Z,1) AND Z < Ref(Z,-1);
\nSell = Z > Ref(Z,1) AND Z > Ref(Z,-1);
\nShort = Sell;
\nCover = Buy;
\nPlot(C,\"\",1,128);
\nPlotShapes(IIf(Buy, shapeSmallUpTriangle, shapeNone),5,0,BuyPrice,0);
\nPlotShapes(IIf(Sell, shapeHollowDownTriangle, shapeNone),4,0,SellPrice,0);
\nPlotShapes(IIf(Cover, shapeHollowUpTriangle, shapeNone),5,0,CoverPrice,0);
\nPlotShapes(IIf(Short, shapeSmallDownTriangle, shapeNone),4,0,ShortPrice,0);<\/code><\/p>\n