Make macd code detect divergence

 
Can somebody help me to make this Macd detect divergence between oscilator and price, and draw arrows on detection?


#include <MovingAverages.mqh>

#property indicator_separate_window
#property indicator_buffers 4

input int InpFastEMA = 12;
input int InpSlowEMA = 26;
input int InpSignalSMA = 9;
input int InpWidth = 2;

double ExtMacdBufferRed[];
double ExtMacdBufferMaroon[];
double ExtMacdBufferGreen[];
double ExtMacdBufferLime[];

int OnInit( void )
{
        SetIndexStyle( 0, DRAW_HISTOGRAM, STYLE_SOLID, InpWidth, Red );
        SetIndexStyle( 1, DRAW_HISTOGRAM, STYLE_SOLID, InpWidth, Maroon );
        SetIndexStyle( 2, DRAW_HISTOGRAM, STYLE_SOLID, InpWidth, Green );
        SetIndexStyle( 3, DRAW_HISTOGRAM, STYLE_SOLID, InpWidth, Lime );

        SetIndexBuffer( 0, ExtMacdBufferRed );
        SetIndexBuffer( 1, ExtMacdBufferMaroon );
        SetIndexBuffer( 2, ExtMacdBufferGreen );
        SetIndexBuffer( 3, ExtMacdBufferLime );

        IndicatorShortName( "MACD 4C" );

        return INIT_SUCCEEDED;
}

int OnCalculate
(
        const int rates_total,
        const int prev_calculated,
        const datetime& time[],
        const double& open[],
        const double& high[],
        const double& low[],
        const double& close[],
        const long& tick_volume[],
        const long& volume[],
        const int& spread[]
)
{
        int limit, i;

        if( rates_total <= InpSignalSMA )
        {
                return 0;
        }

        limit = ( rates_total - prev_calculated );

        if( prev_calculated > 0 )
        {
                limit++;
        }

        double prevVal = 0;

        for( i = 0; i < limit; i++ )
        {
                double fastMa = iMA( NULL, 0, InpFastEMA, 0, MODE_EMA,PRICE_CLOSE, i );
                double slowMa = iMA( NULL, 0, InpSlowEMA, 0, MODE_EMA,PRICE_CLOSE, i );

                double currVal = ( fastMa - slowMa );
                //Alert("Fast iMA: ", fastMa);
                //Alert("Slow iMA: ", slowMa);
                if( currVal > 0 )
                {
                        if( currVal > prevVal )
                        {
                                ExtMacdBufferGreen[i] = currVal;
                        }
                        else
                        {
                                ExtMacdBufferLime[i] = currVal;
                        }
                }
                else
                {
                        if( currVal < prevVal )
                        {
                                ExtMacdBufferRed[i] = currVal;
                        }
                        else
                        {
                                ExtMacdBufferMaroon[i] = currVal;
                        }
                }

                prevVal = currVal;
        }

        return rates_total;
}