Help: Using Multiple Timeframes and MT4 freezing and load history issues

 

Hello all,

I have been coding MQL4 for quite a while and really like the language.

I do have one problem I cannot seem to conquer .....

The problem has to do with using Multiple Timeframes. For example, I am on H1 chart and I have an indicator that uses arraycopyrates, or other multiple timeframefunctions like iBars, iFractal, or using iCustom to get data usinghigher timeframe values.

Quite a lot, MT4 will freeze and I will have to restart. I can mostly avoid the problem if I preload all the currency pair's timeframes using the HOME key before I add my indicator. It then seems to work for quite a while. However I will eventually have a freeze up in the future when changing to another timeframe....

Another example, I drag and drop another currency pair onto the chart with my MTF indicator, it will freeze up immediately, unless I preload the history for that pair with the HOME key.

Is there a better way to do this? I've coded in error checking, etc, but seem to always get the freezups anyway. Sometimes after a MT4 freezeup, MT4 will not even start. I have to go into my profiles directory and delete the charts.Then MT4 will start and I can start over again loading my templates.

Any info or help appreciated.

Thanks,

Steve

 

maybe a faster computer, can solve the problem

 
SKF:

The problem has to do with using Multiple Timeframes. For example, I am on H1 chart and I have an indicator that uses arraycopyrates, or other multiple timeframefunctions like iBars, iFractal, or using iCustom to get data usinghigher timeframe values.

Are you checking the return value? You can't look beyond that size (not iBars). It takes time while it loads the history, initially you'll get zero size with a nonzero iBars.

Also don't use it in init(), start() only.
 
WHRoeder:

Are you checking the return value? You can't look beyond that size (not iBars). It takes time while it loads the history, initially you'll get zero size with a nonzero iBars.

Also don't use it in init(), start() only.


Hi WHRoeder and qjol,

Thanks for responding. I hope I don't need a faster computer. I'm using a newer Samsung laptop with i5 processors and 4GB ram...

I'm not having much luck even checking the return values. For example, using arraycopyrates, I checking for error 4066 and a return of -1 or 0. I return without doing anything else if either of these are present Things still lock up. I know it's because I'm going so far back in history. I'll keep working on it.

Another question: Is there a way to force MT4 to load all history available by Currency Pair and/or timeframe in a command or script?

Thanks,

Steve

 

do not use WindowHandle() and maybe some other chart window related functions without checking IsStopped() before using them.

When you use one of these functions within start() and you initiate a timeframe switch before start has ended all these functions will deadlock and freeze MT4. IsStopped() will indicate that MT4 is going to deinitialize the EA after this tick and you should not call any of these buggy functions anymore and exit start() ASAP.


WindowHandle() is proven to freeze MT4 as soon as a deinit() is pending and I am sure there are a few others.


Insert a Print() between every line in your start() to narrow down / find out where it freezes(). I promise it will be one of the built in functions that have to do with the chart window that will deadlock and never return. Put an

if(IsStopped()) return;

before it or

if(!IsStopped()){

SomeThing = WindowHandle();

}

around it.

 

I don't use any of my MTF collection that uses ArrayCopyRates anymore, it's not reliable. Look for the ones that uses iBarShift instead.

 
cameofx:

I don't use any of my MTF collection that uses ArrayCopyRates anymore, it's not reliable. Look for the ones that uses iBarShift instead.


Hi All,

Once again, thanks for taking the time to respond to my questions

Not using WindowHandle(), the print statements I've tried, but when I change a timeframe nothing prints at all, the freeze happpens before the timeframe actually changes...

ArrayCopyRates was just an example, got others that freeze also...

IsStopped - I haven't tried anything with this so far, I will give it a go

Example function I suspect freezes

Basically, I use this for finding the exact time based on the Chart Timeframe, a high or low occurred on a higher timeframe. For example, if I use iFractal and find a Fractal value on the Daily timeframe, I send in that daily candle's Time, price and whether it's a high or low. It returns the time to the hour when my chart is set to H1. If I switch to the M15, it will return the value to the 15 minute, etc. For Monthly and Weekly candle values, I only return down to the hour.

I removed all attempts at error checking for this example.

What I find is that if I preload all history usig the Home key, it will work no problems. If not, freeze up forever until restarting MT4. Then I will most likely have to remove the charts from my profile before MT4 will even run again.

