
#property indicator_chart_window
#property indicator_buffers   0

#include <MQL4TO5.mqh>
#include<MT4Orders.mqh>

// inputal inputs
input int              InpiCountPeriods         = 2;
input ENUM_TIMEFRAMES  InpiTimePeriod           = PERIOD_D1;
input bool             InpiPlotPivots           = true;
input bool             InpiPlotFuturePivots     = true;
input bool             InpiPlotPivotLabels      = true;
input bool             InpiPlotPivotPrices      = true;
input ENUM_LINE_STYLE  InpiStylePivots          = STYLE_SOLID;
input int              InpiWidthPivots          = 2;
input color            InpiColorRes             = clrRed;
input color            InpiColorPP              = clrBlack;
input color            InpiColorSup             = clrGreen;

input bool             InpiPlotMidpoints        = true;
input ENUM_LINE_STYLE  InpiStyleMidpoints       = STYLE_DASH;
input int              InpiWidthMidpoints       = 1;
input color            InpiColorM35             = clrRed;
input color            InpiColorM02             = clrGreen;

input bool             InpiPlotZones            = true;
input color            InpiColorBuyZone         = clrLightSeaGreen;
input color            InpiColorSellZone        = clrLightSalmon;

input bool             InpiPlotBorders          = false;
input ENUM_LINE_STYLE  InpiStyleBorder          = STYLE_SOLID;
input int              InpiWidthBorder          = 1;
input color            InpiColorBorder          = clrBlack;

input bool             InpiPushNotifications    = false;

ENUM_LINE_STYLE iStylePivots ;
ENUM_TIMEFRAMES iTimePeriod ;
bool iPlotBorders ;
color iColorSup ;
bool iPlotPivotLabels ;
color iColorM35 ;
color iColorM02 ;
int iCountPeriods ;
bool iPlotPivots ;
color iColorSellZone ;
int iWidthPivots ;
int iWidthMidpoints ;
color iColorBorder ;
bool iPushNotifications ;
color iColorBuyZone ;
bool iPlotZones ;
color iColorRes ;
color iColorPP ;
bool iPlotMidpoints ;
bool iPlotPivotPrices ;
int iWidthBorder ;
ENUM_LINE_STYLE iStyleBorder ;
ENUM_LINE_STYLE iStyleMidpoints ;
bool iPlotFuturePivots ;

void InitInputVariables()
{
	iStylePivots=InpiStylePivots;
	iTimePeriod=InpiTimePeriod;
	iPlotBorders=InpiPlotBorders;
	iColorSup=InpiColorSup;
	iPlotPivotLabels=InpiPlotPivotLabels;
	iColorM35=InpiColorM35;
	iColorM02=InpiColorM02;
	iCountPeriods=InpiCountPeriods;
	iPlotPivots=InpiPlotPivots;
	iColorSellZone=InpiColorSellZone;
	iWidthPivots=InpiWidthPivots;
	iWidthMidpoints=InpiWidthMidpoints;
	iColorBorder=InpiColorBorder;
	iPushNotifications=InpiPushNotifications;
	iColorBuyZone=InpiColorBuyZone;
	iPlotZones=InpiPlotZones;
	iColorRes=InpiColorRes;
	iColorPP=InpiColorPP;
	iPlotMidpoints=InpiPlotMidpoints;
	iPlotPivotPrices=InpiPlotPivotPrices;
	iWidthBorder=InpiWidthBorder;
	iStyleBorder=InpiStyleBorder;
	iStyleMidpoints=InpiStyleMidpoints;
	iPlotFuturePivots=InpiPlotFuturePivots;
}



// global variables
string   gPeriod = "";
int      gRealTimePeriod = 0;
datetime gPrevTimePivot = 0;


