//
// "00-VolumeRatio_v100.mq4" -- volume ratio
//                              refer to http://homepage2.nifty.com/portal/tech/vr.htm
//
//    Ver. 1.00  2008/10/26(Sun)  initial version
//
//
#property  copyright "00 - 00mql4@gmail.com"
#property  link      "http://www.mql4.com/"

//---- indicator settings
#property  indicator_separate_window

#property  indicator_buffers  3

#property  indicator_color1  Green
#property  indicator_color2  Yellow
#property  indicator_color3  Red

#property  indicator_width1  1
#property  indicator_width2  1
#property  indicator_width3  1

#property  indicator_style1  STYLE_SOLID
#property  indicator_style2  STYLE_SOLID
#property  indicator_style3  STYLE_SOLID

//---- defines

//---- indicator parameters
extern int  timeFrame  = 0;     // time frame
extern int  nVR        = 14;    // period of VR
extern bool bShowVRA   = true;  // VR[A]
extern bool bShowVRB   = true;  // VR[B]
extern bool bShowWVR   = true;  // Wako VR
extern int  nMaxBars   = 0;     // number of bars, 0: no limit

//---- indicator buffers
double BufferVRA[];  // 0: VR[A]
double BufferVRB[];  // 1: VR[B]
double BufferWVR[];  // 2: Wako VR

//---- vars
string sIndicatorName;
string sIndVolumeRatio = "00-VolumeRatio_v100";

//----------------------------------------------------------------------
string TimeFrameToStr(int timeFrame)
{
    switch (timeFrame) {
    case 1:     return("M1");
    case 5:     return("M5");
    case 15:    return("M15");
    case 30:    return("M30");
    case 60:    return("H1");
    case 240:   return("H4");
    case 1440:  return("D1");
    case 10080: return("W1");
    case 43200: return("MN");
    }
    
    return("??");
}

//----------------------------------------------------------------------
void init()
{
    if (timeFrame == 0) {
	timeFrame = Period();
    }
    
    sIndicatorName = sIndVolumeRatio + "(" + TimeFrameToStr(timeFrame) + "," + nVR + ")";
    
    IndicatorShortName(sIndicatorName);
    
    SetIndexBuffer(0, BufferVRA);
    SetIndexBuffer(1, BufferVRB);
    SetIndexBuffer(2, BufferWVR);
    
    SetIndexLabel(0, "VR[A]");
    SetIndexLabel(1, "VR[B]");
    SetIndexLabel(2, "Wako VR");
    
    SetIndexDrawBegin(0, nVR);
    SetIndexDrawBegin(1, nVR);
    SetIndexDrawBegin(2, nVR);
    
    SetLevelValue(0, 0.0);
    SetLevelValue(1, 100.0);
    SetLevelValue(2, -100.0);
}

//----------------------------------------------------------------------
void start()
{
    int limit;
    int counted_bars = IndicatorCounted();
    
    if (counted_bars > 0) {
	counted_bars--;
    }
    
    limit = Bars - counted_bars;
    limit = MathMax(limit, nMaxBars);
    
    for (int i = 0; i < limit; i++) {
	if (timeFrame != Period()) {
	    datetime t = Time[i];
	    int x = iBarShift(NULL, timeFrame, t);
	    BufferVRA[i] = iCustom(NULL, timeFrame, sIndVolumeRatio, timeFrame, nVR, bShowVRA, bShowVRB, bShowWVR, nMaxBars, 0, x);
	    BufferVRB[i] = iCustom(NULL, timeFrame, sIndVolumeRatio, timeFrame, nVR, bShowVRA, bShowVRB, bShowWVR, nMaxBars, 1, x);
	    BufferWVR[i] = iCustom(NULL, timeFrame, sIndVolumeRatio, timeFrame, nVR, bShowVRA, bShowVRB, bShowWVR, nMaxBars, 2, x);
	    continue;
	}
	
	BufferVRA[i] = EMPTY_VALUE;
	BufferVRB[i] = EMPTY_VALUE;
	BufferWVR[i] = EMPTY_VALUE;
	
	double u = 0;
	double d = 0;
	double s = 0;
	
	for (int j = 0; j < nVR; j++) {
	    x = i + j;
	    double open = Open[x];
	    double close = Close[x];
	    double v = Volume[x];
	    if (close > open) {
		u += v;
	    } else if (close < open) {
		d += v;
	    } else {
		// open == close
		s += v;
	    }
	}
	
	double denom, vr;
	if (bShowVRA) {
	    denom = d + s / 2.0;
	    if (denom == 0) {
		vr = 0;
	    } else {
		vr = (u + s / 2.0) / denom * 100.0;
	    }
	    BufferVRA[i] = vr;
	}
	if (bShowVRB) {
	    denom = d + d + s;
	    vr = 0;
	    if (denom == 0) {
		vr = 0;
	    } else {
		vr = (u + s / 2.0) / denom  * 100.0;
	    }
	    BufferVRB[i] = vr;
	}
	if (bShowWVR) {
	    denom = u + d + s;
	    if (denom == 0) {
		vr = 0;
	    } else {
		vr = (u - d - s) / denom * 100.0;
	    }
	    BufferWVR[i] = vr;
	}
    }
}