int Convert_Time_Candle (int Converted_Time, double In_Price, string Price_Enum )
{


int HourBarNumber;
int MinuteBarNumber;


if (TimeFrame == 43200 || TimeFrame == 10080)
{
HourBarNumber = iBarShift(NULL,60,Converted_Time);
if (Price_Enum == "High")
{
while( iHigh(NULL,60,HourBarNumber) < In_Price)
HourBarNumber--;
}

else if (Price_Enum == "Low")
{
while( iLow(NULL,60,HourBarNumber) > In_Price)
HourBarNumber--;

}
return (iTime(NULL,60,HourBarNumber));

}


switch (Period())
{
case 1:

MinuteBarNumber = iBarShift(NULL,1,Converted_Time, false);



if (Price_Enum == "High")
{
while( iHigh(NULL,1,MinuteBarNumber) < In_Price)
MinuteBarNumber--;
}

else if (Price_Enum == "Low")
{
while( iLow(NULL,1,MinuteBarNumber) > In_Price)
MinuteBarNumber--;

}
return (iTime(NULL,1,MinuteBarNumber));
break;



case 5:


MinuteBarNumber = iBarShift(NULL,5,Converted_Time, false);



if (Price_Enum == "High")
{
while( iHigh(NULL,5,MinuteBarNumber) < In_Price)
MinuteBarNumber--;
}

else if (Price_Enum == "Low")
{
while( iLow(NULL,5,MinuteBarNumber) > In_Price)
MinuteBarNumber--;

}
return (iTime(NULL,5,MinuteBarNumber));
break;




case 15:


MinuteBarNumber = iBarShift(NULL,15,Converted_Time, false);



if (Price_Enum == "High")
{
while( iHigh(NULL,15,MinuteBarNumber) < In_Price)
MinuteBarNumber--;
}

else if (Price_Enum == "Low")
{
while( iLow(NULL,15,MinuteBarNumber) > In_Price)
MinuteBarNumber--;

}
return (iTime(NULL,15,MinuteBarNumber));
break;




case 30:


MinuteBarNumber = iBarShift(NULL,30,Converted_Time, false);



if (Price_Enum == "High")
{
while( iHigh(NULL,30,MinuteBarNumber) < In_Price)
MinuteBarNumber--;
}

else if (Price_Enum == "Low")
{
while( iLow(NULL,30,MinuteBarNumber) > In_Price)
MinuteBarNumber--;

}
return (iTime(NULL,30,MinuteBarNumber));
break;




case 60:


MinuteBarNumber = iBarShift(NULL,60,Converted_Time, false);



if (Price_Enum == "High")
{
while( iHigh(NULL,60,MinuteBarNumber) < In_Price)
MinuteBarNumber--;
}

else if (Price_Enum == "Low")
{
while( iLow(NULL,60,MinuteBarNumber) > In_Price)
MinuteBarNumber--;

}
return (iTime(NULL,60,MinuteBarNumber));
break;




case 240:


MinuteBarNumber = iBarShift(NULL,240,Converted_Time, false);



if (Price_Enum == "High")
{
while( iHigh(NULL,240,MinuteBarNumber) < In_Price)
MinuteBarNumber--;
}

else if (Price_Enum == "Low")
{
while( iLow(NULL,240,MinuteBarNumber) > In_Price)
MinuteBarNumber--;

}
return (iTime(NULL,240,MinuteBarNumber));
break;



case 1440:


MinuteBarNumber = iBarShift(NULL,1440,Converted_Time, false);



if (Price_Enum == "High")
{
while( iHigh(NULL,1440,MinuteBarNumber) < In_Price)
MinuteBarNumber--;
}

else if (Price_Enum == "Low")
{
while( iLow(NULL,1440,MinuteBarNumber) > In_Price)
MinuteBarNumber--;

}
return (iTime(NULL,1440,MinuteBarNumber));
break;

}
}// end Convert_Time_Candle

 

SKF, for short code snippets use the SRC button, for large chunks of code (like the one u just posted) please attach as a file.

 

Have you been able to identify the Indy that causes the freeze up. Sounds like there are multiple indies.

Can't really tell from that snippets. 'Price_Enum' is a parameter taking string input of "High", "Low". And...?

 
switch (Period())
{
case 1:
MinuteBarNumber = iBarShift(NULL,1,Converted_Time, false);

if (Price_Enum == "High")
{
while( iHigh(NULL,1,MinuteBarNumber) < In_Price)
...
case 5:....
Why all this code. Drop the switch and use TF == 0
BarNumber = iBarShift(NULL,0,Converted_Time, false);

if (Price_Enum == "High")
{
while( iHigh(NULL,0,MinuteBarNumber) < In_Price)
 

WHRoeder:
Why all this code. Drop the switch and use TF == 0

Gordon - Sorry about the SRC button, I'm new here and will comply...

Gordon - Sorry about the SRC button, I'm new here and will comply...

cameofx - The "High" and "low" are ingoing parameters, as I choose which price I want from the identified candle. So for example if I'm using a ZigZag or a Fractal indicator for retrieving the candle, I would want the "Low" value at that particular time from the indicator's buffer. So only "High" and "Low" would be passed into it for parameters, Close and Open are not used for this particular indicator.

It doesn't seem to matter which indicators I use to harvest the candle values, I believe it comes from when I send in Daily candle info and try to enumerate it on the M15 Timeframe for example, it may be farther back in history than the current chart M15 timeframe has loaded, so when it returns the time, it locks up because it can't find that particular candle's time in the charts history. This I believe is why it works as long as I go to each Chart timeframe and use the 'Home Key' to preload all the history first...

WHRoeder - I understand the "0" can be used to get whatever timeframe, however I have it broken out for control. For example, I may want to only return the 5 minute candle on the M1 timeframe, so I can easily change the timeframe return value. If I didn't have it cased out, I wouldn't be able to do that separately.... the final code is far from done, and I was hoping to handle the history stuff before I go any further

note: some of the code I am working on is proprietary (however I am not getting paid!) and I cannot post entire thing, I wish I could but....

Thanks again for your interest and time spent on your inputs.

Steve

Reason: