Home > Software engineering >  Using if else statement in MQL5 with "="
Using if else statement in MQL5 with "="

Time:09-13

I was wondering if something like this could work. Can you assign a value to something with 'if' 'else'? So in the code I used = and if else with each other, could this work?

void OnTick()
  {
   int      UMA = if (close>open)
                  {high-close};
                  else
                  {high-open};
                                   
   int      LMA = if (close>open) 
                  {low-open}; 
                  else
                  {low-close};  
    
   int      UMA1 = if (close>open)
                  {close UMAX}; 
                  else
                  {open UMAX};       
   
   int      LMA1 = if (close>open)
                  {open LMAX}; 
                  else
                  {close LMAX};
//---

  }

CodePudding user response:

You can not use if else statement like that in MQL5. Your code would need rearranging as follows.

void OnTick()
{
   int UMA, LMA, UMA1, LMA1;
   if(close>open) UMA=high-close; else UMA=high-open;
   if(close>open) LMA=low-open; else LMA=low-close;
   if(close>open) UMA1=close UMAX; else UMA1=open UMAX;
   if(close>open) LMA1=open LMAX; else LMA1=close LMAX;
}

The above is set out as your code, but this could be further improved as follows.

void OnTick()
{
   int UMA, LMA, UMA1, LMA1;
   if(close>open) {UMA=high-close; LMA=low-open; UMA1=close UMAX; LMA1=open LMAX;}
   else {UMA=high-open; LMA=low-close; UMA1=open UMAX; LMA1=close LMAX;}
}
  • Related