Just been playing around with this in AmiBroker and thought others might be interested in it. Was hoping to use it for some backtesting ideas, but it's far too slow as it. Would need to be rewritten into a plug-in DLL.
I realise AmiBroker has built-in functions similar to some of the ones I've written here myself, but like I say, I've just been experimenting.
I've attached the AFL as a file rather than list it all in a message. Just rename the .TXT extension to .AFL or copy and paste into a new indicator. A couple of plot results also attached to show what it looks like.