//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
#property copyright "Copyright © 2004, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 Red
#property indicator_color2 White
#property indicator_color3 Red
#property indicator_color4 White
#property indicator_width1 1
#property indicator_width2 1
#property indicator_width3 3
#property indicator_width4 3

//----
extern int 		MaxBars = 1000;
extern double  Spread = 0.0002;
extern color color3 = Red;
extern color color4 = White;
//---- buffers
double BuyProspect[];
double SellProspect[];
double WinMrkr[];
double LossMrkr[];
//----
int ExtCountedBars=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//|------------------------------------------------------------------|
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_ARROW, 0, 1, Blue);
   SetIndexBuffer(0, BuyProspect );
   SetIndexArrow(0,217);
   SetIndexLabel(0,"BuyProspect");
   SetIndexStyle(1,DRAW_ARROW, 0, 1, Gold);
   SetIndexBuffer(1, SellProspect);
   SetIndexArrow(1,218);
   SetIndexLabel(1,"SellProspect");
   SetIndexStyle(2,DRAW_ARROW, 0, 1, Lime);
   SetIndexBuffer(2, WinMrkr);
   SetIndexArrow(2,117);
   SetIndexLabel(2,"WinMrkr");
   SetIndexStyle(3,DRAW_ARROW, 0, 1, Red);
   SetIndexBuffer(3, LossMrkr);
   SetIndexArrow(3,117);
   SetIndexLabel(3,"LossMrkr");
//---- indicator buffers mapping
   SetIndexBuffer(0,BuyProspect);
   SetIndexBuffer(1,SellProspect);
   SetIndexBuffer(2,WinMrkr);
   SetIndexBuffer(3,LossMrkr);
//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- TODO: add your code here
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int Win=0, Loss=0; double BuyPips=0, SellPips=0;
int start()
{
	if(Bars<=10) return(0);
	ExtCountedBars=IndicatorCounted();
	if (ExtCountedBars<0) return(-1);
	if (ExtCountedBars>0) ExtCountedBars--;
	int pos=Bars-ExtCountedBars-1;
	pos = MathMin(pos,MaxBars);
	for( int i = pos; i >= 0; i-- ) 
	{
		double BodyP0, BodyP1, BodyP2, BodyP3, HiPos = iHigh(NULL,0,i)+iATR(NULL,0,5,i+1), LoPos = iLow(NULL,0,i)-iATR(NULL,0,5,i+1);
		bool DirUpP0, DirUpP1, DirUpP2, DirUpP3, DirDnP0, DirDnP1, DirDnP2, DirDnP3;
				
		BodyP0 = iClose(NULL,0,i+0) 	- iOpen(NULL,0,i+0); 
		BodyP1 = iClose(NULL,0,i+1) 	- iOpen(NULL,0,i+1); 
		BodyP2 = iClose(NULL,0,i+2) 	- iOpen(NULL,0,i+2); 
		BodyP3 = iClose(NULL,0,i+3) 	- iOpen(NULL,0,i+3);
		DirUpP0  = (BodyP0 > 0); DirDnP0  = (BodyP0 < 0); 
		DirUpP1  = (BodyP1 > 0); DirDnP1  = (BodyP1 < 0); 
		DirUpP2  = (BodyP2 > 0); DirDnP2  = (BodyP2 < 0); 
		DirUpP3  = (BodyP3 > 0); DirDnP3  = (BodyP3 < 0); 
		BodyP0 = MathAbs(BodyP0); 
		BodyP1 = MathAbs(BodyP1); 
		BodyP2 = MathAbs(BodyP2);
		BodyP3 = MathAbs(BodyP3);
		if( BodyP1 > BodyP2 && BodyP1 > BodyP3 )
		{
			if( DirDnP2 ) 
			{
				if( iClose(NULL,0,i+1) > iClose(NULL,0,i+2) )
				{// Bull Prospect
					BuyProspect[i+1] = HiPos;
					if( DirUpP0 && BodyP0 >= Spread )					 
						{ Win++;  BuyPips += BodyP0 -  Spread; WinMrkr[i] = HiPos; }  
					else 
						{ Loss++; BuyPips += -BodyP0 - Spread; LossMrkr[i] = LoPos; }
				}
			}
			if( DirUpP2 )
			{
				if( iClose(NULL,0,i+1) < iClose(NULL,0,i+2) )
				{// Bear Prospect
					SellProspect[i+1] = LoPos;
					if( !DirUpP0 ) 
						{ Win++;  SellPips += BodyP0  - Spread; WinMrkr[i] = HiPos; } 
					else 
						{ Loss++; SellPips += -BodyP0 - Spread; LossMrkr[i] = LoPos; }
				}
			}
		}
			 
		pos--;
	}
	Comment("Wins="+Win+", Losses="+Loss+", BuyPips="+BuyPips+", SellPips="+SellPips);
	//----
	return(0);
}
//+------------------------------------------------------------------+