//
// "00-WeeklyGap_v100.mq4" -- Show weekly gap
//
//    Ver. 1.00  2009/09/16(Wed)  initial version
// 
// 
#property  copyright "00"
#property  link      "http://www.mql4.com/"

//---- defines
#define SEC_PER_DAY   86400
#define SEC_PER_HOUR  3600

//---- indicator settings
#property  indicator_separate_window

#property  indicator_buffers  3

#property  indicator_color1  Aqua
#property  indicator_color2  Yellow
#property  indicator_color3  Orange

#property  indicator_width1  2
#property  indicator_width2  1
#property  indicator_width3  1

#property  indicator_style1  STYLE_SOLID
#property  indicator_style2  STYLE_SOLID
#property  indicator_style3  STYLE_SOLID

#property indicator_level1  0.0

//---- indicator parameters
extern datetime  startDate  = D'2009.1.1 00:00.00';
extern int       nMaxBars   = 20000;

//---- indicator buffers
double BufferGap[];       // 0: Gap Pips
double BufferPlusAve[];   // 1: Average of Plus Gap
double BufferMinusAve[];  // 2: Average of Minus Gap
double BufferCount[];     // 3: Gap Count
double BufferCumPlus[];   // 4: Cumulutive Plus Gap
double BufferCumMinus[];  // 5: Cumulutive Minus Gap
double BufferMaxPlus[];   // 6: Maximum of Plus Gap
double BufferMinMinus[];  // 7: Maximum of Minus Gap

//---- vars
string   sIndicatorName;
string   sIndSelf    = "00-WeeklyGap_v100";
string   sPrefix;
int      g_window;
int      fontSize    = 12;
int      fontMargin  = 3;
string   fontName    = "Arial";
color    colInfo     = Aqua;

//----------------------------------------------------------------------
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()
{
    string tf = TimeFrameToStr(Period());
    sIndicatorName = sIndSelf + "(" + tf + "," + TimeToStr(startDate) + ")";
    sPrefix = sIndicatorName;
    
    IndicatorShortName(sIndicatorName);
    
    IndicatorBuffers(8);
    
    SetIndexBuffer(0, BufferGap);
    SetIndexBuffer(1, BufferPlusAve);
    SetIndexBuffer(2, BufferMinusAve);
    SetIndexBuffer(3, BufferCount);
    SetIndexBuffer(4, BufferCumPlus);
    SetIndexBuffer(5, BufferCumMinus);
    SetIndexBuffer(6, BufferMaxPlus);
    SetIndexBuffer(7, BufferMinMinus);
    
    SetIndexLabel(0, "Gap Pips");
    SetIndexLabel(1, "Average of Plus Gap");
    SetIndexLabel(2, "Average of Minus Gap");
    SetIndexLabel(3, "Gap Count");
    SetIndexLabel(4, "Cumulative Plus Gap");
    SetIndexLabel(5, "Cumulative Minus Gap");
    SetIndexLabel(6, "Maximum of Plus Gap");
    SetIndexLabel(7, "Maximum of Minus Gap");
    
    SetIndexStyle(0, DRAW_HISTOGRAM);
    SetIndexStyle(1, DRAW_LINE);
    SetIndexStyle(2, DRAW_LINE);
}

//----------------------------------------------------------------------
void deinit()
{
    int i;
    int n = ObjectsTotal();
    
    for (i = n - 1; i >= 0; i--) {
	string sName = ObjectName(i);
	if (StringFind(sName, sPrefix) == 0) {
	    ObjectDelete(sName);
	}
    }
}

//----------------------------------------------------------------------
void objLabel(string sName, int corner, int x, int y, string text, color col, int size = 0, string font = "")
{
    sName = sPrefix + sName;
    
    if (size == 0) {
	size = fontSize;
    }
    if (font == "") {
	font = fontName;
    }
    
    ObjectCreate(sName, OBJ_LABEL, g_window, 0, 0);
    ObjectSetText(sName, text, size, font, col);
    ObjectSet(sName, OBJPROP_CORNER, corner);
    ObjectSet(sName, OBJPROP_XDISTANCE, x);
    ObjectSet(sName, OBJPROP_YDISTANCE, y);
}


//----------------------------------------------------------------------
void start()
{
    g_window = WindowFind(sIndicatorName);
    
    int limit;
    int counted_bars = IndicatorCounted();
    
    if (counted_bars > 0) {
	counted_bars--;
    }

    limit = Bars - counted_bars;
    int limit0 = limit;
    if (nMaxBars > 0) {
	limit = MathMin(limit, nMaxBars);
    }
    
    // clear beyond limits
    for (int i = limit0 - 1; i >= limit; i--) {
	BufferGap[i]      = 0;
	BufferPlusAve[i]  = 0;
	BufferMinusAve[i] = 0;
	BufferCount[i]    = 0;
	BufferCumPlus[i]  = 0;
	BufferCumMinus[i] = 0;
	BufferMaxPlus[i]  = 0;
	BufferMinMinus[i] = 0;
    }
    
    for (i = limit - 1; i >= 0; i--) {
	datetime t1 = Time[i + 1];
	datetime t0 = Time[i];
	double gap = 0;
	int nGap = 0;
	if (t0 >= startDate) {
	    if (t0 - t1 > SEC_PER_DAY) {
		int dow = TimeDayOfWeek(t0);
		if (dow <= 1) {
		    gap = (Open[i] - Close[i + 1]) / Point;
		    nGap = 1;
		}
	    }
	}
	
	BufferGap[i] = gap;
	BufferCount[i] = BufferCount[i + 1] + nGap;
	
	BufferCumPlus[i] = BufferCumPlus[i + 1];
	BufferCumMinus[i] = BufferCumMinus[i + 1];
	BufferMaxPlus[i] = BufferMaxPlus[i + 1];
	BufferMinMinus[i] = BufferMinMinus[i + 1];
	
	if (gap >= 0) {
	    BufferCumPlus[i] += gap;
	    if (gap > BufferMaxPlus[i]) {
		BufferMaxPlus[i] = gap;
	    }
	} else {
	    BufferCumMinus[i] += gap;
	    if (gap < BufferMinMinus[i]) {
		BufferMinMinus[i] = gap;
	    }
	}
	
	BufferPlusAve[i] = 0;
	BufferMinusAve[i] = 0;
	
	int n = BufferCount[i];
	if (n != 0) {
	    BufferPlusAve[i] = BufferCumPlus[i] / n;
	    BufferMinusAve[i] = BufferCumMinus[i] / n;
	}
    }
    
    datetime t = Time[0];
    int tx = 4;
    int ty = 4;
    string s;
    
    s = Symbol() + " " + TimeToStr(startDate, TIME_DATE) + "~";
    objLabel("label1", 1, tx, ty, s, colInfo);
    ty += fontSize + fontMargin;
    
    s = "Gap Max: +" + DoubleToStr(BufferMaxPlus[0], 1) + ", " + DoubleToStr(BufferMinMinus[0], 1);
    objLabel("label3", 1, tx, ty, s, colInfo);
    ty += fontSize + fontMargin;
    
    s = "Gap Ave: +" + DoubleToStr(BufferPlusAve[0], 1) + ", " + DoubleToStr(BufferMinusAve[0], 1);
    objLabel("label2", 1, tx, ty, s, colInfo);
    ty += fontSize + fontMargin;
}
