//+------------------------------------------------------------------+
//|                                                  MTF Candles.mq4 |
//|                                       2007, 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		TimeFrame		= 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 ("+TimeFrame+")");
	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 shift1 = iBarShift(NULL,TimeFrame,Time[i]),
			 time1  = iTime    (NULL,TimeFrame,shift1),
			 shift2 = iBarShift(NULL,0,time1);

		double	high		= iHigh(NULL,TimeFrame,shift1),
					low		= iLow(NULL,TimeFrame,shift1),
					open		= iOpen(NULL,TimeFrame,shift1),
					close		= iClose(NULL,TimeFrame,shift1),
			 		bodyHigh	= MathMax(open,close),
					bodyLow	= MathMin(open,close);

		if(open<=close)
		{
			Bar1[shift2] = high;		Candle1[shift2] = bodyHigh;
			Bar2[shift2] = low;		Candle2[shift2] = bodyLow;
		}
		else
		{
			Bar1[shift2] = low;		Candle1[shift2] = bodyLow;
			Bar2[shift2] = high;		Candle2[shift2] = bodyHigh;
		}
	}

	return(0);
}
//+------------------------------------------------------------------+