//+------------------------------------------------------------------+
// LevelsDelete - This function will delete all objects that were 
//    created by this program. 
//+------------------------------------------------------------------+
void LevelsDelete(string PeriodStr,
                  int    Shift)
{
   ObjectDelete(PeriodStr + "R3_T" + Shift);
   ObjectDelete(PeriodStr + "R2_T" + Shift);
   ObjectDelete(PeriodStr + "R1_T" + Shift);
   ObjectDelete(PeriodStr + "PP_T" + Shift);
   ObjectDelete(PeriodStr + "S1_T" + Shift);
   ObjectDelete(PeriodStr + "S2_T" + Shift);
   ObjectDelete(PeriodStr + "S3_T" + Shift);

   ObjectDelete(PeriodStr + "R3_P" + Shift);     
   ObjectDelete(PeriodStr + "R2_P" + Shift);     
   ObjectDelete(PeriodStr + "R1_P" + Shift);     
   ObjectDelete(PeriodStr + "PP_P" + Shift);     
   ObjectDelete(PeriodStr + "S1_P" + Shift);     
   ObjectDelete(PeriodStr + "S2_P" + Shift);     
   ObjectDelete(PeriodStr + "S3_P" + Shift);     
           
   ObjectDelete(PeriodStr + "R3_L" + Shift);     
   ObjectDelete(PeriodStr + "R2_L" + Shift);     
   ObjectDelete(PeriodStr + "R1_L" + Shift);     
   ObjectDelete(PeriodStr + "PP_L" + Shift);     
   ObjectDelete(PeriodStr + "S1_L" + Shift);     
   ObjectDelete(PeriodStr + "S2_L" + Shift);     
   ObjectDelete(PeriodStr + "S3_L" + Shift);     

   ObjectDelete(PeriodStr + "M0_T" + Shift);
   ObjectDelete(PeriodStr + "M1_T" + Shift);
   ObjectDelete(PeriodStr + "M2_T" + Shift);
   ObjectDelete(PeriodStr + "M3_T" + Shift);
   ObjectDelete(PeriodStr + "M4_T" + Shift);
   ObjectDelete(PeriodStr + "M5_T" + Shift);
                 
   ObjectDelete(PeriodStr + "M0_P" + Shift);     
   ObjectDelete(PeriodStr + "M1_P" + Shift);     
   ObjectDelete(PeriodStr + "M2_P" + Shift);     
   ObjectDelete(PeriodStr + "M3_P" + Shift);     
   ObjectDelete(PeriodStr + "M4_P" + Shift);     
   ObjectDelete(PeriodStr + "M5_P" + Shift);     

   ObjectDelete(PeriodStr + "M0_L" + Shift);     
   ObjectDelete(PeriodStr + "M1_L" + Shift);     
   ObjectDelete(PeriodStr + "M2_L" + Shift);     
   ObjectDelete(PeriodStr + "M3_L" + Shift);     
   ObjectDelete(PeriodStr + "M4_L" + Shift);     
   ObjectDelete(PeriodStr + "M5_L" + Shift);     

   ObjectDelete(PeriodStr + "BZ_" + Shift);     
   ObjectDelete(PeriodStr + "SZ_" + Shift);     
       
   ObjectDelete(PeriodStr + "BDL_" + Shift);     
   ObjectDelete(PeriodStr + "BDR_" + Shift);
}

//+------------------------------------------------------------------+
// PlotTrend - This function will plot a pivot level to the chart. 
//+------------------------------------------------------------------+
bool PlotTrend(const long              Chart_ID = 0,
               string                  Name = "trendline",
               const int               Subwindow = 0,
               datetime                Time1 = 0,
               double                  Price1 = 0,
               datetime                Time2 = 0,
               double                  Price2 = 0,             
               const color             Clr = clrBlack,
               const int               Style = STYLE_SOLID,
               const int               Width = 2,
               const bool              Back = true,
               const bool              Selection = false,
               const bool              Ray = false,
               const bool              Hidden = true)
{
   ResetLastError();
   if(!ObjectCreate(Chart_ID, Name, OBJ_TREND, Subwindow, Time1, Price1, Time2, Price2))
   {
      Print(__FUNCTION__, ": failed to create trendline = ", GetLastError());
      return(false);
   }
   ObjectSetInteger(Chart_ID, Name, OBJPROP_COLOR, Clr);
   ObjectSetInteger(Chart_ID, Name, OBJPROP_STYLE, Style);
   ObjectSetInteger(Chart_ID, Name, OBJPROP_WIDTH, Width);
   ObjectSetInteger(Chart_ID, Name, OBJPROP_BACK, Back);
   ObjectSetInteger(Chart_ID, Name, OBJPROP_SELECTABLE, Selection);
   ObjectSetInteger(Chart_ID, Name, OBJPROP_SELECTED, Selection);
   ObjectSetInteger(Chart_ID, Name, OBJPROP_RAY, Ray);
   ObjectSetInteger(Chart_ID, Name, OBJPROP_HIDDEN, Hidden);
   return(true);
}

