I want to list stocks that respect a specific EMA. For example, I want to list those stocks in my watchlist which respect their 20 days EMA.
To do this, I was thinking to consider the percentage of the daily bars that close above required EMA. For example, I assume a stock respects its 20 days EMA if 70% of its daily price is close above 20 days moving average.
I wrote the following code. However, although it counts and calculates the percentage correctly (as I debugged), it does not provide the result I am looking for.
For example, I know that Blink recently respects its 20 days EMA but does not appear in the results.
Besides, I want the list of the stocks, not the list of stocks at a different date that price was above EMA.
Appreciate if someone can advise me.
Thanks
Percenage = 70; // the percentage of price bars close above desired EMA
desiredEMA = 20;
count = 0; //Number of days that price Close above desired EMA
m = EMA(Close,desiredEMA);
for (i = 1; i < BarCount; i++)
{
if (close[i] > m[i]) count++;
}
Filter = (count/BarCount)*100 > Percenage;
Here is a result I'm getting from the above code. But the ideal output for me would be just a list containing AAPL and FB.
As to your issue, it depends on what you are really looking for. You will probably have to refine your expectation and your code...
I would suggest that you start with adding some additional columns to your exploration. Look at the "AddColumn" function.
Also, your method of determining the % (a loop), is not recommended in AB/AFL.
A more AB/AFL method is using the arrays.
// Stocks Respecting EMA
// From forum
// Code by Snoopy.pa30
pDesiredPercentage = Param("Desired Percentage", 70, 1, 100, 1, 0);
pDesiredEMA = Param("Desired EMA", 20, 3, 99, 1, 0);
pLookBack = Param("Look Back", 63, 20, 262, 1, 0); // Alternate time for EMA Respect
vDesiredEMA = EMA(C, pDesiredEMA);
vCaboveEMA = C>vDesiredEMA; // Generate array with status
vSumAll = Sum(vCaboveEMA, LastValue(BarIndex()) -1 - pDesiredEMA); // Sum must exclude the undefined EMA calc space
vSumLookback = Sum(vCaboveEMA, pLookback); // Sum over Alternate time for EMA Respect
vPercentageAll = vSumAll/(LastValue(BarIndex()) - 1 - pDesiredEMA) * 100; // % for All bars, Excluding undefined EMA calc space
vPercentageLookback = vsumlookback/pLookback * 100; // % over lookback
Filter = (vPercentageAll > pDesiredPercentage) OR (vPercentageLookback > pDesiredPercentage);
AddColumn(vPercentageLookback, "Look Back %");
AddColumn(vPercentageAll, "% All");