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 %)
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
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?