I need help to understand the logic of solution for a problem I had while coding a function

I am sorry but I am eager to understand this

The below function is supposed to be buy and sell sending orders that will be executed for EA

The problem is the EA was executing the buy orders only

The solution of the problem was to delete - else{return false;} - in the below code after that it executed both buy and sell but I do not understand why,

would someone help me understand the logic of the solution and how (else{return false;} ) affected the code that way that made it execute only the buy orders

thanks
int TradeEntry(int Type){

if(Type == 0){

  double BTP = Ask + (TakeProfit *Point);
  double BSL = Ask - (StopLoss *Point);
    
    if(OrderSend(Symbol(),OP_BUY,0.1,Ask,10,BSL,BTP,NULL,0,0,clrBlue)){
    return true;}
  
     else{return false;}                                                 //this line I delete then it executed the selling trades[/u][/u]

}
if(Type ==1){
double STP = Bid - (TakeProfit *Point);
double SSL = Bid + (StopLoss *Point);

   if(OrderSend(Symbol(),OP_SELL,0.1,Bid,10,SSL,STP,NULL,0,0,clrRed)){
   return true;}
  
   else{return false;}                               //this line I delete then it executed the selling trades[/u][/u]

}
return false;
}

There is no reason I can see why those lines would have done what they did. Maybe there is a bug in the compiler? However your function definition says that it should return and integer and you are actually returning a boolean type instead, maybe that is messing with things?

Out of curiosity, do you have #property strict set?

The whole reason it forces us into that whole if then else construct for order operations is to make us capture error conditions when something fails or we do something wrong. Return terminates the function and returns control to whatever called it. If removing your else return allows it to continue, it makes me suspect an error somewhere. If that’s the case, you should be able to capture it if you change up a little:

if(OrderSend(Symbol(),OP_BUY,0.1,Ask,10,BSL,BTP,NULL,0,0,clrBlue))
   {                  
                              
   }
 else
   {
      Alert("OrderSend failed, error # ", GetLastError() );
   }

If there are any errors with the operation that should spit them out in your journal and make it easier to troubleshoot.
I actually do mine like they show here:

int ticket=OrderSend(Symbol(),OP_BUY,0.1,Ask,10,BSL,BTP,NULL,0,0,clrBlue);         
   if(ticket<0)
      {
         Print("OrderSend for ",_Symbol," failed with error #",GetLastError());
      }
   else
      Print("Order for ",_Symbol," placed successfully.");

Agreed. I’m wondering if that or something else is getting thrown into the else condition and returning.

Thank you so much for your advice , :pray:

The first “else” that you took out is behaving as if it was the “else” condition on the “If(Task == 0)” statement instead of the inner “if” statement.

the first "else " that I took out was within the curly brackets of the if(type == 0) so i do not think that it worked as else condition on the “If(Type == 0)”, I believe it was compiler issue as the problem solved and even after I rewrite the 2 lines that I have deleted, it now execute both buy and sell orders :man_shrugging: :sweat_smile:
I was trying to figure that the issue were in these 2 lines so I rewirte them expecting to get the buy orders only executed but somehow it executed both

Glad you got it sorted! Cheers!

1 Like

Cheersthank you :grin:

1 Like

That’s the problem with EA’s. Good that you know that coding language but any slip and you might just end up losing more money than what you anticipated to make out of the trade.

Personally, I never felt the need of learning to code in the forex market. There were only little things that I needed help with and the internet had plenty of solutions. As long as you are not looking forward to building your own trading system, I don’t think you need to learn coding.