//+------------------------------------------------------------------+
// PlotRectangle - This function will plot a take profit zone to the chart. 
//+------------------------------------------------------------------+
bool PlotRectangle(  const long        Chart_ID = 0,
                     string            Name = "rectangle", 
                     const int         Subwindow = 0,
                     datetime          Time1 = 0,
                     double            Price1 = 1,
                     datetime          Time2 = 0, 
                     double            Price2 = 0, 
                     const color       Clr = clrGray,
                     const bool        Back = true,
                     const bool        Selection = false,
                     const bool        Hidden = true)
{
   if(!ObjectCreate(Chart_ID, Name, OBJ_RECTANGLE, Subwindow, Time1, Price1, Time2, Price2))
   {
      Print(__FUNCTION__, ": failed to create rectangle = ", GetLastError());
      return(false);
   }
   
   ObjectSetInteger(Chart_ID, Name, OBJPROP_COLOR, Clr);
   ObjectSetInteger(Chart_ID, Name, OBJPROP_BACK, Back);
   ObjectSetInteger(Chart_ID, Name, OBJPROP_SELECTABLE, Selection);
   ObjectSetInteger(Chart_ID, Name, OBJPROP_HIDDEN, Hidden);
   return(true);
}

//+------------------------------------------------------------------+
// PlotText - This function will plot a text box to the chart. Used to
//    plot pivot labels and prices. 
//+------------------------------------------------------------------+
bool PlotText(       const long        Chart_ID = 0,
                     string            Name = "text", 
                     const int         Subwindow = 0,
                     datetime          Time1 = 0, 
                     double            Price1 = 0, 
                     const string      Text = "text",
                     const string      Font = "Arial",
                     const int         Font_size = 10,
                     const color       Clr = clrGray,
                     const int         Anchor = ANCHOR_RIGHT_UPPER,
                     const bool        Back = true,
                     const bool        Selection = false,
                     const bool        Hidden = true)
{
   ResetLastError();
   if(!ObjectCreate(Chart_ID, Name, OBJ_TEXT, Subwindow, Time1, Price1))
   {
      Print(__FUNCTION__,": failed to create text = ",GetLastError());
      return(false);
   }
   ObjectSetString(Chart_ID, Name, OBJPROP_TEXT, Text);
   ObjectSetString(Chart_ID, Name, OBJPROP_FONT, Font);
   ObjectSetInteger(Chart_ID, Name, OBJPROP_FONTSIZE, Font_size);
   ObjectSetInteger(Chart_ID, Name, OBJPROP_COLOR, Clr);
   ObjectSetInteger(Chart_ID, Name, OBJPROP_ANCHOR, Anchor);
   ObjectSetInteger(Chart_ID, Name, OBJPROP_SELECTABLE, Selection);
   ObjectSetInteger(Chart_ID, Name, OBJPROP_SELECTED, Selection);
   ObjectSetInteger(Chart_ID, Name, OBJPROP_HIDDEN, Hidden);
   return(true);
} 

