0

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};
//---

  }

1 Answer 1

1

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;}
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.