//+------------------------------------------------------------------+
//|                                           Graf_Analysis_V2.1.mq5 |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property version   "1.00"
#property description "Gann angles, grids, channels, S/R and Fibonacci levels"

//--- the indicator draws objects only; the empty plot avoids a compiler warning
#property indicator_buffers 1
#property indicator_plots   1
#property indicator_type1   DRAW_NONE
#property indicator_color1  clrNONE

//--- all objects share this prefix and are rebuilt on every tick
#define PR_PREFIX "G_L_"

//--- visibility mask for objects drawn on M1 ... H1 (MQL4 value 31 plus the
//--- intraday timeframes that exist only in MQL5)
#define TF_UP_TO_H1 (OBJ_PERIOD_M1|OBJ_PERIOD_M2|OBJ_PERIOD_M3|OBJ_PERIOD_M4|OBJ_PERIOD_M5|OBJ_PERIOD_M6|OBJ_PERIOD_M10|OBJ_PERIOD_M12|OBJ_PERIOD_M15|OBJ_PERIOD_M20|OBJ_PERIOD_M30|OBJ_PERIOD_H1)

//+------------------------------------------------------------------+
//| Inputs                                                           |
//+------------------------------------------------------------------+
input group "Manual Gann scale"
input double pH1        = 0;  // Gann scale on H1 (0 - automatic)
input double pH4        = 0;  // Gann scale on H4, current week (0 - auto)
input double pH4W_1     = 0;  // Gann scale on H4, previous week (0 - auto)
input double pH4W_2     = 0;  // Gann scale on H4, 2 weeks ago (0 - auto)

input group "Weekly angle scale for H1"
input bool   H4_H1      = false; // Show H4 weekly angles on H1

input group "GMT offset"
input int    DiffTime   = 0;  // Terminal time minus UTC

input group "Session open times"
input int    SessionAsia    = 2;   // Asian session start (UTC)
input int    SessionEurope  = 7;   // European session start (UTC)
input int    SessionAmerica = 13;  // American session start (UTC)

input group "Angles"
input bool   NextWeek          = true;   // True - move Saturday to Monday
input bool   NextDay           = false;  // True - shift one day forward
input bool   ExtDay            = false;  // True - extend the day line by one day
input bool   FromShadow        = false;  // Yes - from candle extremes, No - from bodies
input bool   Line              = false;  // Angles of today and yesterday
input bool   Day_minmax        = true;   // Min/max of the previous day
input bool   Wik_op            = true;   // Current week angle
input bool   Wik_minmax        = false;  // Min/max of the current week
input bool   Wik_1_op          = true;   // Previous week angle
input bool   Wik_1_minmax      = true;   // Min/max of the previous week
input bool   Wik_2_minmax      = false;  // Min/max 2 weeks ago
input bool   Wik_3_minmax      = false;  // Min/max 3 weeks ago

input group "Grids"
input bool   Grid_standard         = true;   // Standard grid layout
input bool   Grid_today            = false;  // Current day grid
input bool   Grid_yesterday        = false;  // Previous day grid
input bool   Grid_double_H_1       = false;  // Doubled grid on H1
input bool   Grid_double_H_4       = false;  // Doubled grid on H4
input bool   Day_before_yesterday  = false;  // Yes - day before and yesterday, No - yesterday and today
input bool   Last_week             = false;  // Yes - shift one week back

input group "Fibonacci selection"
input int             FiboSelect = 1;             // 0-none, 1-Fibo, 2-Gann
input color           FiboColor  = clrBlack;      // Ruler color
input ENUM_LINE_STYLE FiboStyle  = STYLE_DASH;    // Ruler line style
input int             FiboWight  = 0;             // Ruler line width

input group "Channels"
input bool Opening_yesterday = false; // Channel at yesterday's open
input bool High_Low          = false; // Channel at yesterday's min/max

input group "Channels up"
input color           ChColorOpenUp = clrForestGreen; // Up channel color
input color           ChColorMax    = clrFireBrick;   // Up channel color
input ENUM_LINE_STYLE ChStyleMax    = STYLE_DASH;     // Channel line style
input int             ChWightMax    = 1;              // Channel line width

input group "Channels down"
input color           ChColorOpenDown = clrDeepPink;  // Down channel color
input color           ChColorMin      = clrIndigo;    // Down channel color
input ENUM_LINE_STYLE ChStyleMin      = STYLE_DASH;   // Channel line style
input int             ChWightMin      = 1;            // Channel line width

input group "S/R min/max"
input bool  S_R      = false;
input color R_Color  = clrBlue;  // Resistance color
input color S_Color  = clrRed;   // Support color

input group "Resistance"
input ENUM_LINE_STYLE S_R_Max_Style = STYLE_SOLID;  // Resistance style
input int             S_R_Max_Wight = 1;            // Resistance width

input group "Support"
input ENUM_LINE_STYLE S_R_Min_Style = STYLE_SOLID;  // Support style
input int             S_R_Min_Wight = 1;            // Support width

input group "S/R Fibonacci"
input bool            S_R_Fibo       = false;
input color           Op_Day_Color   = clrDarkGoldenrod; // Day open color
input color           R_Fibo_Color   = clrRoyalBlue;     // Resistance color
input color           S_Fibo_Color   = clrDeepPink;      // Support color
input ENUM_LINE_STYLE S_R_Fibo_Style = STYLE_DOT;        // Style
input int             S_R_Fibo_Width = 1;                // Width
input bool            S_R_Fibo_Ray   = false;            // Ray

//+------------------------------------------------------------------+
//| Fibonacci / Gann level tables                                    |
//+------------------------------------------------------------------+
double FiboLevel[] =
  {
   -4.000, -3.618, -3.382, -3.236, -3.000, -2.618, -2.382, -2.236,
   -2.000, -1.618, -1.382, -1.236, -1.000, -0.618, -0.382, -0.236,
    0.000,  0.118,  0.236,  0.382,  0.500,  0.618,  0.764,  0.882,
    1.000,  1.236,  1.382,  1.618,  2.000,  2.236,  2.382,  2.618,
    3.000,  3.236,  3.382,  3.618,  4.000,  4.236,  4.382,  4.618,
    5.000, 100000
  };

string FiboDescription[] =
  {
   " -500  %$",   "  -461.8 %$", "  -438.2  %$", "  -423.6  %$",
   " -400  %$",   " -361.8  %$", " -338.2  %$",  " -323.6  %$",
   " -300  %$",   " -261.8  %$", " -238.2  %$",  " -223.6  %$",
   " -200  %$",   " -161.8  %$", " -138.2  %$",  " -123.6 %$",
   " 0---100  %$", " 11.8---88.2  %$", " 23.6---76.4  %$", " 38.2---61.8  %$",
   " 50  %$",     " 61.8---38.2  %$", " 76.4---23.6  %$", " 88.2---11.8  %$",
   " 100---0  %$", " 123.6  %$", " 138.2  %$",    " 161.8  %$",
   " 200  %$",    " 223.6  %$",  " 238.2  %$",    " 261.8  %$",
   " 300  %$",    " 323.6  %$",  " 338.2  %$",    " 361.8  %$",
   " 400  %$",    " 423.6  %$",  " 438.2  %$",    " 461.8  %$",
   " 500  %$"
  };

double FiboGann[] =
  {
   -1.750, -1.666, -1.500, -1.333, -1.250, -1.125, -1.000, -0.875,
   -0.750, -0.666, -0.500, -0.375, -0.333, -0.250, -0.125,  0.000,
    0.125,  0.250,  0.333,  0.375,  0.500,  0.666,  0.750,  0.875,
    1.000,  1.125,  1.250,  1.333,  1.500,  1.666,  1.750, 1000000
  };

string FiboGannDescription[] =
  {
   " -175 %$",   "  -166 %$",   "  -150 %$",  "  -133 %$",
   "  -125 %$",  "  -112.5 %$", "  -1 %$",     "  -87.5 %$",
   "  -75 %$",   "  -66.6 %$",  "  -50 %$",    "  -37.5 %$",
   "  -33.3 %$", "  -25 %$",    "  -12.5 %$",  "  0 %$",
   "  12.5 %$",  "  25 %$",     "  33.3 %$",   "  37.5 %$",
   "  50 %$",    "  66.6 %$",   "  75 %$",     "  87.5 %$",
   "  1 %$",     "  112.5 %$",  "  125 %$",    "  133 %$",
   "  150 %$",   "  166 %$",    "  175 %$"
  };

//+------------------------------------------------------------------+
//| Globals                                                          |
//+------------------------------------------------------------------+
long     DT     = 0;        // GMT offset in seconds
string   Pr     = PR_PREFIX;
datetime TimeD0 = 0;        // start of the current day

datetime g_time[];
double   g_open[], g_high[], g_low[], g_close[];
datetime g_d1time[];

double   g_dummy[];   // unused plot buffer

//+------------------------------------------------------------------+
//| Helpers                                                          |
//+------------------------------------------------------------------+
int DayOfWeek(const datetime t)
{
   MqlDateTime st;
   TimeToStruct(t, st);
   return st.day_of_week;
}

//--- MQL4 iBarShift(exact=false): shift of the bar that contains t
int BarShiftArr(const datetime &arr[], const datetime t)
{
   int n = ArraySize(arr);
   if(n <= 0)  return -1;
   if(t >= arr[0])   return 0;
   if(t <= arr[n - 1]) return n - 1;

   int lo = 0, hi = n - 1;
   while(lo < hi)
   {
      int mid = (lo + hi) / 2;
      if(arr[mid] > t) lo = mid + 1;
      else             hi = mid;
   }
   return lo;
}

int BarShift(const datetime t)
{
   return BarShiftArr(g_time, t);
}

//--- clamped close access (MQL4 silently returned 0 outside the series)
double CloseAt(const int index)
{
   int n = ArraySize(g_close);
   if(n <= 0) return(0.0);
   int i = index;
   if(i < 0)  i = 0;
   if(i >= n) i = n - 1;
   return(g_close[i]);
}

//--- MQL4 iHighest / iLowest over the chart series
int HighestShift(const int count, const int start)
{
   int n = ArraySize(g_high);
   int best = -1;
   double bestVal = 0.0;
   if(count <= 0 || start < 0) return -1;

   for(int k = 0; k < count; k++)
   {
      int idx = start + k;
      if(idx >= n) break;
      if(idx < 0)  continue;
      if(best < 0 || g_high[idx] > bestVal)
      {
         bestVal = g_high[idx];
         best = idx;
      }
   }
   return best;
}

int LowestShift(const int count, const int start)
{
   int n = ArraySize(g_low);
   int best = -1;
   double bestVal = 0.0;
   if(count <= 0 || start < 0) return -1;

   for(int k = 0; k < count; k++)
   {
      int idx = start + k;
      if(idx >= n) break;
      if(idx < 0)  continue;
      if(best < 0 || g_low[idx] < bestVal)
      {
         bestVal = g_low[idx];
         best = idx;
      }
   }
   return best;
}

//+------------------------------------------------------------------+
//| OnInit                                                           |
//+------------------------------------------------------------------+
int OnInit()
{
   SetIndexBuffer(0, g_dummy, INDICATOR_DATA);

   ArraySetAsSeries(g_time,    true);
   ArraySetAsSeries(g_open,    true);
   ArraySetAsSeries(g_high,    true);
   ArraySetAsSeries(g_low,     true);
   ArraySetAsSeries(g_close,   true);
   ArraySetAsSeries(g_d1time,  true);

   return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| OnDeinit                                                         |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   Delete_My_Obj(Pr);
   Comment("");
   ChartRedraw(0);
}

//+------------------------------------------------------------------+
//| OnCalculate                                                      |
//+------------------------------------------------------------------+
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[])
{
   if(rates_total < 1440 / (int)_Period) return(0);

   int n = rates_total;
   if(CopyTime (_Symbol, _Period, 0, n, g_time)  != n) return(0);
   if(CopyOpen (_Symbol, _Period, 0, n, g_open)  != n) return(0);
   if(CopyHigh (_Symbol, _Period, 0, n, g_high)  != n) return(0);
   if(CopyLow  (_Symbol, _Period, 0, n, g_low)   != n) return(0);
   if(CopyClose(_Symbol, _Period, 0, n, g_close) != n) return(0);

   if(CopyTime(_Symbol, PERIOD_D1, 0, 64, g_d1time) <= 0) return(0);

   //--- GMT offset
   DT = (long)DiffTime * 3600;

   //--- start of the current day
   int d1shift = BarShiftArr(g_d1time, g_time[0]);
   if(d1shift < 0) return(0);
   TimeD0 = (datetime)(g_d1time[d1shift] + DT);

   if(NextWeek)
   {
      if(DayOfWeek(TimeD0) == 6)
      {
         int lw = DayOfWeek(TimeLocal());
         if(lw == 0 || lw == 1 || lw == 6)
            TimeD0 = (datetime)(TimeD0 + 86400 * 3);
      }
   }
   if(DayOfWeek(TimeD0) == 0) TimeD0 = (datetime)(TimeD0 + 86400);
   if(NextDay)                TimeD0 = (datetime)(TimeD0 + 86400);

   Delete_My_Obj(Pr);
   Comment("");

   if((int)_Period <= (int)PERIOD_H1) WriteGH1();
   if((int)_Period == (int)PERIOD_H4) WriteGH4();

   return(rates_total);
}

