[MT4][mql4][programming] Sample code to detect golden cross

Many people would like to create a program that detects golden cross or dead cross with mql4 and performs automatic trading.
Here is a sample code that can check if there is a golden / dead cross using Moving Average or MACD.

There is also a sample code post if you want to predict the golden cross in advance, not after the fact, so please check it as well.

Also, for the sample code that will be the format for implementing mql4 and the outline, first see the format file and outline of Experts Advisor.

Contents

Sample code to detect golden / dead cross using MA

In this sample code, I will introduce the sample code to detect the golden cross or dead cross using Moving Average.
(The moving average is a code that checks the cross between 30 and 60.)
※Please change the moving average value according to your request.

We are using iMA() to get the value of the Moving Average. Please check the reference for detailed function specifications.

Sample code

// Function description : 
//   can get golden(dead) cross or not.
// IN :
//   timeframe : Specifies the Period used to detect the golden cross.
//       shift : Specify the target time (shift amount) used to detect the golden cross.
// OUT : 
//   ret : The calculation result(0 : not cross, 1 : cross to long, -1 : cross to short) will be returned.
//
int getCrossType(int timeframe, int shift)
{
	int ret	= 0;
	
	
	// Whether or not made a golden cross
	// Whether after crossing at the specified time.
	if(iMA(NULL,timeframe,30,0,MODE_SMA,PRICE_CLOSE,shift) > iMA(NULL,timeframe,60,0,MODE_SMA,PRICE_CLOSE,shift))
	{
		// Whether it was not crossed one time before the specified time
		if(iMA(NULL,timeframe,30,0,MODE_SMA,PRICE_CLOSE,shift+1) <= iMA(NULL,timeframe,60,0,MODE_SMA,PRICE_CLOSE,shift+1))
		{
			ret = 1;
		}
	}
	// Whether or not made a dead cross
	// Whether after crossing at the specified time.
	else if(iMA(NULL,timeframe,30,0,MODE_SMA,PRICE_CLOSE,shift) < iMA(NULL,timeframe,60,0,MODE_SMA,PRICE_CLOSE,shift))
	{
		// Whether it was not crossed one time before the specified time
		if(iMA(NULL,timeframe,30,0,MODE_SMA,PRICE_CLOSE,shift+1) >= iMA(NULL,timeframe,60,0,MODE_SMA,PRICE_CLOSE,shift+1))
		{
			ret = -1;
		}
	}
	
	return ret;
}

Explanation of sample code

argument:

Timeframe of the first argument : Specify the reference timeframe when getting the value of Moving Average. The value to be specified is one of the Define values represented by ENUM_TIMEFRAMES.

shift of second argument : When getting the value of Moving Average, specify the reference time (shift amount from current Bar).
If you want to detect the cross with the latest value, specify “0”.

Cross detection processing :

Golden cross detection is on the 16th line, dead cross detection is on the 26th line, and it is a code to check “whether the specified time has been crossed”.

Also, on the 19th or 29th line, it is confirmed that “the one before the specified time does not cross”.

Set a return value indicating that a cross has been detected when both of the above two conditions are cleared.

Return value :

The return value that has the following meanings.
0:not cross
1:golden cross
2:dead cross

Sample code to detect golden / dead cross using MACD

If you change the code of iMA () used in “Cross detection processing” explained above to use the MACD value, you can detect the golden cross / dead cross using MACD.

The function to get the MACD value uses iMACD().

I think you should compare the following two and cross-check.
iMACD(NULL,timeFrame,12,26,9,PRICE_CLOSE,MODE_MAIN,shift) and iMACD(NULL,timeFrame,12,26,9,PRICE_CLOSE,MODE_SIGNAL,shift).
※Change the detailed argument parameters to the values you want.