As far as I see, the script should work as intended, but you can make it simpler. You don't need the 3 extra series from the previous prices. A series already contains all previous prices, as this is the very purpose of a series. Otherwise you could just use normal variables.