//+------------------------------------------------------------------+
//|                                        inv_resistance_pivots.mq4 |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "public domain"
#property link      "http://www.forexfactory.com/showthread.php?t=325018"

#property indicator_chart_window
#property indicator_buffers 5
#property indicator_color1 Red
#property indicator_color2 Red
#property indicator_color3 Red
#property indicator_color4 Red
#property indicator_color5 Red


extern int BrokerOffsetFromUTC = 1;
extern int DayBeginsAtUTC = 22;
extern int MaxBars = 1000;

double point, upNumber, downNumber;
int Offset;

double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
double ExtMapBuffer5[];

int init()
{
  SetIndexStyle(0, DRAW_LINE);
  SetIndexBuffer(0, ExtMapBuffer1);
  SetIndexStyle(1, DRAW_LINE);
  SetIndexBuffer(1, ExtMapBuffer2);
  SetIndexStyle(2, DRAW_LINE);
  SetIndexBuffer(2, ExtMapBuffer3);
  SetIndexStyle(3, DRAW_LINE);
  SetIndexBuffer(3, ExtMapBuffer4);
  SetIndexStyle(4, DRAW_LINE);
  SetIndexBuffer(4, ExtMapBuffer5);
  
  SetIndexLabel(0, "Resistance1");
  SetIndexLabel(1, "Resistance2");
  SetIndexLabel(2, "Resistance3");
  SetIndexLabel(3, "Resistance4");
  SetIndexLabel(4, "Resistance5");

  point = Point;
  if (Digits % 2 == 1) point *= 10;

  BrokerOffsetFromUTC %= 24;
  if (BrokerOffsetFromUTC > 12) BrokerOffsetFromUTC = 24 - BrokerOffsetFromUTC;
  if (BrokerOffsetFromUTC < -12) BrokerOffsetFromUTC += 24;
  DayBeginsAtUTC %= 24;
  if (DayBeginsAtUTC > 12) DayBeginsAtUTC = 24 - DayBeginsAtUTC;
  if (DayBeginsAtUTC < -12) DayBeginsAtUTC += 24;
  Offset = BrokerOffsetFromUTC - DayBeginsAtUTC;
  Offset %= 24;
  if (Offset > 12) Offset = 24 - Offset;
  if (Offset < -12) Offset += 24;
  
  return(0);
}

int deinit()
{
  return(0);
}

int start()
{
  double yHigh, yLow, tOpen, yClose;
  int i, j, StartOfDay, yStart;
  double p, q;
  double r1, r2, r3, r4, r5, s1, s2, s3, s4, s5;
  
  if (Period() > PERIOD_H1) return(-1);
  if (Bars < MaxBars + 2) return(0);
  
  for (i = MaxBars; i >= 0; i--)
  {
    StartOfDay = iBarShift(NULL, Period(), iTime(NULL, PERIOD_D1, iBarShift(NULL, PERIOD_D1, Time[i]))) - (BrokerOffsetFromUTC - DayBeginsAtUTC) * (PERIOD_H1 / Period());
    if (StartOfDay - PERIOD_D1 / Period() > i) StartOfDay -= PERIOD_D1 / Period();
    if (StartOfDay < i) StartOfDay = iBarShift(NULL, Period(), iTime(NULL, PERIOD_D1, iBarShift(NULL, PERIOD_D1, Time[i]) + 1)) - (BrokerOffsetFromUTC - DayBeginsAtUTC) * (PERIOD_H1 / Period());
    tOpen = Open[StartOfDay];
    yStart = StartOfDay + PERIOD_D1 / Period();
    if (TimeDayOfWeek(Time[yStart]) == 0) yStart += PERIOD_D1 / Period();
    yHigh = High[yStart];
    yLow = Low[yStart];
    for (j = 1; j < PERIOD_D1 / Period(); j++)
    {
      yHigh = MathMax(yHigh, High[yStart - j]);
      yLow = MathMin(yLow, Low[yStart - j]);
    }
    yClose = Close[yStart - PERIOD_D1 / Period() - 1];
    p = (yHigh + yLow + yClose) / 3;
    q = yHigh - yLow;
    r1 = p + (q * 0.382); 
    r2 = p + (q * 0.618);
    r3 = p + q;
    r4 = p + (q * 1.618);
    r5 = p + (q * 2.618);
    s1 = p - (q * 0.382);
    s2 = p - (q * 0.618);
    s3 = p - q;
    s4 = p - (q * 1.618);
    s5 = p - (q * 2.618);
    downNumber = MathFloor(Low[i] / point / 100) * 100 * point;
    upNumber = MathCeil(High[i] / point / 100) * 100 * point;
  
    ExtMapBuffer1[i] = r1;
    ExtMapBuffer2[i] = r2;
    ExtMapBuffer3[i] = r3;
    ExtMapBuffer4[i] = r4;
    ExtMapBuffer5[i] = r5;
  }
  
  return(0);
}

