Skip to content

Q & A 29 | An indicator which counts moving average crosses and DOJIs

A Gold Pass member asked how to create an indicator that counted the total number of time two moving averages crossed each other and the total number of DOJI’s that occurred on a chart.

The following is a simple program to meet these requirements:


Inputs: FastLength( 9 ),
SlowLength( 18 ),

DojiPercentage( 10 );

Vars: FastAvg( 0 ),
SlowAvg( 0 ),

_Cross( False ),
Doji( False ),

DojiCtr( 0 ),
XCounter( 0 );

// Calculate the moving averages
FastAvg = AverageFC( C, FastLength );
SlowAvg = AverageFC( C, SlowLength );

// Plot the moving averages
Plot1( FastAvg );
Plot2( SlowAvg );

// Check to see if a cross has occurred, if so set _Cross to TRUE
If FastAvg crosses over SlowAvg or FastAvg crosses under SlowAvg then _Cross = True
Else _Cross = False;

// Determine if Doji has occurred, if so set Doji to TRUE
If C_Doji( DojiPercentage ) = 1 then Doji = True
Else Doji = False;

// On the last tick of the bar, print whether a cross or doji occurred
// Increment Doji and cross counters if Doji or Cross, respectively, occurred
If BarStatus( 1 ) = 2 then
Begin
Print( D, " ", T, " Cross ", _Cross, " Doji ", Doji );
If Doji then DojiCtr += 1;
If _Cross then XCounter += 1;
End;

If LastBarOnChart and BarStatus( 1 ) = 2 then
Print( "Number of times Doji appeared: ", DojiCtr, " Number of times moving average cross occured ", XCounter );