distance between 2 Moving average in different timeframres

 
Hi guys, how to get the distance between 2 Moving average in different timeframres?
 
Ahmed Elsayed:
Hi guys, how to get the distance between 2 Moving average in different timeframres?

Hello . 

How will you be using it ?

The simplest way is to just call the : https://docs.mql4.com/indicators/ima iMA() (built in moving average)

double  iMA(
   string       symbol,           // symbol
   int          timeframe,        // timeframe
   int          ma_period,        // MA averaging period
   int          ma_shift,         // MA shift
   int          ma_method,        // averaging method
   int          applied_price,    // applied price
   int          shift             // shift
   );

For the higher and current timeframe (or another timeframe)

You will have to ensure the data is delivered though . 

bool live_ma_diff(ENUM_TIMEFRAMES first_tf,//the 1st timeframe 
                           int first_tf_period,//1st tf ma period
                           ENUM_MA_METHOD first_tf_method,//1st tf ma method
                           ENUM_APPLIED_PRICE first_tf_price,//1st tf ma price
                           ENUM_TIMEFRAMES second_tf,//the 2nd timeframe 
                           int second_tf_period,//2nd tf ma period
                           ENUM_MA_METHOD second_tf_method,//2nd tf ma method
                           ENUM_APPLIED_PRICE second_tf_price,//2nd tf ma price
                           double &ma1,double &ma2,double &diff){//the result values
ResetLastError();
int errors=0;//error collector
//get live ma1 , do not change the bar index , the process should be different for history bars
ma1=iMA(_Symbol,first_tf,first_tf_period,0,first_tf_method,first_tf_price,0);
        errors+=GetLastError();ResetLastError();
//get live ma2 
ma2=iMA(_Symbol,second_tf,second_tf_period,0,second_tf_method,second_tf_price,0);
        errors+=GetLastError();ResetLastError();
//if no errors 
if(errors==0){
//calculate the difference
diff=ma1-ma2;//or the absolute of , or whatever order you want it in
return(true);
}
return(false);
}
Reason: