//+-------------------------------------------------------------------+
//|                                                       Flippy.mq4  |
//|                                                                   |
//|                                                 Written by: Rusty |
//|                                                                   |
//+-------------------------------------------------------------------+
#property copyright "2007 Free software for trader"
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 LawnGreen
#property indicator_color2 Blue
#property indicator_color3 Red
#property indicator_color4 Magenta
extern int Width=50;
extern int RSXPeriod=6;
datetime BarTime=0;
//---- buffers
double PivotArray[];
double R1Array[];
double S1Array[];
double YesterdayCloseArray[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
  {
   SetIndexStyle(0, DRAW_LINE);
   SetIndexBuffer(0, PivotArray);
   IndicatorShortName("TDPivot");
   SetIndexLabel(0, "Pivot");
   SetIndexStyle(1, DRAW_LINE);
   SetIndexBuffer(1, R1Array);
   SetIndexLabel(1, "R1");
   SetIndexStyle(2, DRAW_LINE);
   SetIndexBuffer(2, S1Array);
   SetIndexLabel(2, "S1");
   SetIndexStyle(3, DRAW_LINE);
   SetIndexBuffer(3, YesterdayCloseArray);
   SetIndexLabel(3, "Yesterday Close"); 
   return(0);
  }
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int i;
   double Pivot, Yclose, R1, S1, R2, S2, pph, ppl, ppc, ph, pl, pc;
  
   if (BarTime == Time[0]) return (0);          // Only calc once per bar
   BarTime = Time[0];
   ph = High[Bars]; pl = Low[Bars]; pc = Close[Bars];
   for (i=Bars;i>=0;i--) {
      if (Bars - i < 12) {          // Cycle thru first two days to get info for pivot calc
         if (TimeDay(Time[i]) != TimeDay(Time[i+1])) {       // New day
            pph = ph; ppl = pl; ppc = pc;
            ph = High[i]; pl = Low[i]; pc = Close[i];
         }
         ph = MathMax(High[i],ph);
         pl = MathMin(Low[i],pl);
         pc = Close[i];
         continue;
      }
      if (TimeDay(Time[i]) != TimeDay(Time[i+1])) {       // New day so calc pivot
         Pivot = (pph + ppl + ppc) / 3;
         pph = ph; ppl = pl; ppc = pc;
         Yclose = pc;
         R1 = EMPTY_VALUE;
         S1 = EMPTY_VALUE;
         if (iCustom(Symbol(),0,"Turbo_JRSX",RSXPeriod,0,i+1) > iCustom(Symbol(),0,"Turbo_JRSX",RSXPeriod,0,i+2)) R1 = Pivot+Width*Point;
         if (iCustom(Symbol(),0,"Turbo_JRSX",RSXPeriod,0,i+1) < iCustom(Symbol(),0,"Turbo_JRSX",RSXPeriod,0,i+2)) S1 = Pivot-Width*Point;
         if (Pivot > Yclose) R1 = EMPTY_VALUE;
         if (Pivot < Yclose) S1 = EMPTY_VALUE;
         ph = High[i]; pl = Low[i]; pc = Close[i];
      }
      ph = MathMax(High[i],ph);
      pl = MathMin(Low[i],pl);
      pc = Close[i];
      PivotArray[i] = Pivot;
      YesterdayCloseArray[i] = Yclose;
      R1Array[i]=R1;
      S1Array[i]=S1;
   }
//----
   return(0);
  }
//+------------------------------------------------------------------+