geFiboPlug





//+------------------------------------------------------------------+
//|																geFiboPlug.mq4		|
//|                      Copyright © 2007, ver 1.0							|
//|                      Forte928           									|
//+------------------------------------------------------------------+
#property copyright "Forte928"
#property link      ""
#define		IndicatorName "geFiboPlug"


#property indicator_chart_window

//#property indicator_minimum -100
//#property indicator_maximum 100

#property indicator_buffers 3
#property indicator_color1 Aqua	//Goldenrod//LightSkyBlue
#property indicator_color2 Khaki
#property indicator_color3 DodgerBlue	//PaleGreen

extern int		Alpha				= 200;
extern int	 	Counter			= 5000;

//double  FreqTOL  =0.0001;//Tolerance of frequency calculation for Method 1

double	FxView1[];
double	FxView2[];
double	FxView3[];

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~V~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Work Variables ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
datetime	TimeCalc;
double	IndexBuf1[];
double	IndexBuf2[];
double	IndexBuf3[];

double	TimeBuf1[0];
double	TimeBuf2[0];
double	TimeBuf3[0];
double	TimeBuf4[0];
double	TimeBuf5[0];
double	TimeBuf6[0];
double	TimeBuf7[0];
double	TimeBuf8[0];

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~V~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Initialization program Buffers ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void	InitBufferam(int iBufferSize)
{
	ArrayResize(IndexBuf1,iBufferSize);
	ArrayResize(IndexBuf2,iBufferSize);
	ArrayResize(IndexBuf3,iBufferSize);

	ArrayResize(TimeBuf1,iBufferSize);
	ArrayResize(TimeBuf2,iBufferSize);
	ArrayResize(TimeBuf3,iBufferSize);
	ArrayResize(TimeBuf4,iBufferSize);
	ArrayResize(TimeBuf5,iBufferSize);
	ArrayResize(TimeBuf6,iBufferSize);
	ArrayResize(TimeBuf7,iBufferSize);
	ArrayResize(TimeBuf8,iBufferSize);
// ------------------------
	return;
}
void	EmptyBufferam()
{
	ArrayInitialize(IndexBuf1,EMPTY_VALUE);
	ArrayInitialize(IndexBuf2,EMPTY_VALUE);
	ArrayInitialize(IndexBuf3,EMPTY_VALUE);

	ArrayInitialize(TimeBuf1,EMPTY_VALUE);
	ArrayInitialize(TimeBuf2,EMPTY_VALUE);
	ArrayInitialize(TimeBuf3,EMPTY_VALUE);
	ArrayInitialize(TimeBuf4,EMPTY_VALUE);
	ArrayInitialize(TimeBuf5,EMPTY_VALUE);
	ArrayInitialize(TimeBuf6,EMPTY_VALUE);
	ArrayInitialize(TimeBuf7,EMPTY_VALUE);
	ArrayInitialize(TimeBuf8,EMPTY_VALUE);
// -->
	InitChart(FxView1);														// -->
	InitChart(FxView2);														// -->
	InitChart(FxView3);														// -->
	return;
}
void	DoneBufferam()
{
	ArrayResize(IndexBuf1,0);
	ArrayResize(IndexBuf2,0);
	ArrayResize(IndexBuf3,0);

	ArrayResize(TimeBuf1,0);
	ArrayResize(TimeBuf2,0);
	ArrayResize(TimeBuf3,0);
	ArrayResize(TimeBuf4,0);
	ArrayResize(TimeBuf5,0);
	ArrayResize(TimeBuf6,0);
	ArrayResize(TimeBuf7,0);
	ArrayResize(TimeBuf8,0);
	//---------------------------------
	return;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~V~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Program Constants ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int		MaxPeriod=0;
int		CalcCount=0;
string	FiboLeft="_FiboLeft";
string	FiboRight="_FiboRight";
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Custom indicator initialization function ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int init()
{
	FiboLeft=Symbol()+FiboLeft;
	FiboRight=Symbol()+FiboRight;
//---- initialization program values  --------------------------------
	MaxPeriod=0;//MathMax(ELMAPrd,MathMax(SlowPrd,MathMax(SlowPrd,SlowPrd)));
	InitBufferam(MaxCounter(Counter,MaxPeriod));
	CalcCount=InitCounter(Counter,MaxPeriod);
	TimeCalc=0;									// Ticket èíäåêñà âðåìåíè,  íà÷àëà îáíîâëåíèÿ áóôåðà õðàíåíèÿ
//---- initialization indicators -------------------------------------
	SetupChartLine(0,FxView1,0,1,"Close ");
	SetupChartLine(1,FxView2,2,2,"Close ");
	SetupChartLine(2,FxView3,2,2,"Close ");
	//IndicatorShortName(IndicatorName);
  	IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
	
	SetIndexStyle(0,DRAW_LINE,STYLE_DOT);

	TicketGo(0);
//----
   return(0);
  }
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~V~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~														 TickGo																		~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
datetime	TickGo;					// ñòàðîå çíà÷åíèå òèêåòà âðåìåíè 
bool TicketGo(int TickInit)
{
	if (TickInit==0)	TickGo=0;
	else {
		if (TickGo==Time[0]) return(false);
		TickGo=Time[0];
	}
	return(true);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Custom indicator deinitialization function ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int deinit()
  {
//----
	ObjectDelete(FiboLeft);
	ObjectDelete(FiboRight);
	DoneBufferam();	
//----
   return(0);
  }
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~V~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Start defination ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int start()
{
	if (!TicketGo(1)) return(0);
	if (!StartCounter(IndicatorName,Counter,CalcCount,MaxPeriod)) return(-1);
//------------------------------------------------------------------------------------
	string	iTools=Symbol();
	int 		BaseFrame=Period();

	int Shifter=0;

	CopySeriesTools(TimeBuf1,Symbol(),Period(),MODE_HIGH,Shifter);	
	CopySeriesTools(TimeBuf2,Symbol(),Period(),MODE_CLOSE,Shifter);	
	CopySeriesTools(TimeBuf3,Symbol(),Period(),MODE_LOW,Shifter);	
	
	HPFilter(TimeBuf2,TimeBuf4,Alpha,CalcCount);
	
	int	IxState=0;
	int	SxState=0;
	if (TimeBuf4[CalcCount-1]>TimeBuf4[CalcCount]) SxState=1;
	if (TimeBuf4[CalcCount-1]<TimeBuf4[CalcCount]) SxState=-1;
	int	IxLeft=EMPTY_VALUE;
	int	IxMidle=EMPTY_VALUE;
	int	IxRight=EMPTY_VALUE;
	int	IxIndex=0;
	ArrayInitialize(IndexBuf2,EMPTY_VALUE);
	for (int Rx=CalcCount-1;Rx>=0;Rx--){
		IxState=0;												// îáíóëåíèå íàïðàâëåíèÿ êðèâîé
		if (TimeBuf4[Rx]>TimeBuf4[Rx+1]) IxState=1;	// Îïðåäåëåíî íàïðàâëåíèå êðèâîé ââåðõ
		if (TimeBuf4[Rx]<TimeBuf4[Rx+1]) IxState=-1;	// Îïðåäåëåíî íàïðàâëåíèå êðèâîé âíèç
//		if (Rx<50) Print(Rx,"  ",IxState,"  ",SxState);
		if ((SxState!=IxState)&&(IxState!=0)) {		// Ïðîâåðêà íà èçìåíåíèå íàïðàâëåíèÿ êðèâîé
//			if (Rx<50) Print(Rx,"  ",IxState,"  ",SxState,"  ",IxRight,"  ",IxMidle,"  ",IxLeft);
			
			if (IxRight==EMPTY_VALUE) IxRight=(Rx-1)*SxState;
			if (IxMidle!=EMPTY_VALUE){
				if (IxLeft!=EMPTY_VALUE) {
					IxIndex=MathAbs(IxLeft);
					IndexBuf2[IxIndex]=IxLeft;
				}
			}
			IxLeft=IxMidle;
			IxMidle=IxRight;
			IxRight=EMPTY_VALUE;
			SxState=IxState;
		} 
		else IxRight=(Rx+1)*SxState;
	}
	IxIndex=MathAbs(IxLeft);
	if (IxLeft!=EMPTY_VALUE) IndexBuf2[IxIndex]=IxLeft;
	IxIndex=MathAbs(IxMidle);
	if (IxMidle!=EMPTY_VALUE) IndexBuf2[IxIndex]=IxMidle;
	IxIndex=MathAbs(IxRight);
	if (IxRight!=EMPTY_VALUE) IndexBuf2[IxIndex]=IxRight;
	
	ArrayInitialize(TimeBuf5,EMPTY_VALUE);
	ArrayInitialize(TimeBuf6,EMPTY_VALUE);
	IxLeft=EMPTY_VALUE;
	IxMidle=EMPTY_VALUE;
	IxRight=CalcCount-1;
	double	FiboValue[3];
	int		FiboIndex[3];
	
	for (Rx=CalcCount-1;Rx>=0;Rx--) {
		if (IndexBuf2[Rx]!=EMPTY_VALUE) {
			IxLeft=IxMidle;
			IxMidle=IxRight;
			IxRight=IndexBuf2[Rx];
			if (IxLeft!=EMPTY_VALUE) {
				if (IxMidle>0) {
					IxIndex=ArrayMaximum(TimeBuf1,MathAbs(IxLeft)-MathAbs(IxRight),MathAbs(IxRight));
					TimeBuf5[IxIndex]=TimeBuf1[IxIndex];
					FiboValue[2]=FiboValue[1];FiboValue[1]=FiboValue[0];
					FiboIndex[2]=FiboIndex[1];FiboIndex[1]=FiboIndex[0];
					FiboValue[0]=TimeBuf1[IxIndex];
					FiboIndex[0]=Time[IxIndex];
				}
				if (IxMidle<0) {
					IxIndex=ArrayMinimum(TimeBuf3,MathAbs(IxLeft)-MathAbs(IxRight),MathAbs(IxRight));
					TimeBuf6[IxIndex]=TimeBuf3[IxIndex];
					FiboValue[2]=FiboValue[1];FiboValue[1]=FiboValue[0];
					FiboIndex[2]=FiboIndex[1];FiboIndex[1]=FiboIndex[0];
					FiboValue[0]=TimeBuf3[IxIndex];
					FiboIndex[0]=Time[IxIndex];
				}
			}
			if ((IxLeft==EMPTY_VALUE)&&(IxMidle!=EMPTY_VALUE)) {
				if (IxMidle>0) {
					IxIndex=ArrayMaximum(TimeBuf1,CalcCount-MathAbs(IxRight),MathAbs(IxRight));
					TimeBuf5[IxIndex]=TimeBuf1[IxIndex];
					FiboValue[2]=FiboValue[1];FiboValue[1]=FiboValue[0];
					FiboIndex[2]=FiboIndex[1];FiboIndex[1]=FiboIndex[0];
					FiboValue[0]=TimeBuf1[IxIndex];
					FiboIndex[0]=Time[IxIndex];
				}
				if (IxMidle<0) {
					IxIndex=ArrayMinimum(TimeBuf3,CalcCount-MathAbs(IxRight),MathAbs(IxRight));
					TimeBuf6[IxIndex]=TimeBuf3[IxIndex];
					FiboValue[2]=FiboValue[1];FiboValue[1]=FiboValue[0];
					FiboIndex[2]=FiboIndex[1];FiboIndex[1]=FiboIndex[0];
					FiboValue[0]=TimeBuf3[IxIndex];
					FiboIndex[0]=Time[IxIndex];
				}
			}
//			if (Rx<50) Print(Rx,")  ",IxRight,"  ",IxMidle,"  ",IxLeft,"> ",IxIndex);
		}
		if (Rx==0) {
			if (IxRight>0) {
				IxIndex=ArrayMaximum(TimeBuf1,MathAbs(IxMidle),0);
				TimeBuf5[IxIndex]=TimeBuf1[IxIndex];
					FiboValue[2]=FiboValue[1];FiboValue[1]=FiboValue[0];
					FiboIndex[2]=FiboIndex[1];FiboIndex[1]=FiboIndex[0];
					FiboValue[0]=TimeBuf1[IxIndex];
					FiboIndex[0]=Time[IxIndex];
			}
			if (IxRight<0) {
				IxIndex=ArrayMinimum(TimeBuf3,MathAbs(IxMidle),0);
				TimeBuf6[IxIndex]=TimeBuf3[IxIndex];
					FiboValue[2]=FiboValue[1];FiboValue[1]=FiboValue[0];
					FiboIndex[2]=FiboIndex[1];FiboIndex[1]=FiboIndex[0];
					FiboValue[0]=TimeBuf3[IxIndex];
					FiboIndex[0]=Time[IxIndex];
//					if (Rx<50) Print(Rx,")  ",IxRight,"  ",IxMidle,"  ",IxLeft,"> ",IxIndex," Min ");
			}
		}
	}
	FiboSeting(FiboLeft,0,FiboIndex[2],FiboValue[2],FiboIndex[1],FiboValue[1],Lime);
	FiboSeting(FiboRight,0,FiboIndex[1],FiboValue[1],FiboIndex[0],FiboValue[0],OrangeRed);
//	FiboSeting("FiboLeft",0,FiboIndex[1],FiboValue[1],FiboIndex[2],FiboValue[2],Lime);
//	FiboSeting("FiboRight",0,FiboIndex[0],FiboValue[0],FiboIndex[1],FiboValue[1],OrangeRed);
	ArrayCopy(FxView1,TimeBuf4,Shifter,0,CalcCount);
	ArrayCopy(FxView2,TimeBuf5,Shifter,0,CalcCount);
	ArrayCopy(FxView3,TimeBuf6,Shifter,0,CalcCount);

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

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void FiboSeting(string FiboName,int iWindow,double iTime1,double iPrice1,double iTime2,double iPrice2,color iColor)
{
	if (ObjectFind(FiboName)<0) {
		ObjectCreate(FiboName,OBJ_FIBO,0,0,0,0,0);
		
		ObjectSet(FiboName,OBJPROP_FIBOLEVELS,23);
		ObjectSet(FiboName,OBJPROP_FIRSTLEVEL+0,0);			ObjectSetFiboDescription(FiboName,0,"0");
		ObjectSet(FiboName,OBJPROP_FIRSTLEVEL+1,0.146);		ObjectSetFiboDescription(FiboName,1,"14.6");
		ObjectSet(FiboName,OBJPROP_FIRSTLEVEL+2,0.236);		ObjectSetFiboDescription(FiboName,2,"23.6");
		ObjectSet(FiboName,OBJPROP_FIRSTLEVEL+3,0.382);		ObjectSetFiboDescription(FiboName,3,"38.2");
		ObjectSet(FiboName,OBJPROP_FIRSTLEVEL+4,0.618);		ObjectSetFiboDescription(FiboName,4,"61.8");
		ObjectSet(FiboName,OBJPROP_FIRSTLEVEL+5,0.50);		ObjectSetFiboDescription(FiboName,5,"50.0");
		ObjectSet(FiboName,OBJPROP_FIRSTLEVEL+6,0.764);		ObjectSetFiboDescription(FiboName,6,"76.4");
		ObjectSet(FiboName,OBJPROP_FIRSTLEVEL+7,0.809);		ObjectSetFiboDescription(FiboName,7,"80.9");
		ObjectSet(FiboName,OBJPROP_FIRSTLEVEL+8,0.854);		ObjectSetFiboDescription(FiboName,8,"85.4");
		ObjectSet(FiboName,OBJPROP_FIRSTLEVEL+9,1.00);		ObjectSetFiboDescription(FiboName,9,"100.0");
		ObjectSet(FiboName,OBJPROP_FIRSTLEVEL+10,1.146);	ObjectSetFiboDescription(FiboName,10,"114.6");
		ObjectSet(FiboName,OBJPROP_FIRSTLEVEL+11,1.236);	ObjectSetFiboDescription(FiboName,11,"123.6");
		ObjectSet(FiboName,OBJPROP_FIRSTLEVEL+12,1.382);	ObjectSetFiboDescription(FiboName,12,"138.2");
		ObjectSet(FiboName,OBJPROP_FIRSTLEVEL+13,1.618);	ObjectSetFiboDescription(FiboName,13,"161.8");
		ObjectSet(FiboName,OBJPROP_FIRSTLEVEL+14,1.50);		ObjectSetFiboDescription(FiboName,14,"150.0");
		ObjectSet(FiboName,OBJPROP_FIRSTLEVEL+15,1.764);	ObjectSetFiboDescription(FiboName,15,"176.4");
		ObjectSet(FiboName,OBJPROP_FIRSTLEVEL+16,1.809);	ObjectSetFiboDescription(FiboName,16,"180.9");
		ObjectSet(FiboName,OBJPROP_FIRSTLEVEL+17,1.854);	ObjectSetFiboDescription(FiboName,17,"185.4");
		ObjectSet(FiboName,OBJPROP_FIRSTLEVEL+18,2.00);		ObjectSetFiboDescription(FiboName,18,"200.0");
		ObjectSet(FiboName,OBJPROP_FIRSTLEVEL+19,2.146);	ObjectSetFiboDescription(FiboName,19,"214.6");
		ObjectSet(FiboName,OBJPROP_FIRSTLEVEL+20,2.236);	ObjectSetFiboDescription(FiboName,20,"223.6");
		ObjectSet(FiboName,OBJPROP_FIRSTLEVEL+21,2.382);	ObjectSetFiboDescription(FiboName,21,"238.2");
		ObjectSet(FiboName,OBJPROP_FIRSTLEVEL+22,2.618);	ObjectSetFiboDescription(FiboName,22,"261.8");

		ObjectSet(FiboName,OBJPROP_LEVELCOLOR,iColor);
		ObjectSet(FiboName,OBJPROP_LEVELSTYLE,STYLE_DOT);
		ObjectSet(FiboName,OBJPROP_RAY,false);
		
	}
	ObjectSet(FiboName,OBJPROP_TIME1,iTime1);ObjectSet(FiboName,OBJPROP_PRICE1,iPrice1);
	ObjectSet(FiboName,OBJPROP_TIME2,iTime2);ObjectSet(FiboName,OBJPROP_PRICE2,iPrice2);
	

	return;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void InitChart(double& ayShift[])									// Ñìåùåíèå
{
	if (ArraySize(ayShift)>0) ArrayInitialize(ayShift,EMPTY_VALUE);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~														 InitCounter																		~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 int InitCounter(int iPreCount,int iMaxPeriod)
{
	// Rule 1
	//if ((Result==0)||(Result>Bars)) Result=Bars;
	int Result=MaxCounter(iPreCount,iMaxPeriod);
	// Rule 2
	if ((Result+iMaxPeriod)>Bars) Result=Bars-iMaxPeriod;
	// Rule 3
	if (Result<iMaxPeriod) Result=0;
	else Result=Result+iMaxPeriod;
	return(Result);
}
//------------------------------------------ StartCounter --------------------------
bool StartCounter(string WindowName,int iPreCount,int& iCalcCount,int iMaxPeriod)
{
	if ((iPreCount>iCalcCount)||(iPreCount==0)) iCalcCount=InitCounter(iPreCount,iMaxPeriod);
	if (iCalcCount==0) 
	{
		Alert(WindowName+" "+Symbol()+"("+Period()+") - Limited Calculate Period");
		return(false);
	}
	return(true);
	//int iBarCount=IndicatorCounted();
	//if (iBarCount>0) return(true);
	//Print("StartCounter");
	return(false);
}
//------------------------------------------ StartCounter --------------------------
int MaxCounter(int iPreCount,int iMaxPeriod)
{
	int Result=iPreCount;
	// Rule 1
	//if ((Result==0)||(Result>Bars)) Result=Bars;
	if (Result==0) Result=Bars-1;
	if (Result<iMaxPeriod) Result=iMaxPeriod;
	return(Result+iMaxPeriod);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~														 Style active Line																~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void SetupChartLine(int Index,double& ViewAy[],int Style,int Width,string Label)
{
	switch(Style)
	{
		case 0 : SetIndexStyle(Index,DRAW_LINE,STYLE_SOLID,Width);break;
		case 1 : SetIndexStyle(Index,DRAW_HISTOGRAM,STYLE_SOLID,Width);break;
		case 2 : SetIndexStyle(Index,DRAW_ARROW,STYLE_SOLID,Width);break;
		default : SetIndexStyle(Index,DRAW_LINE,STYLE_SOLID,Width);break;
	}
	if (Label!="") SetIndexLabel(Index,Label);
	SetIndexBuffer(Index,ViewAy);
	return(0);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~																CopySeriesTools															~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void CopySeriesTools(double& CopyRate[],string iTools,int iFrame,int iModePrice,int iShift)
{
	for (int Cx=0;Cx<ArraySize(CopyRate);Cx++){
		switch (iModePrice)	{
		case MODE_OPEN 	: CopyRate[Cx]=iOpen(iTools,iFrame,Cx+iShift);break;
		case MODE_LOW 		: CopyRate[Cx]=iLow(iTools,iFrame,Cx+iShift);break;
		case MODE_HIGH 	: CopyRate[Cx]=iHigh(iTools,iFrame,Cx+iShift);break;
		case MODE_CLOSE 	: CopyRate[Cx]=iClose(iTools,iFrame,Cx+iShift);break;
		case MODE_VOLUME 	: CopyRate[Cx]=iVolume(iTools,iFrame,Cx+iShift);break;
		case MODE_TIME 	: CopyRate[Cx]=iTime(iTools,iFrame,Cx+iShift);break;
		//case MODE_MEDIAN	: CopyRate[Cx]=(iHigh(iTools,iFrame,Cx+iShift)+iLow(iTools,iFrame,Cx+iShift))/2;break;
		//case MODE_TYPICAL	: CopyRate[Cx]=(iHigh(iTools,iFrame,Cx+iShift)+iLow(iTools,iFrame,Cx+iShift)+iClose(iTools,iFrame,Cx+iShift))/3;break;
		//case MODE_WEIGHTED: CopyRate[Cx]=(iHigh(iTools,iFrame,Cx+iShift)+iLow(iTools,iFrame,Cx+iShift)+iOpen(iTools,iFrame,Cx+iShift)+iClose(iTools,iFrame,Cx+iShift))/4;break;
		//PRICE_MEDIAN 4 Ñðåäíÿÿ öåíà, (high+low)/2 
		//PRICE_TYPICAL 5 Òèïè÷íàÿ öåíà, (high+low+close)/3 
		//PRICE_WEIGHTED 6 Âçâåøåííàÿ öåíà çàêðûòèÿ, (high+low+close+close)/4 
		}
	}
	return;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~											HPFilter	- Ôèëüòð Õîäðèêà-Ïðåñêîòòà														~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Hodrick-Prescott Filter-----------------------------------------------------------------+
void HPFilter(double& aySource[],double& ayResult[],int Lambda,int iCount)
{
   double Ak[],Bk[],Ck[],H1,H2,H3,H4,H5,HH1,HH2,HH3,HH4,HH5,HB,HC,Z;
   ArrayResize(Ak,iCount);
   ArrayResize(Bk,iCount);
   ArrayResize(Ck,iCount);
     
   Ak[0]=1.0+Lambda;
   Bk[0]=-2.0*Lambda;
   Ck[0]=Lambda;
   for(int Hx=1;Hx<iCount-2;Hx++)
   {
      Ak[Hx]=6.0*Lambda+1.0;
      Bk[Hx]=-4.0*Lambda;
      Ck[Hx]=Lambda;
   }
   Ak[1]=5.0*Lambda+1;
   Ak[iCount-1]=1.0+Lambda;
   Ak[iCount-2]=5.0*Lambda+1.0;
   Bk[iCount-2]=-2.0*Lambda;
   Bk[iCount-1]=0.0;
   Ck[iCount-2]=0.0;
   Ck[iCount-1]=0.0;
   
   //Forward
   for(Hx=0;Hx<iCount;Hx++)
   {
      Z=Ak[Hx]-H4*H1-HH5*HH2;
      HB=Bk[Hx];
      HH1=H1;
      H1=(HB-H4*H2)/Z;
      Bk[Hx]=H1;
      HC=Ck[Hx];
      HH2=H2;
      H2=HC/Z;
      Ck[Hx]=H2;
      Ak[Hx]=(aySource[Hx]-HH3*HH5-H3*H4)/Z;
      HH3=H3;
      H3=Ak[Hx];
      H4=HB-H5*HH1;
      HH5=H5;
      H5=HC;
   }
   
   //Backward 
   H2=0;
   H1=Ak[iCount-1];
   ayResult[iCount-1]=H1;
   for(Hx=iCount-2;Hx>=0;Hx--)
   {
      ayResult[Hx]=Ak[Hx]-Bk[Hx]*H1-Ck[Hx]*H2;
      H2=H1;
      H1=ayResult[Hx];
   }
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~





Sample





Analysis



Market Information Used:

Series array that contains open time of each bar
Series array that contains open prices of each bar
Series array that contains the lowest prices of each bar
Series array that contains the highest prices of each bar
Series array that contains close prices for each bar
Series array that contains tick volumes of each bar


Indicator Curves created:

Implements a curve of type DRAW_LINE
Implements a curve of type DRAW_HISTOGRAM
Implements a curve of type DRAW_ARROW


Indicators Used:



Custom Indicators Used:

Order Management characteristics:

Other Features:

It issuies visual alerts to the screen