Ok first - that's a huge paintbar.
Second - yes, it is pretty hard to figure out what/why is happening... We do have a method for debugging - you would use the LogString() function which would output a string to the debug stream. See https://www.medvedtrader.com/trader/WebHelp/debugging_paintbars_scans.htm
Third - I do have to note that you seem to be accumulating sums and their averages but they seem to be over the WHOLE data space. Is that what you really want? Usually the averages (or highest highs or lowest lows) are done over some period. MT has a perfect class to do that - it is StateFIFOQueue. It is set for a particular period and keeps track of sums, averages, stdevs, highest highs etc for you.