Thanks for your prompt reply Thomasz
I have two further questions:
- Does this work for delisted shares? I am using Norgate premium ASX data and have included their code (see below) to ensure that delisted stocks are closed when there is no activity. It seems that the delisted stocks are still exiting at the end of the OSS when the listed are not.
- Doesn’t creating a STATE signal then produce a BUY signal after the initial setup has occurred if there are less than the maximum positions open? This would mean that my system would be entering into a position after the ideal entry point.
Norgate code (https://www.premiumdata.net/support/amibroker.php):
// For backtesting, we never want to be left holding a delisted stock,
// however not all versions of AmiBroker allow us to detect whether a security is delisted
// To simulate this we will provide a sell signal on the
// second last bar only if the last bar's date is >= 30 days from today's date.
// Also, we will not enter on the second or last bar of the trade
// as we will then be unable to exit the position if trade delay is set to 1 day.
// This code has been collaboratively developed by Norgate Investor Services
// www.premiumdata.net and its users and is made freely available to the AmiBroker community.
// Known issue: For highly illiquid securities that trade very infrequently will be considered to
// be inactive. This is probably fine for the purposes of backtesting as you would not want to be
// attempting to trade a rarely-traded security.
NonTradedPeriod = 30; //in calendar days
SecurityIsInactive = LastValue(DateTimeDiff(Now(5),DateTimeAdd(DateTime(), NonTradedPeriod, inDaily))) >= 0;
OnSecondLastBarOfInactiveSecurity = BarIndex() == (LastValue(BarIndex()) -1) AND SecurityIsInactive;
OnLastTwoBarsOfInactiveSecurity = BarIndex() >= (LastValue(BarIndex()) -1) AND SecurityIsInactive;
Buy = Buy AND NOT OnLastTwoBarsOfInactiveSecurity;
Sell = Sell OR OnSecondLastBarOfInactiveSecurity;
// If you have a short selling system you will also need to include the following:
Short = Short AND NOT OnLastTwoBarsOfInactiveSecurity;
Cover = Cover OR OnSecondLastBarOfInactiveSecurity;