Help needed for if conditions

 

Hello, new to coding here. Can someone please tell me are the if conditions contradicting here? It's a strategy combining EMAs and Stochastics. It is not giving me any order when I backtest it. But if I separate the EMA and Stochastic conditons, it works.

Attached is an example of the strategy.


//FOR BUY ORDERS

      if (EMA9 > EMA225 && Close[2] < EMA9 && Close[1] > EMA9 && K533 > D1833 && K533 > 20)

         {

         Print("Executing Buy Order");     

         executeBuy();

         }

      

      

      //FOR SELL ORDERS      

      else if(EMA9 < EMA225 && Close[2] > EMA9 && Close[1] < EMA9 && K533 < D1833 && K533 < 80)

         {

         Print("Executing Sell Order");         

         executeSell();

         } 


Would separating the if statements like this works? 

//FOR BUY ORDERS

      if (EMA9 > EMA225 && Close[2] < EMA9 && Close[1] > EMA9) 

      if (K533 > D1833 && K533 > 20)

         {

         Print("Executing Buy Order");         

         executeBuy();

         }

      

      

      //FOR SELL ORDERS      

      else if(EMA9 < EMA225 && Close[2] > EMA9 && Close[1] < EMA9)

      if(K533 < D1833 && K533 < 80)

         {

         Print("Executing Sell Order");         

         executeSell();

         } 

Files:
tradeprint.PNG  22 kb
 
masterpotato:

Hello, new to coding here. Can someone please tell me are the if conditions contradicting here? It's a strategy combining EMAs and Stochastics. It is not giving me any order when I backtest it. But if I separate the EMA and Stochastic conditons, it works.

Attached is an example of the strategy.


//FOR BUY ORDERS

      if (EMA9 > EMA225 && Close[2] < EMA9 && Close[1] > EMA9 && K533 > D1833 && K533 > 20)

         {

         Print("Executing Buy Order");     

         executeBuy();

         }

      

      

      //FOR SELL ORDERS      

      else if(EMA9 < EMA225 && Close[2] > EMA9 && Close[1] < EMA9 && K533 < D1833 && K533 < 80)

         {

         Print("Executing Sell Order");         

         executeSell();

         } 


Would separating the if statements like this works? 

//FOR BUY ORDERS

      if (EMA9 > EMA225 && Close[2] < EMA9 && Close[1] > EMA9) 

      if (K533 > D1833 && K533 > 20)

         {

         Print("Executing Buy Order");         

         executeBuy();

         }

      

      

      //FOR SELL ORDERS      

      else if(EMA9 < EMA225 && Close[2] > EMA9 && Close[1] < EMA9)

      if(K533 < D1833 && K533 < 80)

         {

         Print("Executing Sell Order");         

         executeSell();

         } 

Just use the Alt+S when you want to type something and paste your code there in the block, then send it.

both the EMA conditions and the Stochastic conditions are combined in a single ifstatement for both buy and sell orders. The problem with this approach might be that both sets of conditions need to be simultaneously met for an order to be executed. If any of the conditions within the if statement fail, the entire condition fails, resulting in no order execution.

By separating the conditions into two if statements, you're effectively making them independent of each other. This means that even if one set of conditions fails, the other set might still be true, allowing for order execution.

//FOR BUY ORDERS
if (EMA9 > EMA225 && Close[2] < EMA9 && Close[1] > EMA9) 
    if (K533 > D1833 && K533 > 20)
    {
        Print("Executing Buy Order");         
        executeBuy();
    }

//FOR SELL ORDERS      
else if (EMA9 < EMA225 && Close[2] > EMA9 && Close[1] < EMA9)
    if (K533 < D1833 && K533 < 80)
    {
        Print("Executing Sell Order");         
        executeSell();
    }
 
Nardus Van Staden #:

Just use the Alt+S when you want to type something and paste your code there in the block, then send it.

both the EMA conditions and the Stochastic conditions are combined in a single ifstatement for both buy and sell orders. The problem with this approach might be that both sets of conditions need to be simultaneously met for an order to be executed. If any of the conditions within the if statement fail, the entire condition fails, resulting in no order execution.

