Hello, could somebody help me adding a limit to the bars that should plot?

 


Theres is a bothering lag when changing timeframes, I wish I could input on the indicators setting how much MTF bars I want to be plotted. Thanks.



//+------------------------------------------------------------------+

//|                                              MTF_Candles.mq4     |
//+------------------------------------------------------------------+
#property copyright "Ab Moncada"
#property version   "1.0"
#property strict
#property indicator_chart_window


enum enumTimeFrames{

   m1 = PERIOD_M1,   //M1
   m5 = PERIOD_M5,   //M5
   m15 = PERIOD_M15, //M15
   m30 = PERIOD_M30, //M30
   h1 = PERIOD_H1,   //H1
   h4 = PERIOD_H4,   //H4
   d1 = PERIOD_D1,   //D1
   w1 = PERIOD_W1,   //W1
   mn1 = PERIOD_MN1  //MN1
};

//input ENUM_TIMEFRAMES MTF_Period = PERIOD_H1;   //Timeframe
input enumTimeFrames MTF_Period = h1;    //Timeframe
input color UpColor = clrGainsboro;                  //ColorBulls
input color DownColor = clrGainsboro;              //ColorBears

input bool LineType = false;                    //Background
input ENUM_LINE_STYLE LineStyle = STYLE_DOT;    //LineStyle
input int LineWidth = 3;                        //LineWidth


string indiName = "MTF_CandleStick";

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit(){
//--- indicator buffers mapping

//---
   return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+  
void OnDeinit(const int reason){

   objDelete(indiName+IntegerToString(MTF_Period));

}
  
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
//---

   if(MTF_Period <= _Period) return(0);

   int limit = rates_total - prev_calculated;
   
   if(limit > 0){
      objDelete(indiName+IntegerToString(MTF_Period));
      limit = rates_total-1;
   }
   else{
      if(MTF_Period < 1440) limit = MTF_Period/_Period;
      else limit = 1;
   }
   
   for(int i=1; i<limit; i++){

      double mtfOpen, mtfClose, mtfHigh, mtfLow;
      int first, last=0;
      
      if(MTF_Period < 1440){
      
         if(MathMod(time[i], MTF_Period*60) != 0) continue;
            
         first = i;
            
         for(int t=i-1; t>=0; t--){
      
            if(time[i]+MTF_Period*60 <= time[t]){
               last = t+1;
               break;
            }
         }
            
         mtfOpen = open[first];
         mtfClose = close[last];
         mtfHigh = high[iHighest(NULL, 0, MODE_HIGH, first-last+1, last)];
         mtfLow = low[iLowest(NULL, 0, MODE_LOW, first-last+1, last)];
      }
      else{

         if(time[Bars-1] > iTime(NULL, MTF_Period, i)) break;
         
         mtfOpen = iOpen(NULL, MTF_Period, i);
         mtfClose = iClose(NULL, MTF_Period, i);
         mtfHigh = iHigh(NULL, MTF_Period, i);
         mtfLow = iLow(NULL, MTF_Period, i);
         
         first = iBarShift(NULL, 0, iTime(NULL, MTF_Period, i), false);
         if(TimeHour(time[first]) != 0) first--;
         
         if(i > 0){
            last = iBarShift(NULL, 0, iTime(NULL, MTF_Period, i-1), false);
            if(TimeHour(time[last]) == 0) last++;
         }
         
         /*
         if(MTF_Period == 1440){
            first = iBarShift(NULL, 0, iTime(NULL, MTF_Period, i), false);
            if(i > 0) last = iBarShift(NULL, 0, iTime(NULL, MTF_Period, i-1), false)+1;
         }
         else{
            first = iBarShift(NULL, 0, iTime(NULL, MTF_Period, i), false)-1;
            if(i > 0) last = iBarShift(NULL, 0, iTime(NULL, MTF_Period, i-1), false);
         }
         */
      }
         
      if(mtfOpen <= mtfClose){
         Rectangle(indiName+IntegerToString(MTF_Period)+"_body"+IntegerToString(i), first, mtfOpen, last, mtfClose, UpColor, LineStyle, LineWidth);
         TrendLine(indiName+IntegerToString(MTF_Period)+"_shadow"+IntegerToString(i), (first+last)/2, mtfClose, mtfHigh, UpColor, LineWidth);
         TrendLine(indiName+IntegerToString(MTF_Period)+"_tail"+IntegerToString(i), (first+last)/2, mtfOpen, mtfLow, UpColor, LineWidth);
      }
      else{
         Rectangle(indiName+IntegerToString(MTF_Period)+"_body"+IntegerToString(i), first, mtfOpen, last, mtfClose, DownColor, LineStyle, LineWidth);
         TrendLine(indiName+IntegerToString(MTF_Period)+"_shadow"+IntegerToString(i), (first+last)/2, mtfOpen, mtfHigh, DownColor, LineWidth);
         TrendLine(indiName+IntegerToString(MTF_Period)+"_tail"+IntegerToString(i), (first+last)/2, mtfClose, mtfLow, DownColor, LineWidth);
      }
   }
   
//--- return value of prev_calculated for next call
   return(rates_total);
}