//+------------------------------------------------------------------+
//| H1 and below                                                     |
//+------------------------------------------------------------------+
int WriteGH1()
{
   datetime TimeD00, TimeD000, TimeD1, TimeD2;
   datetime TimeN0, TimeN1, TimeN2, TimeN3, TimeN4;
   int minD1, maxD1, minD2, maxD2;
   int minN0, maxN0, minN1, maxN1, minN2, maxN2, minN3, maxN3, minN4, maxN4;
   int i, j;
   double Pr1 = 0, Pr2 = 0, Pr3 = 0;
   double MD = 0, MYD = 0, MW = 0, MW_1 = 0, MW_2 = 0;
   double rez, h, r;

   //--- the original modified the grid switches inside the function,
   //--- so keep writable copies of the inputs
   bool gridStandard       = Grid_standard;
   bool gridToday          = Grid_today;
   bool gridYesterday      = Grid_yesterday;
   bool gridDoubleH1       = Grid_double_H_1;
   bool dayBeforeYesterday = Day_before_yesterday;

   //--- start of the previous day
   TimeD1 = (datetime)(TimeD0 - 86400);
   if(DayOfWeek(TimeD0) == 0) TimeD1 = (datetime)(TimeD0 - 86400 * 2);
   if(DayOfWeek(TimeD0) == 1) TimeD1 = (datetime)(TimeD0 - 86400 * 3);

   //--- start of the day before yesterday
   TimeD2 = (datetime)(TimeD1 - 86400);

   //--- start of tomorrow
   TimeD00 = (datetime)(TimeD0 + 86400);

   //--- start of tomorrow or the day after tomorrow
   if(ExtDay == false) TimeD000 = TimeD00;
   else                TimeD000 = (datetime)(TimeD00 + 86400);

   //--- start of the current week
   int D = DayOfWeek(TimeD0);
   TimeN0 = (datetime)(TimeD0 - (D - 1) * 86400);

   TimeN1 = (datetime)(TimeN0 - 86400 * 7);
   TimeN2 = (datetime)(TimeN1 - 86400 * 7);
   TimeN3 = (datetime)(TimeN2 - 86400 * 7);
   TimeN4 = (datetime)(TimeN3 - 86400 * 7);

   //--- min/max of the previous day
   int BarD0 = BarShift((datetime)(TimeD0 + DT));
   int BarD1 = BarShift((datetime)(TimeD1 + DT));
   if(BarD0 < 0 || BarD1 < 0) return(0);

   i = BarD1 - BarD0;
   maxD1 = HighestShift(i, BarD0 + 1);
   minD1 = LowestShift (i, BarD0 + 1);

   int BarD2 = BarShift((datetime)(TimeD2 + DT));
   i = BarD2 - BarD1;
   maxD2 = HighestShift(i, BarD1 + 1);
   minD2 = LowestShift (i, BarD1 + 1);

   //--- min/max of the current week
   int BarN0 = BarShift((datetime)(TimeN0 + DT));
   j = BarShift((datetime)(TimeD00 + DT));
   if(BarN0 < 0 || j < 0) return(0);

   i = BarN0 - j;
   maxN0 = HighestShift(i, j + 1);
   minN0 = LowestShift (i, j + 1);

   //--- min/max of the previous week
   int BarN1 = BarShift((datetime)(TimeN1 + DT));
   j = BarShift((datetime)(TimeN0 + DT));
   i = BarN1 - j;
   maxN1 = HighestShift(i, j + 1);
   minN1 = LowestShift (i, j + 1);

   //--- min/max of 2 weeks ago
   int BarN2 = BarShift((datetime)(TimeN2 + DT));
   j = BarShift((datetime)(TimeN1 + DT));
   i = BarN2 - j;
   maxN2 = HighestShift(i, j + 1);
   minN2 = LowestShift (i, j + 1);

   //--- min/max of 3 weeks ago
   int BarN3 = BarShift((datetime)(TimeN3 + DT));
   j = BarShift((datetime)(TimeN2 + DT));
   i = BarN3 - j;
   maxN3 = HighestShift(i, j + 1);
   minN3 = LowestShift (i, j + 1);

   //--- min/max of 4 weeks ago (kept exactly as in the original)
   int BarN4 = BarShift((datetime)(TimeN4 + DT));
   j = BarShift((datetime)(TimeN3 + DT));
   i = BarN3 - j;
   maxN4 = HighestShift(i, j + 1);
   minN4 = LowestShift (i, j + 1);

   if(maxD1 < 0 || minD1 < 0 || maxN0 < 0 || minN0 < 0 ||
      maxN1 < 0 || minN1 < 0)
      return(0);

   //--- weekly start points taken from the candle shadows
   if(g_close[BarN1] > g_open[BarN1]) Pr2 = g_high[BarN1];
   else                               Pr2 = g_low[BarN1];

   if(g_close[BarN0] > g_open[BarN0]) Pr1 = g_high[BarN0];
   else                               Pr1 = g_low[BarN0];

   int period = (int)_Period;

   if(pH1 == 0)
   {
      h = MathAbs(NormalizeDouble(g_high[maxD1], 4) - NormalizeDouble(g_low[minD1], 4)) / _Point;
      r = MathPow(24, 2);
      rez = MathPow(h, 2);
      MD = MathSqrt(rez + r) / 100 * period / 60;
   }
   else MD = pH1;

   if(dayBeforeYesterday && maxD2 >= 0 && minD2 >= 0)
   {
      h = MathAbs(NormalizeDouble(g_high[maxD2], 4) - NormalizeDouble(g_low[minD2], 4)) / _Point;
      r = MathPow(24, 2);
      rez = MathPow(h, 2);
      MYD = MathSqrt(rez + r) / 100 * period / 60;
      MD = MYD;
   }

   if(pH4 == 0)
   {
      if(H4_H1)
      {
         h = MathAbs(NormalizeDouble(g_high[maxN1], 4) - NormalizeDouble(g_low[minN1], 4)) / _Point;
         r = MathPow(120, 2);
         rez = MathPow(h, 2);
         MW = MathSqrt(rez + r) / 100 * period / 240;
      }
      else
      {
         MW = MD;
      }
   }
   else MW = pH4 / 4;

   if(pH4W_1 == 0)
   {
      if(maxN2 >= 0 && minN2 >= 0)
      {
         h = MathAbs(NormalizeDouble(g_high[maxN2], 4) - NormalizeDouble(g_low[minN2], 4)) / _Point;
         r = MathPow(120, 2);
         rez = MathPow(h, 2);
         MW_1 = MathSqrt(rez + r) / 100 * period / 240;
      }
      else MW_1 = MW;
   }
   else MW_1 = pH4W_1 / 4;

   if(pH4W_2 == 0)
   {
      if(maxN3 >= 0 && minN3 >= 0)
      {
         h = MathAbs(NormalizeDouble(g_high[maxN3], 4) - NormalizeDouble(g_low[minN3], 4)) / _Point;
         r = MathPow(120, 2);
         rez = MathPow(h, 2);
         MW_2 = MathSqrt(rez + r) / 100 * period / 240;
      }
      else MW_2 = MW_1;
   }
   else MW_2 = pH4W_2 / 4;

   Comment("Graf_Analysis V2.1", "\n", "\n",
           "Scale          = ", DoubleToString(NormalizeDouble(MD, 4), 4), "\n");

   //--- time lines
   VLine("Asia",    (datetime)(TimeD0 + 60 * 60 * SessionAsia),    clrDodgerBlue, STYLE_DOT, 1, TF_UP_TO_H1);
   VLine("Europe",  (datetime)(TimeD0 + 60 * 60 * SessionEurope),  clrDodgerBlue, STYLE_DOT, 1, TF_UP_TO_H1);
   VLine("America", (datetime)(TimeD0 + 60 * 60 * SessionAmerica), clrDodgerBlue, STYLE_DOT, 1, TF_UP_TO_H1);
   VLine("Day start",              TimeD0,                        clrMaroon, STYLE_DOT, 1, TF_UP_TO_H1);
   VLine("Day end",                TimeD00,                       clrMaroon, STYLE_DOT, 1, TF_UP_TO_H1);
   VLine("Next day end",           (datetime)(TimeD00 + 86400),   clrMaroon, STYLE_DOT, 1, TF_UP_TO_H1);
   VLine("Week start",             TimeN0,                        clrRed,    STYLE_DOT, 1, TF_UP_TO_H1);
   VLine("Prev week start",        TimeN1,                        clrRed,    STYLE_DOT, 1, TF_UP_TO_H1);

   if(FromShadow == false)
   {
      Pr2 = CloseAt(BarN1 + 1);
      Pr1 = CloseAt(BarN0 + 1);
   }

   //--- angle of the current week
   if(Wik_op)
      GannLine("Current week angle", "Line", TimeN0, Pr1, TimeD00, MW, clrTurquoise, 0, 1, TF_UP_TO_H1);

   //--- min/max of the current week
   if(Wik_minmax)
   {
      GannLine("Current week max", "Line", g_time[maxN0], g_high[maxN0], TimeD00, MW, clrDeepPink, 1, 1, TF_UP_TO_H1);
      GannLine("Current week min", "Line", g_time[minN0], g_low[minN0],  TimeD00, MW, clrDeepPink, 1, 1, TF_UP_TO_H1);
   }

   //--- angle of the previous week
   if(Wik_1_op)
      GannLine("Prev week angle", "Line", TimeN1, Pr2, TimeD00, MW, clrBlack, 0, 1, TF_UP_TO_H1);

   //--- min/max of the previous week
   if(Wik_1_minmax)
   {
      GannLine("Prev week max", "Line", g_time[maxN1], g_high[maxN1], TimeD00, MW, clrGoldenrod, 1, 1, TF_UP_TO_H1);
      GannLine("Prev week min", "Line", g_time[minN1], g_low[minN1],  TimeD00, MW, clrGoldenrod, 1, 1, TF_UP_TO_H1);
   }

   //--- min/max of 2 weeks ago
   if(Wik_2_minmax && maxN2 >= 0 && minN2 >= 0)
   {
      GannLine("2 weeks ago max", "Line", g_time[maxN2], g_high[maxN2], TimeD00, MW_1, clrWhite, 1, 1, TF_UP_TO_H1);
      GannLine("2 weeks ago min", "Line", g_time[minN2], g_low[minN2],  TimeD00, MW_1, clrWhite, 1, 1, TF_UP_TO_H1);
   }

   //--- min/max of 3 weeks ago
   if(Wik_3_minmax && maxN3 >= 0 && minN3 >= 0)
   {
      GannLine("3 weeks ago max", "Line", g_time[maxN3], g_high[maxN3], TimeD00, MW_2, clrSaddleBrown, 1, 1, TF_UP_TO_H1);
      GannLine("3 weeks ago min", "Line", g_time[minN3], g_low[minN3],  TimeD00, MW_2, clrSaddleBrown, 1, 1, TF_UP_TO_H1);
   }

   //--- yesterday's angle
   if(FromShadow == true)
   {
      if(g_close[BarD2] > g_open[BarD2]) Pr3 = g_high[BarD2];
      else                               Pr3 = g_low[BarD2];

      if(g_close[BarD1] > g_open[BarD1]) Pr2 = g_high[BarD1];
      else                               Pr2 = g_low[BarD1];

      if(g_close[BarD0] > g_open[BarD0]) Pr1 = g_high[BarD0];
      else                               Pr1 = g_low[BarD0];
   }
   else
   {
      Pr3 = CloseAt(BarD2 + 1);
      Pr2 = CloseAt(BarD1 + 1);
      Pr1 = CloseAt(BarD0 + 1);
   }

   //--- standard grids
   if(gridStandard)
   {
      gridToday          = false;
      gridYesterday      = false;
      gridDoubleH1       = false;
      dayBeforeYesterday = false;

      GannLine("Yesterday angle grid", "Grid", TimeD1, Pr2, TimeD00, MD, clrMaroon, 0, 1, TF_UP_TO_H1);
      GannLine("Today angle grid",     "Grid", TimeD0, Pr1, TimeD00, MD, clrNavy,   0, 1, TF_UP_TO_H1);
   }

   //--- angle of the previous day
   if(Line)
      GannLine("Yesterday angle", "Line", TimeD1, Pr2, TimeD00, MD, clrMaroon, 0, 1, TF_UP_TO_H1);

   //--- angle of the current day
   if(Line)
      GannLine("Today angle", "Line", TimeD0, Pr1, TimeD000, MD, clrNavy, 0, 1, TF_UP_TO_H1);

   if(dayBeforeYesterday && maxD2 >= 0 && minD2 >= 0) //--- yesterday and the day before
   {
      if(gridDoubleH1)
      {
         if(gridYesterday) GannLine("Day before yesterday angle grid", "Grid", TimeD2, Pr3, TimeD0,  MD, clrMaroon, 0, 2, TF_UP_TO_H1);
         if(gridToday)     GannLine("Yesterday angle grid",             "Grid", TimeD1, Pr1, TimeD00, MD, clrNavy,   0, 2, TF_UP_TO_H1);
      }
      else
      {
         if(gridYesterday) GannLine("Day before yesterday angle grid", "Grid", TimeD2, Pr3, TimeD1, MD, clrMaroon, 0, 1, TF_UP_TO_H1);
         if(gridToday)     GannLine("Yesterday angle grid",             "Grid", TimeD1, Pr2, TimeD0, MD, clrNavy,   0, 1, TF_UP_TO_H1);
      }
   }
   else //--- yesterday and today
   {
      if(gridDoubleH1)
      {
         if(gridYesterday) GannLine("Yesterday angle grid", "Grid", TimeD1, Pr2, TimeD00,          MD, clrMaroon, 0, 2, TF_UP_TO_H1);
         if(gridToday)     GannLine("Today angle grid",     "Grid", TimeD0, Pr1, (datetime)(TimeD00 + 86400), MD, clrNavy, 0, 2, TF_UP_TO_H1);
      }
      else
      {
         if(gridYesterday) GannLine("Yesterday angle grid", "Grid", TimeD1, Pr2, TimeD0,  MD, clrMaroon, 0, 1, TF_UP_TO_H1);
         if(gridToday)     GannLine("Today angle grid",     "Grid", TimeD0, Pr1, TimeD00, MD, clrNavy,   0, 1, TF_UP_TO_H1);
      }
   }

   //--- min/max of the previous day
   if(Day_minmax)
   {
      if(dayBeforeYesterday && maxD2 >= 0 && minD2 >= 0)
      {
         GannLine("Day before yesterday max", "Line", g_time[maxD2], g_high[maxD2], TimeD0,  MD, clrLime, 1, 1, TF_UP_TO_H1);
         GannLine("Day before yesterday min", "Line", g_time[minD2], g_low[minD2],  TimeD0,  MD, clrLime, 1, 1, TF_UP_TO_H1);
      }
      else
      {
         GannLine("Yesterday max", "Line", g_time[maxD1], g_high[maxD1], TimeD00, MD, clrLime, 1, 1, TF_UP_TO_H1);
         GannLine("Yesterday min", "Line", g_time[minD1], g_low[minD1],  TimeD00, MD, clrLime, 1, 1, TF_UP_TO_H1);
      }
   }

   //--- fibo channels
   if(High_Low)
   {
      Channel("Yesterday high-low channel +", g_time[maxD1], g_high[maxD1],
              g_time[minD1], g_low[minD1],  MD, TF_UP_TO_H1, ChColorMax, ChStyleMax, ChWightMax);
      Channel("Yesterday high-low channel -", g_time[maxD1], g_high[maxD1],
              g_time[minD1], g_low[minD1], -MD, TF_UP_TO_H1, ChColorMin, ChStyleMin, ChWightMin);
   }

   if(Opening_yesterday)
   {
      Channel("Yesterday open channel +", TimeD1, Pr2,
              (datetime)(TimeD0 + 86400 * 3), Pr2,  MD, TF_UP_TO_H1, ChColorOpenUp, ChStyleMax, ChWightMax);
      Channel("Yesterday open channel -", TimeD1, Pr2,
              (datetime)(TimeD0 + 86400 * 3), Pr2, -MD, TF_UP_TO_H1, ChColorOpenDown, ChStyleMin, ChWightMin);
   }

   //--- resistance and support
   if(S_R)
   {
      HLineSR("Prev week max", g_high[maxN1], S_R_Max_Style, S_R_Max_Wight, TF_UP_TO_H1);
      HLineSR("Prev week min", g_low[minN1],  S_R_Min_Style, S_R_Min_Wight, TF_UP_TO_H1);

      if(maxN2 >= 0 && minN2 >= 0)
      {
         HLineSR("2 weeks ago max", g_high[maxN2], S_R_Max_Style, S_R_Max_Wight, TF_UP_TO_H1);
         HLineSR("2 weeks ago min", g_low[minN2],  S_R_Min_Style, S_R_Min_Wight, TF_UP_TO_H1);
      }

      if(maxN3 >= 0 && minN3 >= 0)
      {
         HLineSR("3 weeks ago max", g_high[maxN3], S_R_Max_Style, S_R_Max_Wight, TF_UP_TO_H1);
         HLineSR("3 weeks ago min", g_low[minN3],  S_R_Min_Style, S_R_Min_Wight, TF_UP_TO_H1);
      }

      if(maxN4 >= 0 && minN4 >= 0)
      {
         HLineSR("4 weeks ago max", g_high[maxN4], S_R_Max_Style, S_R_Max_Wight, TF_UP_TO_H1);
         HLineSR("4 weeks ago min", g_low[minN4],  S_R_Min_Style, S_R_Min_Wight, TF_UP_TO_H1);
      }
   }

   //--- daily fibo S/R
   if(S_R_Fibo)
   {
      double P = g_open[BarD0];
      Fibo_S_R("Day open", P, TimeD0, P, TimeD00, TF_UP_TO_H1);
      Fibo_S_R("R_1", P, TimeD0, P + 13  * _Point * 10, TimeD00, TF_UP_TO_H1);
      Fibo_S_R("S_1", P, TimeD0, P - 13  * _Point * 10, TimeD00, TF_UP_TO_H1);
      Fibo_S_R("R_2", P, TimeD0, P + 21  * _Point * 10, TimeD00, TF_UP_TO_H1);
      Fibo_S_R("S_2", P, TimeD0, P - 21  * _Point * 10, TimeD00, TF_UP_TO_H1);
      Fibo_S_R("R_3", P, TimeD0, P + 34  * _Point * 10, TimeD00, TF_UP_TO_H1);
      Fibo_S_R("S_3", P, TimeD0, P - 34  * _Point * 10, TimeD00, TF_UP_TO_H1);
      Fibo_S_R("R_4", P, TimeD0, P + 55  * _Point * 10, TimeD00, TF_UP_TO_H1);
      Fibo_S_R("S_4", P, TimeD0, P - 55  * _Point * 10, TimeD00, TF_UP_TO_H1);
      Fibo_S_R("R_5", P, TimeD0, P + 89  * _Point * 10, TimeD00, TF_UP_TO_H1);
      Fibo_S_R("S_5", P, TimeD0, P - 89  * _Point * 10, TimeD00, TF_UP_TO_H1);
      Fibo_S_R("R_6", P, TimeD0, P + 144 * _Point * 10, TimeD00, TF_UP_TO_H1);
      Fibo_S_R("S_6", P, TimeD0, P - 144 * _Point * 10, TimeD00, TF_UP_TO_H1);
   }

   //--- fibo levels from the previous day min/max
   if(FiboSelect == 1)
   {
      Fibo("Fibo", FiboLevel, FiboDescription,
           g_time[maxD1], g_high[maxD1], g_time[minD1], g_low[minD1],
           FiboColor, FiboStyle, FiboWight, TF_UP_TO_H1);
   }
   if(FiboSelect == 2)
   {
      Fibo("Gann", FiboGann, FiboGannDescription,
           g_time[maxD1], g_high[maxD1], g_time[minD1], g_low[minD1],
           FiboColor, FiboStyle, FiboWight, TF_UP_TO_H1);
   }

   ChartRedraw(0);

   return(0);
}