By separating the conditions into two if statements, you're effectively making them independent of each other. This means that even if one set of conditions fails, the other set might still be true, allowing for order execution.

Thanks for you reply! What if I want both the conditions to be met? My strategy is that, for buy orders, if 1. both price and EMA9 above EMA225; 2. there was a pull back and price went below EMA9, then if price crosses and goes above EMA9 again, buy at next candle; while 3. Stochastic is on the same direction i.e. rising, with K above D and K above 20.

So if I separate the two if statements, it wouldn't be the case then? It looks like an "or" situation.

 
  1. Please edit your (original) post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)
          General rules and best pratices of the Forum. - General - MQL5 programming forum #25 (2019)
              Messages Editor
          Forum rules and recommendations - General - MQL5 programming forum (2023)

  2. Always post all relevant code (using Code button) or attach the source file.

    We have no idea what EMA9 is, or how it is updated. Likewise, all those other  nonsensical variable names; are you using decompiled code?

 
Nardus Van Staden #:

Just use the Alt+S when you want to type something and paste your code there in the block, then send it.

both the EMA conditions and the Stochastic conditions are combined in a single ifstatement for both buy and sell orders. The problem with this approach might be that both sets of conditions need to be simultaneously met for an order to be executed. If any of the conditions within the if statement fail, the entire condition fails, resulting in no order execution.

By separating the conditions into two if statements, you're effectively making them independent of each other. This means that even if one set of conditions fails, the other set might still be true, allowing for order execution.

Also for some reason, it is executing buy orders even if EMA9 is below EMA225 when backtesting. But I don't see anything in the conditions that cause it
 
William Roeder #:
  1. Please edit your (original) post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)
          General rules and best pratices of the Forum. - General - MQL5 programming forum #25 (2019)
              Messages Editor
          Forum rules and recommendations - General - MQL5 programming forum (2023)

  2. Always post all relevant code (using Code button) or attach the source file.

    We have no idea what EMA9 is, or how it is updated. Likewise, all those other  nonsensical variable names; are you using decompiled code?

hello, sorry I followed your link but there is only "to pocket" or "reply for the original post. I saw edit on my replies though.
 

I don't know what's wrong with your first code. But the second code, apparently, has an error. I added two extra square brackets to show how your second code works (I didn't test it):

[I removed my code where I made a mistake]

If I'm not mistaken, then my code above is completely equivalent to your second code

[EDIT]

However, I made a mistake trying to show how your code works😄

 
Vladislav Boyko #:

I don't know what's wrong with your first code. But the second code, apparently, has an error. I added two extra square brackets to show how your second code works (I didn't test it):

If I'm not mistaken, then my code above is completely equivalent to your second code

I think he showed u the correct one there mate 
 

There is a good example in the documentation (сheck out the entire documentation page by following the link in the quote):

https://www.mql5.com/en/docs/basis/operators/if

//--- The else part refers to the second if operator:
if(x>1)
   if(y==2) z=5;
else     z=6;
//--- The else part refers to the first if operator:
if(x>l)
  {
   if(y==2) z=5;
  }
else        z=6;
 
Vladislav Boyko #:
However, I made a mistake trying to show how your code works😄

I won't even try again to write code equivalent to your second code. There aren't enough square brackets in your second code. Because of this, it is difficult to read and easy to make mistakes. Add square brackets to understand what is nested where.

 
Vladislav Boyko #:
I won't even try again to write code equivalent to your second code
I tried anyway. I think this code should be equivalent to your second code (unless I'm wrong again)
if(EMA9 > EMA225 && Close[2] < EMA9 && Close[1] > EMA9) 
  {
   if(K533 > D1833 && K533 > 20)
     {
      Print("Executing Buy Order");         
      executeBuy();
     }
   else if(EMA9 < EMA225 && Close[2] > EMA9 && Close[1] < EMA9)
     {
      if(K533 < D1833 && K533 < 80)
        {
         Print("Executing Sell Order");         
         executeSell();
        }
     }
  }
Reason: