How can I shorten this code to avoid redundancy?

  c++, mql, mql4, mql5

Here’s my code snippet:

string trend()
{
    double emaTrend0 = iMA (NULL,0,200,0,MODE_EMA,PRICE_CLOSE,0);
    double emaTrend1 = iMA (NULL,0,200,0,MODE_EMA,PRICE_CLOSE,1); 
    double emaTrend2 = iMA (NULL,0,200,0,MODE_EMA,PRICE_CLOSE,2); 
    double emaTrend3 = iMA (NULL,0,200,0,MODE_EMA,PRICE_CLOSE,3); 
    double emaTrend4 = iMA (NULL,0,200,0,MODE_EMA,PRICE_CLOSE,4); 
    double emaTrend5 = iMA (NULL,0,200,0,MODE_EMA,PRICE_CLOSE,5); 
    double emaTrend6 = iMA (NULL,0,200,0,MODE_EMA,PRICE_CLOSE,6); 
    double emaTrend7 = iMA (NULL,0,200,0,MODE_EMA,PRICE_CLOSE,7); 
    double emaTrend8 = iMA (NULL,0,200,0,MODE_EMA,PRICE_CLOSE,8);  
    double emaTrend9 = iMA (NULL,0,200,0,MODE_EMA,PRICE_CLOSE,9); 
    string signal;
    
    double HighCandle0 = High[0];
    double HighCandle1 = High[1];
    double HighCandle2 = High[2];
    double HighCandle3 = High[3];
    double HighCandle4 = High[4];
    double HighCandle5 = High[5];
    double HighCandle6 = High[6];
    double HighCandle7 = High[7];
    double HighCandle8 = High[8];
    double HighCandle9 = High[9];
    
    double LowCandle0 = Low[0];
    double LowCandle1 = Low[1];
    double LowCandle2 = Low[2];
    double LowCandle3 = Low[3];
    double LowCandle4 = Low[4];
    double LowCandle5 = Low[5];
    double LowCandle6 = Low[6];
    double LowCandle7 = Low[7];
    double LowCandle8 = Low[8];
    double LowCandle9 = Low[9];
    
    if (emaTrend0 > HighCandle0 && emaTrend1 > HighCandle1 && emaTrend2 > HighCandle2 &&emaTrend3 > HighCandle3 &&
    emaTrend4 > HighCandle4 &&emaTrend5 > HighCandle5 &&emaTrend6 > HighCandle6 &&emaTrend7 > HighCandle7 &&
    emaTrend8 > HighCandle8 && emaTrend9 > HighCandle9 )  
      signal = "downtrend";
                                     
    else if (emaTrend0 < LowCandle0 && emaTrend1 < LowCandle1 && emaTrend2 < LowCandle2 &&emaTrend3 < LowCandle3 &&
    emaTrend4 < LowCandle4 &&emaTrend5 < LowCandle5 &&emaTrend6 < LowCandle6 &&emaTrend7 < LowCandle7 &&
    emaTrend8 < LowCandle8 && emaTrend9 < LowCandle9 )  
      signal = "uptrend";
    return signal;
}

What I wanted to happen is to loop the variable and every condition must be true before producing the signal such as downtrend or uptrend.

The code below is my attempt, but it was still no good. My attempt is to produce a signal of downtrend or uptrend when one of the conditions is true, but what I need is for everything to be true before producing a signal.

string trend() {
    string signal = "";
    for (int i = 0; i<=9 ; i++){
        double emaTrend = iMA (NULL,0,200,0,MODE_EMA,PRICE_CLOSE,i);
        double highCandle = High[i];
        double lowCandle = Low[i];
        
            if (emaTrend > highCandle){
            signal = "downtrend";
            }
            else if (emaTrend < lowCandle){
            signal = "uptrend";
            }
    }
    return signal;
}

All replies would be deeply appreciated. Thank you!

Source: Windows Questions C++

LEAVE A COMMENT