Script crashes MT4: Application Hang/ cross thread deadlock

 
#include <SymbolsLib.mqh> 
static int mPeriod[8]={PERIOD_M1,PERIOD_M5,PERIOD_M15,PERIOD_M30,PERIOD_H1,PERIOD_H4,PERIOD_D1,PERIOD_W1,PERIOD_MN1};
 int nPeriods=8;


string DataSource;
 int tProcessed;

int start()
 {
 DataSource = AccountServer();

 string symbols[5000];
 int nSymbols = SymbolsList(symbols,false);
 int Bar1, Bar2;
 Print ("Number of Symbols " +nSymbols );


 for (int x=0; x< nSymbols; x++)
 {

 for ( int iPeriod = 0; iPeriod <= nPeriods ; iPeriod++ )
 {
 Bar1 = iBars(symbols[x],mPeriod[iPeriod]);
 Sleep(50);
 Bar2 = iBars(symbols[x],mPeriod[iPeriod]);
 for (int retryC=0; Bar1 == 0 || Bar1 != Bar2; retryC++) { 
Print(symbols[x] + "/" + mPeriod[iPeriod] + " not ready ->" + Bar1 + "/" + Bar2);
 Sleep(1500);
 Bar1 = iBars(symbols[x],mPeriod[iPeriod]);
 Sleep(200);
 Bar2 = iBars(symbols[x],mPeriod[iPeriod]);
 if (retryC>2)
 {
 Print("Giving up on "+symbols[x] + "/" + mPeriod[iPeriod] + "..." + Bar1 + "/" + Bar2);
 break;
 }
 }

 tProcessed += Bar1;
 }
 }

 Print("Total: " + tProcessed);
 return(0);
 }


Hi there,

i have written a very simple script to download all history data from the server. Unfortunately the script crashes MT4 nearly every time it runns.
Event viewer reports Application hang with a "cross thread deadlock" in the details. This is on windows7 64bit SMP. I tried on a single CPU XP installation with the same problem.

The script does this:

Loop over all symbols
Loop over all periods
Get number of bars in Histoy
If number = 0 wait for histoy to load

or in Code (see above)

Thanks,
Armin

 

Please use this to post code . . . it makes it easier to read.

 

You are missing a ; here int tProcessed I see you have fixed this ;-)

Try adding this . . .

for (int retryC=0; Bar1 == 0 || Bar1 != Bar2; retryC++) { 

Print("retryC= ",retryC);                        // <--- add this,  check the Experts tab while the script runs

Print(symbols[x] + "/" + mPeriod[iPeriod] + " not ready ->" + Bar1 + "/" + Bar2);
Sleep(1500);
 
RaptorUK:

You are missing a ; here int tProcessed I see you have fixed this ;-)

Try adding this . . .


I tried this before already. The deadlock happend at random symbols mostly without the retry loop being even run, since most of the times the history is loaded ofcause.

As so many times i found the solution after posting for help (though i'm on this problem for full two days now :( )

Here it is:
Inside the start method i did:

 int Bar1, Bar2;
 Bar1 = iBars(symbols[x],mPeriod[iPeriod]);
 Sleep(50);
 Bar2 = iBars(symbols[x],mPeriod[iPeriod]);

Note: Bar1 and Bar2 are local vars. The Sleep is ment to catch events when MT4 is just about to write to the history.
It appears those local vars are the source for the deadlock.
This code works:

// global scope
int nBars;


int start()
{
 // somewhere:
  nBars = iBars(symbols[x],mPeriod[iPeriod]);
]

So in short moving to a global did it. Could you please comment wether this makes sense?
Thanks,
Armin

 
____ao:

So in short moving to a global did it. Could you please comment wether this makes sense?
Thanks,
Armin

Doesn't make much sense to me, especially in the context of a Script, an EA maybe but not a script.
 
RaptorUK:
Doesn't make much sense to me, especially in the context of a Script, an EA maybe but not a script.


Sorry to misuse this thread for a different problem, but when trying to post a topic i receive a strange error, indicating that i sent a topic 30 seconds ago and or the issue was already posted, which is not the case. Here's the thing:

i want to capture the ticks of all (>220) symbols. As i understand the MT4 modell, this means that i have to open a chart for all those symbols in order to capture the tick via the start() method.... Writing the EA or indicator to persist the Tick is already done. I just can not find a way to open all those charts in M1 and attach the EA/indicator...

Q: Can this somehow be done semi-automatically?

Thanks,
Armin

 
The easiest way I know . . . add the Indicator to a chart, set the chart to M1, save the template as default.tpl, open a new chart and it will have the Indicator added already . . . if you need to do it with an EA then this won't work.
 
RaptorUK:
The easiest way I know . . . add the Indicator to a chart, set the chart to M1, save the template as default.tpl, open a new chart and it will have the Indicator added already . . . if you need to do it with an EA then this won't work.


pointing to my custom indicator and using default.tpl did the trick.....nearly. There aprears to be a limit of the max open chart windows. At about >130 open windows i cannot open more charts any more. Max bars in chart/History settings have no influence, so this seems to be a hard limit.

Best wishes,
Armin

 
____ao:
There aprears to be a limit of the max open chart windows. At about >130 open windows i cannot open more charts any more.
Install a second terminal in another directory and add to that.
Reason: