//+------------------------------------------------------------------+
//|                                                          EWN.mq4 |
//|                                Copyright © 2005-2006, David W. Thomas |
//|                                           http://www.davidwt.com |
//+------------------------------------------------------------------+
// Elliott Wave Number
#property copyright "Copyright © 2005-2006, David W. Thomas"
#property link      "http://www.davidwt.com"

#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 6
#property indicator_buffers 1
#property indicator_color1 Red

//---- input parameters
extern int Per=70;
extern int Trigger=20;

//---- buffers
double EWNBuffer[];
double mean[];

//---- variables
int indexbegin = 0;
double tr=0;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
   IndicatorBuffers(2);
//---- indicators
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,EWNBuffer);
//----
	SetIndexBuffer(1, mean);
   string short_name = "EWN(" + Per + "," + Trigger + ")";
   IndicatorShortName(short_name);

	indexbegin = Bars - MathMax(Per, 35) - 1;
	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();
	int wave = 3;
	double osc, et;
	double hiosc = -2*Ask,
			hiprice = hiosc,
			hiosc2 = hiosc,
			hiprice2 = hiosc;

	//---- 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;

	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 = iMA(NULL, 0, 5, 0, MODE_SMA, PRICE_MEDIAN, i) - iMA(NULL, 0, 35, 0, MODE_SMA, PRICE_MEDIAN, i);
		//! buffer the mean prices (average of the high and low).
		mean[i] = (High[i] + Low[i]) / 2.0;
		// Is the current wave sequence up or down?
		et = iCustom(NULL, 0, "EWTREND", Per, Trigger, 0, i);
		// When the trend changes from down to up, label it wave 3 and save the current osc and price.
		if (et == 1 && iCustom(NULL, 0, "EWTREND", Per, Trigger, 0, i+1) == -1 && osc > 0)
		{
			hiosc = osc;
			hiprice = mean[i];
			wave = 3;
		}
		// If wave 3 and the oscillator make new highs then save those values.
		if (wave == 3)
		{
			if (mean[i] > hiprice)
				hiprice = mean[i];
			if (osc > hiosc)
				hiosc = osc;
			// Test for the beginning of wave 4.
			if (osc <= 0 && et == 1)
				wave = 4;
		}
		// Test for the beginning of wave 5.
		if (wave == 4 && mean[i] == mean[ArrayMaximum(mean, 5, i)] && osc >= 0)
		{
			wave = 5;
			hiosc2 = osc;
			hiprice2 = mean[i];
		}
		if (wave == 5)
		{
			if (osc > hiosc2)
				hiosc2 = osc;
			if (mean[i] > hiprice2)
				hiprice2 = mean[i];
		}
		// Test for wave 5 becoming wave 3.
		if (wave == 5 && hiosc2 > hiosc && et == 1)
		{
			wave = 3;
			hiosc = hiosc2;
			hiprice = hiprice2;
			hiosc2 = -2*Ask;
			hiprice2 = hiosc2;
		}
		// Identify a wave 3 down while in wave 5.
		if (wave == 5 && et == -1)
		{
			wave = 3;
			hiosc = -2*Ask;
			hiprice = hiosc;
			hiosc2 = hiosc;
			hiprice2 = hiosc;
		}
		EWNBuffer[i] = wave;
	}
	
	return(0);
}
//+------------------------------------------------------------------+