Sleep() within a while() Loop - page 2

 
Fernando Carreiro #:

It is my opinion that no attempt should be made during OnInit to pull data from the trade server for whatever reason, because there is no guarantee that during the OnInit there is even a connection to the server at all.

Imagine the case for when opening MetaTrader that was previously closed with an open chart with indicators on it. As the terminal starts up, the chart opens and the indicators initialise, but the connection to the trade server may not yet be up.


Right, this also includes Symbols... Didn't think about that.

Isn't the symbols DB populated once a connection was made to the specific broker account? I mean, even if the current launch of MT5 hasn't connected again, the symbols should be available, and a call to add a symbol to the market watch shouldn't fail. - At least that's what I thought. Also opening the chart should be possible in OnInit. Actually I never tried it, so I am assuming here.

In subsequent startups of the terminal, or subsequent loadings of the indicator retrieving the data from the foreign symbol should now work, in OnCalculate.

The idea was to have the terminal load the data of that specific symbol.The real challenge is the initial loading, especially on weekends.
 
Dominik Egert #:

Right, this also includes Symbols... Didn't think about that.

Isn't the symbols DB populated once a connection was made to the specific broker account? I mean, even if the current launch of MT5 hasn't connected again, the symbols should be available, and a call to add a symbol to the market watch shouldn't fail. - At least that's what I thought. Also opening the chart should be possible in OnInit. Actually I never tried it, so I am assuming here.

In subsequent startups of the terminal, or subsequent loadings of the indicator retrieving the data from the foreign symbol should now work, in OnCalculate.

The idea was to have the terminal load the data of that specific symbol.The real challenge is the initial loading, especially on weekends.

Here is another scenario ... imagine logging onto another trade account while the chart and indicator's are up?

Again a similar issue, so it is best not to use any trade server related data or functions during OnInit().

 
Fernando Carreiro #:

Here is another scenario ... imagine logging onto another trade account while the chart and indicator's are up?

Again a similar issue, so it is best not to use any trade server related data or functions during OnInit().

Yes, I know that. - Do you know if Symbols itself are considered also "trade server data", as they have to sync only once, and then delta update, eventually. - But I am not referring to tick data, or alike, but only the symbol name itself.

 
Dominik Egert #: Yes, I know that. - Do you know if Symbols itself are considered also "trade server data", as they have to sync only once, and then delta update, eventually. - But I am not referring to tick data, or alike, but only the symbol name itself.

Yes, because different trade servers have different symbol names and different contract specifications. Their data gets updated on every connection start-up at least, and probably also at regular intervals after that or when a broker pushes out changes to the contract specifications.

 
Fernando Carreiro #:

Yes, because different trade servers have different symbol names and different contract specifications. Their data gets updated on every connection start-up at least, and probably also at regular intervals after that or when a broker pushes out changes to the contract specifications.

OK, so my proposed idea will not really solve the issue at hand.

So the only way to actually solve it is by reloading the indicator, be it by code or manually, in offline markets.
 
Dominik Egert #: OK, so my proposed idea will not really solve the issue at hand. So the only way to actually solve it is by reloading the indicator, be it by code or manually, in offline markets.

I still believe that this is a XY problem and that the OP needs to rethink how to approach it.

 

Thanks everybody for the input.

I think this is a general issue and not only related to my case. Some functions (function-combinations) will not success always in one run, the data you are initializing needs time to be handled in order to be able to access it with another function.

A sample would be: Initializing an Indicator handle with iCustom() and then executing CopyBuffer() in the next step. The standard solution would be simply: Do CopyBuffer() on the next OnCalculate(). OK, and what happens if we are OffMarket? The Indicator will not calculate anything, because OnCalculate() runs only once at startup and then on the first Tick.

