# Explore stocks that have clustered moving averages

I have a bunch of moving averages and looking to find stocks that have my moving averages converge tightly.

I am using below code, it works but i am seeing that it has missed some too.

``````_SECTION_BEGIN("Launchpad");
SMA10 = MA( Close, 10);
SMA21 = MA( Close, 21);
EMA23 = EMA( Close, 23);
SMA50 = MA( Close, 50);
EMA65 = EMA( Close, 65);
UpTrend = SMA10 > SMA21 ;
LPRange = 1.5/100; // Variable to adjust the range of moving averages

LPCon = SMA10 < EMA23 * (1 +  LPRange) AND EMA65 > EMA23 * (1 -  LPRange);
LPSetup = IIf( LPCon AND UpTrend, True, False );

_SECTION_END();
``````
2 Likes

A couple of quick thoughts..

1. I don't recall the order of operations, so I would add brackets '()' around the pieces of your LPCon calculation to ensure it is happening the way you want.
2. I would put the components (SMAs/EMAs/LPCon/LPSetup) into an Exploration to confirm calculations.
3. Review the exploration over a time frame where you say a stock has been missed to see if your eves and calculations agree.

Hope that helps.

Another question:
Is there a way i can do a proximity analysis in afl? whether moving averages i am looking for are close to each other?

That search is worth years of study to find a satisfying conclusion. I know nothing of ML, ideally, these type of problems are better served using Clustering.

Why ML? Because in other scenarios, such as, using Std. Dev. or Percentile of MA difference to determine narrow region(s) a threshold needs to be used. Now, using same threshold everywhere is futile. So, optimization is required anyways to ascertain an underlying's wiggle factor.

Instead of using ML, here is a simple application using Stochastics.

``````/*
_____________________________________________________________________________________________
|This code is for AmiBroker Formula Language (AFL) learning (non-commercial) purposes only.  |
|Please do not copy this code (or any other version of it) and paste it over on other forums |
|or anywhere else on the Internet or in any other form without the AmiBroker Forum owner's   |
|consent (https://forum.amibroker.com/).                                            		 |
_____________________________________________________________________________________________|
*/

_SECTION_BEGIN( "Detecting MA Proximity using Stochastics" );
SetChartBkColor( ColorRGB( 2, 18, 30 ) );

price = C;
Plot( price, "Price", colorDefault, styleCandle );

maTypeCh = ParamList( "Choose MA Type", "SMA|EMA|Wilders", 0 );

maPerList = "10,20,50,100"; // comma-separated list of MA periods
maMxRGBColorSet = MxFromString( StrFormat( // a matrix storing RGB for each MA in maPerList
"{
{188, 229, 251},
{108, 197, 246},
{28, 166, 242},
{10, 116, 174}
}"
) );

maxMA = minMA = 0;
numOfMAs = StrCount( maPerList, "," );
for( i = 0; ( maPer = StrExtract( maPerList, i ) ) != ""; ++i ) {
maPer = StrToNum( maPer );

switch( maTypeCh ) {
case "SMA":
movingAvg = MA( price, maPer );
break;
case "EMA":
movingAvg = EMA( price, maPer );
break;
case "Wilders":
movingAvg = Wilders( price, maPer );
break;
}

maxMA = IIf( movingAvg > maxMA, movingAvg, maxMA );
minMA = IIf( movingAvg < minMA, movingAvg, minMA );

maColor = ColorRGB(
maMxRGBColorSet[ i ][ 0 ],
maMxRGBColorSet[ i ][ 1 ],
maMxRGBColorSet[ i ][ 2 ]
);
Plot( movingAvg, maTypeCh + "(" + maPer + ")", maColor, styleNoLabel | styleNoRescale );
}

maRng = maxMA - minMA;
stochMaRngPer = Param( "Set Stochastic Period", 100, 20, 100, 1 );
stochMaRng = 100 * ( ( maRng - LLV( maRng, stochMaRngPer ) ) / ( ( HHV( maRng, stochMaRngPer ) ) - LLV( maRng, stochMaRngPer ) + 1e-12 ) );
// Plot( stochMaRng, "stochMaRng", colorDarkYellow, styleThick | styleOwnScale );

converging = stochMaRng < Ref( stochMaRng, -1 ) AND Ref( stochMaRng, -1 ) > Ref( stochMaRng, -2 );
// diverging = stochMaRng > Ref( stochMaRng, -1 ) AND Ref( stochMaRng, -1 ) < Ref( stochMaRng, -2 );
// confluence = ExRem( converging, diverging );
PlotShapes( converging * shapeSmallSquare, colorGold, 0, Status( "axisMinY" ), 0 );
_SECTION_END();
``````

Whatever method is used, such analysis would always remain subjective.

1 Like

@erukumk , as close (near to something - not ending price) is a relative term, you will need to define it.

A simple way might be the following:

``````ma1 = MA(C,5);
ma2 = MA(C,10);
nearpct = 5;

near = abs(ma1-ma2) < (ma1*nearpct / 100);
``````

Here is one idea using Guppy moving averages

Viewing a thread - Guppy Multiple Moving Averages (GMMA).. Using the coefficient of variation of the moving averages.
This can be coded readily and there is no reason to incorporate the trough (zig zag) indicator noted in the last two lines of code. Values under 0.75 show compression of the moving averages.

1 Like

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