Help Needed with MT4 Disconnect Issue

 
Hello all,

I've been grappling with an issue that I haven't been able to resolve for some time now.

I am using a third party indicator which among other things draws objects to signify entries and exits. It also displays how many entries and exits it has completed on the chart. This number is based on a user input which specifies how many days to look back.

The indicator uses object naming as such: Object 1, Object 2, Object 3, etc.

I have written an EA that will on upon initialization, init(), determine the highest numeration of said objects and place it in a global variable along with the value of total number of objects.

For example:

A chart contains Object 1, Object 2, Object 3, Object 4 which is 4 objects.

The EA simply checks to see on every tick if the current number of objects is more than the value stored in the global variable. Meaning when Object 5 is drawn which would make 5 objects and since that is greater than 4, the EA would proceed with the tasks assigned.

This works fine and dandy. The issue is, however, when MetaTrader inconveniently disconnects for whatever reason and then reconnects a few seconds later. What this does is reinitialize the indicator which makes it redraw all the objects. This also wouldn't be a problem except when the EA has initialized the values a day before the indicator is forced to redraw. If you recall a user input of 30 days may drop off a few of the values that no longer fall within this 30 days or add some objects for whatever reason.

For example:

After re-initialization the chart now contains Objects 1, Objects 2, Objects 3, Objects 4, Object 5, Object 6 which is 6 objects. The EA now compares this value 6 to the global variable containing 4 and goes into an order when in actuality there was no "new" entry object.

My question is this: How do I determine when these disconnects happen, and they happen often, and execute a function identical to the EA initialization to refresh the global variables with the most current values?

I have considered IsConnected(). However, I don't get how this would work. The start() function in an EA executes upon receiving a tick. If MT4 is disconnected there is no tick. If there is no tick start() will not run and IsConnected() can never determine that it's disconnected and re-run a refresh values function upon re-connection.

Is my logic incorrect here?

Any help would be appreciated.

Thanks.

Here is an entry from the Journal which illustrates this disconnect phenomenon.

Previous successful authorization performed from xxx.x.xxx.xx
 

AMDesai1:

...

I have considered IsConnected(). However, I don't get how this would work. The start() function in an EA executes upon receiving a tick. If MT4 is disconnected there is no tick. If there is no tick start() will not run and IsConnected() can never determine that it's disconnected and re-run a refresh values function upon re-connection.


Is my logic incorrect here?

Any help would be appreciated.

Thanks.

Here is an entry from the Journal which illustrates this disconnect phenomenon.

Previous successful authorization performed from xxx.x.xxx.xx

You can use a script with IsConnected(), start() in script is independant of any tick. You can find here an example script I made to manage IsConnected().

 
angevoyageur:

You can use a script with IsConnected(), start() in script is independant of any tick. You can find here an example script I made to manage IsConnected().




Angevoyageur,

Thank you for your reply.  Could you please elaborate on how I can use this to solve my issue?

From what I understand about scripts they execute once and unload from the chart.  From your instructions, I see that the terminal will load, pull up the chart and execute the script.  How does this help me when I am disconnected in the middle of the night while the terminal was already running?  I can understand if the script served as a watch dog on the chart and upon disconnect would set a global variable to false so when the EA comes back online it would see this and refresh all values.

Please explain more if you can.

Thank you very much. 

************************ EDIT

I'm sorry.  I didn't notice the loop.  So the script will execute and then loop the whole time the chart is open.  Is this resource intensive?  If I run this on lets say 10 charts would it be processor heavy?

Thanks. 

 
AMDesai1:


Angevoyageur,

Thank you for your reply.  Could you please elaborate on how I can use this to solve my issue?

From what I understand about scripts they execute once and unload from the chart.  From your instructions, I see that the terminal will load, pull up the chart and execute the script.  How does this help me when I am disconnected in the middle of the night while the terminal was already running?  I can understand if the script served as a watch dog on the chart and upon disconnect would set a global variable to false so when the EA comes back online it would see this and refresh all values.

Please explain more if you can.

Thank you very much. 

************************ EDIT

I'm sorry.  I didn't notice the loop.  So the script will execute and then loop the whole time the chart is open.  Is this resource intensive?  If I run this on lets say 10 charts would it be processor heavy?

Thanks. 

To be honest I do not understand your problem ;-) If it is a connection problem, you could use a script as I give you an example, to detect when you have a disconnection/reconnection and act accordingly. Just an idea.
 
angevoyageur:
To be honest I do not understand your problem ;-) If it is a connection problem, you could use a script as I give you an example, to detect when you have a disconnection/reconnection and act accordingly. Just an idea.


Haha.  Thanks angevoyageur.  I have an idea of how to go ahead and solve this issue.  I am just always worried about resource issues.

Thanks again.