Bar Data Upon Restart or Reconnect

 

Hi there,

I've noticed some strange bar behaviour when MT4 terminal connects to the server after being disconnected for

a period of time.


I wrote a simple indicator to examine this behaviour that prints details of the last 5 bars in a chart

every time the start() function is called. The output below is an extract of that indicators output

when attached to the EURUSD 1 min chart.

>> This is the output when attached to the chart.


1 2008.06.17 00:14:03 test_ind EURUSDm,M1: Bars=40893 total=0 start_bar=40892
2 2008.06.17 00:14:03 test_ind EURUSDm,M1: bar=4 time=2008.06.17 04:10:00
3 2008.06.17 00:14:03 test_ind EURUSDm,M1: bar=3 time=2008.06.17 04:11:00
4 2008.06.17 00:14:03 test_ind EURUSDm,M1: bar=2 time=2008.06.17 04:12:00
5 2008.06.17 00:14:03 test_ind EURUSDm,M1: bar=1 time=2008.06.17 04:13:00
6 2008.06.17 00:14:03 test_ind EURUSDm,M1: bar=0 time=2008.06.17 04:14:00

>> Simple enough. If I close my terminal, wait a few minutes for new 1 min data to be created at the server end

>> and then restart, this is the output I get.

1 2008.06.17 00:22:43 test_ind EURUSDm,M1: Bars=40893 total=0 start_bar=40892
2 2008.06.17 00:22:43 test_ind EURUSDm,M1: bar=4 time=2008.06.17 04:10:00
3 2008.06.17 00:22:43 test_ind EURUSDm,M1: bar=3 time=2008.06.17 04:11:00
4 2008.06.17 00:22:43 test_ind EURUSDm,M1: bar=2 time=2008.06.17 04:12:00
5 2008.06.17 00:22:43 test_ind EURUSDm,M1: bar=1 time=2008.06.17 04:13:00
6 2008.06.17 00:22:43 test_ind EURUSDm,M1: bar=0 time=2008.06.17 04:14:00

>> This is the pre-existing bar data because the terminal has yet to connect to the server as expected.

7 2008.06.17 00:22:44 test_ind EURUSDm,M1: ****************************************************************
8 2008.06.17 00:22:44 test_ind EURUSDm,M1: Bars=40893 total=40893 start_bar=0
9 2008.06.17 00:22:44 test_ind EURUSDm,M1: bar=4 time=2008.06.17 04:10:00
10 2008.06.17 00:22:44 test_ind EURUSDm,M1: bar=3 time=2008.06.17 04:11:00
11 2008.06.17 00:22:44 test_ind EURUSDm,M1: bar=2 time=2008.06.17 04:12:00
12 2008.06.17 00:22:44 test_ind EURUSDm,M1: bar=1 time=2008.06.17 04:13:00
13 2008.06.17 00:22:44 test_ind EURUSDm,M1: bar=0 time=2008.06.17 04:14:00
14 2008.06.17 00:22:44 test_ind EURUSDm,M1: ****************************************************************

>> On line 15 the terminal has reconnected to the server and a bar for 04:22 appears. Notice that there is a gap no

>> data for 04:15 to 04:21.


15 2008.06.17 00:22:44 test_ind EURUSDm,M1: Bars=40894 total=40893 start_bar=1
16 2008.06.17 00:22:44 test_ind EURUSDm,M1: bar=4 time=2008.06.17 04:11:00
17 2008.06.17 00:22:44 test_ind EURUSDm,M1: bar=3 time=2008.06.17 04:12:00
18 2008.06.17 00:22:44 test_ind EURUSDm,M1: bar=2 time=2008.06.17 04:13:00
19 2008.06.17 00:22:44 test_ind EURUSDm,M1: bar=1 time=2008.06.17 04:14:00
20 2008.06.17 00:22:44 test_ind EURUSDm,M1: bar=0 time=2008.06.17 04:22:00
21 2008.06.17 00:22:45 test_ind EURUSDm,M1: ****************************************************************

