#property copyright "Copyright © 2012, Gadi."
#property link      "Gadi @ CompIT mod by Chris Jungen"

#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 Lavender
#property indicator_color2 Lime
#property indicator_color3 Red

extern int TF = 0;
double OBVbuf[];
double OBVbufH[];
double OBVbufL[];
string indicatorName;

int init()
{
    SetIndexBuffer(0, OBVbuf);
    SetIndexBuffer(1, OBVbufH);
    SetIndexBuffer(2, OBVbufL);
    SetIndexStyle(0, DRAW_LINE);
    IndicatorDigits(0);
    IndicatorShortName(indicatorName);
    SetIndexLabel(0, indicatorName);
    return(0);
}

int start()
{
    double closeNow, closePrev, candleHeight, val;
    int i, barCount = IndicatorCounted();

    if ( barCount > 0 )
        barCount--;
    int count = Bars - barCount - 1;

    for ( i = count; i>=0; i-- )
    {
        if ( i == Bars - 1 )
        {
            OBVbuf[i] = iVolume(NULL, TF, i);
            OBVbufH[i] = OBVbuf[i];
            OBVbufL[i] = OBVbuf[i];
        }
        else
        {
            closeNow = iClose( NULL, TF, i );
            closePrev = iClose( NULL, TF, i + 1 );

            if ( closeNow == closePrev )
                OBVbuf[i] = OBVbuf[i + 1];
            else
            {
                candleHeight = iHigh(NULL, TF, i) - iLow(NULL, TF, i);
                if ( candleHeight > 0.0 )
                {
                    if ( closeNow < closePrev )
                    {
                        val = (iOpen(NULL, TF, i) - closeNow) / candleHeight;
                        OBVbuf[i] = OBVbuf[i + 1] - iVolume(NULL, TF, i) * val;
                    }
                    else
                    {
                        val = (closeNow - iOpen(NULL, TF, i)) / candleHeight;
                        OBVbuf[i] = OBVbuf[i + 1] + iVolume(NULL, TF, i) * val;
                    }
                }
                else
                    OBVbuf[i] = OBVbuf[i + 1];
            }

            // --- HIGH ----
            closeNow = iHigh( NULL, TF, i );
            closePrev = iClose( NULL, TF, i + 1 );

            if ( closeNow == closePrev )
                OBVbufH[i] = OBVbuf[i + 1];
            else
            {
                candleHeight = iHigh(NULL, TF, i) - iLow(NULL, TF, i);
                if ( candleHeight > 0.0 )
                {
                    if ( closeNow < closePrev )
                    {
                        val = (iOpen(NULL, TF, i) - closeNow) / candleHeight;
                        OBVbufH[i] = OBVbuf[i + 1] - iVolume(NULL, TF, i) * val;
                    }
                    else
                    {
                        val = (closeNow - iOpen(NULL, TF, i)) / candleHeight;
                        OBVbufH[i] = OBVbuf[i + 1] + iVolume(NULL, TF, i) * val;
                    }
                }
                else
                    OBVbufH[i] = OBVbuf[i + 1];
            }

            // --- LOW ----
            closeNow = iLow( NULL, TF, i );
            closePrev = iClose( NULL, TF, i + 1 );

            if ( closeNow == closePrev )
                OBVbufL[i] = OBVbuf[i + 1];
            else
            {
                candleHeight = iHigh(NULL, TF, i) - iLow(NULL, TF, i);
                if ( candleHeight > 0.0 )
                {
                    if ( closeNow < closePrev )
                    {
                        val = (iOpen(NULL, TF, i) - closeNow) / candleHeight;
                        OBVbufL[i] = OBVbuf[i + 1] - iVolume(NULL, TF, i) * val;
                    }
                    else
                    {
                        val = (closeNow - iOpen(NULL, TF, i)) / candleHeight;
                        OBVbufL[i] = OBVbuf[i + 1] + iVolume(NULL, TF, i) * val;
                    }
                }
                else
                    OBVbufL[i] = OBVbuf[i + 1];
            }


        }
    }

    return(0);
}


