Many people would like to create a program that predicts whether to make a golden cross or dead cross on the next period with mql4 and performs automatic trading.
Here is a sample code that predicts whether to make a golden / dead cross using Moving Average or MACD on the next bar.
Also, Check out the sample program post that detects if you have a golden cross or a dead cross in the current bar.
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 predict whether the next bar will have a golden / dead cross using MACD
In this sample code, I will introduce the sample code that predicts whether to make a golden cross or dead cross on the next bar using MACD.
I am using iMACD () to get the value of MACD. Please check the reference for detailed function specifications.
This sample program for predicting golden cross / dead cross is just sample code for predicting whether or not the next bar is likely to cross from the tilts of the past two lines.
※If the line transition suddenly differs from the past line flow, it may not cross even on the next bar.
Sample code
bool IsCrossInTheNextPeriod(int timeframe)
{
bool ret = false;
double currentDiffValue = iMACD(NULL,timeFrame,12,26,9,PRICE_CLOSE,MODE_MAIN,0) - iMACD(NULL,timeFrame,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0);
double currentTilt = getMacdTilt(timeframe, 0);
//When the inclination is negative, there is a possibility of crossing with the next bar, so check the crossing only when the inclination is negative.
if(currentTilt < 0)
{
currentDiffValue = MathAbs(currentDiffValue);
//Check if the two lines cross at the next bar
if((currentDiffValue + currentTilt) < 0) { ret = true; }
}
return ret;
}
double getMacdTilt(int timeFrame, int shift)
{
double base = (iMACD(NULL,timeFrame,12,26,9,PRICE_CLOSE,MODE_MAIN,shift) - iMACD(NULL,timeFrame,12,26,9,PRICE_CLOSE,MODE_MAIN,shift + 1));
double target = (iMACD(NULL,timeFrame,12,26,9,PRICE_CLOSE,MODE_SIGNAL,shift) - iMACD(NULL,timeFrame,12,26,9,PRICE_CLOSE,MODE_SIGNAL,shift + 1));
double ret = 0;
if(iMACD(NULL,timeFrame,12,26,9,PRICE_CLOSE,MODE_MAIN,shift) > iMACD(NULL,timeFrame,12,26,9,PRICE_CLOSE,MODE_SIGNAL,shift)) {
ret = base - target;
}
else {
ret = target - base;
}
return ret;
}
Explanation of sample code
Function :
IsCrossInTheNextPeriod() : A function that predicts whether a golden / dead cross will occur on the next bar.
getMacdTilt() : A function that gets the tilts of two lines when predicting whether to cross. This function called from IsCrossInTheNextPeriod().
Description of processing :
The fourth line gets the difference between the current latest values of the two lines (this time the two lines of MACD Main and Signal).
The fifth line compares the two lines (this time the two lines of MACD Main and Signal) and calculates the line and the tilt of the line.
By calculating this tilt, you can predict whether the two lines will intersect on the next bar.
If this tilt is positive, it indicates that the two lines are in a expanding relationship.
If this tilt is negative, it means that the two lines are getting narrower.
Line 13 predicts whether the two lines will cross by “summing the latest difference between the two lines and the tilt of the two lines”.
To determine if the next bar is a golden cross or a dead cross, compare the current values of the two lines that predict the cross to see if the next bar is golden or dead. You can judge.
It is an image that implements a process to check whether it is golden or dead when the return value of IsCrossInTheNextPeriod () is true.
Sample code to predict whether to make a golden / dead cross on the next bar using MA
You can predict the golden cross / dead cross using the Moving Average by modifying the code of iMACD () in the sample program described above to use the value of MA.
Use iMA() to get the value of the Moving Average.
If you replace the code that represents the two lines of MACD MAIN and SIGNAL with the two lines of the moving average that you want to cross-predict, you can make a prediction using the moving average.