//+------------------------------------------------------------------+
// LevelsDraw - This function will calculate the pivot levels for the
//    specified timeframe and plot them to the chart. 
//+------------------------------------------------------------------+
void LevelsDraw(  int      Shift,
                  datetime TimeStart, 
                  datetime TimeEnd, 
                  string   PeriodStr,
                  bool     IsFuture)
{
   double open = 0.0;
   double close = 0.0;
   double high = 0.0;
   double low = 0.0;
   double pivP = 0.0;         // Pivot Levels
   double res1 = 0.0;
   double res2 = 0.0;
   double res3 = 0.0;
   double sup1 = 0.0;
   double sup2 = 0.0;
   double sup3 = 0.0;
   double mid0 = 0.0;
   double mid1 = 0.0;
   double mid2 = 0.0;
   double mid3 = 0.0;
   double mid4 = 0.0;
   double mid5 = 0.0;
   string pivMsg = "";
   string midMsg = "";
   
   // get bar info
   high  = iHighMQL4(NULL, iTimePeriod, Shift);
   low   = iLowMQL4(NULL, iTimePeriod, Shift);
   open  = iOpenMQL4(NULL, iTimePeriod, Shift);  
   if (IsFuture == false)
   {
      close = iCloseMQL4(NULL, iTimePeriod, Shift);
   }
   else
   {
      close = BidMQL4();
   }      
     
   pivP = (high + low + close) / 3.0;
           
   res1 = 2 * pivP - low;
   res2 = pivP + (high - low);
   res3 = (2 * pivP) + (high -(2 * low));
            
   sup1 = 2 * pivP - high;
   sup2 = pivP - (high - low);
   sup3 = (2 * pivP) - ((2 * high) - low);
             
   mid0 = 0.5 * (sup2 + sup3);
   mid1 = 0.5 * (sup1 + sup2);
   mid2 = 0.5 * (pivP + sup1);
   mid3 = 0.5 * (pivP + res1);
   mid4 = 0.5 * (res1 + res2);
   mid5 = 0.5 * (res2 + res3);
   
   // plot zones if enabled
   if (iPlotZones)
   {
      PlotRectangle(0, PeriodStr + "BZ_" + Shift, 0, TimeStart, mid1, TimeEnd, sup2, iColorBuyZone);    
      PlotRectangle(0, PeriodStr + "SZ_" + Shift, 0, TimeStart, mid4, TimeEnd, res2, iColorSellZone);
   }

   if (iPlotPivots)
   {                                 
      // plot trendline for pivot levels
      PlotTrend(0, PeriodStr + "R3_T" + Shift, 0, TimeStart, res3, TimeEnd, res3, iColorRes, iStylePivots, iWidthPivots);     
      PlotTrend(0, PeriodStr + "R2_T" + Shift, 0, TimeStart, res2, TimeEnd, res2, iColorRes, iStylePivots, iWidthPivots);     
      PlotTrend(0, PeriodStr + "R1_T" + Shift, 0, TimeStart, res1, TimeEnd, res1, iColorRes, iStylePivots, iWidthPivots);     
      PlotTrend(0, PeriodStr + "PP_T" + Shift, 0, TimeStart, pivP, TimeEnd, pivP, iColorPP, iStylePivots, iWidthPivots);     
      PlotTrend(0, PeriodStr + "S1_T" + Shift, 0, TimeStart, sup1, TimeEnd, sup1, iColorSup, iStylePivots, iWidthPivots);     
      PlotTrend(0, PeriodStr + "S2_T" + Shift, 0, TimeStart, sup2, TimeEnd, sup2, iColorSup, iStylePivots, iWidthPivots);     
      PlotTrend(0, PeriodStr + "S3_T" + Shift, 0, TimeStart, sup3, TimeEnd, sup3, iColorSup, iStylePivots, iWidthPivots);
      
      // concatenate pivots to be sent as push notifications
      pivMsg = StringConcatenateMQL4(
               ", ", PeriodStr, "R3: ", DoubleToStr(res3, _Digits), 
               ", ", PeriodStr, "R2: ", DoubleToStr(res2, _Digits), 
               ", ", PeriodStr, "R1: ", DoubleToStr(res1, _Digits), 
               ", ", PeriodStr, "PP: ", DoubleToStr(pivP, _Digits), 
               ", ", PeriodStr, "S1: ", DoubleToStr(sup1, _Digits), 
               ", ", PeriodStr, "S2: ", DoubleToStr(sup2, _Digits), 
               ", ", PeriodStr, "S3: ", DoubleToStr(sup3, _Digits));
      
      if (iPlotPivotLabels)
      {
         PlotText(0, PeriodStr + "R3_L" + Shift, 0, TimeEnd, res3, PeriodStr + "R3", "Arial", 8, iColorRes, ANCHOR_RIGHT_UPPER);
         PlotText(0, PeriodStr + "R2_L" + Shift, 0, TimeEnd, res2, PeriodStr + "R2", "Arial", 8, iColorRes, ANCHOR_RIGHT_UPPER);
         PlotText(0, PeriodStr + "R1_L" + Shift, 0, TimeEnd, res1, PeriodStr + "R1", "Arial", 8, iColorRes, ANCHOR_RIGHT_UPPER);
         PlotText(0, PeriodStr + "PP_L" + Shift, 0, TimeEnd, pivP, PeriodStr + "PP", "Arial", 8, iColorPP, ANCHOR_RIGHT_UPPER);
         PlotText(0, PeriodStr + "S1_L" + Shift, 0, TimeEnd, sup1, PeriodStr + "S1", "Arial", 8, iColorSup, ANCHOR_RIGHT_UPPER);
         PlotText(0, PeriodStr + "S2_L" + Shift, 0, TimeEnd, sup2, PeriodStr + "S2", "Arial", 8, iColorSup, ANCHOR_RIGHT_UPPER);
         PlotText(0, PeriodStr + "S3_L" + Shift, 0, TimeEnd, sup3, PeriodStr + "S3", "Arial", 8, iColorSup, ANCHOR_RIGHT_UPPER);
      }    
      if (iPlotPivotPrices)
      {
         PlotText(0, PeriodStr + "R3_P" + Shift, 0, TimeStart, res3, DoubleToString(res3, _Digits), "Arial", 8, iColorRes, ANCHOR_LEFT_UPPER);
         PlotText(0, PeriodStr + "R2_P" + Shift, 0, TimeStart, res2, DoubleToString(res2, _Digits), "Arial", 8, iColorRes, ANCHOR_LEFT_UPPER);
         PlotText(0, PeriodStr + "R1_P" + Shift, 0, TimeStart, res1, DoubleToString(res1, _Digits), "Arial", 8, iColorRes, ANCHOR_LEFT_UPPER);
         PlotText(0, PeriodStr + "PP_P" + Shift, 0, TimeStart, pivP, DoubleToString(pivP, _Digits), "Arial", 8, iColorPP, ANCHOR_LEFT_UPPER);
         PlotText(0, PeriodStr + "S1_P" + Shift, 0, TimeStart, sup1, DoubleToString(sup1, _Digits), "Arial", 8, iColorSup, ANCHOR_LEFT_UPPER);
         PlotText(0, PeriodStr + "S2_P" + Shift, 0, TimeStart, sup2, DoubleToString(sup2, _Digits), "Arial", 8, iColorSup, ANCHOR_LEFT_UPPER);
         PlotText(0, PeriodStr + "S3_P" + Shift, 0, TimeStart, sup3, DoubleToString(sup3, _Digits), "Arial", 8, iColorSup, ANCHOR_LEFT_UPPER);
      }
   }    

   if (iPlotMidpoints)
   {
      // plot trendline for midpoint levels
      PlotTrend(0, PeriodStr + "M0_T" + Shift, 0, TimeStart, mid0, TimeEnd, mid0, iColorM02, iStyleMidpoints, iWidthMidpoints);     
      PlotTrend(0, PeriodStr + "M1_T" + Shift, 0, TimeStart, mid1, TimeEnd, mid1, iColorM02, iStyleMidpoints, iWidthMidpoints);     
      PlotTrend(0, PeriodStr + "M2_T" + Shift, 0, TimeStart, mid2, TimeEnd, mid2, iColorM02, iStyleMidpoints, iWidthMidpoints);     
      PlotTrend(0, PeriodStr + "M3_T" + Shift, 0, TimeStart, mid3, TimeEnd, mid3, iColorM35, iStyleMidpoints, iWidthMidpoints);     
      PlotTrend(0, PeriodStr + "M4_T" + Shift, 0, TimeStart, mid4, TimeEnd, mid4, iColorM35, iStyleMidpoints, iWidthMidpoints);     
      PlotTrend(0, PeriodStr + "M5_T" + Shift, 0, TimeStart, mid5, TimeEnd, mid5, iColorM35, iStyleMidpoints, iWidthMidpoints);
      
      // concatenate midpoints to be sent as push notifications
      midMsg = StringConcatenateMQL4(
               ", ", PeriodStr, "M5: ", DoubleToStr(mid5, _Digits), 
               ", ", PeriodStr, "M4: ", DoubleToStr(mid4, _Digits), 
               ", ", PeriodStr, "M3: ", DoubleToStr(mid3, _Digits), 
               ", ", PeriodStr, "M2: ", DoubleToStr(mid2, _Digits), 
               ", ", PeriodStr, "M1: ", DoubleToStr(mid1, _Digits), 
               ", ", PeriodStr, "M0: ", DoubleToStr(mid0, _Digits));
       
      if (iPlotPivotLabels)
      {
         PlotText(0, PeriodStr + "M0_L" + Shift, 0, TimeEnd, mid0, PeriodStr + "M0", "Arial", 8, iColorSup, ANCHOR_RIGHT_UPPER);
         PlotText(0, PeriodStr + "M1_L" + Shift, 0, TimeEnd, mid1, PeriodStr + "M1", "Arial", 8, iColorSup, ANCHOR_RIGHT_UPPER);
         PlotText(0, PeriodStr + "M2_L" + Shift, 0, TimeEnd, mid2, PeriodStr + "M2", "Arial", 8, iColorSup, ANCHOR_RIGHT_UPPER);
         PlotText(0, PeriodStr + "M3_L" + Shift, 0, TimeEnd, mid3, PeriodStr + "M3", "Arial", 8, iColorRes, ANCHOR_RIGHT_UPPER);
         PlotText(0, PeriodStr + "M4_L" + Shift, 0, TimeEnd, mid4, PeriodStr + "M4", "Arial", 8, iColorRes, ANCHOR_RIGHT_UPPER);
         PlotText(0, PeriodStr + "M5_L" + Shift, 0, TimeEnd, mid5, PeriodStr + "M5", "Arial", 8, iColorRes, ANCHOR_RIGHT_UPPER);
      }
      if (iPlotPivotPrices)
      {
         PlotText(0, PeriodStr + "M0_P" + Shift, 0, TimeStart, mid0, DoubleToString(mid0, _Digits), "Arial", 8, iColorSup, ANCHOR_LEFT_UPPER);
         PlotText(0, PeriodStr + "M1_P" + Shift, 0, TimeStart, mid1, DoubleToString(mid1, _Digits), "Arial", 8, iColorSup, ANCHOR_LEFT_UPPER);
         PlotText(0, PeriodStr + "M2_P" + Shift, 0, TimeStart, mid2, DoubleToString(mid2, _Digits), "Arial", 8, iColorSup, ANCHOR_LEFT_UPPER);
         PlotText(0, PeriodStr + "M3_P" + Shift, 0, TimeStart, mid3, DoubleToString(mid3, _Digits), "Arial", 8, iColorRes, ANCHOR_LEFT_UPPER);
         PlotText(0, PeriodStr + "M4_P" + Shift, 0, TimeStart, mid4, DoubleToString(mid4, _Digits), "Arial", 8, iColorRes, ANCHOR_LEFT_UPPER);
         PlotText(0, PeriodStr + "M5_P" + Shift, 0, TimeStart, mid5, DoubleToString(mid5, _Digits), "Arial", 8, iColorRes, ANCHOR_LEFT_UPPER);
      }
   }   
   
   if (iPlotBorders)
   {
      if (IsFuture == false)
      {
         PlotTrend(0, PeriodStr + "BDL_" + Shift, 0, TimeStart, res3, TimeStart, sup3, iColorBorder, iStyleBorder, iWidthBorder);  // left
         PlotTrend(0, PeriodStr + "BDR_" + Shift, 0, TimeEnd, res3, TimeEnd, sup3, iColorBorder, iStyleBorder, iWidthBorder);      // right
      }
   }
   
   // check for push notifications enabled
   if (iPushNotifications == True &&
       Shift == 1 &&                      // only send the current period
       gPrevTimePivot < TimeCurrent())    // time has elapsed
   {
	   // update prev time global to next period so we wont enter here again
	   // DEV NOTE: update to 10 seconds past the period for server sync
      gPrevTimePivot = iTimeMQL4(NULL, iTimePeriod, 0) + (iTimePeriod * 60) + (10);
      if (gPrevTimePivot < TimeCurrent())
      {
         // within current timeframe... add another timeframe to variable
         gPrevTimePivot += iTimePeriod * 60;
      }
      
      // send push notification to mobile and print msg to log
      SendNotification(Symbol() + pivMsg + midMsg);
      Print(Symbol() + pivMsg + midMsg);
   }
}