//+------------------------------------------------------------------+

void Rectangle(string name, int index1, double price1, int index2, double price2, color Rcolor, int Rstyle, int Rwidth){
   
   long id = ChartID();
   
   if (ObjectFind(name) != 0) {
   
      ObjectCreate(id, name, OBJ_RECTANGLE, 0, Time[index1], price1, Time[index2], price2);
      ObjectSetInteger(id, name, OBJPROP_COLOR, Rcolor);
      if(LineType) ObjectSetInteger(id, name, OBJPROP_STYLE, Rstyle);
      ObjectSetInteger(id, name, OBJPROP_WIDTH, Rwidth);
      ObjectSetInteger(id, name, OBJPROP_BACK, !LineType);
      //ObjectSetInteger(id, name, OBJPROP_RAY_RIGHT, false);
      ObjectSetInteger(id, name, OBJPROP_SELECTABLE, false);
      ObjectSetInteger(id, name, OBJPROP_HIDDEN, true);
   }
   else{
      ObjectMove(name, 0, Time[index1], price1);
      ObjectMove(name, 1, Time[index2], price2);
   }
   
   ChartRedraw(id);
}

void TrendLine(string name, int position, double price1, double price2, color Tcolor, int Twidth){
   
   long id = ChartID();
   
   if (ObjectFind(name) != 0) {
   
      ObjectCreate(id, name, OBJ_TREND, 0, Time[position], price1, Time[position], price2);
      ObjectSetInteger(id, name, OBJPROP_COLOR, Tcolor);
      ObjectSetInteger(id, name, OBJPROP_STYLE, STYLE_SOLID);
      ObjectSetInteger(id, name, OBJPROP_WIDTH, Twidth);
      ObjectSetInteger(id, name, OBJPROP_BACK, true);
      ObjectSetInteger(id, name, OBJPROP_RAY_RIGHT, false);
      ObjectSetInteger(id, name, OBJPROP_SELECTABLE, false);
      ObjectSetInteger(id, name, OBJPROP_HIDDEN, true);
   }
   else{
      ObjectMove(name, 0, Time[position], price1);
      ObjectMove(name, 1, Time[position], price2);
   }
   
   ChartRedraw(id);
}

void objDelete(string basicName){

   for(int i=ObjectsTotal();i>=0;i--){
      string ObjName = ObjectName(i);
      if(StringFind(ObjName, basicName) >=0) ObjectDelete(ObjName);
   }
}

 
ab_moncada:


Theres is a bothering lag when changing timeframes, I wish I could input on the indicators setting how much MTF bars I want to be plotted. Thanks.


Here you go...

Files: