//+------------------------------------------------------------------+
//|                                             ClassyFractalSnR.mq4 |
//|                        Copyright 2020, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, by. ganztrade"
#property link      "http://www.metaquotes.net"
#property description "ClassyFractalSnR"
#property version   "1.00"

#property indicator_chart_window

//---- input parameters
input int CountedBars=382;
input color UpperColor = clrDodgerBlue;
input color LowerColor = clrOrange;
input int countedbarsrectangle = 50;

double FractalUp;
double FractalDown;
//----buffers

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---

//---	
   return(0);

  }
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 
ObjectsDeleteAll(0,OBJ_RECTANGLE);  

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{   
//---- 	
	bool BullFractal=false;
	bool BearFractal=false;
	bool BullBox=false;
	bool BearBox=false;
	
	int shift=0;
   int shift1=0;
   int shift2=0;
   int shift3=0;
   int shiftline=0;

	int limit;
	int n;
	
   limit=CountedBars;
	for(shift=limit;shift>=0;shift--) 
	{
		
		shift1=shift+1;
		shift2=shift+2;
		shift3=shift+3;
		shiftline=shift-countedbarsrectangle;		
		
		FractalUp = iFractals(NULL, 0, MODE_UPPER,shift); 
      FractalDown = iFractals(NULL, 0, MODE_LOWER,shift);   
      
	    //---Bullish Fractal
		if(FractalDown > 0 && Close[shift] < Open[shift]) 		      		      
		   
			BullFractal=true;
		else 
			BullFractal=false;
			
		//---Bearish Fractal
		if(FractalUp > 0 && Close[shift] > Open[shift])	      
		   
			BearFractal=true;
		else
			BearFractal=false;
		//==============================
	   if(BullFractal)
	      {
			BullBox=true;
			}
		else 
			{
			BullBox=false; 
			}

		if(BearFractal)
		   {
			BearBox=true;
			}
		else 
			{
			BearBox=false;
			}
			
		//---------TEXT START    
		if(BullFractal)
		{
			if(BullBox)
			  {
				n++;   
            CreateObjects1(n,Time[shift],Close[shift],Time[shiftline],Open[shift],UpperColor);
		     }			   
	   }
		if(BearFractal)
		{
			if(BearBox)
			{
				n++;	
   			CreateObjects2(n,Time[shift],Close[shift],Time[shiftline],Open[shift],LowerColor);				   
		   }		
		}	
	}
//----
   return(0);
}
//+------------------------------------------------------------------+
void CreateObjects1(int name,datetime dts, double price1,datetime dte,double price2, color clr)
{
	ObjectCreate(name+"Upper",OBJ_RECTANGLE,0,dts,price1,dte,price2,clr);
	ObjectSet(name+"Upper", OBJPROP_STYLE, STYLE_SOLID);
  ObjectSet(name+"Upper", OBJPROP_COLOR, clr);
  ObjectSet(name+"Upper", OBJPROP_BACK, false);
}
//+------------------------------------------------------------------+
void CreateObjects2(int name,datetime dts,double price1,datetime dte,double price2, color clr)
{
	ObjectCreate(name+"Lower",OBJ_RECTANGLE,0,dts,price1,dte,price2,clr);
	ObjectSet(name+"Lower", OBJPROP_STYLE, STYLE_SOLID);
  ObjectSet(name+"Lower", OBJPROP_COLOR, clr);
  ObjectSet(name+"Lower", OBJPROP_BACK, false);
}
