MQL5 Syntax - What's wrong?

 
   if(emaTrend() == "downtrend")
     {
      entrySignal = 2;
     }
   if(emaTrend() == "uptrend")
     {
      entrySignal = 1;
     }
   if(PositionsTotal() == 0 &&  entrySignal == 1)
     {
      ticketOrder = trade.Buy(lotsize,_Symbol,Ask,Ask-(stopLoss*getPipValue()),Ask+(takeProfit*getPipValue()));
     }
   if(PositionsTotal() == 0 && entrySignal == 2)
     {
      ticketOrder = trade.Sell(lotsize,_Symbol,Bid,Bid+(stopLoss*getPipValue()),Bid-(takeProfit*getPipValue()));
     }

Why is the first code attached isn't working when there is no mistake in the syntax while the second code attached is working despite no perceived differences

   if(PositionsTotal() == 0 && emaTrend() == "uptrend")
     {
      ticketOrder = trade.Buy(lotsize,_Symbol,Ask,Ask-(stopLoss*getPipValue()),Ask+(takeProfit*getPipValue()));
     }
   if(PositionsTotal() == 0 && emaTrend() == "downtrend")
     {
      ticketOrder = trade.Sell(lotsize,_Symbol,Bid,Bid+(stopLoss*getPipValue()),Bid-(takeProfit*getPipValue()));
     }
 
Renz Carillo:

Why is the first code attached isn't working when there is no mistake in the syntax while the second code attached is working despite no perceived differences

they are not the same piece of code so comparing is not useful.

you say does not work, what does that mean, does not compile? does not trade?  crashes the terminal?

 
Paul Anscombe:

they are not the same piece of code so comparing is not useful.

you say does not work, what does that mean, does not compile? does not trade?  crashes the terminal?

They are the same, i just declared emaTrend() inside the entrySignal; No compiler errors, but the first code is not doing the purpose of emaTrend() while the second code does.

 
Renz Carillo:

They are the same, i just declared emaTrend() inside the entrySignal; No compiler errors, but the first code is not doing the purpose of emaTrend() while the second code does.

based on what you have provided and said;

the first instance you have an extra variable entrySignal

but no evidence that you are resetting before you check it with emaTrend().

you should set it to say 0  before doing the emaTrend() check and instead of two IFs you should use else if


but why bother the second format is more efficent anyway... you could also swap out the "up trend"  "downtrend" with an enumerator for better efficiency.

 

Documentation on MQL5: Common Functions / GetTickCount64
Documentation on MQL5: Common Functions / GetTickCount64
  • www.mql5.com
GetTickCount64 - Common Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Or you could simplify further:
if(PositionsTotal() == 0){
   string trend = emaTrend();
   if(trend == "uptrend")
     {
      ticketOrder = trade.Buy(lotsize,_Symbol,Ask,Ask-(stopLoss*getPipValue()),Ask+(takeProfit*getPipValue()));
     }
   else if(trend == "downtrend")
     {
      ticketOrder = trade.Sell(lotsize,_Symbol,Bid,Bid+(stopLoss*getPipValue()),Bid-(takeProfit*getPipValue()));
     }
}
 
Paul Anscombe:

based on what you have provided and said;

the first instance you have an extra variable entrySignal

but no evidence that you are resetting before you check it with emaTrend().

you should set it to say 0  before doing the emaTrend() check and instead of two IFs you should use else if


but why bother the second format is more efficent anyway... you could also swap out the "up trend"  "downtrend" with an enumerator for better efficiency.

 

What is the advantage of using else if? I tried using else if and if but it seems like there is no difference at all, do you mind enlightening me on this one.
 
William Roeder:
Or you could simplify further:
This actually looks good lmao i might just do this
 
Renz Carillo:
What is the advantage of using else if? I tried using else if and if but it seems like there is no difference at all, do you mind enlightening me on this one.

simple...

in your case you call  emaTrend()  twice but using ELSE IF  you would only call it the second time if the first IF  was untrue, so more efficient

there is also the fact that the return value of emaTrend() may change between your two IF statements making them both true, so you would over-ride the first outcome - although maybe not an issue for you in this case in other scenarios this may cause issues because you are not controlling outcomes properly.

but you asked why your two versions were not working, the rest has been about efficiency and discipline. Your uncontrolled initial variable entrySignal was most likely your problem as I mentioned before, but as we don't have all your code we can't confirm only you can.

and if you like efficency then don't use strings as flags unless you have to, they take a lot more resources to check so just enumerate them