Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Nowhere without you - 6. - page 156

 
hoz:

Often I would declare a variable like this inside functions:

Further inside, at some condition I already call it to see if there is an error. Well, it's not only me but many other places in the code I've encountered. So to say, it's a typical situation. I thought today that it's not logical to declare the same variable each time in each function where you want to control errors. I thought it over and came to the conclusion that it's easier to declare globally once and initialize it in the init. It goes like this:

And further, when you need to call the err variable in user functions, knowing that it already has a value. As a matter of fact, GetLastError() outputs the code of the last error, so if this variable err is called in each function at the end of all calculations, which is responsible for outputting the error code, and calls GetLastError(), then everything will be correct, right?

But if you don't call this variable at the end of some function (following the chain of code execution), it won't reset, and it might be wrong execution, in case there was an error in previous execution chain, but not in current one, but error code is returned last... And to reset it, you have to call this function GetLastError(). I.e. here everything depends on whether it was called or not. If you're not sure that it will be everywhere, it's easier to re-insure and reset it every time, right?

We can declare err globally, then zero it in start() before calling GetLastError(), e.g:

int err;
//------------------
int init() 
{
  // ...
  return(0);
}
//------------------
int start() 
{ 
  // ...
  RefreshRates();
  OrderClose(...);
  err=0;     // обнуление
  err=GetLastError();
  SomeErrFunction(err);
  // ...
return(0);
}
 
ALXIMIKS:

1) why not initialize immediately when declaring int err=0 ?


Try it!:) It didn't work for me. As I understood, initialization by special functions in global variables is not allowed. Although the documentation doesn't seem to say anything about it.
ALXIMIKS:

2) didn't check, but according to documentation err is stored in some variable, which is auto-zeroed when using GetLastError() function,

and also always changes when using some functions (mostly terminal ones)

About zeroing I wrote above, but there it goes, if after value gets into LastError variable(), you have to call the GetLastError function() to return the code of the last error and reset the LastError variable . Otherwise, the code of the last error will be stored permanently until you call GetLastError(), and, consequently, it may return the code of the last error where it does not exist anymore. (i.e. in another function to make it harder to read the log or print).
ALXIMIKS:

3) This is why GetLastError() gave a value of 0 after referring to a non-existent ticket number in OrderSelect:

" The other functions do not change the value of the last_error variable under any conditions. ................., OrderSelect, ...................."

This is most likely not because of OrderSelect(), but something else is wrong (next to it in the same function). Especially it is often affected by many other things.
 
paladin80:

You can declare err globally, then zero it in start() before calling GetLastError(), e.g:

If you declare it at start and then more than 1 error in the code, then the logic is already out of whack. Unless GetLastError() is constantly called (theoretically at the end of each user function).
 
hoz:
then there is already a logical problem. Unless GetLastError() is constantly called (theoretically at the end of each user function).

Sometimes you have to call it more often
 

Victor (hoz), I don't understand your fuss with the last error.

Declare function for error handling and call it where needed. Everything will be local and can be used everywhere.

// Объявление:
void CommentError(int nLastCodeError, string sComment)
 {
  if (nLastCodeError != 0) Print(sComment, "  ", ErrorDescription(nLastCodeError));
 }

// Вызов:
void start()
 {
  CommentError(GetLastError(), "Ошибка в функции start()!");
 }
 
Zhunko:

Victor (hoz), I don't understand your fuss with the last error.

Declare function for error handling and call it where needed. Everything will be local and can be used everywhere.


It's understandable. But this function will be called when I need it, i.e. when I want to check if there is an error. But if there was an error somewhere before and there is no error at this moment, it will return the previous error... Because in your function Vadim, the LastError variable is not zeroed , by calling GetLastError(). Here's the thing...
 
hoz:

It's understandable. But this function will be called when I need it, i.e. when I want to check if there's an error. But if there was an error somewhere earlier, and at this moment there is no error, it will return the previous error... Because in your function Vadim, the LastError variable is not zeroed , by calling GetLastError(). That's the point...

It should not be reset to zero. There should be a habit of checking for errors everywhere after functions. You should also make the call disable. So that it doesn't slow down in combat mode. I.e. it is such a debugger.

The exception, of course, is for errors that need to be analysed, to make a decision.

 
hoz:

That's understandable. But this function will be called when I need it, i.e. when I want to check if there is an error. But if there was an error somewhere before and there is no error at this moment, it will return the previous error... Because in your function Vadim, the LastError variable is not zeroed , by calling GetLastError(). Here's the point...
What prevents it from first zeroing (err=0;), then calling the action, then assigning err=GetLastError(), handling it, and err=0 again.
 
How to calculate profit factor with different t/p and s/l. It is clear, if take and stop are 20 pips each, let's calculate the number of profitable and loss-making trades and divide them. How do I calculate if I take a TP of 40 pips and the stop 30 pips? We'll use it in the same way we use it in the same way we use it in the same place.
 
smart:
How to calculate profit factor with different t/p and s/l. It is clear, if take and stop are 20 pips each, let's calculate the number of profitable and loss-making trades and divide them. How do I calculate if I take a TP of 40 pips and the stop 30 pips? I have already seen this masks.
When testing an EA, put different combinations of profit and loss and the tester will calculate the profit factor by itself.
Reason: