[ARCHIVE!] Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Can't go anywhere without you - 4. - page 268

 
Zhunko:
It will return whatever you indicate depending on the type of system function you specified. The system call will return zero.


Ok. Int start() is declared. I've always returned zero. I've seen both return(-1) and even return(-2) in the code. What's the right way to choose this code?

P.S.

And another natural question, is it possible to declare the start() of an EA/indicator/script of some other type than int() and then what to return to the terminal? In other words, what does the terminal do with the value returned by return()?

 
yosuf:
Thank you, there on the link, there is a software solution to the issue, and I don't know what to do. What does this have to do with and how do I solve this issue? Why was the EA working fine for a month, and now it is glitching? If the error was in the code? It turns out that this thread is not for newbies if it burdens me with answers to questions. I am frustrated.

I wrote such a thing in my EA right after the start:

//--- Section A: start - checking.
   if (!IsExpertEnabled())
      {Comment("Expert is NOT working. Check the reason."); return(0);}
   if (!IsConnected())
      {Comment("No connection!"); return(0);}
   if (IsTradeContextBusy())
      {Comment("Trade flow is busy. Wait..."); Sleep(5*1000); return(0);}
   if (AccountFreeMargin()<1000*Lot_super)
      {Comment("No money to trade. Free Margin = ",AccountFreeMargin()); Sleep(20*1000); return(0);}
//--- Section A: end - checking.

Try it, maybe some of it will be useful for you too.

 
drknn:


1. ok. Int start() was declared. I've always returned zero. I've seen the return(-1) and even return(-2) commands in the code too. What's the correct way to choose this code?

2. So the natural question arises, is it possible to declare start() of EA/indicator/script not of int() type, but of any other type and then what to return to the terminal? In other words, what does the terminal do with the value returned by return() ?

1. How do you decide what a normal function should return ?

2. You can have any type, just like a normal function. The terminal doesn't do anything with the returned values. It ignores them.

Special functions

 
Zhunko:

1. How do you decide what a normal function should return?

2. You can have any type, just like a normal function. The terminal doesn't do anything with the returned values. It ignores them.


1. In order to decide what an ordinary function should return, I need to know why the main code needs such a value. For example, if I want to make a decision to open a buy order when there is a signal, I will write a function that returns the number of buy orders that are already open. Depending on how many there are, the code will make a decision following the logic embedded in the code. I don't know what logic is embedded in the return code. Today I was looking through an article about creating a semaphore and I came across a return code (-2). Surprised, asked a question here. That's all. ( https://www.mql5.com/ru/articles/1412 - article here).

2. the terminal ignores the returned values. Is it so? If so, then again a legitimate question arises, why in the creation of an EA/indicator/script, the developer put into a template exactly int, because it would be more reasonable to put there void - then the terminal does not have to ignore anything - we would gain 1 tact of processor time? Maybe there is some logic in WHAT to return from int init(), int deinit() and int start() functions? I think Roche already explained it to someone somewhere - he remembered a description of when to return zero and when to return (-1). It was this very explanation I was searching for. Too bad I couldn't find it...

P.S.

The author of the article uses return(-2) for his own purposes. But the fact is that sometimes the start() function ends with code returning a value different from zero. And it is not only one person who does it. This is exactly this logic we want to understand.

 

Looks like there in the text.

Predefined functions can have parameters. However, when these functions are called by the client terminal, no parameters will be passed externally, but the default values will be used.

This is about the parameters going into the function, not the parameters returned by it.

 
thank you for your reply!
drknn:
This is an order counter. Well, it might be the case, for example, that you want to know in the code whether there are buy orders in the market for a certain currency pair and with a certain magic number, for example. If there is e.g., another buy order cannot be opened. So the order counter is needed to make a decision.
 
paladin80:

I wrote this in my EA right after the start:

Try it out, you might find some of it useful.

Thank you, I found my mistake. When my EA has got stuck because of the reason given in the article (above link), I have closed it and started again but forgot to change order master from 123 to 124 and two differently-directed EAs got the same mags and started to compete to see who opens orders faster. If the first order was opened first, it was not the same as, say, Sell or vice versa. As soon as I changed the second updated EA's magic, they both started working normally. If the error happens again, I will take your advice. Thanks for the answers.
 

how these lines can be understood:

return orders volume

if(buys>0) return(buys);

else return(-sells);

} ?

 

Write a script:

int start(){
  Print("Попытка ретурна 136");
  return(136);
}

throw it on the chart and watch the log:

2012.07.20 12:04:11 111 NZDUSD,M15: removed
2012.07.20 12:04:11 111 NZDUSD,M15: uninit reason 0
2012.07.20 12:04:11 111 NZDUSD,M15: Попытка ретурна 136
2012.07.20 12:04:11 111 NZDUSD,M15: loaded successfully

(here 111 is the name of the script). We see the line " uninit reason 0 " - apparently the terminal was waiting for zero!

Try the same with (-1) :

int start(){
  Print("Попытка ретурна -1");
  return(-1);
}

The result is the same:

2012.07.20 12:08:19 111 NZDUSD,M15: removed
2012.07.20 12:08:19 111 NZDUSD,M15: uninit reason 0
2012.07.20 12:08:19 111 NZDUSD,M15:Attempted returnee -1

2012.07.20 12:08:19 111 NZDUSD,M15: loaded successfully

The terminal seems to care if zero is returned or not.

P.S.

In general, in the web came across this answer:

return 0 - the program has completed its work without errors
return 1 - or higher number, the program has finished its work with an error and returns the error code for further processing.

In any operating system, any program always returns an error code.

The only trouble is that our return does not return the code to the operating system, but to the terminal. This leaves the question open.

 
drknn:

Write a script:

throw it on the chart and watch the log:

(here 111 is the name of the script). We see the line " uninit reason 0 " - apparently the terminal was waiting for zero!

Try the same with (-1) :

The result is the same:

Apparently the terminal cares whether a zero is returned or not.


uninit reason is the reason code of the Expert Advisor termination

https://docs.mql4.com/ru/constants/uninit

Reason: