//+------------------------------------------------------------------+
//|                                                  APROXIMATOR.mq4 |
//|                                                         Proximus |
//|                             http://www.forexfactory.com/proximus |
//+------------------------------------------------------------------+
#property copyright "Proximus"
#property link      "http://www.forexfactory.com/proximus"

#property indicator_chart_window
#property indicator_buffers 5
#property indicator_color1 Red
#property indicator_color2 Red
#property indicator_color3 DodgerBlue
#property indicator_color4 DodgerBlue
#property indicator_color5 White

double MONTHLYMAXHIGH[];
double DAILY_MEDIUM_HIGH[];
double MONTHLYMAXLOW[];
double DAILY_MEDIUM_LOW[];
double MID[];

extern int MaxBars = 5000;
extern bool   DisplayLabels   = true;
extern color  ResistanceLabelColor = Red;
extern color  MIDLabelColor        = White;
extern color  SupportLabelColor    = DodgerBlue;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   // Draw
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,1,Red  );   
   SetIndexStyle(1,DRAW_LINE,STYLE_DOT,1,Red);   
   SetIndexStyle(2,DRAW_LINE,STYLE_SOLID,1,DodgerBlue);  
   SetIndexStyle(3,DRAW_LINE,STYLE_DOT,1,DodgerBlue);  
   SetIndexStyle(4,DRAW_LINE,STYLE_SOLID,2,White);   

   
   // Bufers 
   SetIndexBuffer(0,MONTHLYMAXHIGH);  
    
   SetIndexBuffer(1,DAILY_MEDIUM_HIGH);  
    
   SetIndexBuffer(2,MONTHLYMAXLOW);   
   
   SetIndexBuffer(3,DAILY_MEDIUM_LOW);  
    
    
   SetIndexBuffer(4,MID); 

      
   SetIndexLabel(0, "MONTHLYMAXHIGH"); 
   SetIndexLabel(1, "DAILY_MEDIUM_HIGH"); 
   SetIndexLabel(2, "MONTHLYMAXLOW"); 
   SetIndexLabel(3, "DAILY_MEDIUM_LOW"); 
   SetIndexLabel(4, "MID"); 

   // Delete objects 
   DeleteObjects();
      
   IndicatorShortName("APROXIMATOR");
   Comment("Made by Proximus");
   return(0);
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int DeleteObjects()
{
   int obj_total=ObjectsTotal();
   for(int i = obj_total - 1; i >= 0; i--)
   {
       string label = ObjectName(i);
       if(StringFind(label, "APROXIMATOR") == -1) continue;
       ObjectDelete(label); 
   }     
   return(0);
}


int deinit()
  {
//----
      Comment("");
   DeleteObjects();
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
   // Start, limit, etc..
   int start = 1;
   int counted_bars = IndicatorCounted();
   
   // nothing else to do?
   if(counted_bars < 0) 
       return(-1);

   // do not check repeated bars
   int limit = MathMin(Bars-counted_bars-1, MaxBars);

   // Iteration
   for (int pos=0; pos <= limit; pos++) 
   {
      
      // High, low, close and open 
      double HIGH           = iHigh(Symbol(), PERIOD_D1,1);
      double LOW            = iLow(Symbol(), PERIOD_D1, 1);   
      double monthlyHIGH    = iHigh(Symbol(),PERIOD_MN1, 1);
      double monthlyLOW     = iLow(Symbol(), PERIOD_MN1, 1);    
      
      // Pivot Point
      double pv = (HIGH + LOW) / 2;
      double monthlypv = (monthlyHIGH + monthlyLOW) / 2;
      
      // Calcuations 
      MONTHLYMAXHIGH[pos] = (monthlypv + monthlyHIGH )/2;                                   // R1
      MONTHLYMAXLOW[pos] = (monthlypv + monthlyLOW )/2;                                  // S1 
      
      MID[pos] = ((4 * pv) - LOW - HIGH)/2 ;
         
      DAILY_MEDIUM_HIGH[pos] = (MID[pos] + (2 * pv) - LOW)/2; // upper
      DAILY_MEDIUM_LOW[pos] = (MID[pos] + (2 * pv) - HIGH)/2; // lower
      
   }

   // Draw labels
   DrawLabel("MONTHLY_MAX_HIGH", 1, MONTHLYMAXHIGH[1], ResistanceLabelColor, 0);
   DrawLabel("MONTHLY_MAX_LOW", 1, MONTHLYMAXLOW[1], SupportLabelColor, 0);
   
   
   DrawLabel("DAILY_MEDIUM_HIGH", 1, DAILY_MEDIUM_HIGH[1], ResistanceLabelColor, 0);
   DrawLabel("DAILY_MEDIUM_LOW", 1, DAILY_MEDIUM_LOW[1], SupportLabelColor, 0);
   
    DrawLabel("DAILY_MID", 1, MID[1], MIDLabelColor, 0);
   
   // Bye
   return(0);
}

////////////////////////////////////

void DrawLabel(string text, int shift, double vPrice, color vcolor, int voffset)
{
   // Time
   datetime x1 = Time[shift];
   
   // Bye if I don't need you
   if(!DisplayLabels) return(0);
   
   // Label
   string label = "APROXIMATOR" +"-"+ text;
   
   // If object exists, destroy it might be repainting
   if(ObjectFind(label) != -1) ObjectDelete(label);
   
   ObjectCreate(label, OBJ_TEXT, 0, x1, vPrice);
   ObjectSetText(label, text, 12, "Tahoma", vcolor); 
   ObjectSet(label, OBJPROP_BACK, true);
}
//+------------------------------------------------------------------+