//+------------------------------------------------------------------+
//| H4                                                               |
//+------------------------------------------------------------------+
int WriteGH4()
{
   datetime TimeD00, TimeN00, TimeN0, TimeN1, TimeN2;
   int minN1, maxN1, minN2, maxN2;
   int i, j;
   double Pr1 = 0, Pr2 = 0, Pr3 = 0;
   double MW = 0, MV, rez, h, r;

   //--- start of tomorrow
   if(ExtDay == false) TimeD00 = (datetime)(TimeD0 + 86400);
   else                TimeD00 = (datetime)(TimeD0 + 86400 * 2);

   //--- start of the current week
   int D = DayOfWeek(TimeD0);
   TimeN0 = (datetime)(TimeD0 - (D - 1) * 86400);

   TimeN1 = (datetime)(TimeN0 - 86400 * 7);
   TimeN2 = (datetime)(TimeN1 - 86400 * 7);

   //--- start of the next week
   TimeN00 = (datetime)(TimeN0 + 86400 * 5);

   //--- min/max of the previous week
   int BarN0 = BarShift((datetime)(TimeN0 + DT));
   int BarN1 = BarShift((datetime)(TimeN1 + DT));
   if(BarN0 < 0 || BarN1 < 0) return(0);

   j = BarShift((datetime)(TimeN0 + DT));
   i = BarN1 - j;
   maxN1 = HighestShift(i, j + 1);
   minN1 = LowestShift (i, j + 1);

   int BarN2 = BarShift((datetime)(TimeN2 + DT));
   j = BarShift((datetime)(TimeN1 + DT));
   i = BarN2 - j;
   maxN2 = HighestShift(i, j + 1);
   minN2 = LowestShift (i, j + 1);

   if(maxN1 < 0 || minN1 < 0) return(0);

   //--- start points
   if(g_close[BarN1] > g_open[BarN1]) Pr2 = g_high[BarN1];
   else                               Pr2 = g_low[BarN1];

   if(g_close[BarN0] > g_open[BarN0]) Pr1 = g_high[BarN0];
   else                               Pr1 = g_low[BarN0];

   int period = (int)_Period;

   if(pH4 == 0)
   {
      h = MathAbs(NormalizeDouble(g_high[maxN1], 4) - NormalizeDouble(g_low[minN1], 4)) / _Point;
      r = MathPow(120, 2);
      rez = MathPow(h, 2);
      MW = MathSqrt(rez + r) / 100 * period / 240;
   }
   else MW = pH4;

   if(Last_week && maxN2 >= 0 && minN2 >= 0)
   {
      h = MathAbs(NormalizeDouble(g_high[maxN2], 4) - NormalizeDouble(g_low[minN2], 4)) / _Point;
      r = MathPow(120, 2);
      rez = MathPow(h, 2);
      MV = MathSqrt(rez + r) / 100 * period / 240;
      MW = MV;
   }

   Comment("Graf_Analysis V2.1", "\n", "\n",
           "Prev week scale = ", DoubleToString(NormalizeDouble(MW, 4), 4), "\n");

   //--- time lines
   VLine("Week end",        TimeN00, clrDodgerBlue, STYLE_DOT, 1, OBJ_PERIOD_H4);
   VLine("Week start",      TimeN0,  clrDodgerBlue, STYLE_DOT, 1, OBJ_PERIOD_H4);
   VLine("Prev week start", TimeN1,  clrDodgerBlue, STYLE_DOT, 1, OBJ_PERIOD_H4);
   VLine("Day start",       TimeD0,  clrPaleGreen,  STYLE_DOT, 1, OBJ_PERIOD_H4);
   VLine("Day end",         TimeD00, clrPaleGreen,  STYLE_DOT, 1, OBJ_PERIOD_H4);

   //--- start points from the candle bodies
   if(FromShadow == false)
   {
      Pr1 = CloseAt(BarN0 + 1);
      Pr2 = CloseAt(BarN1 + 1);
      Pr3 = CloseAt(BarN2 + 1);
   }

   if(Last_week && maxN2 >= 0 && minN2 >= 0)
   {
      if(Grid_double_H_4)
      {
         GannLine("2 weeks ago angle grid", "Grid", TimeN2, Pr3, (datetime)(TimeN1 + 86400 * 5), MW, clrMaroon, 0, 2, OBJ_PERIOD_H4);
         GannLine("Prev week angle grid",   "Grid", TimeN1, Pr2, TimeN0,                         MW, clrNavy,   0, 1, OBJ_PERIOD_H4);
      }
      else
      {
         GannLine("2 weeks ago angle grid", "Grid", TimeN2, Pr3, TimeN1, MW, clrMaroon, 0, 1, OBJ_PERIOD_H4);
         GannLine("Prev week angle grid",   "Grid", TimeN1, Pr2, TimeN0, MW, clrNavy,   0, 1, OBJ_PERIOD_H4);
      }
   }
   else
   {
      if(Grid_double_H_4)
      {
         GannLine("Prev week angle grid",   "Grid", TimeN1, Pr2, (datetime)(TimeN0 + 86400 * 5), MW, clrMaroon, 0, 2, OBJ_PERIOD_H4);
         GannLine("Current week angle grid", "Grid", TimeN0, Pr1, TimeN00,                       MW, clrNavy,   0, 1, OBJ_PERIOD_H4);
      }
      else
      {
         GannLine("Prev week angle grid",   "Grid", TimeN1, Pr2, TimeN0,  MW, clrMaroon, 0, 1, OBJ_PERIOD_H4);
         GannLine("Current week angle grid", "Grid", TimeN0, Pr1, TimeN00, MW, clrNavy,   0, 1, OBJ_PERIOD_H4);
      }
   }

   //--- min/max of the previous week
   if(Last_week && maxN2 >= 0 && minN2 >= 0)
   {
      GannLine("2 weeks ago max", "Line", g_time[maxN2], g_high[maxN2], TimeN0,  MW, clrGoldenrod, 0, 1, OBJ_PERIOD_H4);
      GannLine("2 weeks ago min", "Line", g_time[minN2], g_low[minN2],  TimeN0,  MW, clrGoldenrod, 0, 1, OBJ_PERIOD_H4);
   }
   else
   {
      GannLine("Prev week max", "Line", g_time[maxN1], g_high[maxN1], TimeN00, MW, clrGoldenrod, 0, 1, OBJ_PERIOD_H4);
      GannLine("Prev week min", "Line", g_time[minN1], g_low[minN1],  TimeN00, MW, clrGoldenrod, 0, 1, OBJ_PERIOD_H4);
   }

   //--- fibo channels
   if(High_Low)
   {
      Channel("Yesterday high-low channel +", g_time[maxN1], g_high[maxN1],
              g_time[minN1], g_low[minN1],  MW, OBJ_PERIOD_H4, ChColorMax, ChStyleMax, ChWightMax);
      Channel("Yesterday high-low channel -", g_time[maxN1], g_high[maxN1],
              g_time[minN1], g_low[minN1], -MW, OBJ_PERIOD_H4, ChColorMin, ChStyleMin, ChWightMin);
   }

   if(Opening_yesterday)
   {
      Channel("Yesterday open channel +", TimeN1, Pr2,
              (datetime)(TimeN0 + 86400 * 7), Pr2,  MW, OBJ_PERIOD_H4, ChColorOpenUp, ChStyleMax, ChWightMax);
      Channel("Yesterday open channel -", TimeN1, Pr2,
              (datetime)(TimeN0 + 86400 * 7), Pr2, -MW, OBJ_PERIOD_H4, ChColorOpenDown, ChStyleMin, ChWightMin);
   }

   //--- fibo levels from the previous week min/max
   if(FiboSelect == 1)
   {
      Fibo("Fibo", FiboLevel, FiboDescription,
           g_time[maxN1], g_high[maxN1], g_time[minN1], g_low[minN1],
           FiboColor, FiboStyle, FiboWight, OBJ_PERIOD_H4);
   }
   if(FiboSelect == 2)
   {
      Fibo("Gann", FiboGann, FiboGannDescription,
           g_time[maxN1], g_high[maxN1], g_time[minN1], g_low[minN1],
           FiboColor, FiboStyle, FiboWight, OBJ_PERIOD_H4);
   }

   ChartRedraw(0);

   return(0);
}

