Uptrend and Downtrend of moving average

Please help me in writing an AFL for :
Uptrend = MA 50 is moving upward &
Downtrend = MA 50 is moving downward.

@drana, There are lots of ways to try and do this.... What have you tried?

Show us your code, and tell us what is not working the way you expect (don't forget to use code blocks "</>").

Here is one way.....(probably not the smartest definition of moving up/down :grinning: )

ma50 = ma(c,50);
uptrend = ma50>ref(ma50,-1);
downtrend = ma50<(ref(ma50,-1));
2 Likes

I agree with @snoopy.pa30

In my own quest of hunting Moving Averages I have found McGinley Dynamic as a good start.

Here is the code for you:

_SECTION_BEGIN("McGinley Dynamic Indicator");
	 SetBarsRequired( sbrAll );
	 SetChartOptions(0,chartShowArrows);
	 
	 per = Param( "McGinley Dynamic", 25, 5, 40, 5 );
	 MD[0] = C[0];

	 for( i = 1; i < BarCount; i++ )
	 {
		 MD[ i ] = MD[ i - 1 ] + ( C[ i ] - MD[ i - 1 ] ) / ( per * ( C[ i ] / MD[ i - 1 ] ) ^ 4 );
	 }

	 Plot( MD, "McGinley Dynamic", ParamColor( "McGin. Color", colorDarkBlue ), styleLine | styleThick );
_SECTION_END();

Generally if Close is greater than MD then Uptrend, or if Close is less than MD then Downtrend. BTW depends on the timescale.

So to further ascertain - being a fan of Fibonacci - I modified the above code to create a band of MD lines on a price chart starting from a smaller 8-periods to longer 55-periods. Now if the 8 period line pierces the higher ones, it depicts the continuation or reversal in accordance to the trend.

_SECTION_BEGIN( "Moving Averages" );
	 maType = ParamToggle( "Moving Avg. Type", "EMA|Mc. Gingley", 1 );

	 MD[0] = C[0];
	 t1 = 3;
	 t2 = 5;
	 for( i = 1; i <= 5; ++i )
	 {
		 nextTerm = t1 + t2;
		 t1 = t2;
		 t2 = nextTerm;
     
		 N = nextTerm;
		 colorMA = IIf( N == 8, colorWhite, IIf( N == 13, ColorRGB( 154, 1, 1 ), IIf( N == 21, colorBlue, IIf( N == 34, colorYellow, colorBlack ) ) ) );
		 
		 if( maType )
		 {
			 for( j = 1; j < BarCount; j++ )
			 {
				 MD[ j ] = MD[ j - 1 ] + ( C[ j ] - MD[ j - 1 ] ) / ( N * ( C[ j ] / MD[ j - 1 ] ) ^ 4 );			 
			 }
			 Plot( MD, "MD( " + N + " )", colorMA, styleLine | styleThick | styleNoRescale | styleNoLabel, Null, Null, 0, 0, 2 );
		 }
		 else
		 {
			 Plot( EMA( ( H + L + C + C ) / 4, N ), "EMA( " + N + " )", colorMA, styleLine | styleThick | styleNoRescale | styleNoLabel, Null, Null, 0, 0, 2 );
		 }
}
_SECTION_END();
6 Likes