int limit = Bars - bars_counted;
...
for(int i=limit; i>=0; i--) {
atr = iATR(Symbol(), 0, ATR_Period, i);
...
buffer_line_up[i] = (High[i+1]+Low[i+1])/2;
When bars_counted == 0, accesses High[Bar+1], High[Bar] and buffer[Bar] which do not exist. Also iATR is undefined for i=Bars-ATR_Period and above