Donchian Fibo MTF - slow and freeze

 
The follow indicator was modified to works in MTF
But it makes the graphic slow and freeze
Any body have a ideia how to avoid it ?




#property copyright "Copyright © 2015"
//---- indicator version number
#property version   "1.00"
//---- drawing the indicator in the main window
#property indicator_chart_window 
//---- number of indicator buffers
#property indicator_buffers 14
//---- 7 plots are used
#property indicator_plots   7
//+-----------------------------------+
//| Parameters of indicator drawing   |
//+-----------------------------------+
//Escalas Fibo: 1, .786, .618, .5, .382, .214, .0
//---- drawing of the indicator as a line
#property indicator_type1   DRAW_LINE
//---- use olive color for the indicator line
#property indicator_color1 Blue
//---- indicator line is a solid curve
#property indicator_style1  STYLE_SOLID
//---- indicator line width is equal to 1
#property indicator_width1  2
//---- indicator label display
#property indicator_label1  "Fibo 1.000"
//----
//---- drawing of the indicator as a line
#property indicator_type2   DRAW_LINE
//---- use gray color for the indicator line
#property indicator_color2 DeepSkyBlue
//---- indicator line is a solid curve
#property indicator_style2  STYLE_SOLID
//---- indicator line width is equal to 1
#property indicator_width2  1
//---- indicator label display
#property indicator_label2  "Fibo 0.764"
//----
//---- drawing of the indicator as a line
#property indicator_type3   DRAW_LINE
//---- use pale violet red color for the indicator line
#property indicator_color3 Aqua
//---- indicator line is a solid curve
#property indicator_style3  STYLE_SOLID
//---- indicator line width is equal to 1
#property indicator_width3  1
//---- indicator label display
#property indicator_label3  "Fibo 0.618"
//----
//---- drawing of the indicator as a line
#property indicator_type4   DRAW_LINE
//---- use pale violet red color for the indicator line
#property indicator_color4 White
//---- indicator line is a solid curve
#property indicator_style4  STYLE_SOLID
//---- indicator line width is equal to 1
#property indicator_width4  2
//---- indicator label display
#property indicator_label4  "Fibo 0.500"
//----
//---- drawing of the indicator as a line
#property indicator_type5   DRAW_LINE
//---- use pale violet red color for the indicator line
#property indicator_color5 Yellow
//---- indicator line is a solid curve
#property indicator_style5  STYLE_SOLID
//---- indicator line width is equal to 1
#property indicator_width5  1
//---- indicator label display
#property indicator_label5  "Fibo 0.382"
//----
//---- drawing of the indicator as a line
#property indicator_type6   DRAW_LINE
//---- use pale violet red color for the indicator line
#property indicator_color6 Orange
//---- indicator line is a solid curve
#property indicator_style6  STYLE_SOLID
//---- indicator line width is equal to 1
#property indicator_width6  1
//---- indicator label display
#property indicator_label6  "Fibo 0.236"
//----
//---- drawing of the indicator as a line
#property indicator_type7   DRAW_LINE
//---- use pale violet red color for the indicator line
#property indicator_color7 Red
//---- indicator line is a solid curve
#property indicator_style7  STYLE_SOLID
//---- indicator line width is equal to 1
#property indicator_width7  2
//---- indicator label display
#property indicator_label7  "Fibo 0.000"
//+-----------------------------------+
//| Enumeration declaration           |
//+-----------------------------------+
enum Applied_Extrem //type of extreme points
  {
   HIGH_LOW,
   HIGH_LOW_OPEN,
   HIGH_LOW_CLOSE,
   OPEN_HIGH_LOW,
   CLOSE_HIGH_LOW
  };
