Exclude Stocks using AddToComposite using InWatchListName

This post refers to idea to EXCLUDE symbols from multiple watchlists while calculating the AddToComposites.

Just to explain, the database has Underlying, First Month, Second Month & Third Month futures. The futures are available only for limited stocks. I wish not to include the First Month, Second Month & Third Month futures for calculations so that data is clean for only the underlying alone.

  1. For ease of work, I have put the 3 months futures in a separate watchlist let’s say “2 - NSE FO Exclude”, but how do I exclude it in the code? I tried the following stuff:

if(!InWatchListName(“2 - NSE FO Exclude”)) AddToComposite(C>Ref(C,-1),"~NSE - Total Advances",“X”);

astonishingly there is no syntax error till the time the scan is RUN. But the calculated data is wrong.

The test code is as follows:

_SECTION_BEGIN("Composites");

// Advance / Decline Composite Index

AddToComposite(C>Ref(C,-1),"~Advances","X"); 

AddToComposite(C<Ref(C,-1),"~Declines","X"); 

AddToComposite(C==Ref(C,-1),"~NoChange","X"); 

AddToComposite ( IIf( C>Ref(C,-1), V, 0),"~Advance Volumes","C");

AddToComposite ( IIf( C<Ref(C,-1), V, 0),"~Decline Volumes","C");

AddToComposite ( IIf( C==Ref(C,-1), V, 0),"~Unchanged Volumes","C");
/*
ValChg = ROC(C,1) * V;
AddToComposite( IIf( C>Ref(C,-1), Valchg , 0), "~Advancing Value", "C");

AddToComposite( IIf( C<Ref(C,-1), -Valchg , 0), "~Declining Value", "C");

//NSE - NIFTY 50 SPECIFIC

if( InWatchListName("NSE - NIFTY 50") ) AddToComposite(C>Ref(C,-1),"~NSE - NIFTY 50 Advances","X"); 

if( InWatchListName("NSE - NIFTY 50") ) AddToComposite(C<Ref(C,-1),"~NSE - NIFTY 50 Declines","X"); 

if( InWatchListName("NSE - NIFTY 50") ) AddToComposite(C==Ref(C,-1),"~NSE - NIFTY 50 NoChange","X"); 

if( InWatchListName("NSE - NIFTY 50") ) AddToComposite ( IIf( C>Ref(C,-1), V, 0),"~NSE - NIFTY 50 Advance Volumes","C");

if( InWatchListName("NSE - NIFTY 50") ) AddToComposite ( IIf( C<Ref(C,-1), V, 0),"~NSE - NIFTY 50 Decline Volumes","C");

if( InWatchListName("NSE - NIFTY 50") ) AddToComposite ( IIf( C==Ref(C,-1), V, 0),"~NSE - NIFTY 50 Unchanged Volumes","C");

ValChg = ROC(C,1) * V;
if( InWatchListName("NSE - NIFTY 50") ) AddToComposite( IIf( C>Ref(C,-1), Valchg , 0), "~NSE - NIFTY 50 Advancing Value", "C");

if( InWatchListName("NSE - NIFTY 50") ) AddToComposite( IIf( C<Ref(C,-1), -Valchg , 0), "~NSE - NIFTY 50 Declining Value", "C");

Buy = 0;

//NSE - NIFTY MIDCAP100 SPECIFIC

if( InWatchListName("NSE - NIFTY MIDCAP100") ) AddToComposite(C>Ref(C,-1),"~NSE - NIFTY MIDCAP100 Advances","X"); 

if( InWatchListName("NSE - NIFTY MIDCAP100") ) AddToComposite(C<Ref(C,-1),"~NSE - NIFTY MIDCAP100 Declines","X"); 

if( InWatchListName("NSE - NIFTY MIDCAP100") ) AddToComposite(C==Ref(C,-1),"~NSE - NIFTY MIDCAP100 NoChange","X"); 

if( InWatchListName("NSE - NIFTY MIDCAP100") ) AddToComposite ( IIf( C>Ref(C,-1), V, 0),"~NSE - NIFTY MIDCAP100 Advance Volumes","C");

if( InWatchListName("NSE - NIFTY MIDCAP100") ) AddToComposite ( IIf( C<Ref(C,-1), V, 0),"~NSE - NIFTY MIDCAP100 Decline Volumes","C");

if( InWatchListName("NSE - NIFTY MIDCAP100") ) AddToComposite ( IIf( C==Ref(C,-1), V, 0),"~NSE - NIFTY MIDCAP100 Unchanged Volumes","C");

ValChg = ROC(C,1) * V;
if( InWatchListName("NSE - NIFTY MIDCAP100") ) AddToComposite( IIf( C>Ref(C,-1), Valchg , 0), "~NSE - NIFTY MIDCAP100 Advancing Value", "C");

if( InWatchListName("NSE - NIFTY MIDCAP100") ) AddToComposite( IIf( C<Ref(C,-1), -Valchg , 0), "~NSE - NIFTY MIDCAP100 Declining Value", "C");
*/

Buy = 0;

_SECTION_END();

I did not have time to check your code, maybe it works maybe not. The very first thing that I see that in your code you don’t have ! (NOT) operator, while in post you mention ! Inwatchlist. So the logic in your post is REVERSE to what you are doing in the code.

Also AddToComposite needs to do its “reset” magic on first symbol in scan, so multiple scans don’t cause “accumulation” of all previous data, but start fresh. If you skip the very first symbol you may be getting accumulated values.
For this reason it is better to “skip” symbols by adding ZERO:

data = C > Ref( C, -1 );
data = IIF( InWatchListName("blablah"), data  , 0 ); // use ZERO if does not belong to watch list
AddToComposite( data, "~Name", "X" ); // unconditional call
1 Like