//+------------------------------------------------------------------+
//|                                             TigertronPiviots.mq4 |
//+------------------------------------------------------------------+
#property copyright "Copyright 2009 Tigertron"

#import "kernel32.dll"
int  GetTimeZoneInformation(int& TZInfoArray[]);
#import

//----
#property indicator_chart_window
//+--------------------------------------------------------------------------------+ 
//|                                                                                |
//|         Calculation of pivot and similar levels based on time zones.           |
//|                                                                                |
//+--------------------------------------------------------------------------------+
extern string _Traditional_Pivots_= "--------------------";
extern bool ShowPivots           = true;    // Traditional daily pivot points
extern bool ShowWeekPivots       = true; //false;   // Traditional weekly pivot points
extern bool ShowMonthPivots      = false;   // Traditional monthly pivot points
extern bool ShowMidPivot         = true;
extern string _Fibo_Extention_Pivots_= "--------------------";
extern bool ShowFibos            = true;// false;   // Fibonacci extentions from todays pivot point
extern bool ShowWeekFibos        = false;   // Fibonacci extentions from weekly pivot point
extern bool ShowMonthFibos       = false;   // Fibonacci extentions from monthly pivot point
extern bool ShowMidFibos         = false;
extern string _Extra_Features_= "--------------------";
extern bool ShowFiftyPct         = true;//false;   // 50% of Yesterdays range
extern bool ShowTodayOpen        = true;
extern bool ShowYestHighLow      = true;
extern bool ShowPriorHighLow     = true;
extern bool ShowSweetSpots       = true;    // 3 dynamic lines at round numbers 1 above PA,1 below PA and 1 half between
extern bool ShowDailyBar         = true;
extern bool ShowDailySMA         = true;
extern int  DailySMA1            = 50;      // Simple moving averages calculated on daily bars
extern int  DailySMA2            = 20;
extern string _Options_= "--------------------";
extern int  CalcInWhatTimeZone   = 3;  //-4;      // Time zone where you want the calculations to be done
extern int  CalcFromWhatHour     = 00;      // The hour you want the calulations to start from (calculations are on a 24 hour period)
extern int  GMTFridayClose       = 24;      // The GMT hour that market closes on Friday
extern int  GMTSundayOpen        = 24;      // The GMT hour that market opens on Sunday
extern bool ShowPrices           = true;    // Prices will be printed next to label
extern bool MakeVarsGlobal       = false;   // If true all variables will be made global and accesable from other indicators or EAs
extern string _Font_0= "--------------------";
extern string _Font_1= "- Colors and Styles -";
extern string _Font_2= "--------------------";
extern int  FontSize             = 8;
extern int  VertFontSize         = 8;
extern string _Colors_= "--------------------";
extern color Vert_Color          = DimGray;
extern color Pivot_Color         = Magenta;
extern color Pivot_R_Color       = Red;
extern color Pivot_S_Color       = Green;
extern color Fibo_R_Color        = Tomato;
extern color Fibo_S_Color        = YellowGreen;
extern color Mid_Pivot_Color     = Thistle;
extern color Mid_Fibo_Color      = PaleTurquoise;
extern color Today_Open_Color    = Orange;
extern color Fifty_Pct_Color     = Orange;
extern color Highs_Color         = Khaki;
extern color Lows_Color          = Khaki;
extern color SweetSpot_Color     = Gold;
extern color SMA1_Color          = Aqua;
extern color SMA2_Color          = PowderBlue;
extern color Up_Candle           = C'20,70,20';
extern color Dn_Candle           = C'70,20,20';

extern string _Line_Styles_= "--------------------";
extern string _Line_Styles_0= "0 = Solid.";
extern string _Line_Styles_1= "1 = Dashed.";
extern string _Line_Styles_2= "2 = Dotted";
extern string _Line_Styles_3= "3 = Dashes and Dots";
extern string _Line_Styles_4= "4 = Dashes and Double Dots";
extern string _Line_Styles_5= "--------------------";
extern int Pivot_Daily_Style     = 0;
extern int Pivot_Week_Style      = 3;
extern int Pivot_Month_Style     = 4;
extern int Fibo_Daily_Style      = 0;
extern int Fibo_Week_Style       = 3;
extern int Fibo_Month_Style      = 4;
extern int Mid_Pivot_Style       = 3;
extern int Mid_Fibo_Style        = 3;
extern int Today_Open_Style      = 1;
extern int Fifty_Pct_Style       = 0;
extern int Highs_Style           = 1;
extern int Lows_Style            = 1;
extern int SweetSpot_Style       = 2;
extern int SMA1_Style            = 0;
extern int SMA2_Style            = 0;
extern string _Line_Width_= "--------------------";
extern string _Line_Width_1= "1 = thinest";
extern string _Line_Width_5= "5 = thickest";
extern string _Line_Width_6= "--------------------";
extern int Global_Line_Width     = 1;
extern bool debug = true;


//----
datetime    tTemp,
            tTodayStart,
            tYesterdayStart,
            tPriorDayStart,
            tTomorrowStart,
            t24Hours,
            tBar,
            tBarZero,
            tEndOfThisWeek,
            GMT,
            DestTime,
            BrokerTime;

//----
double      dTemp,
            dTodayHigh,
            dTodayLow,
            dTodayOpen,
            dYesterdayHigh,
            dYesterdayOpen,
            dYesterdayLow,
            dYesterdayClose,
            dPriorDayHigh,
            dPriorDayOpen,
            dPriorDayLow,
            dPriorDayClose,
            dPriorPriorDayHigh,
            dPriorPriorDayOpen,
            dPriorPriorDayLow,
            dPriorPriorDayClose,
            dLastWeekHigh,
            dLastWeekLow,
            dLastWeekClose,
            dLastMonthHigh,
            dLastMonthLow,
            dLastMonthClose,
            GlobalValue;
//----
int         iTemp,
            iTodayFirstBarIndex,
            iYesterdayFirstBarIndex,
            iYesterdayLastBarIndex,
            iPriorDayFirstBarIndex,
            iPriorDayLastBarIndex,
            iPriorPriorDayFirstBarIndex,
            iPriorPriorDayLastBarIndex,
            iTodayFirstBarIndex1M,
            iYesterdayFirstBarIndex1M,
            iYesterdayLastBarIndex1M,
            iPriorDayFirstBarIndex1M,
            iPriorDayLastBarIndex1M,
            iPriorPriorDayFirstBarIndex1M,
            iPriorPriorDayLastBarIndex1M,
            iFirstBarOfWeek,
            iFirstBarOfLastWeek,
            iStartOfWeekIndex,
            iEndOfLastWeekIndex,
            i12Hours,
            TZInfoArray[43],
            TZDiff,
            offset,
            offsetY,
            offsetP,
            offsetPP,
            CalcCnt,
            dst,
            GMTShift;
//----
string      GlobalName,
            Pair;
//----
double      multi = 1,
            uss,
            lss,
            mss,
            sma1,
            sma2,
            ysma1,
            ysma2,
            psma1,
            psma2,
            level,
            p,
            q,
            r1,
            r2,
            r3,
            s1,
            s2,
            s3,
            yp,
            yq,
            yr1,
            yr2,
            yr3,
            ys1,
            ys2,
            ys3,
            pp,
            wp,
            wq,
            wr1,
            wr2,
            wr3,
            ws1,
            ws2,
            ws3,
            mp,
            mq,
            mr1,
            mr2,
            mr3,
            ms1,
            ms2,
            ms3,
            fr1,
            fr2,
            fr3,
            fs1,
            fs2,
            fs3,
            yfr1,
            yfr2,
            yfr3,
            yfs1,
            yfs2,
            yfs3,
            wfr1,
            wfr2,
            wfr3,
            wfs1,
            wfs2,
            wfs3,
            mfr1,
            mfr2,
            mfr3,
            mfs1,
            mfs2,
            mfs3,
            f50;
//----
bool        enabled            = true,
            MarketClosed       = false,
            LoadBars           = true;