//+-----------------------------------+
//| Input parameters of the indicator |
//+-----------------------------------+
input ENUM_TIMEFRAMES TimeFrame   =  PERIOD_CURRENT;  //  Time Frame:
input int             FiboPeriod  =  72;              //  Period of averaging:
input Applied_Extrem  Extremes    =  HIGH_LOW;        //  Type of extreme points:
input int             Margins     = -2;               //  Margins:
input int             Shift       =  0;               //  Horizontal shift of the indicator in bars:
input color           ColorLabel  =  clrBlack;        //  Text color:
input string          NameLabels  = "Arial Black";    //  Font name:
input int             SizeLabels  =  9;               //  Font size:
string                IndID       = "Donchian_Fibo";

//---- indicator buffers
double Buffer1000[];
double Buffer0786[];
double Buffer0618[];
double Buffer0500[];
double Buffer0382[];
double Buffer0214[];
double Buffer0000[];

double Buffer10002[];
double Buffer07862[];
double Buffer06182[];
double Buffer05002[];
double Buffer03822[];
double Buffer02142[];
double Buffer00002[];

double Low[];
double High[];
double Open[];
double Close[];

datetime curbar;
datetime lastbar;
bool     update=true;

ENUM_TIMEFRAMES   TF;

//————————————————————————————|

void OnDeinit(const int reason)
{
   objDelete(IndID);
   Comment("");
}
//+------------------------------------------------------------------+
//| Searching index of the highest bar                               |
//+------------------------------------------------------------------+
int iHighests(
             const double &array[],   // array for searching for maximum element index
             int count,               // the number of the array elements (from a current bar to the index descending), 
             // along which the searching must be performed.
             int startPos             // the initial bar index (shift relative to a current bar), 
             // the search for the greatest value begins from
             )
  {
//----
   int index=startPos;
//----checking correctness of the initial index
   if(startPos<0)
     {
      Print("Bad value in the function iHighest, startPos = ",startPos);
      return(0);
     }
//---- checking correctness of startPos value
   if(startPos-count<0)
      count=startPos;
//---
   double max=array[startPos];
//---- searching for an index
   for(int i=startPos; i>startPos-count; i--)
     {
      if(array[i]>max)
        {
         index=i;
         max=array[i];
        }
     }
//---- returning of the greatest bar index
   return(index);
  }
//+------------------------------------------------------------------+
//| Searching index of the lowest bar                                |
//+------------------------------------------------------------------+
int iLowests(
            const double &array[],// array for searching for minimum element index
            int count,// the number of the array elements (from a current bar to the index descending), 
            // along which the searching must be performed.
            int startPos //the initial bar index (shift relative to a current bar), 
            // the search for the lowest value begins from
            )
  {
   int index=startPos;
//----checking correctness of the initial index
   if(startPos<0)
     {
      Print("Bad value in the function iLowest, startPos = ",startPos);
      return(0);
     }
//---- checking correctness of startPos value
   if(startPos-count<0)
      count=startPos;
//---
   double min=array[startPos];
//---- searching for an index
   for(int i=startPos; i>startPos-count; i--)
     {
      if(array[i]<min)
        {
         index=i;
         min=array[i];
        }
     }
//---- returning of the lowest bar index
   return(index);
  }
//+------------------------------------------------------------------+    
//| Donchian Channel indicator initialization function               | 
//+------------------------------------------------------------------+  
void OnInit()
{
//---- turning a dynamic array into an indicator buffer
   SetIndexBuffer(0,Buffer1000,INDICATOR_DATA);
//---- shifting the indicator 1 horizontally by AroonShift
   PlotIndexSetInteger(0,PLOT_SHIFT,Shift);
//---- shifting the start of drawing of the indicator 1
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,FiboPeriod-1);
//--- create label to display in DataWindow
   PlotIndexSetString(0,PLOT_LABEL,"Fibo 1.000");
//---- setting values of the indicator that won't be visible on the chart
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,EMPTY_VALUE);
//----
//---- turning a dynamic array into an indicator buffer
   SetIndexBuffer(1,Buffer0786,INDICATOR_DATA);
//---- shifting the indicator 2 horizontally
   PlotIndexSetInteger(1,PLOT_SHIFT,Shift);
//---- shifting the start of drawing of the indicator 2
   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,FiboPeriod-1);
//--- create label to display in DataWindow
   PlotIndexSetString(1,PLOT_LABEL,"Fibo 0.764");
//---- setting values of the indicator that won't be visible on the chart
   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,EMPTY_VALUE);
//----
//---- turning a dynamic array into an indicator buffer
   SetIndexBuffer(2,Buffer0618,INDICATOR_DATA);
//---- shifting the indicator 3 horizontally
   PlotIndexSetInteger(2,PLOT_SHIFT,Shift);
//---- shifting the start of drawing of the indicator 3
   PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,FiboPeriod-1);
//--- create label to display in DataWindow
   PlotIndexSetString(2,PLOT_LABEL,"Fibo 0.618");
//---- setting values of the indicator that won't be visible on the chart
   PlotIndexSetDouble(2,PLOT_EMPTY_VALUE,EMPTY_VALUE);
//----
//---- turning a dynamic array into an indicator buffer
   SetIndexBuffer(3,Buffer0500,INDICATOR_DATA);
//---- shifting the indicator 3 horizontally
   PlotIndexSetInteger(3,PLOT_SHIFT,Shift);
//---- shifting the start of drawing of the indicator 3
   PlotIndexSetInteger(3,PLOT_DRAW_BEGIN,FiboPeriod-1);
//--- create label to display in DataWindow
   PlotIndexSetString(3,PLOT_LABEL,"Fibo 0.500");
//---- setting values of the indicator that won't be visible on the chart
   PlotIndexSetDouble(3,PLOT_EMPTY_VALUE,EMPTY_VALUE);
//----
//---- turning a dynamic array into an indicator buffer
   SetIndexBuffer(4,Buffer0382,INDICATOR_DATA);
//---- shifting the indicator 3 horizontally
   PlotIndexSetInteger(4,PLOT_SHIFT,Shift);
//---- shifting the start of drawing of the indicator 3
   PlotIndexSetInteger(4,PLOT_DRAW_BEGIN,FiboPeriod-1);
//--- create label to display in DataWindow
   PlotIndexSetString(4,PLOT_LABEL,"Fibo 0.382");
//---- setting values of the indicator that won't be visible on the chart
   PlotIndexSetDouble(4,PLOT_EMPTY_VALUE,EMPTY_VALUE);
//----
//---- turning a dynamic array into an indicator buffer
   SetIndexBuffer(5,Buffer0214,INDICATOR_DATA);
//---- shifting the indicator 3 horizontally
   PlotIndexSetInteger(5,PLOT_SHIFT,Shift);
//---- shifting the start of drawing of the indicator 3
   PlotIndexSetInteger(5,PLOT_DRAW_BEGIN,FiboPeriod-1);
//--- create label to display in DataWindow
   PlotIndexSetString(5,PLOT_LABEL,"Fibo 0.236");
//---- setting values of the indicator that won't be visible on the chart
   PlotIndexSetDouble(5,PLOT_EMPTY_VALUE,EMPTY_VALUE);
//----
//---- turning a dynamic array into an indicator buffer
   SetIndexBuffer(6,Buffer0000,INDICATOR_DATA);
//---- shifting the indicator 3 horizontally
   PlotIndexSetInteger(6,PLOT_SHIFT,Shift);
//---- shifting the start of drawing of the indicator 3
   PlotIndexSetInteger(6,PLOT_DRAW_BEGIN,FiboPeriod-1);
//--- create label to display in DataWindow
   PlotIndexSetString(6,PLOT_LABEL,"Fibo 0.000");
//---- setting values of the indicator that won't be visible on the chart
   PlotIndexSetDouble(6,PLOT_EMPTY_VALUE,EMPTY_VALUE);
//----
   SetIndexBuffer(7,Buffer10002,INDICATOR_CALCULATIONS);
   SetIndexBuffer(8,Buffer07862,INDICATOR_CALCULATIONS);
   SetIndexBuffer(9,Buffer06182,INDICATOR_CALCULATIONS);
   SetIndexBuffer(10,Buffer05002,INDICATOR_CALCULATIONS);
   SetIndexBuffer(11,Buffer03822,INDICATOR_CALCULATIONS);
   SetIndexBuffer(12,Buffer02142,INDICATOR_CALCULATIONS);
   SetIndexBuffer(13,Buffer00002,INDICATOR_CALCULATIONS);
//----
   ArraySetAsSeries(Buffer1000,true);
   ArraySetAsSeries(Buffer0786,true);
   ArraySetAsSeries(Buffer0618,true);
   ArraySetAsSeries(Buffer0500,true);
   ArraySetAsSeries(Buffer0382,true);
   ArraySetAsSeries(Buffer0214,true);
   ArraySetAsSeries(Buffer0000,true);
//---- initialization of variable for indicator short name
   string shortname;
   StringConcatenate(shortname,"Donchian Fibo(Period = ",FiboPeriod,")");
//--- creation of the name to be displayed in a separate sub-window and in a pop up help
   IndicatorSetString(INDICATOR_SHORTNAME,shortname);
//--- determination of accuracy of displaying of the indicator values
   IndicatorSetInteger(INDICATOR_DIGITS,_Digits+1);
//---- end of initialization
}

//+------------------------------------------------------------------+  
//| Donchian Channel iteration function                              | 
//+------------------------------------------------------------------+

