This code, if I understand it right, can cause an endless loop or even a crash. If you program an indicator with a loop, you must build in some stop criteria for preventing that it exceeds the Lookback period.