How do i shorten this code

 

I've been trying out things but i really can't seem to figure it out, i tried doing loops but i'm probably doing something wrong.

What i wanted to happen is to only do a sell signal when all 10 candles is below the moving average.

Here is the photo to visualize more : https://ibb.co/LtHDPhp

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); 

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];

if (emaTrend0 > HighCandle0)  
   if (emaTrend1 > HighCandle1)
      if (emaTrend2 > HighCandle2)  
         if (emaTrend3 > HighCandle3)
            if (emaTrend4 > HighCandle4)  
               if (emaTrend5 > HighCandle5)
                  if (emaTrend6 > HighCandle6)  
                     if (emaTrend7 > HighCandle7)
                         if (emaTrend8 > HighCandle8)  
                              if (emaTrend9 > HighCandle9)
                                 Print("Sell Signal"); 
Untitled
Untitled
  • ibb.co
Image Untitled hosted in ImgBB
 
Topics concerning MT4 and MQL4 have their own section.
In future please post in the correct section.
I have moved your topic to the MQL4 and Metatrader 4 section.
 
Keith Watford:
Topics concerning MT4 and MQL4 have their own section.
In future please post in the correct section.
I have moved your topic to the MQL4 and Metatrader 4 section.
Noted. I'm sorry, i'll do better next time!
 
   bool conditionMet=true;
   int x=0;

   for(; x<=9; x++)
      {
      double emaTrend = iMA (NULL,0,200,0,MODE_EMA,PRICE_CLOSE,x);
      if (emaTrend > High[x])
         continue;
      else
         {
         conditionMet=false;
         break;
         }
      }

   if(conditionMet)
      Print("Sell Signal");
   else
      Print("No Sell Signal");

This may give you a few ideas how to shorten your code.

 

Incidentally, it can be shortened further.

Often in my codes I will actually look for when a condition is NOT met

   for(; x<=9; x++)
      {
      double emaTrend = iMA (NULL,0,200,0,MODE_EMA,PRICE_CLOSE,x);
      if (emaTrend <= High[x])
         {
         conditionMet=false;
         break;
         }
      }
 
Keith Watford:

Incidentally, it can be shortened further.

Often in my codes I will actually look for when a condition is NOT met

What would be the purpose of break, the loop would only run when all of the conditions met isn't it?
 
Renz Carillo:
What would be the purpose of break, the loop would only run when all of the conditions met isn't it?
I tried creating a function out of your concept but it returns not all control path returns a value
bool trend()
{
bool status;
int i;
for (i=0; i<10; i++)
{
double emaTrend = iMA (NULL,0,200,0,MODE_EMA,PRICE_CLOSE,i);
    if (emaTrend > High[i]) 
    {
    status = true;
    } 
    else if (emaTrend <Low[i])
    {
    status = false;
    } 
      return status;

}
 
Renz Carillo:
What would be the purpose of break, the loop would only run when all of the conditions met isn't it?

The execution of the loop has nothing to do with any conditions.

When it is found that one EA is smaller than the high, there is no point in continuing with the loop. Hence the break.

 
Renz Carillo:
I tried creating a function out of your concept but it returns not all control path returns a value
 return status;

Move this outside the loop

 
else if (emaTrend <Low[i])

Why do you have this?

The Low doesn't figure in your outline at all!

 
Keith Watford:

Why do you have this?

The Low doesn't figure in your outline at all!

It is originally in my condition for a buy/sell signal, i only incorporated sell signal in my code above so it would be alot easier to help me
Reason: