//+------------------------------------------------------------------+
//|                                                  MTF Candles.mq4 |
//|                                             Christof Risch (iya) |
//| Shows candles from another (usually higher) timeframe.				|
//+------------------------------------------------------------------+
#property link "http://www.forexfactory.com/"
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 RoyalBlue
#property indicator_color2 Crimson
#property indicator_color3 RoyalBlue
#property indicator_color4 Crimson
#property indicator_width1 1
#property indicator_width2 1
#property indicator_width3 5
#property indicator_width4 5

//---- input parameters
extern int		PERIOD			= 60,		// {1=M1, 5=M5, ..., 60=H1, 240=H4, 1440=D1, ...}
					BarWidth			= 1,
					CandleWidth		= 5;

//---- buffers
double Bar1[],	 Candle1[],
		 Bar2[],	 Candle2[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
	IndicatorShortName("MTF Candles ("+PERIOD+")");
	SetIndexBuffer(0,Bar1);
	SetIndexBuffer(1,Bar2);				
	SetIndexBuffer(2,Candle1);
	SetIndexBuffer(3,Candle2);
	SetIndexStyle(0,DRAW_HISTOGRAM,0,BarWidth);
	SetIndexStyle(1,DRAW_HISTOGRAM,0,BarWidth);
	SetIndexStyle(2,DRAW_HISTOGRAM,0,CandleWidth);
	SetIndexStyle(3,DRAW_HISTOGRAM,0,CandleWidth);
	return(0);
}


//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
	for(int i = Bars-1-IndicatorCounted(); i>=0; i--)
	{
		int shift = iBarShift(NULL,PERIOD,Time[i],true);
		if(iTime(NULL,PERIOD,shift)!=Time[i])
			continue;

		double	high		= iHigh(NULL,PERIOD,shift),
					low		= iLow(NULL,PERIOD,shift),
					open		= iOpen(NULL,PERIOD,shift),
					close		= iClose(NULL,PERIOD,shift),
			 		bodyHigh	= MathMax(open,close),
					bodyLow	= MathMin(open,close);

		if(open<=close)
		{
			Bar1[i] = high;	Candle1[i] = bodyHigh;
			Bar2[i] = low;		Candle2[i] = bodyLow;
		}
		else
		{
			Bar1[i] = low;		Candle1[i] = bodyLow;
			Bar2[i] = high;	Candle2[i] = bodyHigh;
		}
	}

	return(0);
}
//+------------------------------------------------------------------+

