Problem with my EA

 
Hello,

I'm working on an EA that retrieves the closing price of the last closed bar i.e. bar[1] using the hour timeframe. My initial assumption was that once the hour changed lets say from 11:59:59 to 12:00:00 close price of the bar would be the opening price of the next bar unless a new tick came in. That assumption was obviously wrong as the new doesn't update until a new tick comes in. What a stupid idea that was.

Timing on this EA is critical and I need to tally up the close price for the last closed bar for each pair asap after the new bar opens. The problem is if I'm reading say bar[1] for gbpusd at 12:00:00 but a new tick doesn't come in until say 12:00:05, then I'm getting data from what should now be bar[2].

So I Created a function that that gets the current server time and the open time for bar[0] then use the hour (12) for each to compare. If the server time hour is 12 and the open time hour for bar[0] is 12 then I assume the bar has updated. If not, I use the bid price for bar[0] to plug in as the close price for bar[1]. Most of the time this works fine.

But every so often, the times for the comparison are not updating even though the bar on the chart has clearly updated. Here is the code that checks to see of the bar has updated.....


My EA doesn't use the 1 minute window but I coded it in for debugging so I don't have to wait an hour for a bar to close.
Also, this EA does not use the start() function. All of the code is called from the init() function so it runs continuously regardless of ticks.
Can anyone shed some light on why this would occasionally fail?

----------------------------------------------------------------------------------------------------------------------
bool bHasBarUpdated(string sSymbolName)
{
datetime tCurrentBarOpenTime, tCurrentServerTime, tPreviousBarOpenTime;
int iCurrentBarOpenTime, iCurrentServerTime, iPreviousBarOpenTime, iDifference;

RefreshRates();

tPreviousBarOpenTime=iTime(sSymbolName,iTimeFrame,iBar_Shift);
tCurrentBarOpenTime =iTime(sSymbolName,iTimeFrame,0);
tCurrentServerTime=TimeCurrent();

switch(iTimeFrame) //extract the hour from the time to make the comparison
{
case 1: //minute TF
iCurrentBarOpenTime = StrToInteger(StringSubstr(TimeToStr(tCurrentBarOpenTime,TIME_DATE|TIME_SECONDS),14,2));
iCurrentServerTime = StrToInteger(StringSubstr(TimeToStr(tCurrentServerTime,TIME_DATE|TIME_SECONDS),14,2));
break;

case 60: //1 hour TF
iCurrentBarOpenTime = StrToInteger(StringSubstr(TimeToStr(tCurrentBarOpenTime,TIME_DATE|TIME_SECONDS),11,2));
iCurrentServerTime = StrToInteger(StringSubstr(TimeToStr(tCurrentServerTime,TIME_DATE|TIME_SECONDS),11,2));
break;

default:
Print("Time Frame Not Supported");
return(TRUE);
}

iDifference=iCurrentServerTime-iCurrentBarOpenTime;

if(iDifference==0)
return(true);

return(False);
}

---------------------------------------------------------------------------------------------------------------------------
 

try using this code :


init :

prevBars=0;


start:
newBar=(Bars > prevBars);

if (newBar)
prevBars=Bars;


 
TuTBaluT:

try using this code :


init :

prevBars=0;


start:
newBar=(Bars > prevBars);

if (newBar)
prevBars=Bars;


Thanks for the reply!


I'm not using start() for a reason.

This EA scans the last closed bar for 23 pairs every hour tallying up certain data.....

If the criteria is correct, one or more orders is opened... only one per pair currently though.


Once the orders are open, the EA then monitors prices and adjust the trailing stops if needed.

As it scanning orders it is also keeping an eye on the time for the next hour change.


The function above is used to determine if a bar for a pair has closed and is used in several places in the program.

I have no need to process price ticks... just tallying it up after it has closed.


The problem is that the above function occasionally does not detect the closing of the bar when the chart has clearly updated and price has changed.

The line: tCurrentBarOpenTime =iTime(sSymbolName,iTimeFrame,0) doesn't always seem to catch the time change. I have no idea why it would fail.


I'll have to say.... MQL4 is about weakest programming language I've ever had the displeasure to program in. I hope MQL5 is better.


Thanks for any suggestions.