//+------------------------------------------------------------------+
// DeleteAllObjects - This function will delete all objects from the
//    chart for all history counts and future counts. 
//+------------------------------------------------------------------+
void DeleteAllObjects()
{
   int shift = 0;
   
   // loop through all periods and remove objects
   for (int shift=0; shift <= iCountPeriods; shift++)
   {
      LevelsDelete(gPeriod, shift);
   }
   LevelsDelete("F" + gPeriod, 0);

   return;
}

//+------------------------------------------------------------------+
// init - This function will set the period string and real time. The
//    period string is used on label plots and object names. The real
//    time period is used to plot correct time lenghts on higher 
//    timeframes. 
//+------------------------------------------------------------------+
int OnInit(){
 InitInputVariables();
   // go through each timeframe and assign period string and real time period minutes
   // NOTE: real minutes are used to calculate end times and future times
   switch (iTimePeriod)
   {
      case PERIOD_M1:
         gPeriod = "M1";
         gRealTimePeriod = PERIOD_M1;  // 1 minute
         break;
      case PERIOD_M5:
         gPeriod = "M5";
         gRealTimePeriod = PERIOD_M5;  // 5 minutes
         break;
      case PERIOD_M15:
         gPeriod = "M15";
         gRealTimePeriod = PERIOD_M15; // 15 minutes
         break;
      case PERIOD_M30:
         gPeriod = "M30";
         gRealTimePeriod = PERIOD_M30; // 30 minutes
         break;
      case PERIOD_H1:
         gPeriod = "H1";
         gRealTimePeriod = PERIOD_H1;  // 60 minutes
         break;
      case PERIOD_H4:
         gPeriod = "H4";
         gRealTimePeriod = PERIOD_H4;  // 240 minutes
         break;
      case PERIOD_D1:
         gPeriod = "D1";
         gRealTimePeriod = PERIOD_D1;  // 1440 minutes
         break;
      case PERIOD_W1:
         gPeriod = "W1";
         gRealTimePeriod = 8640;       // 8640 minutes (update to draw weekly line from Sunday to Friday - 6 days)
         break;
      case PERIOD_MN1:
         gPeriod = "MN1";
         gRealTimePeriod = PERIOD_MN1; // 43200 minutes (30 days)
         break;
      default:
         gPeriod = "";
         break;
   } 
   
   return(INIT_SUCCEEDED);}

