//+------------------------------------------------------------------+
//|                                                   fibiSupRes.mq4 |
//|                                                       bfis108137 |
//|                                             baaaaaary at msn.com |
//+------------------------------------------------------------------+
#property copyright "bfis108137"
#property link      "baaaaaary at msn.com"
#property version   "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 7
#property indicator_plots   7
//--- plot r3
#property indicator_label1  "r3"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot r2
#property indicator_label2  "r2"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrBlue
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
//--- plot r1
#property indicator_label3  "r1"
#property indicator_type3   DRAW_LINE
#property indicator_color3  clrChartreuse
#property indicator_style3  STYLE_SOLID
#property indicator_width3  1
//--- plot p
#property indicator_label4  "p"
#property indicator_type4   DRAW_LINE
#property indicator_color4  clrWhite
#property indicator_style4  STYLE_SOLID
#property indicator_width4  1
//--- plot s1
#property indicator_label5  "s1"
#property indicator_type5   DRAW_LINE
#property indicator_color5  clrChartreuse
#property indicator_style5  STYLE_SOLID
#property indicator_width5  1
//--- plot s2
#property indicator_label6  "s2"
#property indicator_type6   DRAW_LINE
#property indicator_color6  clrBlue
#property indicator_style6  STYLE_SOLID
#property indicator_width6  1
//--- plot s3
#property indicator_label7  "s3"
#property indicator_type7   DRAW_LINE
#property indicator_color7  clrRed
#property indicator_style7  STYLE_SOLID
#property indicator_width7  1
//--- input parameters
input int      bars=500;
input          zaman=1440
//--- indicator buffers
double         r3Buffer[];
double         r2Buffer[];
double         r1Buffer[];
double         pBuffer[];
double         s1Buffer[];
double         s2Buffer[];
double         s3Buffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,r3Buffer);
   SetIndexBuffer(1,r2Buffer);
   SetIndexBuffer(2,r1Buffer);
   SetIndexBuffer(3,pBuffer);
   SetIndexBuffer(4,s1Buffer);
   SetIndexBuffer(5,s2Buffer);
   SetIndexBuffer(6,s3Buffer);
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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[])
  {
//---
   int lookBack = MathMin(rates_total-prev_calculated+1,bars);
   for(int i=0;i<lookBack;i++)
   {
      int dailyCandle = iBarShift(Symbol(),zaman,Time[i]);
      double hg = 0;
      double lw = 1.7976931348623158 * MathPow(10,308);
      int today = TimeDay(Time[i]);
      for(int j=i;j<1440;j++)
      {
         if(TimeDay(Time[j]) != today)break;
         if(High[j]>hg)hg = High[j];
         if(Low[j]<lw)lw = Low[j];
      }
      double cls= iClose(Symbol(),zaman,dailyCandle+1);
      double range = hg - lw;
      pBuffer[i] = (hg + lw + cls)/3;
      double p = pBuffer[i];
      r3Buffer[i] = p + range;
      r2Buffer[i] = p + 0.618 * range;
      r1Buffer[i] = p + 0.382 * range;
      s1Buffer[i] = p - 0.382 * range;
      s2Buffer[i] = p - 0.618 * range;
      s3Buffer[i] = p - range;
   }
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
