How to code divergence?

I have developed a trading strategy and would like to autotrade it. However, it does involve, both for the setup and for part of the exit, a divergence on a MACD which must be checked before a trade can be placed. I was wondering if there is a command, or perhaps a subroutine for that. I can create a flow chart for it but it doesn't seem so easy to code and my coding skills are certainly not exceptional.