//+------------------------------------------------------------------+
//| Gann line / grid pair                                            |
//+------------------------------------------------------------------+
void GannLine(const string name, const string type,
              const datetime x1, const double y1,
              const datetime x2, const double scaleValue,
              const color clr, const int style, const int width, const int period)
{
   string nmP = Pr + name + "+";
   string nmM = Pr + name + "-";

   //--- the second anchor is placed on the scale line, so the terminal
   //--- derives the same angle whether it uses the scale or both points
   int    bars = BarShift(x1) - BarShift(x2);
   double y2p  = y1 + scaleValue * _Point * (double)bars;
   double y2m  = y1 - scaleValue * _Point * (double)bars;

   if(type == "Line") ObjectCreate(0, nmP, OBJ_GANNLINE, 0, x1, y1, x2, y2p);
   else               ObjectCreate(0, nmP, OBJ_GANNGRID, 0, x1, y1, x2, y2p);

   ObjectSetInteger(0, nmP, OBJPROP_COLOR, clr);
   ObjectSetInteger(0, nmP, OBJPROP_STYLE, style);
   ObjectSetInteger(0, nmP, OBJPROP_WIDTH, width);
   ObjectSetInteger(0, nmP, OBJPROP_TIMEFRAMES, period);
   ObjectSetDouble (0, nmP, OBJPROP_SCALE, scaleValue);

   if(type == "Line") ObjectCreate(0, nmM, OBJ_GANNLINE, 0, x1, y1, x2, y2m);
   else               ObjectCreate(0, nmM, OBJ_GANNGRID, 0, x1, y1, x2, y2m);

   ObjectSetInteger(0, nmM, OBJPROP_COLOR, clr);
   ObjectSetInteger(0, nmM, OBJPROP_STYLE, style);
   ObjectSetInteger(0, nmM, OBJPROP_WIDTH, width);
   ObjectSetInteger(0, nmM, OBJPROP_TIMEFRAMES, period);
   ObjectSetDouble (0, nmM, OBJPROP_SCALE, -scaleValue);
}

//+------------------------------------------------------------------+
//| Fibo channel                                                     |
//+------------------------------------------------------------------+
void Channel(const string name, const datetime x1, const double y1,
             const datetime x2, const double y2, const double scaleValue,
             const int period, const color chColor, const int chStyle, const int chWidth)
{
   int BarN0 = BarShift(x1);
   double y3 = y1 - ((double)BarN0) * scaleValue * _Point;
   string nm = Pr + name;

   ObjectCreate(0, nm, OBJ_FIBOCHANNEL, 0, x1, y1, g_time[0], y3, x2, y2);

   ObjectSetInteger(0, nm, OBJPROP_LEVELCOLOR, chColor);
   ObjectSetInteger(0, nm, OBJPROP_LEVELSTYLE, chStyle);
   ObjectSetInteger(0, nm, OBJPROP_LEVELWIDTH, chWidth);
   ObjectSetInteger(0, nm, OBJPROP_TIMEFRAMES, period);
   ObjectSetInteger(0, nm, OBJPROP_LEVELS, 20);

   double levels[21] =
     {
      0.0,  0.5,  1.0,  1.5,  2.0,  2.5,  3.0,  3.5,  4.0,  4.5,  5.0,
     -0.5, -1.0, -1.5, -2.0, -2.5, -3.0, -3.5, -4.0, -4.5, -5.0
     };

   for(int k = 0; k < 21; k++)
      ObjectSetDouble(0, nm, OBJPROP_LEVELVALUE, k, levels[k]);
}

