GetLastError with Function name using if statement - page 2

 
Vladislav Boyko #:

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.


_LastError is a global variable, you can read it as often as you want, it has zero influence on the actual value.

GetLastError () is a function, and it will reset the value in _LastError.


 
anuj71 #:

I agree.



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

There is no issue, see my previous post.
 
Please be aware, there are actually two error codes indicating "no error".

0 and 4000


Also, you should use the named constants instead of the numbers.
 
anuj71 #:

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

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

Perhaps you should read the manual. GetLastError - Checkup - MQL4 Reference
   How To Ask Questions The Smart Way. (2004)
      How To Interpret Answers.
         RTFM and STFW: How To Tell You've Seriously Screwed Up.

The second call always returns zero.

After the function call, the contents of _LastError are reset.

the __FUNCTION__ is always equal.

 
Dominik Egert #:
there are actually two error codes indicating "no error".

0 and 4000

Thanks for this information.

This code is perfect?

if(_LastError != 0 && _LastError != 4000)
   Print(__FUNCTION__ + " => Buy Trailing Error Code : " + GetLastError());
 
Dominik Egert #:

_LastError is a global variable, you can read it as often as you want, it has zero influence on the actual value.

GetLastError () is a function, and it will reset the value in _LastError.

Forum on trading, automated trading systems and testing trading strategies

GetLastError with Function name using if statement

anuj71, 2024.02.19 10:20

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

@anuj71 used both GetLastError and _LastError. This is why I suggested using a variable. If @anuj71 explicitly reset the error using GetLastError, then ok.

In any case, I think it's better to use either GetLastError only or _LastError only. And I would suggest adding ResetLastError before OrderModify

[EDIT]

By "GetLastError only" I mean assigning the value of GetLastError to a variable

 
Vladislav Boyko #:
I would suggest adding ResetLastError before OrderModify
Like this?


if(stopLoss < TrailingStopLoss_Buy_newStopLoss || stopLoss == 0.0)
                    {
                     ResetLastError();
                     OrderModify(OrderTicket(), TrailingStopLoss_entryPrice + Trailing_TP * Pips, TrailingStopLoss_Buy_newStopLoss, OrderTakeProfit(), 0, clrNONE);
                     if(_LastError != 0 && _LastError != 4000)
                     Print(__FUNCTION__ + " => Buy Trailing Error Code : " + GetLastError());
                    }
 
anuj71 #:
Like this?

Yes, I think ResetLastError before OrderModify would be appropriate. Now you can be sure that if you find an error (in an if statement), that error occurred in OrderModify

 
Vladislav Boyko #:

Yes, I think ResetLastError before OrderModify would be appropriate. Now you can be sure that if you find an error (in an if statement), that error occurred in OrderModify

The point is, GetLastError will call ResetLastError implicit. That's why the variable _LastError is cleared after a call to GetLastError.

So if you need the error code multiple times, you can simply use _LastError, as it is a variable.

If you want to reset the current error code, you either call GetLastError, which will give you the error code in return AND reset the value, or you just call ResetLastError, which will not give you the last error code, and simply reset the code to zero.

There is no requirement for a secondary variable to store the error code, except maybe for some special cases, which is not given here.

So, if you want to first compare the current error code, you use _LastError, and if you want to print and reset the error code, you can safely use GetLastError.
 
anuj71 #:

Thanks for this information.

This code is perfect?


Almost. You are using the integer values 0 and 4000. You should be using the named constants.