/*------------------------------------------------------------------+
//| dayHL_Average.mq4 |
//+------------------------------------------------------------------+
/*
Name := dayHL_Average
Author := KCBT
Link := http://www.kcbt.ru/forum/index.php?
*/

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Red
#property indicator_color3 Blue
//---- input parameters5
extern int show_comment=0; // comments on the chart (0 - no, 1 - yes)
extern int how_long=100000; // bars to be counted (-1 - all the bars)
//---- indicator buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
SetIndexBuffer(0, ExtMapBuffer1);
SetIndexStyle(0, DRAW_LINE, STYLE_DOT);
SetIndexBuffer(1, ExtMapBuffer2);
SetIndexStyle(1, DRAW_LINE, STYLE_DOT);
SetIndexBuffer(2, ExtMapBuffer3);
SetIndexStyle(2, DRAW_LINE, STYLE_DOT);
return(0);
}

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
Comment("");
return(0);
}

int start()
{
int cnt=0; 
int begin_bar=0; 
int prev_month, cur_month; 
double month_high=0; 
double month_low=0; 
double lastmonth_high=0; 
double lastmonth_low=0; 
double lastmonth_close=0; 
double P, S, R;

// 
if (Period() >= PERIOD_MN1) {
Comment("WARNING: Invalid timeframe! Valid value < W1.");
return(0);
}

// 
if (how_long == -1) {
begin_bar = Bars;
} else {
begin_bar = how_long;
}

// 
for (cnt = begin_bar; cnt >= 0; cnt--) {
cur_month = TimeMonth(datetime Time());
if (prev_month != cur_month) {
lastmonth_close = Close[cnt+1];
lastmonth_high = month_high;
lastmonth_low = month_low;
P = (lastmonth_high + lastmonth_low ) / 2;
R = lastmonth_high;
S = lastmonth_low;

// 
month_high = High[cnt];
month_low = Low[cnt];

// 
prev_month = cur_month;
}

// 
month_high = MathMax(month_high, High[cnt]);
month_low = MathMin(month_low, Low[cnt]);

// 
ExtMapBuffer1[cnt] = R;
ExtMapBuffer3[cnt] = S; 
}

if (show_comment == 1) {
P = (lastmonth_high + lastmonth_low ) / 2;
R = lastmonth_high;
S = lastmonth_low;

Comment("Current H=", R, ", L=", S, ", HL/2=", P, ", H-L=", (R-S)/Point );
}
return(0);
}
//+------------------------------------------------------------------+