//+------------------------------------------------------------------+
//|                                              Universal Pivot.mq4 |
//|                               Copyright © 2004, Poul_Trade_Forum |
//|                                                         Aborigen |
//|                                          http://forex.kbpauk.ru/ |
//+------------------------------------------------------------------+
#property copyright "Poul Trade Forum"
#property link      "http://forex.kbpauk.ru/"
//----
#property indicator_chart_window
//#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Red

extern int  period = 240;
extern bool ShowLabel = true;
extern bool ShowLine  = true;

double PBuffer[];
string Pv, Pivot, txtPivot;

double P;
double x;
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
   ObjectDelete(Pivot);
   ObjectDelete(txtPivot);
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   if (ShowLine) {
      SetIndexStyle(0,DRAW_LINE,0,2,Red);
      SetIndexBuffer(0,PBuffer);
      string short_name="Filter H4";
      IndicatorShortName(short_name);
      SetIndexLabel(0,short_name);
      SetIndexDrawBegin(0,1);
   }
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
      if (Period() > period) return(-1);
   int    dayi, counted_bars=IndicatorCounted();
   int limit, i;
   if (counted_bars <0) return(-1);
   limit=(Bars-counted_bars)-1;
   color clrLabel;

   for(i=limit; i>=0;i--)
   {
      dayi = iBarShift(Symbol(), period, Time[i], false);
      P = (iHigh(Symbol(), period, dayi + 1) + iLow(Symbol(), period, dayi + 1) + iClose(Symbol(), period, dayi + 1)) / 3;
      if (ShowLine)     PBuffer[i]=P;
      if (ShowLabel) {
         if (P > iClose(Symbol(), 0, 0))  { Pv = "filter H4: SELL"; clrLabel = Red; } 
         else                             { Pv = "filter H4: BUY";  clrLabel = Green;}
         Pivot = "Pivot";
         txtPivot = "txtPivot";
         SetPrice("Pivot", Time[i], P, clrLabel);
         SetText("txtPivot", Pv, Time[i]+2*60*Period(), P-10*Point, clrLabel);
      }
   }
//----
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void SetPrice(string name, datetime Tm, double Prc, color clr)
  {
   if(ObjectFind(name) == -1)
     {
       ObjectCreate(name, OBJ_ARROW, 0, Tm, Prc);
       ObjectSet(name, OBJPROP_COLOR, clr);
       ObjectSet(name, OBJPROP_WIDTH, 1);
       ObjectSet(name, OBJPROP_ARROWCODE, SYMBOL_RIGHTPRICE);
     }
   else
     {
       ObjectSet(name, OBJPROP_TIME1, Tm);
       ObjectSet(name, OBJPROP_PRICE1, Prc);
       ObjectSet(name, OBJPROP_COLOR, clr);
       ObjectSet(name, OBJPROP_WIDTH, 1);
       ObjectSet(name, OBJPROP_ARROWCODE, SYMBOL_RIGHTPRICE);
     } 
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void SetText(string name, string txt, datetime Tm, double Prc, color clr)
  {
   if(ObjectFind(name) == -1)
     {
       ObjectCreate(name, OBJ_TEXT, 0, Tm, Prc);
       ObjectSetText(name, txt, 10, "Times New Roman", clr);
       ObjectSet(name, OBJPROP_CORNER, 2);
     }
   else
     {
       ObjectSet(name, OBJPROP_TIME1, Tm);
       ObjectSet(name, OBJPROP_PRICE1, Prc);
       ObjectSetText(name, txt, 10, "Times New Roman", clr);
       ObjectSet(name, OBJPROP_CORNER, 2);
     } 
  }
//+------------------------------------------------------------------+