//+------------------------------------------------------------------+
//|                                             Visual_BT_Pivots.mq4 |
//|                                                         MicroMan |
//|                                                         Fish.net |
//+------------------------------------------------------------------+
#property copyright "MicroMan"
#property link      "Fish.net" 
#property  indicator_chart_window
//+------------------------------------------------------------------+
//|   Visual_BT_Pivots_Indicator.mq4  by Micro-MiniMe
//+------------------------------------------------------------------+

extern bool Monthly = False;
extern bool Weekly  = True; 
extern bool Daily   = False;
extern string comment = "per choices:0=future 1=current 2=past";
extern int  per = 1;// 1= used for current period pivots, 0= used for tomorrows pivots, 2= yesterdays pivots
extern color indiColor = Black;// used for lines and text

int init()
{
   if(per==0)Comment("Future Pivots are Displayed");
   if(per==1)Comment("Current Pivots are Displayed");
   if(per>=2)Comment("Past Days are Displayed");
   if(Monthly==True && Weekly==True || Monthly==True && Daily==True || Weekly==True && Daily==True)
   {Comment("More than one period are selected. Select just one."); }
}
int deinit()
{  
   ObjectDelete("PivotLine");
   ObjectDelete("PivotLabel");   
   ObjectDelete("S1Line");
   ObjectDelete("S1Label");
   ObjectDelete("R1Line");
   ObjectDelete("R1Label");  
   ObjectDelete("S2Line");
   ObjectDelete("S2Label");
   ObjectDelete("R2Line");
   ObjectDelete("R2Label");   
   ObjectDelete("S3Line");
   ObjectDelete("S3Label");
   ObjectDelete("R3Line");
   ObjectDelete("R3Label");             
   return(0);
}
//+------------------------------------------------------------------+
//+-------------------Start Function---------------------------------+
int start()
{
   int CalcPeriod;
	string pLineStr,pStr,p,s1Str,S1LineStr,s1,r1Str,R1LineStr,r1,s2Str,S2LineStr,s2,r2Str,R2LineStr,r2; 
	string s3Str,S3LineStr,s3,r3Str,R3LineStr,r3;
 
   if(Monthly == True)
   { 
      CalcPeriod = 43200;
      pLineStr = "Monthly Pivot ";
      S1LineStr = "Monthly S1 ";
      R1LineStr = "Monthly R1 ";
      S2LineStr = "Monthly S2 ";
      R2LineStr = "Monthly R2 ";
      S3LineStr = "Monthly S3 ";
      R3LineStr = "Monthly R3 ";           
   }
   if(Weekly == True)
   {
      CalcPeriod = 10080;
      pLineStr = "Weekly Pivot ";
      S1LineStr = "Weekly S1 ";
      R1LineStr = "Weekly R1 ";
      S2LineStr = "Weekly S2 ";
      R2LineStr = "Weekly R2 ";
      S3LineStr = "Weekly S3 ";
      R3LineStr = "Weekly R3 ";     
   }
   if(Daily == True)
   {
      CalcPeriod = 1440;
      pLineStr = "Daily Pivot ";
      S1LineStr = "Daily S1 ";
      R1LineStr = "Daily R1 ";
      S2LineStr = "Daily S2 ";
      R2LineStr = "Daily R2 ";
      S3LineStr = "Daily S3 ";
      R3LineStr = "Daily R3 ";
   }
DrawLines(CalcPeriod,pLineStr,S1LineStr,R1LineStr,S2LineStr,R2LineStr,S3LineStr,R3LineStr);      
   return(0);
}   