int OnCalculate(const int rates_total,    // amount of history in bars at the current tick
                const int prev_calculated,// amount of history in bars at the previous tick
                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[])
{
//---- checking the number of bars to be enough for the calculation
   if(rates_total<FiboPeriod+1) return(0);
//---- declaration of variables with a floating point  
   double smin=0,smax=0,sdiff=0;
//---- declaration of integer variables
   int first,bar;
//---- calculation of the starting number 'first' for the cycle of recalculation of bars
   if(prev_calculated==0) // checking for the first start of the indicator calculation
   {
      first=FiboPeriod;    // starting number for calculation of all bars
   }
   else
   {
      first=prev_calculated-1; // starting number for calculation of new bars
   }

//———————————————————————————————————————————————|

   TF =_Period<TimeFrame?TimeFrame:PERIOD_CURRENT;

//———————————————————————————————————————————————|

   ArrayResize(Low,rates_total);
   ArrayResize(High,rates_total);
   ArrayResize(Open,rates_total);
   ArrayResize(Close,rates_total);

   for(int i=rates_total-1; i>=0 && !IsStopped(); i--)
   {
      Low[i]= iLow(NULL,TF,rates_total-i-1);
      High[i]= iHigh(NULL,TF,rates_total-i-1);
      Open[i]= iOpen(NULL,TF,rates_total-i-1);
      Close[i]= iClose(NULL,TF,rates_total-i-1);
   }

//—————————————————————————————————————————————|

   ArraySetAsSeries(Buffer10002,false);
   ArraySetAsSeries(Buffer07862,false);
   ArraySetAsSeries(Buffer06182,false);
   ArraySetAsSeries(Buffer05002,false);
   ArraySetAsSeries(Buffer03822,false);
   ArraySetAsSeries(Buffer02142,false);
   ArraySetAsSeries(Buffer00002,false);

   for(bar=first; bar<rates_total; bar++)
   {
      switch(Extremes)
      {
         case HIGH_LOW:
            smax = High[iHighests(High,FiboPeriod,bar)];
            smin = Low[iLowests(Low,FiboPeriod,bar)];
            break;
            //---
         case HIGH_LOW_OPEN:
            smax = (Open[iHighests(Open,FiboPeriod,bar)]+High[iHighests(High,FiboPeriod,bar)])/2;
            smin = (Open[iLowests(Open,FiboPeriod,bar)]+Low[iLowests(Low,FiboPeriod,bar)])/2;
            break;
            //---
         case HIGH_LOW_CLOSE:
            smax = (Close[iHighests(Close,FiboPeriod,bar)]+High[iHighests(High,FiboPeriod,bar)])/2;
            smin = (Close[iLowests(Close,FiboPeriod,bar)]+Low[iLowests(Low,FiboPeriod,bar)])/2;
            break;
            //---
         case OPEN_HIGH_LOW:
            smax = Open[iHighests(Open,FiboPeriod,bar)];
            smin = Open[iLowests(Open,FiboPeriod,bar)];
            break;
            //---
         case CLOSE_HIGH_LOW:
            smax = Close[iHighests(Close,FiboPeriod,bar)];
            smin = Close[iLowests(Close,FiboPeriod,bar)];
            break;
      }

      sdiff=smax-smin;
      Buffer10002[bar] = smax;
      Buffer07862[bar] = smin + sdiff * .764;
      Buffer06182[bar] = smin + sdiff * .618;
      Buffer05002[bar] = smin + sdiff * .5;
      Buffer03822[bar] = smin + sdiff * .382;
      Buffer02142[bar] = smin + sdiff * .236;
      Buffer00002[bar] = smin;
   }

//—————————————————————————————————————|

   int limit = Bars(NULL,TF)-FiboPeriod;

   ArraySetAsSeries(time,true);

   ArraySetAsSeries(Buffer10002,true);
   ArraySetAsSeries(Buffer07862,true);
   ArraySetAsSeries(Buffer06182,true);
   ArraySetAsSeries(Buffer05002,true);
   ArraySetAsSeries(Buffer03822,true);
   ArraySetAsSeries(Buffer02142,true);
   ArraySetAsSeries(Buffer00002,true);

   ArraySetAsSeries(Buffer1000,true);
   ArraySetAsSeries(Buffer0786,true);
   ArraySetAsSeries(Buffer0618,true);
   ArraySetAsSeries(Buffer0500,true);
   ArraySetAsSeries(Buffer0382,true);
   ArraySetAsSeries(Buffer0214,true);
   ArraySetAsSeries(Buffer0000,true);

   for(int i=limit-1; i>=0; i--)
   {
      if(TF != PERIOD_CURRENT)
      {
         int y = GetBarShift(NULL,TF,time[i]);
         if(y<0) y=y+1;

         Buffer1000[i]= Buffer10002[y];
         Buffer0786[i]= Buffer07862[y];
         Buffer0618[i]= Buffer06182[y];
         Buffer0500[i]= Buffer05002[y];
         Buffer0382[i]= Buffer03822[y];
         Buffer0214[i]= Buffer02142[y];
         Buffer0000[i]= Buffer00002[y];
      }
      else
      {
         Buffer1000[i]= Buffer10002[i];
         Buffer0786[i]= Buffer07862[i];
         Buffer0618[i]= Buffer06182[i];
         Buffer0500[i]= Buffer05002[i];
         Buffer0382[i]= Buffer03822[i];
         Buffer0214[i]= Buffer02142[i];
         Buffer0000[i]= Buffer00002[i];
      }
   }

//———————————————————————————————————————————————————————————————————————————————————|

   Text("Fibo 1.000","100.0", time[0], Buffer1000[0], ColorLabel, ANCHOR_RIGHT_LOWER);
   Text("Fibo 0.764", "76.4", time[0], Buffer0786[0], ColorLabel, ANCHOR_RIGHT_LOWER);
   Text("Fibo 0.618", "61.8", time[0], Buffer0618[0], ColorLabel, ANCHOR_RIGHT_LOWER);
   Text("Fibo 0.500", "50.0", time[0], Buffer0500[0], ColorLabel, ANCHOR_RIGHT_LOWER);
   Text("Fibo 0.382", "38.2", time[0], Buffer0382[0], ColorLabel, ANCHOR_RIGHT_LOWER);
   Text("Fibo 0.236", "23.6", time[0], Buffer0214[0], ColorLabel, ANCHOR_RIGHT_LOWER);
   Text("Fibo 0.000", "0.0",  time[0], Buffer0000[0], ColorLabel, ANCHOR_RIGHT_LOWER);

//———————————————————————————————————————————————————————————————————————————————————|

   bool NB = new_bar(NULL,PERIOD_CURRENT);

   if(NB && update==true){update=false; ChartSetSymbolPeriod(0,NULL,0);}
   if(!NB && update==false){update=true;}

   ChartRedraw(0);

   return(rates_total);
}

