I'm trying to come up with a filter where you could reject trading any stock that has too many recent empty bars or days that it didn't trade. In my first attempt, I tried to compare the number of bars in the last N days to the number of bars of an index or in this case I used an ETF named SPY. The trouble is when you do that, as far as I understand, Amibroker does the opposite of Pad and Align in that for the foreign stock SPY, it will align the bars for it, to the stock. It removes any bars for SPY when the bars for the stock are empty.
For my second attempt, what I did was to detect the number of days passed between each bar of the stock. For weekdays, you'll normally have 1 day between each bar, but on weekends you'll get 3 days between bars. Then if there is a holiday or two, you can get as many as 5 days between bars. To get around this, I averaged the days between bars, then I would look at the highest high for that average for the last N days and my filter was equal to 1 as long as the highest high didn't exceed a certain amount.
If anyone has a better or cleaner method to do this, I'd love to see it. Below here are examples of my two attempts:
Comparing the number bars in the last N days between a stock and an ETF (doesn't work):
//ADXN is good stock for testing
Lookback = 100;//how many days to look back
LookBackDay = IIf( Now( 10 ) - lookback < 0, Now( 10 ) - lookback + 365, Now( 10 ) - lookback ); //adds 365 days if you cross back into the previous year
DayNumber = DayOfYear();
CrossDay = cross( DayNumber, LookBackDay );
StockCheck = BarsSince( CrossDay );
SetForeign( "SPY" );
ETFCheck = BarsSince( CrossDay );
RestorePriceArrays();
Filter = 1;
AddColumn( Close, "Close", 1.2 );
AddColumn( LookBackDay, "LookBackDay", 1.0 );
AddColumn( DayNumber, "DayNumber", 1.0 );
AddColumn( CrossDay, "CrossDay", 1.0 );
AddColumn( StockCheck, "StockCheck", 1.0 );
AddColumn( ETFCheck, "ETFCheck", 1.0 );
Switch based on the average number days between bars in the last N days (works):
//ADXN is good stock for testing
//Make sure that pad and align is turned off
Today = DateTime();
PreviousBar = Ref( Today, -1 );
DateChange = DateTimeDiff( Today, PreviousBar ) / 60 / 60 / 24;//will go up as high as 5 for Easter
ChangeinNDays = Sum( DateChange, 8 );//adds up the day changes in the last 8 bars
ChangeHigh = HHV( ChangeinNDays, 8 );//takes the highest sum over the last two weeks so you're not turning off on Mondays etc
IndexFilterOn = ChangeHigh < 15;
Swti
Plot( DateChange, "Date Change", colorWhite, styleLine );
Filter = 1;
AddColumn( DateChange, "DateChange", 1.0 );
AddColumn( ChangeinNDays, "ChangeinNDays", 1.0 );
AddColumn( ChangeHigh, "ChangeHigh", 1.0 );
AddColumn( IndexFilterOn, "IndexFilterOn", 1.0 );
//Can be shortened to a single line as such:
//IndexFilterOn = HHV( Sum( DateTimeDiff( DateTime(), Ref( DateTime(), -1 ) ) / 60 / 60 / 24, 8 ), 8 ) < 15;