Thank you @Tomasz . My apologies for missing that. I have been reading through a number of different threads and still cannot figure out how to solve the issue.
I use Norgate Premium Data, am using the "S&P 500 Current and Past" watchlist, have pad & align turned on, and have gone to "tools => Database purify" but still see a number of positions held in my back test for many multiples beyond the N bars specified. Would you please point me in the right direction as to how to tell the system to eliminate these positions? You were correct in that there appears to be no more bars only a few days after the position was entered. How do I tell the system to eliminate these positions, rather than hold an Open Long for thousands of days? Thank you, any guidance is greatly appreciated!
@nicolasvaughan I can't replicate your problem but perhaps you need to review how you are using your historical watchlists from Norgate. They include delisted stocks and Norgate provides you with instructions that you have not used in your code. https://norgatedata.com/amibroker-faq.php
Scroll down to the discussion,
"How do I exit a position in a backtest prior to a stock being delisted?"
Where they suggest adding lines like this
OnSecondLastBarOfDelistedSecurity = !IsNull(GetFnData("DelistingDate")) AND (BarIndex() == (LastValue(BarIndex()) -1) OR DateTime() >= GetFnData("DelistingDate") ) ;
OnLastTwoBarsOfDelistedSecurity = !IsNull(GetFnData("DelistingDate")) AND (BarIndex() >= (LastValue(BarIndex()) -1) OR DateTime() >= GetFnData("DelistingDate") );
Buy = Buy AND NOT OnLastTwoBarsOfDelistedSecurity;
Sell = Sell OR OnSecondLastBarOfDelistedSecurity;
I don't know if that will solve your problem but appears necessary to add to your code anyway.