//+------------------------------------------------------------------+
//| Vertical line                                                    |
//+------------------------------------------------------------------+
void VLine(const string name, const datetime x1,
           const color clr, const int style, const int back, const int period)
{
   string nm = Pr + name;

   ObjectCreate(0, nm, OBJ_VLINE, 0, x1, 0.0);

   ObjectSetInteger(0, nm, OBJPROP_COLOR, clr);
   ObjectSetInteger(0, nm, OBJPROP_STYLE, style);
   ObjectSetInteger(0, nm, OBJPROP_BACK, back);
   ObjectSetInteger(0, nm, OBJPROP_TIMEFRAMES, period);
}

//+------------------------------------------------------------------+
//| Horizontal line (above price - resistance, below - support)      |
//+------------------------------------------------------------------+
void HLineSR(const string name, const double y1,
             const int style, const int width, const int period)
{
   color clr = (y1 >= SymbolInfoDouble(_Symbol, SYMBOL_BID)) ? R_Color : S_Color;
   string nm = Pr + name;

   ObjectCreate(0, nm, OBJ_HLINE, 0, 0, y1);

   ObjectSetInteger(0, nm, OBJPROP_COLOR, clr);
   ObjectSetInteger(0, nm, OBJPROP_STYLE, style);
   ObjectSetInteger(0, nm, OBJPROP_WIDTH, width);
   ObjectSetInteger(0, nm, OBJPROP_TIMEFRAMES, period);
}

