As far as I see, the code above can be greatly simplified, but you can also just copy it - it's plain C and works exactly the same way in Zorro.

You only need to emulate the iMA. AFAIK it's equivalent to the SMA. A shift like "i+j" can just be added to the series, f.i. SMA(Price+i+j,...). Make sure that the lookback is long enough for covering the time period plus maximum i+j. Otherwise you'll get that crash that you mentioned. You can see under "Tips & Tricks" how to emulate a MT4 indicator.