//+------------------------------------------------------------------+
// deinit - This function will delete all objects on the chart. 
//+------------------------------------------------------------------+
void OnDeInit(const int reason)
{
   DeleteAllObjects();
   }

//+------------------------------------------------------------------+
// start - This function will calculate the start/end times for the
//    specified timeframe and draw the pivots for all history counts
//    and future pivots. 
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
   UpdateMQL4Variables(prev_calculated);
   int shift = 0;
   datetime timeStart;
   datetime timeEnd;
   
   // remove all objects upon entry
   DeleteAllObjects();
   
   // draw pivots/midpoints
   if (iPlotPivots && iTimePeriod != 0)
   {
      for (int shift=iCountPeriods-1; shift >= 0; shift--)
      {
         timeStart = iTimeMQL4(NULL, iTimePeriod, shift);
         timeEnd   = iTimeMQL4(NULL, iTimePeriod, shift) + (gRealTimePeriod * 60);   
            
         LevelsDraw(shift + 1, timeStart, timeEnd, gPeriod, false);                
      }
      
      // draw future pivots/midpoints
      if (iPlotFuturePivots && iTimePeriod != 0)
      {
         timeStart = iTimeMQL4(NULL, iTimePeriod, 0) + (gRealTimePeriod * 60);
         timeEnd   = iTimeMQL4(NULL, iTimePeriod, 0) + (gRealTimePeriod * 120);
   
         LevelsDraw(0, timeStart, timeEnd, "F" + gPeriod, true);      
      }
   }
   
   return(rates_total);}