I do not know the system but at least this line looks wrong in your code. You set buyStopLevel to 0:

var buyStopLevel = 0;

and you don't set it afterwards except for the rare crossing points, so you enter most trades with entry stop 0. This seems wrong because then you would not need an entry stop at all.

If you want to keep the variable value even when the function is terminated, you need a static variable:

static var buyStopLevel = 0;