Simple Moving Average Cross Alert

 

Hi everyone,

I have a lot of experience with C and Java, so I though I'd start working on a simple MQL4 script that would alert me every time a 10EMA crosses a 5EMA, in any currencies available.

I don-t want it to trade, just to alert me, then I would manually review whether it's a good idea or not to trade.

Ideally it'd also check the stocha, MACD and ADX, but for now I'm having trouble simply with the EMA crosses.

Here's the basic code (the whole code is a bit more complexe as it handles all the currencies but here's the idea):



void start()

{
	// Get the current EMAs
	double d10EMA = iMA(NULL,0,10,0,MODE_EMA,PRICE_CLOSE,0);

	double d5EMA = iMA(NULL,0,5,0,MODE_EMA,PRICE_CLOSE,0);

	double diff = d10EMA-d5EMA;
	// Get the previous EMAs	
d10EMA = iMA(NULL,0,10,0,MODE_EMA,PRICE_CLOSE,1);
	d5EMA = iMA(NULL,0,5,0,MODE_EMA,PRICE_CLOSE,1);

	double previous_diff = d10EMA-d5EMA;

	if((previous_diff > 0 && diff < 0)||(previous_diff < 0 && diff > 0)) {

		Alert("CROSS!");

	}



}
 
 

Here's the way I would Naturally do it. Other efficient methods https://www.mql5.com/en/forum/139415.

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void start(){
    Alert_Ma_CrossOver();
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void Alert_Ma_CrossOver(){
    if(StringFind(Symbol(),"JPY")>-1){
        int _2Real=100;}else{_2Real=10000;}
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    int d5EMA_C = iMA(NULL,0,5,0,MODE_EMA,PRICE_CLOSE,0)*_2Real;
    int d5EMA_P = iMA(NULL,0,5,0,MODE_EMA,PRICE_CLOSE,1)*_2Real;
    int d10EMA_C =iMA(NULL,0,10,0,MODE_EMA,PRICE_CLOSE,0)*_2Real;
    int d10EMA_P =iMA(NULL,0,10,0,MODE_EMA,PRICE_CLOSE,1)*_2Real;
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    if(d5EMA_C>d10EMA_C && d5EMA_P<=d10EMA_P){bool CrossUp=true;}
    if(d5EMA_C<d10EMA_C && d5EMA_P>=d10EMA_P){bool CrossDn=true;}
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    if(CrossUp || CrossDn){Alert("Cross");}
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
Thanks ubzen, it's exactly what I needed
Reason: