//+-------------------------------------------------------------------+
//|	CCI_Trend_with_trail.mq4
//| Based on Symphonie_Trendline_Indicator
//| !!! this file is created with Tab size 4 and no space indentation
//+-------------------------------------------------------------------+
#property copyright "Comer"
#property link		""

#property indicator_chart_window

#property indicator_buffers	2

#property indicator_color1	DodgerBlue
#property indicator_style1	STYLE_SOLID
#property indicator_width1	2

#property indicator_color2	Red
#property indicator_style2	STYLE_SOLID
#property indicator_width2	2


extern int		CCI_Period		= 45;
extern int		ATR_Period		= 15;
extern double	ATR_Multiplier	= 1.0;

double CCI_Trigger_Level = 0.0;

double TrendUp[];
double TrendDown[];

int BarNumber;
int BarIndex;

int init() {
	SetIndexStyle ( 0, DRAW_LINE );
	SetIndexBuffer( 0, TrendUp );
	
	SetIndexStyle ( 1, DRAW_LINE );
	SetIndexBuffer( 1, TrendDown );
	
	return(0);
}
 
int deinit() {
	return(0);
}

int start() {
	BarNumber = IndicatorCounted();	// [0 .. Bars]
   
	for( BarIndex = Bars - BarNumber; BarIndex > 0; ) {
		BarIndex--;		// [Bars-1 .. 0]
		processBar();
		BarNumber++;
	}
   
	return(0);
}

void processBar() {
	double	cciNow;
	double	cciPrevious;
	bool	trendUp;
	bool	trendChanged = false;

	if( BarNumber == 0 ) // BarIndex == Bars-1
		return;

	cciNow		= iCCI( NULL, 0, CCI_Period, PRICE_TYPICAL, BarIndex	);
	cciPrevious	= iCCI( NULL, 0, CCI_Period, PRICE_TYPICAL, BarIndex+1	);
	
	if( (cciNow > CCI_Trigger_Level) && (cciPrevious <= CCI_Trigger_Level) ) { // crossed up through the trigger level
		trendChanged = true;
		trendUp = true;
	}
	else
	if( (cciNow < CCI_Trigger_Level) && (cciPrevious >= CCI_Trigger_Level) ) { // crossed down through the trigger level
		trendChanged = true;
		trendUp = false;
	}
	else	// trend did not change
		trendUp = (TrendUp[BarIndex+1] != EMPTY_VALUE);

	if( trendUp ) {
		TrendUp[BarIndex] = Low[BarIndex] - ATR_Multiplier*iATR(NULL, 0, ATR_Period, BarIndex );
		if( trendChanged )
			TrendUp[BarIndex+1] = TrendDown[BarIndex+1];	// not repaint - stitch two lines together
		if( TrendUp[BarIndex] < TrendUp[BarIndex+1] )
			TrendUp[BarIndex] = TrendUp[BarIndex+1];
		TrendDown[BarIndex] = EMPTY_VALUE;
	}
	else {
		TrendDown[BarIndex] = High[BarIndex] + ATR_Multiplier*iATR(NULL, 0, ATR_Period, BarIndex );
		if( trendChanged )
			TrendDown[BarIndex+1] = TrendUp[BarIndex+1];	// not repaint - stitch two lines together
		if (TrendDown[BarIndex] > TrendDown[BarIndex+1])
			TrendDown[BarIndex] = TrendDown[BarIndex+1];
		TrendUp[BarIndex] = EMPTY_VALUE;
	}
}

