That is because you are only collecting "50" bars but then testing "noofbars" which is probably more than 50. Also, please take note of the "for" loop using "k < noofbars" instead of the "<=" which adds one more iteration.

Fernando Carreiro: That is because you are only collecting "50" bars but then testing "noofbars" which is probably more than 50. Also, please take note of the "for" loop using "k < noofbars" instead of the "<=" which adds one more iteration.