//+------------------------------------------------------------------+
//|                                                      EWTREND.mq4 |
//|                                Copyright © 2005-2006, David W. Thomas |
//|                                           http://www.davidwt.com |
//+------------------------------------------------------------------+
// Elliott Wave Trend
#property copyright "Copyright © 2005-2006, David W. Thomas"
#property link      "http://www.davidwt.com"

#property indicator_separate_window
#property indicator_minimum -2
#property indicator_maximum 2
#property indicator_buffers 1
#property indicator_color1 DeepSkyBlue

//---- input parameters
extern int Per=70;
extern int Trigger=20;

//---- buffers
double EWTRENDBuffer[];
double osc[];

//---- variables
int indexbegin = 0;
double tr=0;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
   IndicatorBuffers(2);
//---- indicators
	SetIndexStyle(0, DRAW_LINE);
	SetIndexBuffer(0, EWTRENDBuffer);
	SetIndexLabel(0, "EWTREND");
//----
	SetIndexBuffer(1, osc);
   string short_name = "EWTREND(" + Per + "," + Trigger + ")";
   IndicatorShortName(short_name);

	indexbegin = Bars - MathMax(Per, 35);
	if (indexbegin < 0)
		indexbegin = 0;
	tr = Trigger / 100.0;

	return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
	return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
   int i;
	int counted_bars = IndicatorCounted();
	double oscmax, oscmin, trend;

	//---- check for possible errors
	if (counted_bars < 0) counted_bars = 0;
	//---- last counted bar will be recounted
	if (counted_bars > 0) counted_bars--;
	if (counted_bars > indexbegin) counted_bars = indexbegin;

	//! current bar in following comments actually refers to the i bar in the loop.
	//! prior bar refers to the i+1 bar (which is the prior bar to the i bar in the loop).
	for (i = indexbegin-counted_bars; i >= 0; i--)
	{
		//! compute EWO for the current bar.  If EWO is > 0, then the 5SMA is above
		//! the 35SMA.  If EWO < 0, then the 5SMA is below the 35SMA.
		osc[i] = iMA(NULL, 0, 5, 0, MODE_SMA, PRICE_MEDIAN, i) - iMA(NULL, 0, 35, 0, MODE_SMA, PRICE_MEDIAN, i);
		//! determine the max and min EWO values for the past Per bars.
		oscmax = osc[ArrayMaximum(osc, Per, i)];
		oscmin = osc[ArrayMinimum(osc, Per, i)];
		//! if trend is unitialized, set it to zero.
		if (EWTRENDBuffer[i+1] > 1 || EWTRENDBuffer[i+1] < -1)
			EWTRENDBuffer[i+1] = 0;
		//! assume the current bar trend is the same as the prior bar trend.
		trend = EWTRENDBuffer[i+1];
		//! initialize the trend indicator to zero (sideways).
		EWTRENDBuffer[i] = 0;
		//! if the trend is zero and the current EWO is the max EWO (of the prior Per bars),
		//! then set the trend to one (up).
		if (osc[i] == oscmax && trend == 0)
			trend = 1;
		//! if the trend is zero and the current EWO is the min EWO (of the prior Per bars),
		//! then set the trend to negative one (down).
		if (osc[i] == oscmin && trend == 0)
			trend = -1;
		//! if the min EWO is less than zero and the trend is down and the EWO is above
		//! trigger point (if trigger is 70%, then if EWO is above 70% of the positve value
		//! of the min EWO), then set the trend to one (up).
		if (oscmin < 0 && trend == -1 && osc[i] > -1 * tr * oscmin)
			trend = 1;
		//! if the max EWO is greater than zero and the trend is up and the EWO is below
		//! trigger point (if trigger is 70%, then if EWO is below 70% of the negative value
		//! of the max EWO), then set the trend to negative one (down).
		if (oscmax > 0 && trend == 1 && osc[i] < -1 * tr * oscmax)
			trend = -1;
		//! set the trend buffer to the computed trend.
		EWTRENDBuffer[i] = trend;
	}
	
	return(0);
}
//+------------------------------------------------------------------+