22 2008.06.17 00:22:45 test_ind EURUSDm,M1: Bars=40894 total=40894 start_bar=0
23 2008.06.17 00:22:45 test_ind EURUSDm,M1: bar=4 time=2008.06.17 04:11:00
24 2008.06.17 00:22:45 test_ind EURUSDm,M1: bar=3 time=2008.06.17 04:12:00
25 2008.06.17 00:22:45 test_ind EURUSDm,M1: bar=2 time=2008.06.17 04:13:00
26 2008.06.17 00:22:45 test_ind EURUSDm,M1: bar=1 time=2008.06.17 04:14:00
27 2008.06.17 00:22:45 test_ind EURUSDm,M1: bar=0 time=2008.06.17 04:22:00
28 2008.06.17 00:22:45 test_ind EURUSDm,M1: ****************************************************************
29 2008.06.17 00:22:45 test_ind EURUSDm,M1: Bars=40894 total=40894 start_bar=0
30 2008.06.17 00:22:45 test_ind EURUSDm,M1: bar=4 time=2008.06.17 04:11:00
31 2008.06.17 00:22:45 test_ind EURUSDm,M1: bar=3 time=2008.06.17 04:12:00
32 2008.06.17 00:22:45 test_ind EURUSDm,M1: bar=2 time=2008.06.17 04:13:00
33 2008.06.17 00:22:45 test_ind EURUSDm,M1: bar=1 time=2008.06.17 04:14:00
34 2008.06.17 00:22:45 test_ind EURUSDm,M1: bar=0 time=2008.06.17 04:22:00
35 2008.06.17 00:22:46 test_ind EURUSDm,M1: ****************************************************************
36 2008.06.17 00:22:46 test_ind EURUSDm,M1: Bars=40894 total=40894 start_bar=0
37 2008.06.17 00:22:46 test_ind EURUSDm,M1: bar=4 time=2008.06.17 04:11:00
38 2008.06.17 00:22:46 test_ind EURUSDm,M1: bar=3 time=2008.06.17 04:12:00
39 2008.06.17 00:22:46 test_ind EURUSDm,M1: bar=2 time=2008.06.17 04:13:00
40 2008.06.17 00:22:46 test_ind EURUSDm,M1: bar=1 time=2008.06.17 04:14:00
41 2008.06.17 00:22:46 test_ind EURUSDm,M1: bar=0 time=2008.06.17 04:22:00
42 2008.06.17 00:22:46 test_ind EURUSDm,M1: ****************************************************************
43 2008.06.17 00:22:46 test_ind EURUSDm,M1: Bars=40894 total=40894 start_bar=0
44 2008.06.17 00:22:46 test_ind EURUSDm,M1: bar=4 time=2008.06.17 04:11:00
45 2008.06.17 00:22:46 test_ind EURUSDm,M1: bar=3 time=2008.06.17 04:12:00
46 2008.06.17 00:22:46 test_ind EURUSDm,M1: bar=2 time=2008.06.17 04:13:00
47 2008.06.17 00:22:46 test_ind EURUSDm,M1: bar=1 time=2008.06.17 04:14:00
48 2008.06.17 00:22:46 test_ind EURUSDm,M1: bar=0 time=2008.06.17 04:22:00
49 2008.06.17 00:22:46 test_ind EURUSDm,M1: ****************************************************************
50 2008.06.17 00:22:46 test_ind EURUSDm,M1: Bars=40894 total=40894 start_bar=0
51 2008.06.17 00:22:46 test_ind EURUSDm,M1: bar=4 time=2008.06.17 04:11:00
52 2008.06.17 00:22:46 test_ind EURUSDm,M1: bar=3 time=2008.06.17 04:12:00
53 2008.06.17 00:22:46 test_ind EURUSDm,M1: bar=2 time=2008.06.17 04:13:00
54 2008.06.17 00:22:46 test_ind EURUSDm,M1: bar=1 time=2008.06.17 04:14:00
55 2008.06.17 00:22:46 test_ind EURUSDm,M1: bar=0 time=2008.06.17 04:22:00
56 2008.06.17 00:22:46 test_ind EURUSDm,M1: ****************************************************************
57 2008.06.17 00:22:46 test_ind EURUSDm,M1: Bars=40894 total=40894 start_bar=0
58 2008.06.17 00:22:46 test_ind EURUSDm,M1: bar=4 time=2008.06.17 04:11:00
59 2008.06.17 00:22:46 test_ind EURUSDm,M1: bar=3 time=2008.06.17 04:12:00
60 2008.06.17 00:22:46 test_ind EURUSDm,M1: bar=2 time=2008.06.17 04:13:00
61 2008.06.17 00:22:46 test_ind EURUSDm,M1: bar=1 time=2008.06.17 04:14:00
62 2008.06.17 00:22:46 test_ind EURUSDm,M1: bar=0 time=2008.06.17 04:22:00
63 2008.06.17 00:22:46 test_ind EURUSDm,M1: ****************************************************************
64 2008.06.17 00:22:46 test_ind EURUSDm,M1: Bars=40894 total=40894 start_bar=0
65 2008.06.17 00:22:46 test_ind EURUSDm,M1: bar=4 time=2008.06.17 04:11:00
66 2008.06.17 00:22:46 test_ind EURUSDm,M1: bar=3 time=2008.06.17 04:12:00
67 2008.06.17 00:22:46 test_ind EURUSDm,M1: bar=2 time=2008.06.17 04:13:00
68 2008.06.17 00:22:46 test_ind EURUSDm,M1: bar=1 time=2008.06.17 04:14:00
69 2008.06.17 00:22:46 test_ind EURUSDm,M1: bar=0 time=2008.06.17 04:22:00
70 2008.06.17 00:22:46 test_ind EURUSDm,M1: ****************************************************************
71 2008.06.17 00:22:46 test_ind EURUSDm,M1: Bars=40894 total=40894 start_bar=0
72 2008.06.17 00:22:46 test_ind EURUSDm,M1: bar=4 time=2008.06.17 04:11:00
73 2008.06.17 00:22:46 test_ind EURUSDm,M1: bar=3 time=2008.06.17 04:12:00
74 2008.06.17 00:22:46 test_ind EURUSDm,M1: bar=2 time=2008.06.17 04:13:00
75 2008.06.17 00:22:46 test_ind EURUSDm,M1: bar=1 time=2008.06.17 04:14:00
76 2008.06.17 00:22:46 test_ind EURUSDm,M1: bar=0 time=2008.06.17 04:22:00
77 2008.06.17 00:22:47 test_ind EURUSDm,M1: ****************************************************************
78 2008.06.17 00:22:47 test_ind EURUSDm,M1: Bars=40894 total=40894 start_bar=0
79 2008.06.17 00:22:47 test_ind EURUSDm,M1: bar=4 time=2008.06.17 04:11:00
80 2008.06.17 00:22:47 test_ind EURUSDm,M1: bar=3 time=2008.06.17 04:12:00
81 2008.06.17 00:22:47 test_ind EURUSDm,M1: bar=2 time=2008.06.17 04:13:00
82 2008.06.17 00:22:47 test_ind EURUSDm,M1: bar=1 time=2008.06.17 04:14:00
83 2008.06.17 00:22:47 test_ind EURUSDm,M1: bar=0 time=2008.06.17 04:22:00
84 2008.06.17 00:22:47 test_ind EURUSDm,M1: ****************************************************************
85 2008.06.17 00:22:47 test_ind EURUSDm,M1: Bars=40894 total=40894 start_bar=0
86 2008.06.17 00:22:47 test_ind EURUSDm,M1: bar=4 time=2008.06.17 04:11:00
87 2008.06.17 00:22:47 test_ind EURUSDm,M1: bar=3 time=2008.06.17 04:12:00
88 2008.06.17 00:22:47 test_ind EURUSDm,M1: bar=2 time=2008.06.17 04:13:00
89 2008.06.17 00:22:47 test_ind EURUSDm,M1: bar=1 time=2008.06.17 04:14:00
90 2008.06.17 00:22:47 test_ind EURUSDm,M1: bar=0 time=2008.06.17 04:22:00
91 2008.06.17 00:22:47 test_ind EURUSDm,M1: ****************************************************************