In my particular case, the problem is, that I am writing custom OHLC data to a custom Symbol, and then accessing it through a 3rd party Indicator handle, iCustom(), then doing CopyBuffer(), and also TimeCopy() or iBarShift() is used (the custom Symbol doesn't always have the same number of Bars like my executing Chart). This will not work completely on the first run.

I first tried to make a Loop, but this caused hyperactivity without the Sleep() function. This is why I was thinking to use a Timer to recall the missing logic every 1-2 seconds, until success of the whole chain. And I think I am not the only guy who wants his Indicator to show up-to-date results on weekends.

If there is some mistake in my logic or you still think this is an XY problem, please tell me. Maybe I have to divide my functionality into a separate Script and an Indicator, but I would love to make a one-stop-solution.

 
Anas Morad #:

Thanks everybody for the input.

I think this is a general issue and not only related to my case. Some functions (function-combinations) will not success always in one run, the data you are initializing needs time to be handled in order to be able to access it with another function.

A sample would be: Initializing an Indicator handle with iCustom() and then executing CopyBuffer() in the next step. The standard solution would be simply: Do CopyBuffer() on the next OnCalculate(). OK, and what happens if we are OffMarket? The Indicator will not calculate anything, because OnCalculate() runs only once at startup and then on the first Tick.

In my particular case, the problem is, that I am writing custom OHLC data to a custom Symbol, and then accessing it through a 3rd party Indicator handle, iCustom(), then doing CopyBuffer(), and also TimeCopy() or iBarShift() is used (the custom Symbol doesn't always have the same number of Bars like my executing Chart). This will not work completely on the first run.

I first tried to make a Loop, but this caused hyperactivity without the Sleep() function. This is why I was thinking to use a Timer to recall the missing logic every 1-2 seconds, until success of the whole chain. And I think I am not the only guy who wants his Indicator to show up-to-date results on weekends.

If there is some mistake in my logic or you still think this is an XY problem, please tell me. Maybe I have to divide my functionality into a separate Script and an Indicator, but I would love to make a one-stop-solution.

The only solution I can think of to solve this is to actually remove and reload the indicator.

You can do that with a helper-indicator, as I have outlined in my previous post.

I am not sure if you can have the helper indicator as a resource in the main indicator, but if so, you can actually have it all contained in your master indicator.

But since you need to unload and load the master, I am not sure it will work as resource.

Also you might want to look at code base, there is a library to sync your OnInit and OnDeinit calls, which you probably also need.

It's a difficult task off markets...
 
Anas Morad #:

Thanks everybody for the input.

I think this is a general issue and not only related to my case. Some functions (function-combinations) will not success always in one run, the data you are initializing needs time to be handled in order to be able to access it with another function.

A sample would be: Initializing an Indicator handle with iCustom() and then executing CopyBuffer() in the next step. The standard solution would be simply: Do CopyBuffer() on the next OnCalculate(). OK, and what happens if we are OffMarket? The Indicator will not calculate anything, because OnCalculate() runs only once at startup and then on the first Tick.

In my particular case, the problem is, that I am writing custom OHLC data to a custom Symbol, and then accessing it through a 3rd party Indicator handle, iCustom(), then doing CopyBuffer(), and also TimeCopy() or iBarShift() is used (the custom Symbol doesn't always have the same number of Bars like my executing Chart). This will not work completely on the first run.

I first tried to make a Loop, but this caused hyperactivity without the Sleep() function. This is why I was thinking to use a Timer to recall the missing logic every 1-2 seconds, until success of the whole chain. And I think I am not the only guy who wants his Indicator to show up-to-date results on weekends.

If there is some mistake in my logic or you still think this is an XY problem, please tell me. Maybe I have to divide my functionality into a separate Script and an Indicator, but I would love to make a one-stop-solution.

It's solvable but there is no simple mechanism provided by MQL. You need to play with the OnCalculate() return value and call OnCalculate() yourself. And there are several possible corner cases to deal with.

If you are cascading several custom indicators (using iCustom/CopyBuffer), it's even more complex as you need to "synchronize" the indicators, but it's still solvable.

Forget loops, it's a bad idea in an indicator.

 
Alain Verleyen #:

It's solvable but there is no simple mechanism provided by MQL. You need to play with the OnCalculate() return value and call OnCalculate() yourself. And there are several possible corner cases to deal with.

If you are cascading several custom indicators (using iCustom/CopyBuffer), it's even more complex as you need to "synchronize" the indicators, but it's still solvable.

Forget loops, it's a bad idea in an indicator.

Calling OnCalculate myself is really an innovative idea.. I think I ll have to make own copies of the OHLC & time arrays and call OnCalculate again through a timer? Or how would you do that?