Bullish/Bearish Outlook-Stocks in an Index above EMA 200 - Continued

This topic is continuation to the Topic "Bullish/Bearish Outlook - Stocks in an Index above EMA 200" which is closed now

To give a background:
The idea was to create an Index from a watchlist. The process is to check the percentage of bullishness and bearishness at a particular point of time.

Average % Change happened to an index of stocks above EMA 200 on Daily (or EMA 40 on Weekly) was calculated by adding individual % changes above
EMA 200 and then dividing by the number of stocks in the watchlist to get an average.

Basically this index does not have "Weightage" of individual stocks which are published by the stock exchanges.

Below is the code that worked:

//Reused @fxshrat's code originally developed for composite ATR
// Building a composite % increase from 200 EMA of Stocks in an Index
//e.g. In Amibroker Analysis select NIFTY 500 as watchlist. This Watchlist must be precreated with the stocks list
//In the setting make periodicity as Daily/Weekly as per the EMA value selected
// Select Filter -> Watchlist in AA window, 
// Check Pad&align and choose reference symbol
// Then 1st run Scan and then run Exploration
wlnum = GetOption( "FilterIncludeWatchlist" );
List = CategoryGetSymbols( categoryWatchlist, wlnum );
emaCrossValue = valuewhen( cross( close, ema(close,40) ) ,C, 1); //on Daily this is 200 and on weekly this is 40
deltaAmount = C - emaCrossValue;
percentageChange = (deltaAmount/emaCrossValue)*100;

if ( Status( "action" ) == actionScan ) {
	if ( Status( "stocknum" ) == 0 )	{		
		// cleanup variables created in previous runs
		StaticVarRemove( "~PercentChange*" );
	}
	// prevent accidentely applying to "All symbols"
	if ( StrFind(","+list+",",","+Name()+",") ) 
		StaticVarAdd( "~PercentChange", percentageChange );
}

svPERtotal= StaticVarGet( "~PercentChange" );
svSymbolCount = StrCount(list, ",")+1;
AvePercentageofIndexConstituents = svPERtotal/svSymbolCount;

///////////////////////////////////////////////////
// Explore, let's display the whole WL and confirm
///////////////////////////////////////////////////
Filter = NOT IsNull(svPERtotal);
AddColumn(percentageChange, "percentageChange");
AddColumn(svPERtotal , "svPERtotal", 1.2 );
AddColumn(svSymbolCount, "Total Number of Stocks", 1.0 );
AddColumn(AvePercentageofIndexConstituents , "AvePercentageofIndexConstituents ", 1.2 );

//AddToComposite(AvePercentageofIndexConstituents, "~~AVGAbove200EMA1", "X", atcFlagDeleteValues | atcFlagEnableInPortfolio );
//Equity Curve - Starts
if (Status("action") == actionExplore)
{
	AddToComposite(AvePercentageofIndexConstituents/svSymbolCount, "~~AVGAbove200EMA", "C", atcFlagDeleteValues | atcFlagEnableInExplore ); 
}
//Equity Curve - Ends


///////////////
// Chart
///////////////
Plot( AvePercentageofIndexConstituents, "AvePercentageofIndexConstituents", colorAqua, styleThick );

I encountered an issue off late and below is the description:

Old Watchlist (NIFTY 500) for Stocks. The ‚ÄúpercentageChange‚ÄĚ is blank for stocks that did not have price data at that point of time, the ‚ÄúsvPERtotal‚ÄĚ had positive value (summation of total %)
Ami1

New Watchlist (updated NIFTY 500 list) for Stocks. The ‚ÄúpercentageChange‚ÄĚ is blank for stocks that did not yet have price data at that point of time but the ‚ÄúsvPERtotal‚ÄĚ has big Negative value (it‚Äôs not the summation of total %). This leads to the incorrect final result ‚ÄúAvePercentageofIndexConstituents‚ÄĚ and the result is completely incorrect for that period of time for the Index created

Ami2

Root Cause:
I observed that this behaviour is happening because of one Stock (360ONE.NS) that has price data from 8/21/2020. If I remove this stock from the ‚ÄúLatest NIFTY 500 watchlist‚ÄĚ then the code works good as earlier.

I am not able to understand the reason for this issue just because of this one STOCK (360ONE.NS) addition to the watchlist because there are a lot of STOCKS which did not have price data. Any thoughts?

@mradtke
@fxshrat

svSymbolCount in that code is a number just counting watchlist members but it is not an array.
So to count only stocks having data at a bar make it an array by checking for Not IsNull(percentageChange) and adding a second Staticvaradd with its result plus another StaticvarRemove and etc.

In the svSymbolCount line you have to replace StrCount with StaticVarget to call the array result.

And...

Filter = NOT IsNull(svPERtotal) AND NOT IsNull(percentageChange);


That is non sense line as it leads to different results.

1 Like

Hello @fxshrat,

To correct the svSymbolCount i am trying with the below code but its not working. It is not entering inside the If loop. I am still trying. I am a little unclear on the array approach that you suggested, if possible please share some references:

count = 0;
for( i = 0; ( symbol = StrExtract( List, i ) ) != ""; i++ )
{
	if( NOT IsNull( percentageChange[i] ) )
	{
	count++;
	}
}

But the thing I want to understand is why is "svPERtotal" returing a very big negative number? And that too by adding this one stock "360ONE.NS to the watchlist.

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.