string comment ="";
//GlobalVariableSet("test",Digits); // use for debuging

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void init()
  {
  if (!IsDllsAllowed())
      {
      Alert( "DLLs are disabled.  To enable tick the checkbox in the Common Tab of indicator" );
      enabled = false;
      return;
      }
   deinit();

   if(Digits==5)
      multi = 10;
   if(Digits==4)
      multi = 1;
   if(Digits==3)
      multi = 10;
   if(Digits==2)
      multi = 1;
   t24Hours = 24*3600;
   i12Hours = (12*60);
   tBar = Period()*60;
   Pair = StringSubstr(Symbol(),0,6);
   if(CalcFromWhatHour<0 || CalcFromWhatHour>23)CalcFromWhatHour = 0;
   return;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void deinit()
   {
   Comment("");
   int    obj_total = ObjectsTotal();
   string GlobalName;
//----
   for(int i = obj_total; i >= 0; i--) 
   {
      string name      = ObjectName(i);
//----
      if (StringSubstr(name,0,11)=="[Tigertron]")
         ObjectDelete(name);
   }
   if(!MakeVarsGlobal)return;
   GlobalName=Pair+"st";
   GlobalVariableDel(GlobalName);
   GlobalName=Pair+"sy";
   GlobalVariableDel(GlobalName);
   GlobalName=Pair+"sp";
   GlobalVariableDel(GlobalName);
   GlobalName=Pair+"p";
   GlobalVariableDel(GlobalName);
   GlobalName=Pair+"r1";
   GlobalVariableDel(GlobalName);
   GlobalName=Pair+"r2";
   GlobalVariableDel(GlobalName);
   GlobalName=Pair+"r3";
   GlobalVariableDel(GlobalName);
   GlobalName=Pair+"s1";
   GlobalVariableDel(GlobalName);
   GlobalName=Pair+"s2";
   GlobalVariableDel(GlobalName);
   GlobalName=Pair+"s3";
   GlobalVariableDel(GlobalName);
   GlobalVariableDel(GlobalName);
   GlobalName=Pair+"yh";
   GlobalVariableDel(GlobalName);
   GlobalName=Pair+"to";
   GlobalVariableDel(GlobalName);
   GlobalName=Pair+"yl";
   GlobalVariableDel(GlobalName);
   GlobalName=Pair+"uss";
   GlobalVariableDel(GlobalName);
   GlobalName=Pair+"lss";
   GlobalVariableDel(GlobalName);
   GlobalName=Pair+"mss";
   GlobalVariableDel(GlobalName);
   GlobalName=Pair+"fr3";
   GlobalVariableDel(GlobalName);
   GlobalName=Pair+"fr2";
   GlobalVariableDel(GlobalName);
   GlobalName=Pair+"fr1";
   GlobalVariableDel(GlobalName);
   GlobalName=Pair+"f50";
   GlobalVariableDel(GlobalName);
   GlobalName=Pair+"fs1";
   GlobalVariableDel(GlobalName);
   GlobalName=Pair+"fs2";
   GlobalVariableDel(GlobalName);
   GlobalName=Pair+"fs3";
   GlobalVariableDel(GlobalName);
   GlobalName=Pair+"sma1";
   GlobalVariableDel(GlobalName);
   GlobalName=Pair+"sma2";
   GlobalVariableDel(GlobalName);
   GlobalName=Pair+"pms3";
   GlobalVariableDel(GlobalName);
   GlobalName=Pair+"pms2";
   GlobalVariableDel(GlobalName);
   GlobalName=Pair+"pms1";
   GlobalVariableDel(GlobalName);
   GlobalName=Pair+"pmr3";
   GlobalVariableDel(GlobalName);
   GlobalName=Pair+"pmr2";
   GlobalVariableDel(GlobalName);
   GlobalName=Pair+"pmr1";
   GlobalVariableDel(GlobalName);
   GlobalName=Pair+"fms3";
   GlobalVariableDel(GlobalName);
   GlobalName=Pair+"fms2";
   GlobalVariableDel(GlobalName);
   GlobalName=Pair+"fms1";
   GlobalVariableDel(GlobalName);
   GlobalName=Pair+"fmr3";
   GlobalVariableDel(GlobalName);
   GlobalName=Pair+"fmr2";
   GlobalVariableDel(GlobalName);
   GlobalName=Pair+"fmr1";
   GlobalVariableDel(GlobalName);
//----
   return;
   }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
   {
      if(!enabled)return;

//---- exit if period is greater than daily charts
      if(Period() > PERIOD_D1) 
         {
         Alert("Tigertron Pivots Error - Chart period is greater than 1 day.");
         enabled = false;
         return(-1); // then exit
         }

//---- Get Local GMT Shift in minutes. Add this to local time to get GMT
      dst=GetTimeZoneInformation(TZInfoArray);
      if(dst >= 0)                               //Any timezones that do not adjust for daylight savings (like, say Indiana) return TIME_ZONE_ID_UNKNOWN
         GMTShift=TZInfoArray[0];
      if(dst==2)
         GMTShift+=TZInfoArray[42];
         GMT = TimeLocal()+(GMTShift*60);        // We now have GMT
      comment = TimeToStr(GMT);

/*      
On March 8, the US goes on DST     22:00 Close -4  GMT = 21:00 2pm PST
On Nov. 1, the US goes on Std.     21:00 Close -5  GMT = 22:00 2pm PST
On April 5, NZ goes on Std. time   21:00 Open  +12 GMT = 22:00 3pm PST
On Sept. 27, NZ goes on DST        22:00 Open  +13 GMT = 21:00 2pm PST
*/

      MarketClosed = false;
      if(TimeDayOfWeek(GMT)==6 || ((TimeDayOfWeek(GMT)==5 && TimeHour(GMT) >= GMTFridayClose) || (TimeDayOfWeek(GMT)==0 && TimeHour(GMT) <GMTSundayOpen)))MarketClosed = true;
      if(MarketClosed)GMT = StrToTime(StringConcatenate(TimeYear(tEndOfThisWeek),".",TimeMonth(tEndOfThisWeek),".",TimeDay(tEndOfThisWeek)," ",GMTFridayClose,":00"));

//---- Find the end of last week and start of this week
      for(int x = 0; x < Bars; x++)if(TimeDayOfWeek(iTime(NULL,1,x))== 1 || TimeDayOfWeek(iTime(NULL,1,x))== 0)break; // First look for Monday or Sunday in case it is Friday today
      for(x = x; x < Bars; x++)if(TimeDayOfWeek(iTime(NULL,1,x))>= 5)break;                                   // then look for Friday
      iEndOfLastWeekIndex = iBarShift(NULL,0,iTime(NULL,1,x),false);
      iStartOfWeekIndex = iEndOfLastWeekIndex-1;
      tEndOfThisWeek = Time[iEndOfLastWeekIndex]+(t24Hours*7)+tBar;

//      if(Bars-IndicatorCounted() > 1)
      if(TimeCurrent()>=tTomorrowStart)DoDaily();                   // DoCalcs at start of every new day

//----
//---- The following needs to be done every tick
//----
//----
//---- Sweet Spots
//----
int ssmod,
    sspips,
    ssupips,
    sslpips;
double point = Point;
if (point == 0)point = 1;
//----
   sspips   = (Bid / (point*multi));    //Here we turn Bid price into pips and shift out fractions. Point is smallest price i.e. 4 digits = .0001 
   ssmod    = sspips%100;               //remainder of ss/100 in pips. i.e. modulo of ss/100
   ssupips  = sspips+(100-ssmod);       //Upper SweetSpot in pips
   uss      = ssupips*(point*multi);    //convert back to price
   sslpips  = sspips-ssmod;             //Lower SweetSpot in pips
   lss      = sslpips*(point*multi);    //convert back to price
   mss      = (uss+lss)/2;              //middle SweetSpot in price

if(MakeVarsGlobal)SetGlobalVars();

if (ShowSweetSpots) 
   {
//----
//----
   SetPriceLevel("noUpperSS",uss,SweetSpot_Color,SweetSpot_Style,Global_Line_Width,10,0);
   SetPriceLevel("noLowerSS",lss,SweetSpot_Color,SweetSpot_Style,Global_Line_Width,10,0);
   SetPriceLevel("noMidSS",mss,SweetSpot_Color,SweetSpot_Style,Global_Line_Width,10,0);
   }

//----
//---- Daily Bar
//----
   dTodayOpen = Open[iTodayFirstBarIndex];  // open of today at start trading hour
if(iTodayFirstBarIndex == 0)
   {
   dTodayHigh = High[0];
   dTodayLow = Low[0];
   }
      else 
      {
      dTodayHigh = High[iHighest(NULL,0,MODE_HIGH,iTodayFirstBarIndex+1,0)];
      dTodayLow  = Low[iLowest(NULL,0,MODE_LOW,iTodayFirstBarIndex+1,0)];
      }
if(ShowDailyBar && Period() < PERIOD_D1 )
   {
   DrawDayBar("Today",dTodayHigh,dTodayLow,dTodayOpen,Close[0],0);
   }

if(debug)
   {
   if(MarketClosed)comment=StringConcatenate(comment,"\nMarket is Closed");
   else comment = StringConcatenate(comment,"\nMarket is Open");

   comment = 
   StringConcatenate(comment,
   "\n",GlobalVariablesTotal()," Global Variables Detected. ",Bars," Bars Detected. ",iBars(NULL,1)," M1 Bars Detected. ",
   CalcCnt," DoDaily() Accesses. ",Bars-IndicatorCounted()," Uncounted Bars. ",LoadBars,//" iTemp ",iTemp," i12Hours ",i12Hours,
   "\n\nBid: ",DoubleToStr(Bid,Digits)," Digits: ",Digits," Point: ",DoubleToStr(Point,Digits)," Bid in Pips (Bid/Point):",DoubleToStr((Bid/Point),0),
   "\n\nSweet Spots sspips: ",sspips," ssmod: ",ssmod," uss: ",uss," lss: ",lss," mss: ",mss,
   "\n\nToday High: ",dTodayHigh," Today Low: ",dTodayLow," Today Open: ",dTodayOpen,
   "\nToday Pivot: ",p,
   "");
   comment = 
   StringConcatenate(comment,
   "\n\nCalcInWhatTimeZone: ",CalcInWhatTimeZone,"  CalcFromWhatHour: ",CalcFromWhatHour,
   "\n\nGMT: ",TimeToStr(GMT),"  TZDiff: ",TimeToStr(TZDiff,TIME_SECONDS),
   "  DST: ",dst,"  TZInfoArray[0]: ",TZInfoArray[0],"  TZInfoArray[42]: ",TZInfoArray[42],"  GMT Shift: ",GMTShift," ",//TimeToStr(marketpulse - TimeCurrent(),TIME_SECONDS),//(MarketInfo(Symbol(),23)),

   "\n\nComputer Time (TimeLocal()): ",TimeToStr(TimeLocal()),"  Broker Time (TimeCurrent()): ",TimeToStr(TimeCurrent()),
   "\nBar Time (Time[0]): ",TimeToStr(Time[0]),"  Bar end time (Time[0]+Period()*60): ",TimeToStr(Time[0]+tBar),//"  ",TimeDayOfWeek(GMT)," ",TimeHour(GMT),
   "\nDest Time (GMT+CalcInWhatTimeZone): ",TimeToStr(GMT+(CalcInWhatTimeZone*3600)),
   "\nTommorrow Start: ",TimeToStr(tTomorrowStart),
   "\nEnd of last week: ",TimeToStr(Time[iEndOfLastWeekIndex])," Start of this week: ",TimeToStr(Time[iStartOfWeekIndex])," End of this week: ",TimeToStr(tEndOfThisWeek),
   "");

   comment = 
   StringConcatenate(comment,
   "\n\nPrior Prior day start to search for: ",TimeToStr(tTodayStart-offsetPP),
   "\nDest time zone\'s Prior Prior day starts: ",TimeToStr(Time[iPriorPriorDayFirstBarIndex]),
                                                   " (chart time),idxbar= ",iPriorPriorDayFirstBarIndex,
   "\nDest time zone\'s 1M prior Prior day starts: ",TimeToStr(iTime(NULL,1,iPriorPriorDayFirstBarIndex1M)),
                                                   " (1M chart time),idxbar= ",iPriorPriorDayFirstBarIndex1M,
   "\nDest time zone\'s Prior Prior day ends: ",TimeToStr(Time[iPriorPriorDayLastBarIndex]),
                                                   " (chart time),idxbar= ",iPriorPriorDayLastBarIndex,
   "\nDest time zone\'s 1M Prior Prior day ends: ",TimeToStr(iTime(NULL,1,iPriorPriorDayLastBarIndex1M)),
                                                   " (1M chart time),idxbar= ",iPriorPriorDayLastBarIndex1M,
   "");

   comment = 
   StringConcatenate(comment,
   "\n\nPrior day start to search for: ",TimeToStr(tTodayStart-offsetP),
   "\nDest time zone\'s Prior day starts: ",TimeToStr(Time[iPriorDayFirstBarIndex]),
                                                   " (chart time),idxbar= ",iPriorDayFirstBarIndex,
   "\nDest time zone\'s 1M Prior day starts: ",TimeToStr(iTime(NULL,1,iPriorDayFirstBarIndex1M)),
                                                   " (1M chart time),idxbar= ",iPriorDayFirstBarIndex1M,
   "\nDest time zone\'s Prior day ends: ",TimeToStr(Time[iPriorDayLastBarIndex]),
                                                   " (chart time),idxbar= ",iPriorDayLastBarIndex,
   "\nDest time zone\'s 1M Prior day ends: ",TimeToStr(iTime(NULL,1,iPriorDayLastBarIndex1M)),
                                                   " (1M chart time),idxbar= ",iPriorDayLastBarIndex1M,

   "\n\nYesterday start to search for: ",TimeToStr(tTodayStart-offsetY),
   "\nDest time zone\'s Yesterday starts: ",TimeToStr(Time[iYesterdayFirstBarIndex]),
                                                   " (chart time),idxbar= ",iYesterdayFirstBarIndex,
   "\nDest time zone\'s 1M Yesterday starts: ",TimeToStr(iTime(NULL,1,iYesterdayFirstBarIndex1M)),
                                                   " (1M chart time),idxbar= ",iYesterdayFirstBarIndex1M,
   "\nDest time zone\'s Yesterday ends: ",TimeToStr(Time[iYesterdayLastBarIndex]),
                                                   " (chart time),idxbar= ",iYesterdayLastBarIndex,
   "\nDest time zone\'s 1M Yesterday ends: ",TimeToStr(iTime(NULL,1,iYesterdayLastBarIndex1M)),
                                                   " (1M chart time),idxbar= ",iYesterdayLastBarIndex1M,

   "\n\nToday start to search for: ",TimeToStr(tTodayStart),
   "\nDest time zone\'s Today starts: ",TimeToStr(Time[iTodayFirstBarIndex]),
                                                   " (chart time),idxbar= ",iTodayFirstBarIndex,
   "\nDest time zone\'s 1M Today starts: ",TimeToStr(iTime(NULL,1,iTodayFirstBarIndex1M)),
                                                   " (1M chart time),idxbar= ",iTodayFirstBarIndex1M,
   "\n\nOffsetY ",offsetY/t24Hours," OffsetP ",offsetP/t24Hours," OffsetPP ",offsetPP/t24Hours,
   "");
   Comment(comment);
   }
comment = "";

   while(iBars(NULL,1)<2048 && LoadBars == true){DoDaily();tBarZero = 0;return;} //wait for bars to be loaded
    LoadBars = false;

//----
//---- The Following needs to be done every new bar
//----
if(tBarZero == Time[0])return;
tBarZero = Time[0];


if(ShowTodayOpen)
   {
   SetPriceLevel("                                T\'s Open",dTodayOpen,Today_Open_Color,Today_Open_Style,Global_Line_Width,iTodayFirstBarIndex,0);
   }
if(ShowYestHighLow)
   {
   SetPriceLevel("                              Y\'s High",dYesterdayHigh,Highs_Color,Highs_Style,Global_Line_Width,iYesterdayFirstBarIndex,0);
   SetPriceLevel("                              Y\'s Low",dYesterdayLow,Lows_Color,Lows_Style,Global_Line_Width,iYesterdayFirstBarIndex,0);
   }
if(ShowPriorHighLow)
   {
   SetPriceLevel("                              P\'s High",dPriorDayHigh,Highs_Color,Highs_Style,Global_Line_Width,iPriorDayFirstBarIndex,0);
   SetPriceLevel("                              P\'s Low",dPriorDayLow,Lows_Color,Lows_Style,Global_Line_Width,iPriorDayFirstBarIndex,0);
   }
//----
   SetPriceLevel("                         Pivot",p,Pivot_Color,Pivot_Daily_Style,Global_Line_Width,iTodayFirstBarIndex,0);
if(ShowPivots)
   {
   SetPriceLevel("                      R1",r1,Pivot_R_Color,Pivot_Daily_Style,Global_Line_Width,iTodayFirstBarIndex,0);
   SetPriceLevel("                      R2",r2,Pivot_R_Color,Pivot_Daily_Style,Global_Line_Width,iTodayFirstBarIndex,0);
   SetPriceLevel("                      R3",r3,Pivot_R_Color,Pivot_Daily_Style,Global_Line_Width,iTodayFirstBarIndex,0);
   SetPriceLevel("                      S1",s1,Pivot_S_Color,Pivot_Daily_Style,Global_Line_Width,iTodayFirstBarIndex,0);
   SetPriceLevel("                      S2",s2,Pivot_S_Color,Pivot_Daily_Style,Global_Line_Width,iTodayFirstBarIndex,0);
   SetPriceLevel("                      S3",s3,Pivot_S_Color,Pivot_Daily_Style,Global_Line_Width,iTodayFirstBarIndex,0);
      if(ShowMidPivot)
         {
//---- mid levels between pivots
         SetPriceLevel("noPMR3",(r2+r3)/2,Mid_Pivot_Color,Mid_Pivot_Style,Global_Line_Width,iTodayFirstBarIndex,0);
         SetPriceLevel("noPMR2",(r1+r2)/2,Mid_Pivot_Color,Mid_Pivot_Style,Global_Line_Width,iTodayFirstBarIndex,0);
         SetPriceLevel("noPMR1",(p+r1)/2,Mid_Pivot_Color,Mid_Pivot_Style,Global_Line_Width,iTodayFirstBarIndex,0);
         SetPriceLevel("noPMS1",(p+s1)/2,Mid_Pivot_Color,Mid_Pivot_Style,Global_Line_Width,iTodayFirstBarIndex,0);
         SetPriceLevel("noPMS2",(s1+s2)/2,Mid_Pivot_Color,Mid_Pivot_Style,Global_Line_Width,iTodayFirstBarIndex,0);
         SetPriceLevel("noPMS3",(s2+s3)/2,Mid_Pivot_Color,Mid_Pivot_Style,Global_Line_Width,iTodayFirstBarIndex,0);
         } 
   }
//----
//---- Weekly Pivot Lines
//----
if (ShowWeekPivots) 
   {
      SetPriceLevel("                                WeekR1",wr1,Pivot_R_Color,Pivot_Week_Style,Global_Line_Width,iPriorDayFirstBarIndex,0);
      SetPriceLevel("                                WeekR2",wr2,Pivot_R_Color,Pivot_Week_Style,Global_Line_Width,iPriorDayFirstBarIndex,0);
      SetPriceLevel("                                WeekR3",wr3,Pivot_R_Color,Pivot_Week_Style,Global_Line_Width,iPriorDayFirstBarIndex,0);
      SetPriceLevel("                                WeekS1",ws1,Pivot_S_Color,Pivot_Week_Style,Global_Line_Width,iPriorDayFirstBarIndex,0);
      SetPriceLevel("                                WeekS2",ws2,Pivot_S_Color,Pivot_Week_Style,Global_Line_Width,iPriorDayFirstBarIndex,0);
      SetPriceLevel("                                WeekS3",ws3,Pivot_S_Color,Pivot_Week_Style,Global_Line_Width,iPriorDayFirstBarIndex,0);
   }
//----
//---- Monthly Pivot Lines
//----
if (ShowMonthPivots) 
   {
      SetPriceLevel("                                MonthR1",mr1,Pivot_R_Color,Pivot_Month_Style,Global_Line_Width,iPriorDayFirstBarIndex,0);
      SetPriceLevel("                                MonthR2",mr2,Pivot_R_Color,Pivot_Month_Style,Global_Line_Width,iPriorDayFirstBarIndex,0);
      SetPriceLevel("                                MonthR3",mr3,Pivot_R_Color,Pivot_Month_Style,Global_Line_Width,iPriorDayFirstBarIndex,0);
      SetPriceLevel("                                MonthS1",ms1,Pivot_S_Color,Pivot_Month_Style,Global_Line_Width,iPriorDayFirstBarIndex,0);
      SetPriceLevel("                                MonthS2",ms2,Pivot_S_Color,Pivot_Month_Style,Global_Line_Width,iPriorDayFirstBarIndex,0);
      SetPriceLevel("                                MonthS3",ms3,Pivot_S_Color,Pivot_Month_Style,Global_Line_Width,iPriorDayFirstBarIndex,0);
   }
if(ShowFibos)
   {
//---- Fibos .618,.5 and .382
   SetPriceLevel("                          +61.8%",fr2,Fibo_R_Color,Fibo_Daily_Style,Global_Line_Width,iTodayFirstBarIndex,0);
   SetPriceLevel("                          +38.2%",fr1,Fibo_R_Color,Fibo_Daily_Style,Global_Line_Width,iTodayFirstBarIndex,0);
   SetPriceLevel("                          -38.2%",fs1,Fibo_S_Color,Fibo_Daily_Style,Global_Line_Width,iTodayFirstBarIndex,0);
   SetPriceLevel("                          -61.8%",fs2,Fibo_S_Color,Fibo_Daily_Style,Global_Line_Width,iTodayFirstBarIndex,0);
   if(!ShowPivots)// Fibo 100 is the same a pivot R3,S3 so only show one or the other
      {
      SetPriceLevel("                          +100%",fr3,Fibo_R_Color,Fibo_Daily_Style,Global_Line_Width,iTodayFirstBarIndex,0);
      SetPriceLevel("                          -100%",fs3,Fibo_S_Color,Fibo_Daily_Style,Global_Line_Width,iTodayFirstBarIndex,0);
      }
   if(ShowMidFibos)
      {
//---- mid levels between fibos
      SetPriceLevel("noFMR3",(fr2+fr3)/2,Mid_Fibo_Color,Mid_Fibo_Style,Global_Line_Width,iTodayFirstBarIndex,0);
      SetPriceLevel("noFMR2",(fr1+fr2)/2,Mid_Fibo_Color,Mid_Fibo_Style,Global_Line_Width,iTodayFirstBarIndex,0);
      SetPriceLevel("noFMR1",(p+fr1)/2,Mid_Fibo_Color,Mid_Fibo_Style,Global_Line_Width,iTodayFirstBarIndex,0);
      SetPriceLevel("noFMS1",(p+fs1)/2,Mid_Fibo_Color,Mid_Fibo_Style,Global_Line_Width,iTodayFirstBarIndex,0);
      SetPriceLevel("noFMS2",(fs1+fs2)/2,Mid_Fibo_Color,Mid_Fibo_Style,Global_Line_Width,iTodayFirstBarIndex,0);
      SetPriceLevel("noFMS3",(fs2+fs3)/2,Mid_Fibo_Color,Mid_Fibo_Style,Global_Line_Width,iTodayFirstBarIndex,0);
      }
   }
if(ShowWeekFibos)
   {
//---- Fibos .618,.5 and .382
   SetPriceLevel("                                  Week+61.8%",wfr2,Fibo_R_Color,Fibo_Week_Style,Global_Line_Width,iPriorDayFirstBarIndex,0);
   SetPriceLevel("                                  Week+38.2%",wfr1,Fibo_R_Color,Fibo_Week_Style,Global_Line_Width,iPriorDayFirstBarIndex,0);
   SetPriceLevel("                                  Week-38.2%",wfs1,Fibo_S_Color,Fibo_Week_Style,Global_Line_Width,iPriorDayFirstBarIndex,0);
   SetPriceLevel("                                  Week-61.8%",wfs2,Fibo_S_Color,Fibo_Week_Style,Global_Line_Width,iPriorDayFirstBarIndex,0);
      if(!ShowWeekPivots) // Fibo 100 is the same a pivot R2,S2 so only show one or the other
         {
         SetPriceLevel("                                  Week+100%",wfr3,Fibo_R_Color,Fibo_Week_Style,Global_Line_Width,iPriorDayFirstBarIndex,0);
         SetPriceLevel("                                  Week-100%",wfs3,Fibo_S_Color,Fibo_Week_Style,Global_Line_Width,iPriorDayFirstBarIndex,0);
         }
   }
if(ShowMonthFibos)
   {
//---- Fibos .618,.5 and .382
   SetPriceLevel("                                  Month+61.8%",mfr2,Fibo_R_Color,Fibo_Month_Style,Global_Line_Width,iPriorDayFirstBarIndex,0);
   SetPriceLevel("                                  Month+38.2%",mfr1,Fibo_R_Color,Fibo_Month_Style,Global_Line_Width,iPriorDayFirstBarIndex,0);
   SetPriceLevel("                                  Month-38.2%",mfs1,Fibo_S_Color,Fibo_Month_Style,Global_Line_Width,iPriorDayFirstBarIndex,0);
   SetPriceLevel("                                  Month-61.8%",mfs2,Fibo_S_Color,Fibo_Month_Style,Global_Line_Width,iPriorDayFirstBarIndex,0);
      if(!ShowMonthPivots) // Fibo 100 is the same a pivot R2,S2 so only show one or the other
         {
         SetPriceLevel("                                  Month+100%",mfr3,Fibo_R_Color,Fibo_Month_Style,Global_Line_Width,iPriorDayFirstBarIndex,0);
         SetPriceLevel("                                  Month-100%",mfs3,Fibo_S_Color,Fibo_Month_Style,Global_Line_Width,iPriorDayFirstBarIndex,0);
         }
   }
if(ShowFiftyPct)
   {
   SetPriceLevel("                       50%",f50,Fifty_Pct_Color,Fifty_Pct_Style,Global_Line_Width,iTodayFirstBarIndex,0);
   }
if(ShowDailySMA)
   {
   SetPriceLevel(StringConcatenate("                                  ",DailySMA1," Day SMA"),sma1,SMA1_Color,SMA1_Style,Global_Line_Width,iTodayFirstBarIndex,0);
   SetPriceLevel(StringConcatenate("                                  ",DailySMA2," Day SMA"),sma2,SMA2_Color,SMA2_Style,Global_Line_Width,iTodayFirstBarIndex,0);
   }         
//
iTodayFirstBarIndex++;
iYesterdayFirstBarIndex++;
iPriorDayFirstBarIndex++;
   return;
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void DoDaily()
   {
      CalcCnt++;
      int i;
      DestTime = (GMT+(CalcInWhatTimeZone*3600));// so we can calc what time it is in our desination TZ
      BrokerTime = TimeCurrent();                // chart time

//---- Find the diffrence between chart time and the destination
      if(BrokerTime>DestTime)
         TZDiff = BrokerTime-DestTime;
      else TZDiff = (t24Hours)-(DestTime-BrokerTime);
      int x = TimeMinute(TZDiff); // fix differences between system clock and server clock
      TZDiff = TimeHour(TZDiff)*3600;
      if (x > 30)TZDiff += 3600;

//----  What is "chart time" of our "destination time". All time references are based off of tTodayStart
      tTodayStart = StrToTime(StringConcatenate(Year(),".",Month(),".",Day()," 00:00"))+TZDiff+(CalcFromWhatHour*3600);
      if(Time[0] >= tTodayStart+t24Hours)tTodayStart += t24Hours;
      if(MarketClosed)tTodayStart = Time[0];

//---- Get indexes to various days
      while(tTodayStart>=Time[0]+tBar && i < 10){tTodayStart -= t24Hours;i++;} // its in the future so go back one day at a time
      i = 0;
//      if(!MarketClosed && TimeHour(Time[0]) == TimeHour(tTodayStart)) tTodayStart = tTodayStart+t24Hours;
      offset = t24Hours;
      if(tTodayStart>Time[0] && tTodayStart<Time[0]+tBar)iTodayFirstBarIndex1M = 0;
      else 
      {
      iTodayFirstBarIndex1M = iBarShift(NULL,1,tTodayStart,false);
      while(TimeHour(iTime(NULL,1,iTodayFirstBarIndex1M))<TimeHour(tTodayStart) && i < 10){iTodayFirstBarIndex1M--;i++;}
      }
      i=0;
      if(iTodayFirstBarIndex1M == iEndOfLastWeekIndex)iTodayFirstBarIndex1M--; // if tTodayStart falls on a Saturday iBarShift will find Fridays last bar
      iYesterdayLastBarIndex1M = iTodayFirstBarIndex1M+1;

//---- Get Yesterdays Index
      while((tTodayStart-offset > Time[iEndOfLastWeekIndex] && tTodayStart-offset < Time[iStartOfWeekIndex]) // is the time in the weekend?
      && TimeDay(tTodayStart-offset) != TimeDay(Time[iStartOfWeekIndex])&& i < 10) // I forgot why I need this
      {offset += t24Hours;i++;} // figure out how much offset is needed to get past a weekend if needed
      i = 0;
      
      iYesterdayFirstBarIndex1M = iBarShift(NULL,1,tTodayStart-offset,false);
      if(iYesterdayFirstBarIndex1M == iEndOfLastWeekIndex)iYesterdayFirstBarIndex1M--; // if tTodayStart falls on a Saturday iBarShift will find Fridays last bar

      if(iYesterdayFirstBarIndex1M-iTodayFirstBarIndex1M<i12Hours) //add short days (i.e. Sunday) to today
      {
      iTodayFirstBarIndex1M = iYesterdayFirstBarIndex1M;
      iYesterdayLastBarIndex1M = iTodayFirstBarIndex1M+1;
      offset += t24Hours;
      while((tTodayStart-offset > Time[iEndOfLastWeekIndex] && tTodayStart-offset < Time[iStartOfWeekIndex]) // is the time in the weekend?
      && TimeDay(tTodayStart-offset) != TimeDay(Time[iStartOfWeekIndex])&& i < 10) // I forgot why I need this
      {offset += t24Hours;i++;} // figure out how much offset is needed to get past a weekend if needed
      i = 0;

      iYesterdayFirstBarIndex1M = iBarShift(NULL,1,tTodayStart-offset,false);
      if(iYesterdayFirstBarIndex1M == iEndOfLastWeekIndex)iYesterdayFirstBarIndex1M--; // if tTodayStart falls on a Saturday iBarShift will find Fridays last bar
      }
      offsetY = offset;
      while(iTime(NULL,1,iYesterdayFirstBarIndex1M)<tTodayStart-offsetY && i < 10){iYesterdayFirstBarIndex1M--;i++;}
      i = 0;
      iPriorDayLastBarIndex1M = iYesterdayFirstBarIndex1M+1;

//---- Get Prior Days Index
      offset += t24Hours; // add one day
      while((tTodayStart-offset > Time[iEndOfLastWeekIndex] && tTodayStart-offset < Time[iStartOfWeekIndex]) // is the time in the weekend?
      && TimeDay(tTodayStart-offset) != TimeDay(Time[iStartOfWeekIndex])&& i < 10) // I forgot why I need this
      {offset += t24Hours;i++;} // figure out how much offset is needed to get past a weekend if needed
      i = 0;

      iPriorDayFirstBarIndex1M = iBarShift(NULL,1,tTodayStart-offset,false);
      if(iPriorDayFirstBarIndex1M == iEndOfLastWeekIndex)iPriorDayFirstBarIndex1M--; // if tTodayStart-offset falls on a Saturday iBarShift will find Fridays last bar

      if(iPriorDayFirstBarIndex1M-iYesterdayFirstBarIndex1M<i12Hours) //add short days (i.e. Sunday) to today
      {
      iYesterdayFirstBarIndex1M = iPriorDayFirstBarIndex1M;
      iPriorDayLastBarIndex1M = iYesterdayFirstBarIndex1M+1;
      offset += t24Hours;
      while((tTodayStart-offset > Time[iEndOfLastWeekIndex] && tTodayStart-offset < Time[iStartOfWeekIndex]) // is the time in the weekend?
      && TimeDay(tTodayStart-offset) != TimeDay(Time[iStartOfWeekIndex])&& i < 10) // I forgot why I need this
      {offset += t24Hours;i++;} // figure out how much offset is needed to get past a weekend if needed
      i = 0;

      iPriorDayFirstBarIndex1M = iBarShift(NULL,1,tTodayStart-offset,false);
      if(iPriorDayFirstBarIndex1M == iEndOfLastWeekIndex)iPriorDayFirstBarIndex1M--; // if tTodayStart falls on a Saturday iBarShift will find Fridays last bar
      }
      offsetP = offset;
      while(iTime(NULL,1,iPriorDayFirstBarIndex1M)<tTodayStart-offsetP && i < 10){iPriorDayFirstBarIndex1M--;i++;}
      i = 0;
      iPriorPriorDayLastBarIndex1M = iPriorDayFirstBarIndex1M+1;

//---- Get Prior Prior Days Index
      offset += t24Hours; // add one day
      while((tTodayStart-offset > Time[iEndOfLastWeekIndex] && tTodayStart-offset < Time[iStartOfWeekIndex]) // is the time in the weekend?
      && TimeDay(tTodayStart-offset) != TimeDay(Time[iStartOfWeekIndex]) && i < 10) // I forgot why I need this
      {offset += t24Hours;i++;} // figure out how much offset is needed to get past a weekend if needed
      i = 0;

      iPriorPriorDayFirstBarIndex1M = iBarShift(NULL,1,tTodayStart-offset,false);
      if(iPriorPriorDayFirstBarIndex1M == iEndOfLastWeekIndex)iPriorPriorDayFirstBarIndex1M--; // if tTodayStart-offset falls on a Saturday iBarShift will find Fridays last bar

      if(iPriorPriorDayFirstBarIndex1M-iPriorDayFirstBarIndex1M<i12Hours) //add short days (i.e. Sunday) to today
      {
      iPriorDayFirstBarIndex1M = iPriorPriorDayFirstBarIndex1M;
      iPriorPriorDayLastBarIndex1M = iPriorDayFirstBarIndex1M+1;
      offset += t24Hours;
      while((tTodayStart-offset > Time[iEndOfLastWeekIndex] && tTodayStart-offset < Time[iStartOfWeekIndex]) // is the time in the weekend?
      && TimeDay(tTodayStart-offset) != TimeDay(Time[iStartOfWeekIndex]) && i < 10) // I forgot why I need this
      {offset += t24Hours;i++;} // figure out how much offset is needed to get past a weekend if needed
      i = 0;

      iPriorPriorDayFirstBarIndex1M = iBarShift(NULL,1,tTodayStart-offset,false);
      if(iPriorPriorDayFirstBarIndex1M == iEndOfLastWeekIndex)iPriorPriorDayFirstBarIndex1M--; // if tTodayStart falls on a Saturday iBarShift will find Fridays last bar
      }
      offsetPP = offset;
      while(iTime(NULL,1,iPriorPriorDayFirstBarIndex1M)<tTodayStart-offsetPP && i < 10){iPriorPriorDayFirstBarIndex1M--;i++;}

      if(iTodayFirstBarIndex1M == 0)iTodayFirstBarIndex = 0;
      else iTodayFirstBarIndex = iBarShift(NULL,0,iTime(NULL,1,iTodayFirstBarIndex1M),false);
      iYesterdayFirstBarIndex = iBarShift(NULL,0,iTime(NULL,1,iYesterdayFirstBarIndex1M),false);
      iYesterdayLastBarIndex = iBarShift(NULL,0,iTime(NULL,1,iYesterdayLastBarIndex1M),false);
      iPriorDayFirstBarIndex = iBarShift(NULL,0,iTime(NULL,1,iPriorDayFirstBarIndex1M),false);
      iPriorDayLastBarIndex = iBarShift(NULL,0,iTime(NULL,1,iPriorDayLastBarIndex1M),false);
      iPriorPriorDayFirstBarIndex = iBarShift(NULL,0,iTime(NULL,1,iPriorPriorDayFirstBarIndex1M),false);
      iPriorPriorDayLastBarIndex = iBarShift(NULL,0,iTime(NULL,1,iPriorPriorDayLastBarIndex1M),false);
      if(Period()==PERIOD_D1)
         {
         iTodayFirstBarIndex = 0;
         iYesterdayFirstBarIndex = 1;
         iYesterdayLastBarIndex = 1;
         iPriorDayFirstBarIndex = 2;
         iPriorDayLastBarIndex = 2;
         iPriorPriorDayFirstBarIndex = 3;
         iPriorPriorDayLastBarIndex = 3;
         }

//---- Get various times
      tTomorrowStart  = tTodayStart+t24Hours;
      tYesterdayStart = Time[iYesterdayFirstBarIndex];
      tPriorDayStart  = Time[iPriorDayFirstBarIndex];

//---- Get various prices
      dYesterdayHigh  = iHigh(NULL,1,iHighest(NULL,1,MODE_HIGH,iYesterdayFirstBarIndex1M-iYesterdayLastBarIndex1M+1,iYesterdayLastBarIndex1M));
      dYesterdayLow   = iLow(NULL,1,iLowest(NULL,1,MODE_LOW,iYesterdayFirstBarIndex1M-iYesterdayLastBarIndex1M+1,iYesterdayLastBarIndex1M));
      dYesterdayOpen  = iOpen(NULL,1,iYesterdayFirstBarIndex1M);
      dYesterdayClose = iClose(NULL,1,iYesterdayLastBarIndex1M);
//----
      dPriorDayHigh   = iHigh(NULL,1,iHighest(NULL,1,MODE_HIGH,iPriorDayFirstBarIndex1M-iPriorDayLastBarIndex1M+1,iPriorDayLastBarIndex1M));
      dPriorDayLow    = iLow(NULL,1,iLowest(NULL,1,MODE_LOW,iPriorDayFirstBarIndex1M-iPriorDayLastBarIndex1M+1,iPriorDayLastBarIndex1M));
      dPriorDayOpen   = iOpen(NULL,1,iPriorDayFirstBarIndex1M);
      dPriorDayClose  = iClose(NULL,1,iPriorDayLastBarIndex1M);
//----
      dPriorPriorDayHigh   = iHigh(NULL,1,iHighest(NULL,1,MODE_HIGH,iPriorPriorDayFirstBarIndex1M-iPriorPriorDayLastBarIndex1M+1,iPriorPriorDayLastBarIndex1M));
      dPriorPriorDayLow    = iLow(NULL,1,iLowest(NULL,1,MODE_LOW,iPriorPriorDayFirstBarIndex1M-iPriorPriorDayLastBarIndex1M+1,iPriorPriorDayLastBarIndex1M));
      dPriorPriorDayOpen   = iOpen(NULL,1,iPriorPriorDayFirstBarIndex1M);
      dPriorPriorDayClose  = iClose(NULL,1,iPriorPriorDayLastBarIndex1M);
//----
      dLastWeekHigh   = iHigh(NULL,PERIOD_W1,1);
      dLastWeekLow    = iLow(NULL,PERIOD_W1,1);
      dLastWeekClose  = iClose(NULL,PERIOD_W1,1);
      dLastMonthHigh  = iHigh(NULL,PERIOD_MN1,1);
      dLastMonthLow   = iLow(NULL,PERIOD_MN1,1);
      dLastMonthClose = iClose(NULL,PERIOD_MN1,1);
//----
//---- Calculate pivot levels
//----
      q=(dYesterdayHigh - dYesterdayLow);
//----
      p=(dYesterdayHigh + dYesterdayLow + dYesterdayClose)/3;
      p=NormalizeDouble(p,Digits);
      r1=(2*p)-dYesterdayLow;
      r1=NormalizeDouble(r1,Digits);
      r2=p+(dYesterdayHigh - dYesterdayLow);
      r2=NormalizeDouble(r2,Digits);
      r3=(2*p)+(dYesterdayHigh-(2*dYesterdayLow));
      r3=NormalizeDouble(r3,Digits);
      s1=(2*p)-dYesterdayHigh;
      s1=NormalizeDouble(s1,Digits);
      s2=p-(dYesterdayHigh - dYesterdayLow);
      s2=NormalizeDouble(s2,Digits);
      s3=(2*p)-((2* dYesterdayHigh)-dYesterdayLow);
      s3=NormalizeDouble(s3,Digits);
//----
//---- yesterday piviots
//----
      yq=(dPriorDayHigh - dPriorDayLow);
//----
      yp=(dPriorDayHigh + dPriorDayLow + dPriorDayClose)/3;
      yp=NormalizeDouble(yp,Digits);
      yr1=(2*yp)-dPriorDayLow;
      yr1=NormalizeDouble(yr1,Digits);
      yr2=yp+(dPriorDayHigh - dPriorDayLow);
      yr2=NormalizeDouble(yr2,Digits);
      yr3=(2*yp)+(dPriorDayHigh-(2*dPriorDayLow));
      yr3=NormalizeDouble(yr3,Digits);
      ys1=(2*yp)-dPriorDayHigh;
      ys1=NormalizeDouble(ys1,Digits);
      ys2=yp-(dPriorDayHigh - dPriorDayLow);
      ys2=NormalizeDouble(ys2,Digits);
      ys3=(2*yp)-((2* dPriorDayHigh)-dPriorDayLow);
      ys3=NormalizeDouble(ys3,Digits);
//----
//---- Weekly piviots
//----
      wq=(dLastWeekHigh - dLastWeekLow);
//----
      wp=(dLastWeekHigh + dLastWeekLow + dLastWeekClose)/3;
      wp=NormalizeDouble(wp,Digits);
      wr1=(2*wp)-dLastWeekLow;
      wr1=NormalizeDouble(wr1,Digits);
      wr2=wp+(dLastWeekHigh - dLastWeekLow);
      wr2=NormalizeDouble(wr2,Digits);
      wr3=(2*wp)+(dLastWeekHigh-(2*dLastWeekLow));
      wr3=NormalizeDouble(wr3,Digits);
      ws1=(2*wp)-dLastWeekHigh;
      ws1=NormalizeDouble(ws1,Digits);
      ws2=wp-(dLastWeekHigh - dLastWeekLow);
      ws2=NormalizeDouble(ws2,Digits);
      ws3=(2*wp)-((2* dLastWeekHigh)-dLastWeekLow);
      ws3=NormalizeDouble(ws3,Digits);
//----
//---- Monthly piviots
//----
      mq=(dLastMonthHigh - dLastMonthLow);
//----
      mp=(dLastMonthHigh + dLastMonthLow + dLastMonthClose)/3;
      mp=NormalizeDouble(mp,Digits);
      mr1=(2*mp)-dLastMonthLow;
      mr1=NormalizeDouble(mr1,Digits);
      mr2=mp+(dLastMonthHigh - dLastMonthLow);
      mr2=NormalizeDouble(mr2,Digits);
      mr3=(2*mp)+(dLastMonthHigh-(2*dLastMonthLow));
      mr3=NormalizeDouble(mr3,Digits);
      ms1=(2*mp)-dLastMonthHigh;
      ms1=NormalizeDouble(ms1,Digits);
      ms2=mp-(dLastMonthHigh - dLastMonthLow);
      ms2=NormalizeDouble(ms2,Digits);
      ms3=(2*mp)-((2* dLastMonthHigh)-dLastMonthLow);
      ms3=NormalizeDouble(ms3,Digits);
//----
//---- draw the vertical bars that marks the time span
//----
      level= (WindowPriceMax()+WindowPriceMin())/2;
      SetTimeLine("TodayStart","Today",iTodayFirstBarIndex,Vert_Color,level);
      SetTimeLine("YesterdayStart","Yesterday",iYesterdayFirstBarIndex,Vert_Color,level);
      SetTimeLine("PriorStart","Prior",iPriorDayFirstBarIndex,Vert_Color,level);
//----
//---- draw daily bars
//----
      if(ShowDailyBar && Period() < PERIOD_D1 )
      {
         DrawDayBar("Yesterday",dYesterdayHigh,dYesterdayLow,Open[iYesterdayFirstBarIndex],Close[iYesterdayLastBarIndex],iTodayFirstBarIndex);
         DrawDayBar("PriorDay",dPriorDayHigh,dPriorDayLow,Open[iPriorDayFirstBarIndex],Close[iPriorDayLastBarIndex],iYesterdayFirstBarIndex);
         DrawDayBar("PriorPriorDay",dPriorPriorDayHigh,dPriorPriorDayLow,Open[iPriorPriorDayFirstBarIndex],Close[iPriorPriorDayLastBarIndex],iPriorDayFirstBarIndex);
      }
//----
//---- Yesterdays Pivot Lines
//----
         SetPriceLevel("noYPivot",yp,Pivot_Color,Pivot_Daily_Style,Global_Line_Width,iTodayFirstBarIndex,iYesterdayFirstBarIndex);
      if (ShowPivots) 
      {
         SetPriceLevel("noYR1",yr1,Pivot_R_Color,Pivot_Daily_Style,Global_Line_Width,iTodayFirstBarIndex,iYesterdayFirstBarIndex);
         SetPriceLevel("noYR2",yr2,Pivot_R_Color,Pivot_Daily_Style,Global_Line_Width,iTodayFirstBarIndex,iYesterdayFirstBarIndex);
         SetPriceLevel("noYR3",yr3,Pivot_R_Color,Pivot_Daily_Style,Global_Line_Width,iTodayFirstBarIndex,iYesterdayFirstBarIndex);
         SetPriceLevel("noYS1",ys1,Pivot_S_Color,Pivot_Daily_Style,Global_Line_Width,iTodayFirstBarIndex,iYesterdayFirstBarIndex);
         SetPriceLevel("noYS2",ys2,Pivot_S_Color,Pivot_Daily_Style,Global_Line_Width,iTodayFirstBarIndex,iYesterdayFirstBarIndex);
         SetPriceLevel("noYS3",ys3,Pivot_S_Color,Pivot_Daily_Style,Global_Line_Width,iTodayFirstBarIndex,iYesterdayFirstBarIndex);
      }
//----
//---- Prior Days Pivot
//----
      pp=(dPriorPriorDayHigh + dPriorPriorDayLow + dPriorPriorDayClose)/3;
      pp=NormalizeDouble(pp,Digits);
      SetPriceLevel("noPPivot",pp,Pivot_Color,Pivot_Daily_Style,Global_Line_Width,iPriorDayFirstBarIndex,iYesterdayFirstBarIndex);
//----
//---- 50% of prior days range
//----
         f50=(dYesterdayLow+dYesterdayHigh)/2;
         f50=NormalizeDouble(f50,Digits);
//----
//---- Fibos of yesterday's range
//----
         fr3=p+q*1;
         fr3=NormalizeDouble(fr3,Digits);
         fr2=p+q*0.618;
         fr2=NormalizeDouble(fr2,Digits);
         fr1=p+q*0.382;
         fr1=NormalizeDouble(fr1,Digits);
         fs1=p-q*0.382;
         fs1=NormalizeDouble(fs1,Digits);
         fs2=p-q*0.618;
         fs2=NormalizeDouble(fs2,Digits);
         fs3=p-q*1;
         fs3=NormalizeDouble(fs3,Digits);
//----
//---- Fibos of prior days range
//----
         yfr3=yp+yq*1;
         yfr3=NormalizeDouble(yfr3,Digits);
         yfr2=yp+yq*0.618;
         yfr2=NormalizeDouble(yfr2,Digits);
         yfr1=yp+yq*0.382;
         yfr1=NormalizeDouble(yfr1,Digits);
         yfs1=yp-yq*0.382;
         yfs1=NormalizeDouble(yfs1,Digits);
         yfs2=yp-yq*0.618;
         yfs2=NormalizeDouble(yfs2,Digits);
         yfs3=yp-yq*1;
         yfs3=NormalizeDouble(yfs3,Digits);
//----
//---- Fibos of Last week's range
//----
         wfr3=wp+wq*1;
         wfr3=NormalizeDouble(wfr3,Digits);
         wfr2=wp+wq*0.618;
         wfr2=NormalizeDouble(wfr2,Digits);
         wfr1=wp+wq*0.382;
         wfr1=NormalizeDouble(wfr1,Digits);
         wfs1=wp-wq*0.382;
         wfs1=NormalizeDouble(wfs1,Digits);
         wfs2=wp-wq*0.618;
         wfs2=NormalizeDouble(wfs2,Digits);
         wfs3=wp-wq*1;
         wfs3=NormalizeDouble(wfs3,Digits);
//----
//---- Fibos of Last Month's range
//----
         mfr3=mp+mq*1;
         mfr3=NormalizeDouble(mfr3,Digits);
         mfr2=mp+mq*0.618;
         mfr2=NormalizeDouble(mfr2,Digits);
         mfr1=mp+mq*0.382;
         mfr1=NormalizeDouble(mfr1,Digits);
         mfs1=mp-mq*0.382;
         mfs1=NormalizeDouble(mfs1,Digits);
         mfs2=mp-mq*0.618;
         mfs2=NormalizeDouble(mfs2,Digits);
         mfs3=mp-mq*1;
         mfs3=NormalizeDouble(mfs3,Digits);

      if(ShowFibos)
      {
//---- Fibos .618,.5 and .382
         SetPriceLevel("no+61.8%",yfr2,Fibo_R_Color,Fibo_Daily_Style,Global_Line_Width,iTodayFirstBarIndex,iYesterdayFirstBarIndex);
         SetPriceLevel("no+38.2%",yfr1,Fibo_R_Color,Fibo_Daily_Style,Global_Line_Width,iTodayFirstBarIndex,iYesterdayFirstBarIndex);
         SetPriceLevel("no-38.2%",yfs1,Fibo_S_Color,Fibo_Daily_Style,Global_Line_Width,iTodayFirstBarIndex,iYesterdayFirstBarIndex);
         SetPriceLevel("no-61.8%",yfs2,Fibo_S_Color,Fibo_Daily_Style,Global_Line_Width,iTodayFirstBarIndex,iYesterdayFirstBarIndex);
         if(!ShowPivots) // Fibo 100 is the same a pivot R2,S2 so only show one or the other
            {
            SetPriceLevel("no+100%",yfr3,Fibo_R_Color,Fibo_Daily_Style,Global_Line_Width,iTodayFirstBarIndex,iYesterdayFirstBarIndex);
            SetPriceLevel("no-100%",yfs3,Fibo_S_Color,Fibo_Daily_Style,Global_Line_Width,iTodayFirstBarIndex,iYesterdayFirstBarIndex);
            }
      }
//----
//----- Moving Averages Lines
//----
         sma1  = iMA(NULL,PERIOD_D1,DailySMA1,0,MODE_SMA,PRICE_CLOSE,0);
         sma2  = iMA(NULL,PERIOD_D1,DailySMA2,0,MODE_SMA,PRICE_CLOSE,0);
         ysma1 = iMA(NULL,PERIOD_D1,DailySMA1,0,MODE_SMA,PRICE_CLOSE,1);
         ysma2 = iMA(NULL,PERIOD_D1,DailySMA2,0,MODE_SMA,PRICE_CLOSE,1);
         psma1 = iMA(NULL,PERIOD_D1,DailySMA1,0,MODE_SMA,PRICE_CLOSE,2);
         psma2 = iMA(NULL,PERIOD_D1,DailySMA2,0,MODE_SMA,PRICE_CLOSE,2);

      if (ShowDailySMA) 
      {
         SetPriceLevel(StringConcatenate("noY",DailySMA1," Day SMA"),ysma1,SMA1_Color,SMA1_Style,Global_Line_Width,iTodayFirstBarIndex,iYesterdayFirstBarIndex);
         SetPriceLevel(StringConcatenate("noY",DailySMA2," Day SMA"),ysma2,SMA2_Color,SMA2_Style,Global_Line_Width,iTodayFirstBarIndex,iYesterdayFirstBarIndex);
         SetPriceLevel(StringConcatenate("noP",DailySMA1," Day SMA"),psma1,SMA1_Color,SMA1_Style,Global_Line_Width,iPriorDayFirstBarIndex,iYesterdayFirstBarIndex);
         SetPriceLevel(StringConcatenate("noP",DailySMA2," Day SMA"),psma2,SMA2_Color,SMA2_Style,Global_Line_Width,iPriorDayFirstBarIndex,iYesterdayFirstBarIndex);
      }
   }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

void SetGlobalVars()
   {
//---- day starts
      GlobalName=Pair+"st";
      GlobalValue=tTodayStart;
      GlobalVariableSet(GlobalName,GlobalValue);
      GlobalName=Pair+"sy";
      GlobalValue=tYesterdayStart;
      GlobalVariableSet(GlobalName,GlobalValue);
      GlobalName=Pair+"sp";
      GlobalValue=tPriorDayStart;
      GlobalVariableSet(GlobalName,GlobalValue);

//---- High,Low,Open
      GlobalName=Pair+"yh";
      GlobalValue=dYesterdayHigh;
      GlobalVariableSet(GlobalName,GlobalValue);
      GlobalName=Pair+"to";
      GlobalValue=dTodayOpen;
      GlobalVariableSet(GlobalName,GlobalValue);
      GlobalName=Pair+"yl";
      GlobalValue=dYesterdayLow;
      GlobalVariableSet(GlobalName,GlobalValue);
//---- pivot
      GlobalName=Pair+"p";
      GlobalValue=p;
      GlobalVariableSet(GlobalName,GlobalValue);
      GlobalName=Pair+"r1";
      GlobalValue=r1;
      GlobalVariableSet(GlobalName,GlobalValue);
      GlobalName=Pair+"r2";
      GlobalValue=r2;
      GlobalVariableSet(GlobalName,GlobalValue);
      GlobalName=Pair+"r3";
      GlobalValue=r3;
      GlobalVariableSet(GlobalName,GlobalValue);
      GlobalName=Pair+"s1";
      GlobalValue=s1;
      GlobalVariableSet(GlobalName,GlobalValue);
      GlobalName=Pair+"s2";
      GlobalValue=s2;
      GlobalVariableSet(GlobalName,GlobalValue);
      GlobalName=Pair+"s3";
      GlobalValue=s3;
      GlobalVariableSet(GlobalName,GlobalValue);
//---- Mid points
      GlobalName=Pair+"pmr3";
      GlobalValue=(r2+r3)/2;
      GlobalValue=NormalizeDouble(GlobalValue,Digits);
      GlobalVariableSet(GlobalName,GlobalValue);
      GlobalName=Pair+"pmr2";
      GlobalValue=(r1+r2)/2;
      GlobalValue=NormalizeDouble(GlobalValue,Digits);
      GlobalVariableSet(GlobalName,GlobalValue);
      GlobalName=Pair+"pmr1";
      GlobalValue=(p+r1)/2;
      GlobalValue=NormalizeDouble(GlobalValue,Digits);
      GlobalVariableSet(GlobalName,GlobalValue);
      GlobalName=Pair+"pms1";
      GlobalValue=(p+s1)/2;
      GlobalValue=NormalizeDouble(GlobalValue,Digits);
      GlobalVariableSet(GlobalName,GlobalValue);
      GlobalName=Pair+"pms2";
      GlobalValue=(s1+s2)/2;
      GlobalValue=NormalizeDouble(GlobalValue,Digits);
      GlobalVariableSet(GlobalName,GlobalValue);
      GlobalName=Pair+"pms3";
      GlobalValue=(s2+s3)/2;
      GlobalValue=NormalizeDouble(GlobalValue,Digits);
      GlobalVariableSet(GlobalName,GlobalValue);
//----50%
      GlobalName=Pair+"f50";
      GlobalValue=f50;
      GlobalValue=NormalizeDouble(GlobalValue,Digits);
      GlobalVariableSet(GlobalName,GlobalValue);
//---- Fibos
      GlobalName=Pair+"fr3";
      GlobalValue=fr3;
      GlobalValue=NormalizeDouble(GlobalValue,Digits);
      GlobalVariableSet(GlobalName,GlobalValue);

      GlobalName=Pair+"fr2";
      GlobalValue=fr2;
      GlobalValue=NormalizeDouble(GlobalValue,Digits);
      GlobalVariableSet(GlobalName,GlobalValue);

      GlobalName=Pair+"fr1";
      GlobalValue=fr1;
      GlobalValue=NormalizeDouble(GlobalValue,Digits);
      GlobalVariableSet(GlobalName,GlobalValue);

      GlobalName=Pair+"fs1";
      GlobalValue=fs1;
      GlobalValue=NormalizeDouble(GlobalValue,Digits);
      GlobalVariableSet(GlobalName,GlobalValue);

      GlobalName=Pair+"fs2";
      GlobalValue=fs2;
      GlobalValue=NormalizeDouble(GlobalValue,Digits);
      GlobalVariableSet(GlobalName,GlobalValue);

      GlobalName=Pair+"fs3";
      GlobalValue=fs3;
      GlobalValue=NormalizeDouble(GlobalValue,Digits);
      GlobalVariableSet(GlobalName,GlobalValue);
//---- Mid points
      GlobalName=Pair+"fmr3";
      GlobalValue=(fr2+fr3)/2;
      GlobalValue=NormalizeDouble(GlobalValue,Digits);
      GlobalVariableSet(GlobalName,GlobalValue);
      GlobalName=Pair+"fmr2";
      GlobalValue=(fr1+fr2)/2;
      GlobalValue=NormalizeDouble(GlobalValue,Digits);
      GlobalVariableSet(GlobalName,GlobalValue);
      GlobalName=Pair+"fmr1";
      GlobalValue=(p+fr1)/2;
      GlobalValue=NormalizeDouble(GlobalValue,Digits);
      GlobalVariableSet(GlobalName,GlobalValue);
      GlobalName=Pair+"fms1";
      GlobalValue=(p+fs1)/2;
      GlobalValue=NormalizeDouble(GlobalValue,Digits);
      GlobalVariableSet(GlobalName,GlobalValue);
      GlobalName=Pair+"fms2";
      GlobalValue=(fs1+fs2)/2;
      GlobalValue=NormalizeDouble(GlobalValue,Digits);
      GlobalVariableSet(GlobalName,GlobalValue);
      GlobalName=Pair+"fms3";
      GlobalValue=(fs2+fs3)/2;
      GlobalValue=NormalizeDouble(GlobalValue,Digits);
      GlobalVariableSet(GlobalName,GlobalValue);
//---- SweetSpots
      GlobalName=Pair+"lss";
      GlobalValue=lss;
      GlobalVariableSet(GlobalName,GlobalValue);
      GlobalName=Pair+"uss";
      GlobalValue=uss;
      GlobalVariableSet(GlobalName,GlobalValue);
      GlobalName=Pair+"mss";
      GlobalValue=mss;
      GlobalVariableSet(GlobalName,GlobalValue);
//---- MA
      GlobalName=Pair+"sma1";
      GlobalValue=sma1;
      GlobalValue=NormalizeDouble(GlobalValue,Digits);
      GlobalVariableSet(GlobalName,GlobalValue);
      GlobalName=Pair+"sma2";
      GlobalValue=sma2;
      GlobalValue=NormalizeDouble(GlobalValue,Digits);
      GlobalVariableSet(GlobalName,GlobalValue);
   }
//+------------------------------------------------------------------+
//|   Create or move a horizontal line and label                     |
//+------------------------------------------------------------------+
void SetPriceLevel(string text,double level,color col1,int style,int thickness,int startindx,int endindx)
   {
      string labelname = "[Tigertron] " + text + " Label",
             linename  = "[Tigertron] " + text + " Line",
             pricelabel;
      datetime start = Time[startindx],
               end = Time[endindx];      

//---- create or move the horizontal line   
      if (ObjectFind(linename)!=0) 
         {
         ObjectCreate(linename,OBJ_TREND,0,start,level,end,level);
         ObjectSet(linename,OBJPROP_STYLE,style);
         ObjectSet(linename,OBJPROP_COLOR,col1);
         ObjectSet(linename,OBJPROP_WIDTH,thickness);
         ObjectSet(linename,OBJPROP_BACK,false);
         ObjectSet(linename,OBJPROP_RAY,false);
         }
      else 
         {
         ObjectMove(linename,1,end,level);
         ObjectMove(linename,0,start,level);
         }

//---- put a label on the line   
      if (ObjectFind(labelname)!=0) 
         {
         ObjectCreate(labelname,OBJ_TEXT,0,Time[0],level);
         ObjectSet(labelname,OBJPROP_BACK,false);
         }
      else 
         ObjectMove(labelname,0,Time[0],level);

//---- if prefixed with "no" do not put a label
      if (StringSubstr( text,0,2)=="no")
         {
         pricelabel= " ";
         ObjectSetText(labelname,pricelabel,0,"",CLR_NONE);
         return;
         }
      pricelabel= " " + text;
      if (ShowPrices)
         pricelabel= pricelabel + ": "+DoubleToStr(level,Digits);
      ObjectSetText(labelname,pricelabel,FontSize,"Arial",col1);
   }

//+------------------------------------------------------------------+
//|   Create or move a vertical line and label                       |
//+------------------------------------------------------------------+
void SetTimeLine(string objname,string text,int idx,color col1,double vleveltext)
   {
      string name = "[Tigertron] " + objname;
      int    x    = Time[idx];

      if (ObjectFind(name)!=0)
         ObjectCreate(name,OBJ_TREND,0,x,0,x,100);
      else 
         {
         ObjectMove(name,0,x,0);
         ObjectMove(name,1,x,100);
         }
      ObjectSet(name,OBJPROP_STYLE,STYLE_DOT);
      ObjectSet(name,OBJPROP_COLOR,col1);
      if (ObjectFind(name + " Label")!=0)
         ObjectCreate(name + " Label",OBJ_TEXT,0,x,vleveltext);
      else
         ObjectMove(name + " Label",0,x,vleveltext);
      ObjectSetText(name + " Label",text,VertFontSize,"Arial",col1);
      ObjectSet(name + " Label",OBJPROP_ANGLE,-90.0);
   }

//+------------------------------------------------------------------+
//|   Create the daily candlestick at the zero bar                   |
//+------------------------------------------------------------------+
void DrawDayBar(string objname,double high,double low,double open,double close,int index)
   {
   //if(index == iTodayFirstBarIndex) index = -3;
      double   top   = open,
               bot   = close;
      datetime bodystarttime = Time[index],
               wickstarttime = Time[index+1],
               wickendtime   = Time[index+2],
               bodyendtime   = Time[index+3];
      color    col1  = Dn_Candle;
//      datetime time  = Time[0];
//      int      width = tBar;
      string   name  = "[Tigertron] Body"+objname;
      if(open <= close)
         {
         bot = open;
         top = close;
         col1 = Up_Candle;
         }

      if(high<top)high=top;
      if(low>bot)low=bot;
      if (ObjectFind(name)!=0)
         ObjectCreate(name,OBJ_RECTANGLE,0,bodystarttime,top,bodyendtime,bot);
      else 
         {
         ObjectMove(name,0,bodystarttime,top);
         ObjectMove(name,1,bodyendtime,bot);
         }
      ObjectSet(name,OBJPROP_COLOR,col1);
      ObjectSet(name,OBJPROP_BACK,true);

      name = "[Tigertron] Upper"+objname;
      if (ObjectFind(name)!=0)
         ObjectCreate(name,OBJ_RECTANGLE,0,wickstarttime,high,wickendtime,top);
      else 
         {
         ObjectMove(name,0,wickstarttime,high);
         ObjectMove(name,1,wickendtime,top);
         }
      ObjectSet(name,OBJPROP_COLOR,col1);

      name = "[Tigertron] Lower"+objname;
      
      if (ObjectFind(name)!=0)
         ObjectCreate(name,OBJ_RECTANGLE,0,wickstarttime,bot,wickendtime,low);
      else 
         {
         ObjectMove(name,0,wickstarttime,bot);
         ObjectMove(name,1,wickendtime,low);
         }
      ObjectSet(name,OBJPROP_COLOR,col1);
   }
//+------------------------------------------------------------------+
//|                             End                                  |
//+------------------------------------------------------------------+