//+------------------------------------------------------------------+
//| Fibo object: positive and negative levels                        |
//+------------------------------------------------------------------+
void Fibo(const string name, double &fi_[], string &fitxt_[],
          const datetime x1, const double y1,
          const datetime x2, const double y2,
          const color clr, const int style, const int width, const int period)
{
   int j = 0, m = 0, p = 0;

   while(fi_[j] < 100)
   {
      if(fi_[j] >= 0) p++;
      else            m++;
      j++;
   }

   string nmP = Pr + name + "p";
   string nmM = Pr + name + "m";

   ObjectCreate(0, nmP, OBJ_FIBO, 0, x1, y1, x2, y2);
   ObjectSetInteger(0, nmP, OBJPROP_LEVELS, p);

   ObjectCreate(0, nmM, OBJ_FIBO, 0, x1, y1, x2, y2);
   ObjectSetInteger(0, nmM, OBJPROP_LEVELS, m);

   m = 0;
   p = 0;

   //--- the level colour has to be written per level index: MT5 keeps its own
   //--- default colours for levels that are not explicitly recoloured
   for(int i = 0; i < j; i++)
   {
      if(fi_[i] >= 0)
      {
         ObjectSetDouble (0, nmP, OBJPROP_LEVELVALUE, p, fi_[i]);
         ObjectSetString (0, nmP, OBJPROP_LEVELTEXT,  p, fitxt_[i]);
         ObjectSetInteger(0, nmP, OBJPROP_LEVELCOLOR, p, clrGoldenrod);
         ObjectSetInteger(0, nmP, OBJPROP_LEVELSTYLE, p, style);
         ObjectSetInteger(0, nmP, OBJPROP_LEVELWIDTH, p, width);
         p++;
      }
      else
      {
         ObjectSetDouble (0, nmM, OBJPROP_LEVELVALUE, m, fi_[i]);
         ObjectSetString (0, nmM, OBJPROP_LEVELTEXT,  m, fitxt_[i]);
         ObjectSetInteger(0, nmM, OBJPROP_LEVELCOLOR, m, clrGoldenrod);
         ObjectSetInteger(0, nmM, OBJPROP_LEVELSTYLE, m, style);
         ObjectSetInteger(0, nmM, OBJPROP_LEVELWIDTH, m, width);
         m++;
      }
   }

   //--- OBJPROP_COLOR paints the main line only
   ObjectSetInteger(0, nmP, OBJPROP_COLOR, clr);
   ObjectSetInteger(0, nmP, OBJPROP_TIMEFRAMES, period);

   ObjectSetInteger(0, nmM, OBJPROP_COLOR, clr);
   ObjectSetInteger(0, nmM, OBJPROP_TIMEFRAMES, period);
}

//+------------------------------------------------------------------+
//| Daily fibo S/R level                                             |
//+------------------------------------------------------------------+
void Fibo_S_R(const string name, const double y1, const datetime x1,
              const double y2, const datetime x2, const int period)
{
   color clr = Op_Day_Color;
   if(y1 < y2) clr = R_Fibo_Color;
   if(y1 > y2) clr = S_Fibo_Color;

   string nm = Pr + name;

   ObjectCreate(0, nm, OBJ_TREND, 0, x1, y2, x2, y2);

   ObjectSetInteger(0, nm, OBJPROP_COLOR, clr);
   ObjectSetInteger(0, nm, OBJPROP_STYLE, S_R_Fibo_Style);
   ObjectSetInteger(0, nm, OBJPROP_WIDTH, S_R_Fibo_Width);
   ObjectSetInteger(0, nm, OBJPROP_RAY, S_R_Fibo_Ray);
   ObjectSetInteger(0, nm, OBJPROP_TIMEFRAMES, period);
}

//+------------------------------------------------------------------+
//| Delete all objects of this indicator                             |
//+------------------------------------------------------------------+
void Delete_My_Obj(const string prefix)
{
   int total = ObjectsTotal(0, -1, -1);

   for(int k = total - 1; k >= 0; k--)
   {
      string nm = ObjectName(0, k, -1, -1);
      if(StringSubstr(nm, 0, StringLen(prefix)) == prefix)
         ObjectDelete(0, nm);
   }
}
//+------------------------------------------------------------------+