//————————————————————————————————————————————————|

bool new_bar(string symbol,ENUM_TIMEFRAMES periods)
{
   curbar=(datetime)SeriesInfoInteger(symbol,periods,SERIES_LASTBAR_DATE);

   if(lastbar==0) lastbar=(datetime)SeriesInfoInteger(symbol,periods,SERIES_LASTBAR_DATE);
   if(lastbar!=curbar)
   {
      lastbar=curbar;
      return(true);
   }
   return(false);
}

//————————————————————————————————————————————————————————————————————————————————————————————|

int GetBarShift(const string symbol_name, const ENUM_TIMEFRAMES timeframe, const datetime time)
{
   int res=-1;
   datetime last_bar;
   if(SeriesInfoInteger(symbol_name,timeframe,SERIES_LASTBAR_DATE,last_bar))
   {
      if(time>last_bar) res=0;
      else
      {
         const int shift=Bars(symbol_name,timeframe,time,last_bar);
         if(shift>0) res=shift-1;
      }
   }
   return(res);
}

//—————————————————————————————————————————————————————————————————————————————————————|

void Text(string main, string text, datetime time, double price, color clrs, int anchor)
{
          string         name  =          (IndID+"_"+main);
   if(ObjectFind      (0,name)!=-1)   ObjectDelete(0,name);
   {
      ObjectCreate    (0,name,OBJ_TEXT,0,      time,price);
      ObjectSetString (0,name,OBJPROP_TEXT,          text);
      ObjectSetInteger(0,name,OBJPROP_COLOR,         clrs);
      ObjectSetInteger(0,name,OBJPROP_ANCHOR,      anchor);
      ObjectSetInteger(0,name,OBJPROP_BACK,         false);
      ObjectSetString (0,name,OBJPROP_FONT,    NameLabels);
      ObjectSetInteger(0,name,OBJPROP_FONTSIZE,SizeLabels);
      ObjectSetInteger(0,name,OBJPROP_SELECTED,     false);
      ObjectSetInteger(0,name,OBJPROP_SELECTABLE,   false);
   }
}

//————————————————————————————————————————————————————————|

void objDelete(string prefix)
{
       int     objTotals = ObjectsTotal(0); string nume;
   for(int i = objTotals - 1; i >= 0; i--)
   {                nume = ObjectName(0,i);
      if(StringFind(nume , prefix) == 0) ObjectDelete(0,nume);
   }
}

//—————————————————| end of the algorithm |——————————————————|
 
  1. Please edit your (original) post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)
          General rules and best pratices of the Forum. - General - MQL5 programming forum #25 (2019)
              Messages Editor
          Forum rules and recommendations - General - MQL5 programming forum (2023)

  2. Your rudeness put you on my Do Not Help List; live in ignorance.

 
William Roeder #:
  1. Please edit your (original) post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)
          General rules and best pratices of the Forum. - General - MQL5 programming forum #25 (2019)
              Messages Editor
          Forum rules and recommendations - General - MQL5 programming forum (2023)

  2. Your rudeness put you on my Do Not Help List; live in ignorance.

Sorry Mr William
But The Edit / Delete option is not avaliable to me. I see only

to pocket | complain | reply


I see Delete if I go in my Mansagers board... But don´t know if I delete there it will delete here

It was not my intention
I realy like to correct it
 

Reason: