IsConnected() - page 2

 
Matutin:

Just call start() from init () ...and you will be in start() even no tick coming !

thanks u 2
 
Viffer:

Expanding on my link above... I run a seperate EA to monitor connection status. It basically has all the code for my test coded inside start with a sleep at the end. Start is basically a big loop, when it finishes, it goes back to the beginning. But rather than wait for a tick to trigger the start, I call start() inside init(). In this way start() continues to loop with the sleep and init() never finishes so the code runs based on time intervals from sleep rather than waiting for a tick... the link above shows the principal...

hth

V


Matutin:

Just call start() from init () ...and you will be in start() even no tick coming !


neither of this solve the problem because init() also didn't start if there is no connection

the only way to do this is whit a script; the problem is that there is no way to open the terminal with a script Loaded

 
qjol:


neither of this solve the problem because init() also didn't start if there is no connection

the only way to do this is whit a script; the problem is that there is no way to open the terminal with a script Loaded


Let have a look at MT4i.com tools. I use them to monitot connexion and send alert by mail or sms.

http://www.mt4i.com/mtdashboard.aspx

 
Matutin:

Just call start() from init () ...and you will be in start() even no tick coming !

NO! This is not allowed. init() is expected to return after 2 seconds. Init is not allowed to block! All kinds of problems might (and will) arise from that.

Just do the loop in start and expect start to be called automatically on the next available tick after loading the EA. There were ticks before the connection dropped so your loop will be already running when this event occurs.


If you really need to trigger a start() from within init() because you need it to run on the weekend then the only correct way is to use the PostMessage() trick to enforce a tick. You post the message and then you let init return! MT4 will then call start() the normal way and init() won't block.


See the next posting for an example.

 
// this is an EA

#import "user32.dll"
   int PostMessageA(int hWnd,int Msg,int wParam,int lParam);
   int RegisterWindowMessageA(string lpString);
#import

int init(){
   int hwnd=WindowHandle(Symbol(), Period());
   int msg = RegisterWindowMessageA("MetaTrader4_Internal_Message");
   PostMessageA(hwnd, msg, 2, 1); // enqueue a fake tick and let init() return
   return(0);
}


int start(){
   while (!IsStopped()){
      // do your stuff here
      Sleep(1000);
   }
   return(0);
}
This is the only correct way to force a start() from within init() in an EA. You are not allowed to do this in an indicator since there start() will run in the GUI thread.
 
/**
* MT4/experts/scripts/ticks.mq4
* send a fake tick every 200 ms to the chart and
* all its indicators and EA until this script is removed.
*/

#property copyright "© Bernd Kreuss"

#import "user32.dll"
   int PostMessageA(int hWnd,int Msg,int wParam,int lParam);
   int RegisterWindowMessageA(string lpString);
#import

int start(){
   int hwnd=WindowHandle(Symbol(), Period());
   int msg = RegisterWindowMessageA("MetaTrader4_Internal_Message");
   while(!IsStopped()){
      PostMessageA(hwnd, msg, 2, 1);
      Sleep(200);
   }
}
For completeness sake the above is a script I use to simulate fake ticks on the weekend. You don't need it, its only an example. This is mostly useful for debugging other EAs and indicators.
 
/**
* this is a variation of the above script meant to be run as an EA.
* Send a fake tick every 200 ms to the chart and
* all its indicators until this EA is removed.
*/

#property copyright "© Bernd Kreuss"

#import "user32.dll"
   int PostMessageA(int hWnd,int Msg,int wParam,int lParam);
   int RegisterWindowMessageA(string lpString);
#import

int init(){
   int hwnd=WindowHandle(Symbol(), Period());
   int msg = RegisterWindowMessageA("MetaTrader4_Internal_Message");
   PostMessageA(hwnd, msg, 2, 1); // enqueue the first fake tick
}

int start(){
   int hwnd=WindowHandle(Symbol(), Period());
   int msg = RegisterWindowMessageA("MetaTrader4_Internal_Message");
   while(!IsStopped()){
      PostMessageA(hwnd, msg, 2, 1);  // produce more fake ticks
      Sleep(200);
   }
}

And this is the same as the script above but implemented as an EA, demonstratig both concepts in one EA.

In init() it will enqueue the very first tick to trigger the first start() and then in start() it will produce more ticks in an infinite loop.These ticks will then be picked up by all indicators on the same chart.

 
7bit:
This is the only correct way to force a start() from within init() in an EA. You are not allowed to do this in an indicator since there start() will run in the GUI thread.

// this is an EA

#import "user32.dll"
   int PostMessageA(int hWnd,int Msg,int wParam,int lParam);
   int RegisterWindowMessageA(string lpString);
#import

int init(){
   int hwnd=WindowHandle(Symbol(), Period());
   int msg = RegisterWindowMessageA("MetaTrader4_Internal_Message");
   PostMessageA(hwnd, msg, 2, 1); // enqueue a fake tick and let init() return
   return(0);
}


int start(){
   while (!IsStopped()){
      // do your stuff here
      Sleep(1000);
   }
   return(0);
}

try to put this EA on a chart, close the terminal, than disable your internet conection & turn on your terminal (While the connection to the internet is turned off) & You'll see it does not work

 
qjol:


try to put this ea on a chart close the terminal than disable your internet conection & turn on your terminal (While the connection to the internet is turned off) & You'll see it does not work

But it should continue running once it has been started under normal conditions (which should be the normal use case). Try to disconnect after it has been started.


And it serves as an example of how to avoid a blocking init() and run it as an EA instead of a script.

 
7bit:

But it should continue running once it has been started under normal conditions (which should be the normal use case). Try to disconnect after it has been started.


And it serves as an example of how to avoid a blocking init() and run it as an EA instead of a script.


You're right, but I'm not looking for a solution to the situation you describe, I'm looking for a solution to the situation I describe, and right now the only solution I found, that, external software, will run a script

Reason: