GetLastError with Function name using if statement

 

Hello,


I am adding a GetLastError, which only print if error belongs to particular function.


I am trying like :

if(__FUNCTION__ == "TrailingStopLoss" && GetLastError() != 0)
   Print(__FUNCTION__ + " Returning Error Code for Buy Trailing : " + GetLastError());

if(StringCompare(__FUNCTION__, "TrailingStopLoss") == 0 && GetLastError() != 0)                                  
Print(__FUNCTION__ + " Returning Error Code for Sell Trailing : " + GetLastError());


Full code :


void TrailingStopLoss(int TrailingStopLoss_magic)
  {
// Loop through all open orders
   for(int i = OrdersTotal() - 1; i >= 0; i--)
     {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
         double TrailingStopLoss_entryPrice = OrderOpenPrice();



         if(OrderSymbol() == Symbol() && OrderMagicNumber() == TrailingStopLoss_magic && OrderType() == OP_BUY)
           {

            if(Bid - (TrailingStopLoss_entryPrice + Trailing_TP * Pips) > Pips * Trailing_Gap)
              {
               if(OrderStopLoss() < Bid - Pips * Trailing_Gap)
                 {
                  OrderModify(OrderTicket(), TrailingStopLoss_entryPrice + Trailing_TP * Pips, Bid - Pips * Trailing_Gap, OrderTakeProfit(), 0, clrNONE);
                  if(__FUNCTION__ == "TrailingStopLoss" && GetLastError() != 0)
                                  Print(__FUNCTION__ + " Returning Error Code for Buy Trailing : " + GetLastError());
                 }
              }
           }


         if(OrderSymbol() == Symbol() && OrderMagicNumber() == TrailingStopLoss_magic && OrderType() == OP_SELL)
           {

            if((TrailingStopLoss_entryPrice - Trailing_TP * Pips) - Ask > Pips * Trailing_Gap)
              {
               if(OrderStopLoss() > Ask + Pips * Trailing_Gap || OrderStopLoss() == 0.0)
                 {

                  OrderModify(OrderTicket(), TrailingStopLoss_entryPrice - Trailing_TP * Pips, Ask + Pips * Trailing_Gap, OrderTakeProfit(), 0, clrNONE);
                                  if(__FUNCTION__ == "TrailingStopLoss" && GetLastError() != 0)
                                  Print(__FUNCTION__ + " Returning Error Code for Sell Trailing : " + GetLastError());

                 }
              }
           }
        }
     }
  }


Error :

Even GetLastError return 0 is print the if statement, it should only print when GetLastError is not equal to Zero.

 
anuj71:

Full code :

if(__FUNCTION__ == "TrailingStopLoss" && GetLastError() != 0)

This makes no sense since you always check this condition inside the "TrailingStopLoss" function. Therefore it can be simplified to this:

if(true && GetLastError() != 0)

And then simplify it to:

if(GetLastError() != 0)
 
anuj71:

Hello,


I am adding a GetLastError, which only print if error belongs to particular function.


I am trying like :


Full code :



Error :

Even GetLastError return 0 is print the if statement, it should only print when GetLastError is not equal to Zero.

Calling GetLastError resets the _LastError variable:  GetLastError - Checkup - MQL4 Reference

So, store the error code in a variable at the top of your code.

GetLastError - Checkup - MQL4 Reference
GetLastError - Checkup - MQL4 Reference
  • docs.mql4.com
GetLastError - Checkup - MQL4 Reference
 
Laszlo Tormasi #:

Calling GetLastError resets the _LastError variable:  GetLastError - Checkup - MQL4 Reference

So, store the error code in a variable at the top of your code.

Or alternatively use _LastError variable as your source.
 
Vladislav Boyko #:


if(true && GetLastError() != 0)

What does true mean here?. we are not using any bool


Laszlo Tormasi #:
Calling GetLastError resets the _LastError variable:  GetLastError - Checkup - MQL4 Reference
Dominik Egert #:
Or alternatively use _LastError variable as your source.
if(__FUNCTION__ == "TrailingStopLoss" && GetLastError() != 0)
   Print(__FUNCTION__ + " Returning Error Code for Buy Trailing : " + _LastError);

// OR

if(__FUNCTION__ == "TrailingStopLoss" && _LastError != 0)
   Print(__FUNCTION__ + " Returning Error Code for Buy Trailing : " + _LastError);
 
anuj71 #:

What does true mean here?. we are not using any bool



this will always evaluate to "true"

__FUNCTION__ == "TrailingStopLoss"

Calling GetLastError will reset the content of _LastError to zero, you want to call GetLastError in your printf statement.




 
Dominik Egert #:
this will always evaluate to "true"


Calling GetLastError will reset the content of _LastError to zero, you want to call GetLastError in your printf statement.




if(__FUNCTION__ == "TrailingStopLoss" && _LastError != 0)
   Print(__FUNCTION__ + " Returning Error Code for Buy Trailing : " + GetLastError());

Is this correct, here i am not calling GetLastError on if statement

 
anuj71 #:
What does true mean here?. we are not using any bool

This means that the following expression will always return true:

__FUNCTION__ == "TrailingStopLoss"

Because you are using this expression inside the "Trailing Stop Loss" function.

Also think about the fact that if you change the name of the function and forget to make changes to these conditions, then you will accidentally disable your logs

 
int err = GetLastError();
if(err != 0)
   Print(__FUNCTION__ + " Returning Error Code for Buy Trailing : ", err);
 
Vladislav Boyko #:
Also think about the fact that if you change the name of the function and forget to make changes to these conditions, then you will accidentally disable your logs

I agree.


Vladislav Boyko #:

int err = GetLastError();
if(err != 0)
   Print(__FUNCTION__ + " Returning Error Code for Buy Trailing : ", err);


I got this and thanks for this code but what the issue with my this code :

if(_LastError != 0)
   Print(__FUNCTION__ + " Returning Error Code for Buy Trailing : " + GetLastError());
 
anuj71 #:
I got this and thanks for this code but what the issue with my this code :

This will most likely work, but it's best not to do it. You request the same error from the terminal 2 times and do it in two different ways. At the very least, this is not logical and may confuse you in the future. By storing the GetLastError value in a variable, you are guaranteed to use the same value in both the if statement and the Print function.

Reason: