Bad data or bad code?

I am trying to debug a very simple program in mql5. In the journal tab I get the message that the start time has changed to ........., which is about three days after when the data actually starts from. I have outputted alot of the variables to the journal tab so that I can see what is what.

Most of the time the output will start from the first pass through the OnTick() function, but some times it might start a thousand or two later.  When it starts from the first pass, it might output correctly for 20 or 30 passes, then it will do a wrong output and then skip 2 0r 3 thousand ticks, then start outputting properly for 20 or 30 ticks and then repeat the problem.

I will attach the code that I'm using, which I'm pretty sure is right, but it has been many years since I have done this sort of thing, and a sample of a  good and bad output.

I'm not 100% whether the problem is with the code or the data, if it is the data, which does seem to have elements missing, ie there are blank fields where there should be data.  The data was obtained by clicking on the 'ticks' tab then request. Same with the bars.

Where can I find a comprehensive tutorial about data,  exactly what fields are needed, for tick and bar data, the affects of missing data and how to check it, and synchronization ect. I don't mind writing a program to interrogate  the data for integrity, but I would need to know more about the data.

thanks in advance
code_used.png  134 kb
output_good.png  35 kb
output_bad.png  37 kb