Is there a difference to what you want to implement and the existing zig zag indicator?

http://www.zorro-trader.com/manual/en/ta.htm