////////////////Draw Pivot Line and S/R Lines/Labels/////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////// 
int DrawLines(int CalcPeriod,string pLineStr,string S1LineStr,string R1LineStr,string 
               S2LineStr,string R2LineStr,string S3LineStr,string R3LineStr ) 
{
   int shift,dow; 
	string pStr,p,s1Str,s1,r1Str,r1,s2Str,s2,r2Str,r2,s3Str,s3,r3Str,r3,dowStr;
	double HiPrice,LoPrice,ClosePrice,Pivot,R1,S1,R2,S2,R3,S3 ;
   shift	= iBarShift(NULL,CalcPeriod,Time[0])+per;	// default = one period ago	 
	datetime StartTime	= iTime(NULL,CalcPeriod,shift);	 
	dow = TimeDayOfWeek(StartTime);
	 switch(dow)
	 {
	  case 5:	 
      dowStr = "Sunday  ";
      break;
    case 0:
      shift	= iBarShift(NULL,CalcPeriod,Time[0])+per+1;	// two periods ago or Friday value	 
      dowStr = "Monday  ";     
      break;
    case 1:
      dowStr = "Tuesday  ";
      break;
    case 2:
      dowStr = "Wednesday  ";
      break;
    case 3:
      dowStr = "Thursday  ";
      break;
    case 4:
      dowStr = "Friday  "; 
      break; 
   } 	 
   if(Monthly==true || Weekly==true)dowStr="";
 	ClosePrice  = NormalizeDouble(iClose(NULL,CalcPeriod,shift),4);
   HiPrice = NormalizeDouble (iHigh(NULL,CalcPeriod,shift),4); 
   LoPrice = NormalizeDouble (iLow(NULL,CalcPeriod,shift),4);
   Pivot = NormalizeDouble((HiPrice+LoPrice+ClosePrice)/3,4);
   R1 = (2*Pivot)-LoPrice;
   S1 = (2*Pivot)-HiPrice;
   R2 = Pivot+(R1-S1);
   S2 = Pivot-(R1-S1); 
   R3 = ( 2.0 * Pivot) + ( HiPrice - ( 2.0 * LoPrice ) );
   S3 = ( 2.0 * Pivot) - ( ( 2.0 * HiPrice ) - LoPrice );
   double Piv_S1 = (Pivot-S1)*100;
   double Piv_R1 = (R1-Pivot)*100;
   
   
   pStr = DoubleToStr(Pivot,4);
   p = StringSubstr(pStr,0,6); 
 
   s1Str = DoubleToStr(S1,4);
   s1 = StringSubstr(s1Str,0,6);

   r1Str = DoubleToStr(R1,4);
   r1 = StringSubstr(r1Str,0,6);
   
   s2Str = DoubleToStr(S2,4);
   s2 = StringSubstr(s2Str,0,6);

   r2Str = DoubleToStr(R2,4);
   r2 = StringSubstr(r2Str,0,6);

   s3Str = DoubleToStr(S3,4);
   s3 = StringSubstr(s3Str,0,6);

   r3Str = DoubleToStr(R3,4);
   r3 = StringSubstr(r3Str,0,6);
      
////////////////////Pivot Line///////////////////////////  
/////////////////////////////////////////////////////////      
 if(ObjectFind("PivotLine")==-1)
  {
	   //StartTime	= iTime(NULL,CalcPeriod,-1);  
   ObjectCreate("PivotLine", OBJ_HLINE,0, Time[0],Pivot);
   ObjectSet("PivotLine", OBJPROP_COLOR, indiColor);
   ObjectSet("PivotLine", OBJPROP_STYLE, STYLE_SOLID);
   ObjectSet("PivotLine", OBJPROP_WIDTH, 3);
  }
 else
  {
   ObjectMove("PivotLine",0,Time[0],Pivot);
  }
 if(ObjectFind("PivotLabel") == -1)
  {
  	   //StartTime	= iTime(NULL,CalcPeriod,-1);
   ObjectCreate("PivotLabel", OBJ_TEXT, 0, Time[0], Pivot);
   ObjectSetText("PivotLabel",dowStr+pLineStr+ p, 10, "Arial",indiColor);
  }
 else
  {
   ObjectMove("PivotLabel", 0, Time[0], Pivot);
   ObjectSetText("PivotLabel",dowStr+pLineStr+ p, 10, "Arial",indiColor); 
  
  } 
//////////////////// S1 Line/Label////////////////////
//////////////////////////////////////////////////////
 if(ObjectFind("S1Line")==-1)
  {
   ObjectCreate("S1Line", OBJ_HLINE,0, Time[0],S1);
   ObjectSet("S1Line", OBJPROP_COLOR, indiColor);
   ObjectSet("S1Line", OBJPROP_STYLE, STYLE_SOLID);
  }
 else
  {
   ObjectMove("S1Line",0,Time[0],S1);
  }
 if(ObjectFind("S1Label") == -1)
  {
   ObjectCreate("S1Label", OBJ_TEXT, 0, Time[0], S1);
   ObjectSetText("S1Label",S1LineStr+ s1, 10, "Arial", indiColor);
   ObjectSet("S1Label",OBJPROP_BACK,false);
  }
 else
  {
   ObjectMove("S1Label", 0, Time[0], S1);
   ObjectSetText("S1Label",S1LineStr+ s1, 10, "Arial", indiColor);   
  }
/////////////////////R1 Line/Label////////////////////
//////////////////////////////////////////////////////
 if(ObjectFind("R1Line")==-1)
  {
   ObjectCreate("R1Line", OBJ_HLINE,0, Time[0],R1);
   ObjectSet("R1Line", OBJPROP_COLOR, indiColor);
   ObjectSet("R1Line", OBJPROP_STYLE, STYLE_SOLID);
  }
 else
  {
   ObjectMove("R1Line",0,Time[0],R1);
  }
  
 if(ObjectFind("R1Label") == -1)
  {
   ObjectCreate("R1Label", OBJ_TEXT, 0, Time[0], R1);
   ObjectSetText("R1Label",R1LineStr+ r1, 10, "Arial", indiColor);
  }
 else
  {
   ObjectMove("R1Label", 0, Time[0], R1);
   ObjectSetText("R1Label",R1LineStr+ r1, 10, "Arial", indiColor);   
  }
   
///////////////////// S2 Line/Label////////////////////
//////////////////////////////////////////////////////
 if(ObjectFind("S2Line")==-1)
  {
   ObjectCreate("S2Line", OBJ_HLINE,0, Time[0],S2);
   ObjectSet("S2Line", OBJPROP_COLOR, indiColor);
   ObjectSet("S2Line", OBJPROP_STYLE, STYLE_SOLID);
  }
 else
  {
   ObjectMove("S2Line",0,Time[0],S2);
  }
 if(ObjectFind("S2Label") == -1)
  {
   ObjectCreate("S2Label", OBJ_TEXT, 0, Time[0], S2);
   ObjectSetText("S2Label",S2LineStr+ s2, 10, "Arial", indiColor);
  }
 else
  {
   ObjectMove("S2Label", 0, Time[0], S2);
   ObjectSetText("S2Label",S2LineStr+ s2, 10, "Arial", indiColor);   
  }
//////////////////// R2 Line/Label////////////////////
//////////////////////////////////////////////////////
 if(ObjectFind("R2Line")==-1)
  {
   ObjectCreate("R2Line", OBJ_HLINE,0, Time[0],R2);
   ObjectSet("R2Line", OBJPROP_COLOR, indiColor);
   ObjectSet("R2Line", OBJPROP_STYLE, STYLE_SOLID);
  }
 else
  {
   ObjectMove("R2Line",0,Time[0],R2);
  }
  
 if(ObjectFind("R2Label") == -1)
  {
   ObjectCreate("R2Label", OBJ_TEXT, 0, Time[0], R2);
   ObjectSetText("R2Label",R2LineStr+ r2, 10, "Arial", indiColor);
  }
 else
  {
   ObjectMove("R2Label", 0, Time[0], R2);
   ObjectSetText("R2Label",R2LineStr+ r2, 10, "Arial", indiColor);   
  }
  
////////////// S3 Line/Label /////////////////////////          
//////////////////////////////////////////////////////
 if(ObjectFind("S3Line")==-1)
  {
   ObjectCreate("S3Line", OBJ_HLINE,0, Time[0],S3);
   ObjectSet("S3Line", OBJPROP_COLOR, indiColor);
   ObjectSet("S3Line", OBJPROP_STYLE, STYLE_SOLID);
  }
 else
  {
   ObjectMove("S3Line",0,Time[0],S3);
  }
 if(ObjectFind("S3Label") == -1)
  {
   ObjectCreate("S3Label", OBJ_TEXT, 0, Time[0], S3);
   ObjectSetText("S3Label",S3LineStr+ s3, 10, "Arial", indiColor);
  }
 else
  {
   ObjectMove("S3Label", 0, Time[0], S3);
   ObjectSetText("S3Label",S3LineStr+ s3, 10, "Arial", indiColor);   
  }
//////////////////// R3 Line/Label ///////////////////
//////////////////////////////////////////////////////
 if(ObjectFind("R3Line")==-1)
  {
   ObjectCreate("R3Line", OBJ_HLINE,0, Time[0],R3);
   ObjectSet("R3Line", OBJPROP_COLOR, indiColor);
   ObjectSet("R3Line", OBJPROP_STYLE, STYLE_SOLID);
  }
 else
  {
   ObjectMove("R3Line",0,Time[0],R3);
  }
  
 if(ObjectFind("R3Label") == -1)
  {
   ObjectCreate("R3Label", OBJ_TEXT, 0, Time[0], R3);
   ObjectSetText("R3Label",R3LineStr+ r3, 10, "Arial", indiColor);
  }
 else
  {
   ObjectMove("R3Label", 0, Time[0], R3);
   ObjectSetText("R3Label",R3LineStr+ r3, 10, "Arial", indiColor);   
  }        

 
return(0); 
}
//+------------------------------------------------------------------+----------------------------------------------------------+