>> On line 92 the bars for 04:15 - 04:21 appear and get inserted in the bar series before the 04:22 bar.

92 2008.06.17 00:22:47 test_ind EURUSDm,M1: Bars=40901 total=40894 start_bar=7
93 2008.06.17 00:22:47 test_ind EURUSDm,M1: bar=4 time=2008.06.17 04:18:00
94 2008.06.17 00:22:47 test_ind EURUSDm,M1: bar=3 time=2008.06.17 04:19:00
95 2008.06.17 00:22:47 test_ind EURUSDm,M1: bar=2 time=2008.06.17 04:20:00
96 2008.06.17 00:22:47 test_ind EURUSDm,M1: bar=1 time=2008.06.17 04:21:00
97 2008.06.17 00:22:47 test_ind EURUSDm,M1: bar=0 time=2008.06.17 04:22:00

This insertion of bars before the last bar is problematic if one is keeping trying to keep track of the last bar processed

since when a reconnect to the server occurs bars may appear to be inserted before what was the last bar.

For example if I was basing a moving average on the bar data above then on line 85 my average would be based

on the bar 04:22 being the last bar and bar 04:14 being the bar before the last bar. This really shouldn't change.

85 2008.06.17 00:22:47 test_ind EURUSDm,M1: Bars=40894 total=40894 start_bar=0
86 2008.06.17 00:22:47 test_ind EURUSDm,M1: bar=4 time=2008.06.17 04:11:00
87 2008.06.17 00:22:47 test_ind EURUSDm,M1: bar=3 time=2008.06.17 04:12:00
88 2008.06.17 00:22:47 test_ind EURUSDm,M1: bar=2 time=2008.06.17 04:13:00
89 2008.06.17 00:22:47 test_ind EURUSDm,M1: bar=1 time=2008.06.17 04:14:00
90 2008.06.17 00:22:47 test_ind EURUSDm,M1: bar=0 time=2008.06.17 04:22:00
91 2008.06.17 00:22:47 test_ind EURUSDm,M1:

