Bar

 
Hi there,

I've noticed some strange bar behaviour when launching the MT4 terminal with a pre-existing indicator on a chart.

This is the output of a very simple indicator that just prints details of the last 5 bars in a chart every time the start() function is called. It is run on a 1 min 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 say 7 minutes 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
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: ****************************************************************
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: ****************************************************************
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



After the 5 minute wait notice initially that the bar data is the same as it was when the Terminal was closed 5 minutes ago. This is to be expected.

On line 20 of the output a new bar appears...

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

Notice the 7 minute gap in time between the new bar at 04:22 and the previous bar at 04:14. No bars for 04:15, 04:16, 04:17, 04:18, 04:19 04:20 or 04:21. Very odd.

On line 92 all of sudden these missing bars appear!

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 is obviously due to the way the MT4 terminal is refreshing its data.

Processing bars for an indicator loaded in the MT4 terminal after a restart is therefore problematic if one is keeping trying to keep track of the last bar processed since bars appear to be inserted into the bar sequence.

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

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:

but on the very next iteration of start() the bar data would be...

After a restart one would expect to see...

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

Bar data for 04:18, 04:19, 04:20 and 04:21 has been inserted before what was the last bar at 04:22. I presume that this is because data is being refreshed but even so this can't be right can it? How can bar data be inserted before what was the previous last bar?

Is there any way to workaround the way that bar data is being inserted in this fashion when MT4 is first started and pre-existing indicators on charts are loaded as a consequence.

Regards,

Laurence.