...on the very next iteration of start() the last bar is still 04:22 which is OK but the bar before it becomes

04:21. This can't be right since the bar before 04:22 has already been determined to be 04:14.

92 2008.06.17 00:22:47 test_ind EURUSDm,M1: Bars=40901 total=40894 start_bar=7
93 2008.06.17 00:22:47 test_ind EURUSDm,M1: bar=4 time=2008.06.17 04:18:00
94 2008.06.17 00:22:47 test_ind EURUSDm,M1: bar=3 time=2008.06.17 04:19:00
95 2008.06.17 00:22:47 test_ind EURUSDm,M1: bar=2 time=2008.06.17 04:20:00
96 2008.06.17 00:22:47 test_ind EURUSDm,M1: bar=1 time=2008.06.17 04:21:00
97 2008.06.17 00:22:47 test_ind EURUSDm,M1: bar=0 time=2008.06.17 04:22:00

I presume this has something to do with the way the MT4 terminal is refreshing its data. Even so doesn't the insertion of bars before the

last bar cause potential problems when processing bar data?

I have attached the indicator test_ind.mq4 to this post should anyone wish to replicate this behaviour. Attach the file to a reasonably

volatile 1 min chart then close the MT4 terminal. Wait say 10 minutes then reopen the terminal. The test_ind indicator will run and

the output of test_ind will show the way that bars are being inserted before the last bar as described above.


Is there any way to workaround the way that MT4 appears to insert bar data before the last bar in this fashion?

Regards,

Laurence.

Files:
test_ind.mq4  2 kb
 

Indicators are calculated in the interface thread. Indicators are calculated when just client terminal starts, then after tick incoming or after part of history downloading. It needed some time for connection and downloading rest of history. In your case it is time from 2008.06.17 00:22:43 to 2008.06.17 00:22:47 ie 4-5 seconds. What is strange?

If your indicator called from some EA then it calculated in the EA's thread. EA started after history download (in common cases). Try to call yor test indicator from some test EA (don't attach this indicator to chart!) and see logs.

 
stringo wrote >>

Indicators are calculated in the interface thread. Indicators are calculated when just client terminal starts, then after tick incoming or after part of history downloading. It needed some time for connection and downloading rest of history. In your case it is time from 2008.06.17 00:22:43 to 2008.06.17 00:22:47 ie 4-5 seconds. What is strange?

If your indicator called from some EA then it calculated in the EA's thread. EA started after history download (in common cases). Try to call yor test indicator from some test EA (don't attach this indicator to chart!) and see logs.

What is strange is that on one tick the data is...

85 2008.06.17 00:22:47 test_ind EURUSDm,M1: Bars=40894 total=40894 start_bar=0
86 2008.06.17 00:22:47 test_ind EURUSDm,M1: bar=4 time=2008.06.17 04:11:00
87 2008.06.17 00:22:47 test_ind EURUSDm,M1: bar=3 time=2008.06.17 04:12:00
88 2008.06.17 00:22:47 test_ind EURUSDm,M1: bar=2 time=2008.06.17 04:13:00
89 2008.06.17 00:22:47 test_ind EURUSDm,M1: bar=1 time=2008.06.17 04:14:00
90 2008.06.17 00:22:47 test_ind EURUSDm,M1: bar=0 time=2008.06.17 04:22:00

04:22:00 is the time of the last bar and 04:14:00 is the time of the bar before that.

Then on the very next tick the data becomes...

92 2008.06.17 00:22:47 test_ind EURUSDm,M1: Bars=40901 total=40894 start_bar=7
93 2008.06.17 00:22:47 test_ind EURUSDm,M1: bar=4 time=2008.06.17 04:18:00
94 2008.06.17 00:22:47 test_ind EURUSDm,M1: bar=3 time=2008.06.17 04:19:00
95 2008.06.17 00:22:47 test_ind EURUSDm,M1: bar=2 time=2008.06.17 04:20:00
96 2008.06.17 00:22:47 test_ind EURUSDm,M1: bar=1 time=2008.06.17 04:21:00
97 2008.06.17 00:22:47 test_ind EURUSDm,M1: bar=0 time=2008.06.17 04:22:00

04:22:00 is still the time of the last bar as one would expect but 04:21:00 is now the time of the bar before that.

Bars are obviously being inserted before the last bar. That is the issue.

Regards,

Laurence.

 

Looks like a few seconds are needed to get things up to date when MT4 starts.

Put a delay in the start of your EA.

 
phy wrote >>

Looks like a few seconds are needed to get things up to date when MT4 starts.

Put a delay in the start of your EA.

Thank you for the advice. It is worth noting however that this issue doesn't just occur when the terminal is started. It also occurs if the terminal is already running and reconnects to the trade server after a period of disconnection.

Even with a delay there is no guarantee that this issue will not occur since there is no way to determine how long one should wait for the trade server to update the terminal history properly.

I could program in say a 10 second wait which will work if the trade server updates its history within 10 seconds of restarting the terminal or reconnecting to the trade server after being disconnected. If it takes longer than 10 seconds for the terminal to update its history then the issue will rear its head again.

This wouldn't be an issue if the MT terminal updated its history properly and did not insert bars before the last bar as described in my original post.

Regards.

Laurence.

 

Re-read Stringo's response:

stringo wrote >>

Indicators are calculated in the interface thread. Indicators are calculated when just client terminal starts, then after tick incoming or after part of history downloading. It needed some time for connection and downloading rest of history. In your case it is time from 2008.06.17 00:22:43 to 2008.06.17 00:22:47 ie 4-5 seconds. What is strange?

If your indicator called from some EA then it calculated in the EA's thread. EA started after history download (in common cases). Try to call yor test indicator from some test EA (don't attach this indicator to chart!) and see logs.

It appears you are still testing from an indicator.

Retest from an EA.

 
phy wrote >>

Re-read Stringo's response:

It appears you are still testing from an indicator.

Retest from an EA.

You are correct. I tested using an EA and it does not exhibit the same behaviour as an Indicator does.

After a restart the EA waits for the history to be updated before start() is called for the first time.

After a restart the Indicator does not wait for the history to be updated before start() is called for the first time. When the history is eventually updated bars are inserted before the last bar as described in my original post.

Regards,

Laurence.

Reason: